Java Code Examples for com.alibaba.cloud.nacos.NacosDiscoveryProperties

The following examples show how to use com.alibaba.cloud.nacos.NacosDiscoveryProperties. These examples are extracted from open source projects. 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 Project: spring-cloud-alibaba   Source File: NacosServerListTests.java    License: 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
@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 3
@Bean
@ConditionalOnBean(AutoServiceRegistrationProperties.class)
public NacosRegistration nacosRegistration(
		NacosDiscoveryProperties nacosDiscoveryProperties,
		ApplicationContext context) {
	return new NacosRegistration(nacosDiscoveryProperties, context);
}
 
Example 4
@Bean
@ConditionalOnMissingBean
@ConditionalOnEnabledEndpoint
public NacosDiscoveryEndpoint nacosDiscoveryEndpoint(
		NacosDiscoveryProperties nacosDiscoveryProperties) {
	return new NacosDiscoveryEndpoint(nacosDiscoveryProperties);
}
 
Example 5
@Bean
@ConditionalOnEnabledHealthIndicator("nacos-discovery")
public HealthIndicator nacosDiscoveryHealthIndicator(
		NacosDiscoveryProperties nacosDiscoveryProperties) {
	return new NacosDiscoveryHealthIndicator(
			nacosDiscoveryProperties.namingServiceInstance());
}
 
Example 6
@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
@Test
public void testDefaultInitialization() {
	contextRunner.run(context -> {
		assertThat(context).hasSingleBean(NacosDiscoveryProperties.class);
		assertThat(context).hasSingleBean(NacosServiceDiscovery.class);
	});
}
 
Example 8
Source Project: spring-cloud-alibaba   Source File: NacosServerListTests.java    License: 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 9
Source Project: spring-cloud-alibaba   Source File: NacosServerListTests.java    License: 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 10
Source Project: spring-cloud-gray   Source File: NacosServiceDiscovery.java    License: Apache License 2.0 4 votes vote down vote up
public NacosServiceDiscovery(NacosDiscoveryProperties discoveryProperties) {
    this.discoveryProperties = discoveryProperties;
}
 
Example 11
@Bean
public NacosServiceDiscovery nacosServiceDiscovery(NacosDiscoveryProperties discoveryProperties){
    return new NacosServiceDiscovery(discoveryProperties);
}
 
Example 12
NacosConfiguration(NacosDiscoveryProperties nacosDiscoveryProperties) {
	this.namingService = nacosDiscoveryProperties.namingServiceInstance();
	this.nacosDiscoveryProperties = nacosDiscoveryProperties;
	this.listeningServices = new ConcurrentSkipListSet<>();
}
 
Example 13
@Bean
@ConditionalOnMissingBean
public NacosDiscoveryProperties nacosProperties() {
	return new NacosDiscoveryProperties();
}
 
Example 14
@Bean
@ConditionalOnMissingBean
public NacosServiceDiscovery nacosServiceDiscovery(
		NacosDiscoveryProperties discoveryProperties) {
	return new NacosServiceDiscovery(discoveryProperties);
}
 
Example 15
public NacosServiceDiscovery(NacosDiscoveryProperties discoveryProperties) {
	this.discoveryProperties = discoveryProperties;
}
 
Example 16
Source Project: spring-cloud-alibaba   Source File: NacosWatch.java    License: Apache License 2.0 4 votes vote down vote up
public NacosWatch(NacosDiscoveryProperties properties) {
	this(properties, getTaskScheduler());
}
 
Example 17
Source Project: spring-cloud-alibaba   Source File: NacosWatch.java    License: Apache License 2.0 4 votes vote down vote up
public NacosWatch(NacosDiscoveryProperties properties, TaskScheduler taskScheduler) {
	this.properties = properties;
	this.taskScheduler = taskScheduler;
}
 
Example 18
Source Project: spring-cloud-alibaba   Source File: NacosServerList.java    License: Apache License 2.0 4 votes vote down vote up
public NacosServerList(NacosDiscoveryProperties discoveryProperties) {
	this.discoveryProperties = discoveryProperties;
}
 
Example 19
Source Project: spring-cloud-alibaba   Source File: NacosServiceRegistry.java    License: Apache License 2.0 4 votes vote down vote up
public NacosServiceRegistry(NacosDiscoveryProperties nacosDiscoveryProperties) {
	this.nacosDiscoveryProperties = nacosDiscoveryProperties;
}
 
Example 20
@Bean
public NacosServiceRegistry nacosServiceRegistry(
		NacosDiscoveryProperties nacosDiscoveryProperties) {
	return new NacosServiceRegistry(nacosDiscoveryProperties);
}
 
Example 21
Source Project: spring-cloud-alibaba   Source File: NacosRegistration.java    License: Apache License 2.0 4 votes vote down vote up
public NacosRegistration(NacosDiscoveryProperties nacosDiscoveryProperties,
		ApplicationContext context) {
	this.nacosDiscoveryProperties = nacosDiscoveryProperties;
	this.context = context;
}
 
Example 22
Source Project: spring-cloud-alibaba   Source File: NacosRegistration.java    License: Apache License 2.0 4 votes vote down vote up
public NacosDiscoveryProperties getNacosDiscoveryProperties() {
	return nacosDiscoveryProperties;
}
 
Example 23
public NacosDiscoveryEndpoint(NacosDiscoveryProperties nacosDiscoveryProperties) {
	this.nacosDiscoveryProperties = nacosDiscoveryProperties;
}
 
Example 24
Source Project: spring-cloud-alibaba   Source File: NacosServerListTests.java    License: 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 25
Source Project: spring-cloud-alibaba   Source File: NacosWatch.java    License: 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));
}