1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

Polish alibaba/spring-cloud-alibaba#1758 : [CodeBase] Sync the source code from greenwich to finichley

This commit is contained in:
mercyblitz
2020-09-21 15:58:45 +08:00
parent 201a4eaa01
commit 8cbd14b2bb
71 changed files with 341 additions and 376 deletions

View File

@@ -67,14 +67,12 @@ import static com.alibaba.nacos.api.PropertyKeyConst.USERNAME;
@ConfigurationProperties("spring.cloud.nacos.discovery")
public class NacosDiscoveryProperties {
private static final Logger log = LoggerFactory
.getLogger(NacosDiscoveryProperties.class);
/**
* Prefix of {@link NacosDiscoveryProperties}.
*/
public static final String PREFIX = "spring.cloud.nacos.discovery";
private static final Logger log = LoggerFactory
.getLogger(NacosDiscoveryProperties.class);
private static final Pattern PATTERN = Pattern.compile("-(\\w)");
/**

View File

@@ -42,21 +42,37 @@ public class NacosServiceInstance implements ServiceInstance {
return serviceId;
}
public void setServiceId(String serviceId) {
this.serviceId = serviceId;
}
@Override
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
@Override
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
@Override
public boolean isSecure() {
return secure;
}
public void setSecure(boolean secure) {
this.secure = secure;
}
@Override
public URI getUri() {
return DefaultServiceInstance.getUri(this);
@@ -67,22 +83,6 @@ public class NacosServiceInstance implements ServiceInstance {
return metadata;
}
public void setServiceId(String serviceId) {
this.serviceId = serviceId;
}
public void setHost(String host) {
this.host = host;
}
public void setPort(int port) {
this.port = port;
}
public void setSecure(boolean secure) {
this.secure = secure;
}
public void setMetadata(Map<String, String> metadata) {
this.metadata = metadata;
}

View File

@@ -32,13 +32,11 @@ import org.springframework.cloud.client.discovery.DiscoveryClient;
*/
public class NacosDiscoveryClient implements DiscoveryClient {
private static final Logger log = LoggerFactory.getLogger(NacosDiscoveryClient.class);
/**
* Nacos Discovery Client Description.
*/
public static final String DESCRIPTION = "Spring Cloud Nacos Discovery Client";
private static final Logger log = LoggerFactory.getLogger(NacosDiscoveryClient.class);
private NacosServiceDiscovery serviceDiscovery;
public NacosDiscoveryClient(NacosServiceDiscovery nacosServiceDiscovery) {

View File

@@ -57,8 +57,8 @@ public class NacosDiscoveryClientConfiguration {
@ConditionalOnMissingBean
@ConditionalOnProperty(value = "spring.cloud.nacos.discovery.watch.enabled", matchIfMissing = true)
public NacosWatch nacosWatch(NacosServiceManager nacosServiceManager,
NacosDiscoveryProperties nacosDiscoveryProperties,
ObjectProvider<TaskScheduler> taskExecutorObjectProvider) {
NacosDiscoveryProperties nacosDiscoveryProperties,
ObjectProvider<TaskScheduler> taskExecutorObjectProvider) {
return new NacosWatch(nacosServiceManager, nacosDiscoveryProperties,
taskExecutorObjectProvider);
}

View File

@@ -46,33 +46,8 @@ public class NacosServiceDiscovery {
this.nacosServiceManager = nacosServiceManager;
}
/**
* Return all instances for the given service.
* @param serviceId id of service
* @return list of instances
* @throws NacosException nacosException
*/
public List<ServiceInstance> getInstances(String serviceId) throws NacosException {
String group = discoveryProperties.getGroup();
List<Instance> instances = namingService().selectInstances(serviceId, group,
true);
return hostToServiceInstanceList(instances, serviceId);
}
/**
* Return the names of all services.
* @return list of service names
* @throws NacosException nacosException
*/
public List<String> getServices() throws NacosException {
String group = discoveryProperties.getGroup();
ListView<String> services = namingService().getServicesOfServer(1,
Integer.MAX_VALUE, group);
return services.getData();
}
public static List<ServiceInstance> hostToServiceInstanceList(
List<Instance> instances, String serviceId) {
List<Instance> instances, String serviceId) {
List<ServiceInstance> result = new ArrayList<>(instances.size());
for (Instance instance : instances) {
ServiceInstance serviceInstance = hostToServiceInstance(instance, serviceId);
@@ -84,7 +59,7 @@ public class NacosServiceDiscovery {
}
public static ServiceInstance hostToServiceInstance(Instance instance,
String serviceId) {
String serviceId) {
if (instance == null || !instance.isEnabled() || !instance.isHealthy()) {
return null;
}
@@ -111,6 +86,31 @@ public class NacosServiceDiscovery {
return nacosServiceInstance;
}
/**
* Return all instances for the given service.
* @param serviceId id of service
* @return list of instances
* @throws NacosException nacosException
*/
public List<ServiceInstance> getInstances(String serviceId) throws NacosException {
String group = discoveryProperties.getGroup();
List<Instance> instances = namingService().selectInstances(serviceId, group,
true);
return hostToServiceInstanceList(instances, serviceId);
}
/**
* Return the names of all services.
* @return list of service names
* @throws NacosException nacosException
*/
public List<String> getServices() throws NacosException {
String group = discoveryProperties.getGroup();
ListView<String> services = namingService().getServicesOfServer(1,
Integer.MAX_VALUE, group);
return services.getData();
}
private NamingService namingService() {
return nacosServiceManager
.getNamingService(discoveryProperties.getNacosProperties());

View File

@@ -51,26 +51,18 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
public class NacosWatch implements ApplicationEventPublisherAware, SmartLifecycle {
private static final Logger log = LoggerFactory.getLogger(NacosWatch.class);
private Map<String, EventListener> listenerMap = new ConcurrentHashMap<>(16);
private final AtomicBoolean running = new AtomicBoolean(false);
private final AtomicLong nacosWatchIndex = new AtomicLong(0);
private final NacosDiscoveryProperties properties;
private final TaskScheduler taskScheduler;
private Map<String, EventListener> listenerMap = new ConcurrentHashMap<>(16);
private ApplicationEventPublisher publisher;
private ScheduledFuture<?> watchFuture;
private NacosServiceManager nacosServiceManager;
private final NacosDiscoveryProperties properties;
private final TaskScheduler taskScheduler;
public NacosWatch(NacosServiceManager nacosServiceManager,
NacosDiscoveryProperties properties,
ObjectProvider<TaskScheduler> taskScheduler) {
NacosDiscoveryProperties properties,
ObjectProvider<TaskScheduler> taskScheduler) {
this.nacosServiceManager = nacosServiceManager;
this.properties = properties;
this.taskScheduler = taskScheduler.getIfAvailable(NacosWatch::getTaskScheduler);

View File

@@ -47,7 +47,7 @@ public class NacosDiscoveryEndpoint {
private NacosDiscoveryProperties nacosDiscoveryProperties;
public NacosDiscoveryEndpoint(NacosServiceManager nacosServiceManager,
NacosDiscoveryProperties nacosDiscoveryProperties) {
NacosDiscoveryProperties nacosDiscoveryProperties) {
this.nacosServiceManager = nacosServiceManager;
this.nacosDiscoveryProperties = nacosDiscoveryProperties;
}

View File

@@ -41,8 +41,8 @@ public class NacosAutoServiceRegistration
private NacosRegistration registration;
public NacosAutoServiceRegistration(ServiceRegistry<Registration> serviceRegistry,
AutoServiceRegistrationProperties autoServiceRegistrationProperties,
NacosRegistration registration) {
AutoServiceRegistrationProperties autoServiceRegistrationProperties,
NacosRegistration registration) {
super(serviceRegistry, autoServiceRegistrationProperties);
this.registration = registration;
}

View File

@@ -72,6 +72,16 @@ public class NacosRegistration implements Registration, ServiceInstance {
this.context = context;
}
private static void customize(
List<NacosRegistrationCustomizer> registrationCustomizers,
NacosRegistration registration) {
if (registrationCustomizers != null) {
for (NacosRegistrationCustomizer customizer : registrationCustomizers) {
customizer.customize(registration);
}
}
}
@PostConstruct
public void init() {
@@ -112,16 +122,6 @@ public class NacosRegistration implements Registration, ServiceInstance {
customize(registrationCustomizers, this);
}
private static void customize(
List<NacosRegistrationCustomizer> registrationCustomizers,
NacosRegistration registration) {
if (registrationCustomizers != null) {
for (NacosRegistrationCustomizer customizer : registrationCustomizers) {
customizer.customize(registration);
}
}
}
@Override
public String getServiceId() {
return nacosDiscoveryProperties.getService();

View File

@@ -42,7 +42,7 @@ public class NacosRibbonClientConfiguration {
@Bean
@ConditionalOnMissingBean
public ServerList<?> ribbonServerList(IClientConfig config,
NacosDiscoveryProperties nacosDiscoveryProperties) {
NacosDiscoveryProperties nacosDiscoveryProperties) {
if (this.propertiesFactory.isSet(ServerList.class, config.getClientName())) {
ServerList serverList = this.propertiesFactory.get(ServerList.class, config,
config.getClientName());

View File

@@ -122,7 +122,7 @@ public class MockNamingService implements NamingService {
@Override
public List<Instance> getAllInstances(String serviceName, String groupName,
boolean subscribe) throws NacosException {
boolean subscribe) throws NacosException {
return null;
}
@@ -134,19 +134,19 @@ public class MockNamingService implements NamingService {
@Override
public List<Instance> getAllInstances(String serviceName, String groupName,
List<String> clusters) throws NacosException {
List<String> clusters) throws NacosException {
return null;
}
@Override
public List<Instance> getAllInstances(String serviceName, List<String> clusters,
boolean subscribe) throws NacosException {
boolean subscribe) throws NacosException {
return null;
}
@Override
public List<Instance> getAllInstances(String serviceName, String groupName,
List<String> clusters, boolean subscribe) throws NacosException {
List<String> clusters, boolean subscribe) throws NacosException {
return null;
}
@@ -158,43 +158,43 @@ public class MockNamingService implements NamingService {
@Override
public List<Instance> selectInstances(String serviceName, String groupName,
boolean healthy) throws NacosException {
boolean healthy) throws NacosException {
return null;
}
@Override
public List<Instance> selectInstances(String serviceName, boolean healthy,
boolean subscribe) throws NacosException {
boolean subscribe) throws NacosException {
return null;
}
@Override
public List<Instance> selectInstances(String serviceName, String groupName,
boolean healthy, boolean subscribe) throws NacosException {
boolean healthy, boolean subscribe) throws NacosException {
return null;
}
@Override
public List<Instance> selectInstances(String serviceName, List<String> clusters,
boolean healthy) throws NacosException {
boolean healthy) throws NacosException {
return null;
}
@Override
public List<Instance> selectInstances(String serviceName, String groupName,
List<String> clusters, boolean healthy) throws NacosException {
List<String> clusters, boolean healthy) throws NacosException {
return null;
}
@Override
public List<Instance> selectInstances(String serviceName, List<String> clusters,
boolean healthy, boolean subscribe) throws NacosException {
boolean healthy, boolean subscribe) throws NacosException {
return null;
}
@Override
public List<Instance> selectInstances(String serviceName, String groupName,
List<String> clusters, boolean healthy, boolean subscribe)
List<String> clusters, boolean healthy, boolean subscribe)
throws NacosException {
return null;
}
@@ -218,7 +218,7 @@ public class MockNamingService implements NamingService {
@Override
public Instance selectOneHealthyInstance(String serviceName, String groupName,
boolean subscribe) throws NacosException {
boolean subscribe) throws NacosException {
return null;
}
@@ -230,19 +230,19 @@ public class MockNamingService implements NamingService {
@Override
public Instance selectOneHealthyInstance(String serviceName, String groupName,
List<String> clusters) throws NacosException {
List<String> clusters) throws NacosException {
return null;
}
@Override
public Instance selectOneHealthyInstance(String serviceName, List<String> clusters,
boolean subscribe) throws NacosException {
boolean subscribe) throws NacosException {
return null;
}
@Override
public Instance selectOneHealthyInstance(String serviceName, String groupName,
List<String> clusters, boolean subscribe) throws NacosException {
List<String> clusters, boolean subscribe) throws NacosException {
return null;
}
@@ -302,19 +302,19 @@ public class MockNamingService implements NamingService {
@Override
public ListView<String> getServicesOfServer(int pageNo, int pageSize,
String groupName) throws NacosException {
String groupName) throws NacosException {
return null;
}
@Override
public ListView<String> getServicesOfServer(int pageNo, int pageSize,
AbstractSelector selector) throws NacosException {
AbstractSelector selector) throws NacosException {
return null;
}
@Override
public ListView<String> getServicesOfServer(int pageNo, int pageSize,
String groupName, AbstractSelector selector) throws NacosException {
String groupName, AbstractSelector selector) throws NacosException {
return null;
}

View File

@@ -61,18 +61,6 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
"spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848" }, webEnvironment = RANDOM_PORT)
public class NacosAutoServiceRegistrationIpNetworkInterfaceTests {
@Autowired
private NacosRegistration registration;
@Autowired
private NacosAutoServiceRegistration nacosAutoServiceRegistration;
@Autowired
private NacosDiscoveryProperties properties;
@Autowired
private InetUtils inetUtils;
static {
try {
Method method = PowerMockito.method(NacosFactory.class, "createNamingService",
@@ -90,6 +78,15 @@ public class NacosAutoServiceRegistrationIpNetworkInterfaceTests {
}
}
@Autowired
private NacosRegistration registration;
@Autowired
private NacosAutoServiceRegistration nacosAutoServiceRegistration;
@Autowired
private NacosDiscoveryProperties properties;
@Autowired
private InetUtils inetUtils;
@Test
public void contextLoads() throws Exception {
assertThat(registration).isNotNull();

View File

@@ -57,15 +57,6 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
"spring.cloud.nacos.discovery.ip=123.123.123.123" }, webEnvironment = RANDOM_PORT)
public class NacosAutoServiceRegistrationIpTests {
@Autowired
private NacosRegistration registration;
@Autowired
private NacosAutoServiceRegistration nacosAutoServiceRegistration;
@Autowired
private NacosDiscoveryProperties properties;
static {
try {
Method method = PowerMockito.method(NacosFactory.class, "createNamingService",
@@ -83,6 +74,13 @@ public class NacosAutoServiceRegistrationIpTests {
}
}
@Autowired
private NacosRegistration registration;
@Autowired
private NacosAutoServiceRegistration nacosAutoServiceRegistration;
@Autowired
private NacosDiscoveryProperties properties;
@Test
public void contextLoads() throws Exception {
assertThat(registration).isNotNull();

View File

@@ -58,15 +58,6 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
"spring.cloud.nacos.discovery.port=8888" }, webEnvironment = RANDOM_PORT)
public class NacosAutoServiceRegistrationManagementPortTests {
@Autowired
private NacosRegistration registration;
@Autowired
private NacosAutoServiceRegistration nacosAutoServiceRegistration;
@Autowired
private NacosDiscoveryProperties properties;
static {
try {
Method method = PowerMockito.method(NacosFactory.class, "createNamingService",
@@ -84,6 +75,13 @@ public class NacosAutoServiceRegistrationManagementPortTests {
}
}
@Autowired
private NacosRegistration registration;
@Autowired
private NacosAutoServiceRegistration nacosAutoServiceRegistration;
@Autowired
private NacosDiscoveryProperties properties;
@Test
public void contextLoads() throws Exception {
assertThat(registration).isNotNull();

View File

@@ -57,15 +57,6 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
"spring.cloud.nacos.discovery.port=8888" }, webEnvironment = RANDOM_PORT)
public class NacosAutoServiceRegistrationPortTests {
@Autowired
private NacosRegistration registration;
@Autowired
private NacosAutoServiceRegistration nacosAutoServiceRegistration;
@Autowired
private NacosDiscoveryProperties properties;
static {
try {
Method method = PowerMockito.method(NacosFactory.class, "createNamingService",
@@ -83,6 +74,13 @@ public class NacosAutoServiceRegistrationPortTests {
}
}
@Autowired
private NacosRegistration registration;
@Autowired
private NacosAutoServiceRegistration nacosAutoServiceRegistration;
@Autowired
private NacosDiscoveryProperties properties;
@Test
public void contextLoads() throws Exception {
assertThat(registration).isNotNull();

View File

@@ -74,24 +74,6 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
"spring.cloud.nacos.discovery.ip-delete-timeout=9" }, webEnvironment = RANDOM_PORT)
public class NacosAutoServiceRegistrationTests {
@Autowired
private NacosRegistration registration;
@Autowired
private NacosAutoServiceRegistration nacosAutoServiceRegistration;
@LocalServerPort
private int port;
@Autowired
private NacosDiscoveryProperties properties;
@Autowired
private NacosServiceManager nacosServiceManager;
@Autowired
private InetUtils inetUtils;
static {
try {
Method method = PowerMockito.method(NacosFactory.class, "createNamingService",
@@ -109,6 +91,19 @@ public class NacosAutoServiceRegistrationTests {
}
}
@Autowired
private NacosRegistration registration;
@Autowired
private NacosAutoServiceRegistration nacosAutoServiceRegistration;
@LocalServerPort
private int port;
@Autowired
private NacosDiscoveryProperties properties;
@Autowired
private NacosServiceManager nacosServiceManager;
@Autowired
private InetUtils inetUtils;
@Test
public void contextLoads() throws Exception {
assertThat(registration).isNotNull();

View File

@@ -61,7 +61,7 @@ public class NacosRibbonClientConfigurationTests {
static class NacosRibbonTestConfiguration {
@Bean
IClientConfig iClientConfig() {
IClientConfig iClientConfig() {
DefaultClientConfigImpl config = new DefaultClientConfigImpl();
config.setClientName("myapp");
return config;
@@ -69,7 +69,7 @@ public class NacosRibbonClientConfigurationTests {
@Bean
@LoadBalanced
RestTemplate restTemplate() {
RestTemplate restTemplate() {
return new RestTemplate();
}

View File

@@ -30,7 +30,7 @@ public class CommonTestConfig {
@Bean
@LoadBalanced
RestTemplate loadBalancedRestTemplate() {
RestTemplate loadBalancedRestTemplate() {
return new RestTemplate();
}

View File

@@ -31,7 +31,7 @@ public final class NacosMockTest {
}
public static Instance serviceInstance(String serviceName, boolean isHealthy,
Map<String, String> metadata) {
Map<String, String> metadata) {
Instance instance = new Instance();
instance.setInstanceId(UUID.randomUUID().toString());
instance.setServiceName(serviceName);
@@ -41,7 +41,7 @@ public final class NacosMockTest {
}
public static Instance serviceInstance(String serviceName, boolean isHealthy,
String host, int port, Map<String, String> metadata) {
String host, int port, Map<String, String> metadata) {
Instance instance = new Instance();
instance.setIp(host);
instance.setPort(port);