com.alibaba.cloud.nacos.NacosDiscoveryProperties Java Examples

The following examples show how to use com.alibaba.cloud.nacos.NacosDiscoveryProperties. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NacosServerListTests.java    From spring-cloud-alibaba with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testEmptyInstancesReturnsEmptyList() throws Exception {
	NacosDiscoveryProperties nacosDiscoveryProperties = mock(
			NacosDiscoveryProperties.class);

	NamingService namingService = mock(NamingService.class);

	when(nacosDiscoveryProperties.namingServiceInstance()).thenReturn(namingService);
	when(namingService.selectInstances(anyString(), eq("DEFAULT"), eq(true)))
			.thenReturn(null);

	NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties);
	List<NacosServer> servers = serverList.getInitialListOfServers();
	assertThat(servers).isEmpty();
}
 
Example #2
Source File: NacosServerListTests.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpdateServers() throws Exception {
	ArrayList<Instance> instances = new ArrayList<>();

	HashMap<String, String> map = new HashMap<>();
	map.put("instanceNum", "1");
	instances.add(NacosMockTest.serviceInstance("test-service", true, map));

	NacosDiscoveryProperties nacosDiscoveryProperties = mock(
			NacosDiscoveryProperties.class);

	NamingService namingService = mock(NamingService.class);

	when(nacosDiscoveryProperties.namingServiceInstance()).thenReturn(namingService);
	when(nacosDiscoveryProperties.getGroup()).thenReturn("DEFAULT");
	when(namingService.selectInstances(eq("test-service"), eq("DEFAULT"), eq(true)))
			.thenReturn(instances.stream().filter(Instance::isHealthy)
					.collect(Collectors.toList()));

	IClientConfig clientConfig = mock(IClientConfig.class);
	when(clientConfig.getClientName()).thenReturn("test-service");
	NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties);
	serverList.initWithNiwsConfig(clientConfig);

	List<NacosServer> servers = serverList.getUpdatedListOfServers();
	assertThat(servers).hasSize(1);

	assertThat(servers.get(0).getInstance().isHealthy()).isEqualTo(true);
	assertThat(servers.get(0).getInstance().getMetadata().get("instanceNum"))
			.isEqualTo("1");
}
 
Example #3
Source File: NacosDiscoveryClientConfiguration.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(value = "spring.cloud.nacos.discovery.watch.enabled",
		matchIfMissing = true)
public NacosWatch nacosWatch(NacosDiscoveryProperties nacosDiscoveryProperties,
		ObjectProvider<TaskScheduler> taskScheduler) {
	return new NacosWatch(nacosDiscoveryProperties, taskScheduler);
}
 
Example #4
Source File: NacosServerListTests.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testGetServers() throws Exception {

	ArrayList<Instance> instances = new ArrayList<>();
	instances.add(NacosMockTest.serviceInstance("test-service", false,
			Collections.emptyMap()));

	NacosDiscoveryProperties nacosDiscoveryProperties = mock(
			NacosDiscoveryProperties.class);

	NamingService namingService = mock(NamingService.class);

	when(nacosDiscoveryProperties.namingServiceInstance()).thenReturn(namingService);
	when(nacosDiscoveryProperties.getGroup()).thenReturn("DEFAULT");
	when(nacosDiscoveryProperties.getGroup()).thenReturn("DEFAULT");
	when(namingService.selectInstances(eq("test-service"), eq("DEFAULT"), eq(true)))
			.thenReturn(instances);

	IClientConfig clientConfig = mock(IClientConfig.class);
	when(clientConfig.getClientName()).thenReturn("test-service");
	NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties);
	serverList.initWithNiwsConfig(clientConfig);
	List<NacosServer> servers = serverList.getInitialListOfServers();
	assertThat(servers).hasSize(1);

	servers = serverList.getUpdatedListOfServers();
	assertThat(servers).hasSize(1);
}
 
Example #5
Source File: NacosDiscoveryAutoConfigurationTests.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefaultInitialization() {
	contextRunner.run(context -> {
		assertThat(context).hasSingleBean(NacosDiscoveryProperties.class);
		assertThat(context).hasSingleBean(NacosServiceDiscovery.class);
	});
}
 
Example #6
Source File: NacosServiceDiscoveryTest.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetServices() throws NacosException {
	ListView<String> nacosServices = new ListView<>();

	nacosServices.setData(new LinkedList<>());

	nacosServices.getData().add(serviceName + "1");
	nacosServices.getData().add(serviceName + "2");
	nacosServices.getData().add(serviceName + "3");

	NacosDiscoveryProperties nacosDiscoveryProperties = mock(
			NacosDiscoveryProperties.class);

	NamingService namingService = mock(NamingService.class);

	when(nacosDiscoveryProperties.namingServiceInstance()).thenReturn(namingService);
	when(nacosDiscoveryProperties.getGroup()).thenReturn("DEFAULT");
	when(namingService.getServicesOfServer(eq(1), eq(Integer.MAX_VALUE),
			eq("DEFAULT"))).thenReturn(nacosServices);

	NacosServiceDiscovery serviceDiscovery = new NacosServiceDiscovery(
			nacosDiscoveryProperties);

	List<String> services = serviceDiscovery.getServices();

	assertThat(services.size()).isEqualTo(3);
	assertThat(services.contains(serviceName + "1"));
	assertThat(services.contains(serviceName + "2"));
	assertThat(services.contains(serviceName + "3"));
}
 
Example #7
Source File: NacosDiscoveryEndpointAutoConfiguration.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnEnabledHealthIndicator("nacos-discovery")
public HealthIndicator nacosDiscoveryHealthIndicator(
		NacosDiscoveryProperties nacosDiscoveryProperties) {
	return new NacosDiscoveryHealthIndicator(
			nacosDiscoveryProperties.namingServiceInstance());
}
 
Example #8
Source File: NacosDiscoveryEndpointAutoConfiguration.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
@ConditionalOnEnabledEndpoint
public NacosDiscoveryEndpoint nacosDiscoveryEndpoint(
		NacosDiscoveryProperties nacosDiscoveryProperties) {
	return new NacosDiscoveryEndpoint(nacosDiscoveryProperties);
}
 
Example #9
Source File: NacosServiceRegistryAutoConfiguration.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnBean(AutoServiceRegistrationProperties.class)
public NacosRegistration nacosRegistration(
		NacosDiscoveryProperties nacosDiscoveryProperties,
		ApplicationContext context) {
	return new NacosRegistration(nacosDiscoveryProperties, context);
}
 
Example #10
Source File: NacosServiceRegistryAutoConfiguration.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@Bean
public NacosServiceRegistry nacosServiceRegistry(
		NacosDiscoveryProperties nacosDiscoveryProperties) {
	return new NacosServiceRegistry(nacosDiscoveryProperties);
}
 
Example #11
Source File: NacosServerListTests.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testGetServersWithInstanceStatus() throws Exception {
	ArrayList<Instance> instances = new ArrayList<>();

	HashMap<String, String> map1 = new HashMap<>();
	map1.put("instanceNum", "1");
	HashMap<String, String> map2 = new HashMap<>();
	map2.put("instanceNum", "2");
	instances.add(NacosMockTest.serviceInstance("test-service", false, map1));
	instances.add(NacosMockTest.serviceInstance("test-service", true, map2));

	NacosDiscoveryProperties nacosDiscoveryProperties = mock(
			NacosDiscoveryProperties.class);

	NamingService namingService = mock(NamingService.class);

	when(nacosDiscoveryProperties.namingServiceInstance()).thenReturn(namingService);
	when(nacosDiscoveryProperties.getGroup()).thenReturn("DEFAULT");
	when(namingService.selectInstances(eq("test-service"), eq("DEFAULT"), eq(true)))
			.thenReturn(instances.stream().filter(Instance::isHealthy)
					.collect(Collectors.toList()));

	IClientConfig clientConfig = mock(IClientConfig.class);
	when(clientConfig.getClientName()).thenReturn("test-service");
	NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties);
	serverList.initWithNiwsConfig(clientConfig);
	List<NacosServer> servers = serverList.getInitialListOfServers();
	assertThat(servers).hasSize(1);

	NacosServer nacosServer = servers.get(0);

	assertThat(nacosServer.getMetaInfo().getInstanceId())
			.isEqualTo(instances.get(1).getInstanceId());
	assertThat(nacosServer.getMetadata()).isEqualTo(map2);
	assertThat(nacosServer.getInstance().isHealthy()).isEqualTo(true);
	assertThat(nacosServer.getInstance().getServiceName()).isEqualTo("test-service");
	assertThat(nacosServer.getInstance().getMetadata().get("instanceNum"))
			.isEqualTo("2");

}
 
Example #12
Source File: NacosDiscoveryEndpoint.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
public NacosDiscoveryEndpoint(NacosDiscoveryProperties nacosDiscoveryProperties) {
	this.nacosDiscoveryProperties = nacosDiscoveryProperties;
}
 
Example #13
Source File: NacosRegistration.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
public NacosDiscoveryProperties getNacosDiscoveryProperties() {
	return nacosDiscoveryProperties;
}
 
Example #14
Source File: NacosRegistration.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
public NacosRegistration(NacosDiscoveryProperties nacosDiscoveryProperties,
		ApplicationContext context) {
	this.nacosDiscoveryProperties = nacosDiscoveryProperties;
	this.context = context;
}
 
Example #15
Source File: NacosServiceDiscovery.java    From spring-cloud-gray with Apache License 2.0 4 votes vote down vote up
public NacosServiceDiscovery(NacosDiscoveryProperties discoveryProperties) {
    this.discoveryProperties = discoveryProperties;
}
 
Example #16
Source File: NacosServiceRegistry.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
public NacosServiceRegistry(NacosDiscoveryProperties nacosDiscoveryProperties) {
	this.nacosDiscoveryProperties = nacosDiscoveryProperties;
}
 
Example #17
Source File: NacosServerList.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
public NacosServerList(NacosDiscoveryProperties discoveryProperties) {
	this.discoveryProperties = discoveryProperties;
}
 
Example #18
Source File: NacosWatch.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
public NacosWatch(NacosDiscoveryProperties properties, TaskScheduler taskScheduler) {
	this.properties = properties;
	this.taskScheduler = taskScheduler;
}
 
Example #19
Source File: NacosWatch.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
public NacosWatch(NacosDiscoveryProperties properties) {
	this(properties, getTaskScheduler());
}
 
Example #20
Source File: NacosServiceDiscovery.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
public NacosServiceDiscovery(NacosDiscoveryProperties discoveryProperties) {
	this.discoveryProperties = discoveryProperties;
}
 
Example #21
Source File: NacosDiscoveryAutoConfiguration.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public NacosServiceDiscovery nacosServiceDiscovery(
		NacosDiscoveryProperties discoveryProperties) {
	return new NacosServiceDiscovery(discoveryProperties);
}
 
Example #22
Source File: NacosDiscoveryAutoConfiguration.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public NacosDiscoveryProperties nacosProperties() {
	return new NacosDiscoveryProperties();
}
 
Example #23
Source File: DubboServiceDiscoveryAutoConfiguration.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
NacosConfiguration(NacosDiscoveryProperties nacosDiscoveryProperties) {
	this.namingService = nacosDiscoveryProperties.namingServiceInstance();
	this.nacosDiscoveryProperties = nacosDiscoveryProperties;
	this.listeningServices = new ConcurrentSkipListSet<>();
}
 
Example #24
Source File: GrayServerPluginNacosDiscoveryAutoConfiguration.java    From spring-cloud-gray with Apache License 2.0 4 votes vote down vote up
@Bean
public NacosServiceDiscovery nacosServiceDiscovery(NacosDiscoveryProperties discoveryProperties){
    return new NacosServiceDiscovery(discoveryProperties);
}
 
Example #25
Source File: NacosWatch.java    From spring-cloud-alibaba with Apache License 2.0 2 votes vote down vote up
/**
 * The constructor with {@link NacosDiscoveryProperties} bean and the optional.
 * {@link TaskScheduler} bean
 * @param properties {@link NacosDiscoveryProperties} bean
 * @param taskScheduler the optional {@link TaskScheduler} bean
 * @since 2.2.0
 */
public NacosWatch(NacosDiscoveryProperties properties,
		ObjectProvider<TaskScheduler> taskScheduler) {
	this(properties, taskScheduler.getIfAvailable(NacosWatch::getTaskScheduler));
}