diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfiguration.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfiguration.java index 98c6e676c..ac2872808 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfiguration.java +++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfiguration.java @@ -16,6 +16,7 @@ package org.springframework.cloud.alibaba.nacos; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; @@ -25,6 +26,7 @@ import org.springframework.cloud.alibaba.nacos.registry.NacosAutoServiceRegistra import org.springframework.cloud.alibaba.nacos.registry.NacosRegistration; import org.springframework.cloud.alibaba.nacos.registry.NacosServiceRegistry; import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration; +import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration; import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; @@ -41,6 +43,7 @@ import org.springframework.context.annotation.Configuration; @ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true) @AutoConfigureBefore({ AutoServiceRegistrationAutoConfiguration.class, NacosDiscoveryClientAutoConfiguration.class }) +@AutoConfigureAfter(AutoServiceRegistrationConfiguration.class) public class NacosDiscoveryAutoConfiguration { @Bean diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClient.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClient.java index a4837449c..178333faa 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClient.java +++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClient.java @@ -72,6 +72,11 @@ public class NacosDiscoveryClient implements DiscoveryClient { metadata.put("cluster", instance.getClusterName() + ""); metadata.putAll(instance.getMetadata()); nacosServiceInstance.setMetadata(metadata); + + if (metadata.containsKey("secure")) { + boolean secure = Boolean.parseBoolean(metadata.get("secure")); + nacosServiceInstance.setSecure(secure); + } return nacosServiceInstance; } diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryProperties.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryProperties.java index c4d7e0e3a..86b73bf39 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryProperties.java +++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryProperties.java @@ -143,6 +143,10 @@ public class NacosDiscoveryProperties { @PostConstruct public void init() throws SocketException { + if (secure) { + metadata.put("secure", "true"); + } + serverAddr = Objects.toString(serverAddr, ""); endpoint = Objects.toString(endpoint, ""); namespace = Objects.toString(namespace, ""); @@ -351,7 +355,7 @@ public class NacosDiscoveryProperties { } if (StringUtils.isEmpty(this.getClusterName())) { this.setClusterName(env.resolvePlaceholders( - "${spring.cloud.nacos.discovery.clusterName-name:}")); + "${spring.cloud.nacos.discovery.cluster-name:}")); } if (StringUtils.isEmpty(this.getEndpoint())) { this.setEndpoint( diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosRegistration.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosRegistration.java index b9f9eb577..587cc4759 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosRegistration.java +++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosRegistration.java @@ -37,9 +37,9 @@ import com.alibaba.nacos.api.naming.NamingService; */ public class NacosRegistration implements Registration, ServiceInstance { - private static final String MANAGEMENT_PORT = "management.port"; - private static final String MANAGEMENT_CONTEXT_PATH = "management.context-path"; - private static final String MANAGEMENT_ADDRESS = "management.address"; + public static final String MANAGEMENT_PORT = "management.port"; + public static final String MANAGEMENT_CONTEXT_PATH = "management.context-path"; + public static final String MANAGEMENT_ADDRESS = "management.address"; private NacosDiscoveryProperties nacosDiscoveryProperties; @@ -125,11 +125,6 @@ public class NacosRegistration implements Registration, ServiceInstance { return nacosDiscoveryProperties.namingServiceInstance(); } - public void setNacosDiscoveryProperties( - NacosDiscoveryProperties nacosDiscoveryProperties) { - this.nacosDiscoveryProperties = nacosDiscoveryProperties; - } - @Override public String toString() { return "NacosRegistration{" + "nacosDiscoveryProperties=" diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosServerList.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosServerList.java index 292dbc0e0..c2b4990f5 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosServerList.java +++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosServerList.java @@ -35,9 +35,6 @@ public class NacosServerList extends AbstractServerList { private String serviceId; - public NacosServerList() { - } - public NacosServerList(NacosDiscoveryProperties discoveryProperties) { this.discoveryProperties = discoveryProperties; } diff --git a/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfigurationTests.java b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfigurationTests.java deleted file mode 100644 index 9b6531dbf..000000000 --- a/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfigurationTests.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright (C) 2018 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.cloud.alibaba.nacos; - -import org.junit.Test; -import org.springframework.boot.autoconfigure.AutoConfigurations; -import org.springframework.boot.autoconfigure.AutoConfigureBefore; -import org.springframework.boot.test.context.runner.WebApplicationContextRunner; -import org.springframework.cloud.alibaba.nacos.registry.NacosRegistration; -import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties; -import org.springframework.cloud.commons.util.InetUtils; -import org.springframework.cloud.commons.util.InetUtilsProperties; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - * @author xiaojing - */ -public class NacosDiscoveryAutoConfigurationTests { - - private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() - .withConfiguration( - AutoConfigurations.of(NacosDiscoveryTestConfiguration.class, - NacosDiscoveryAutoConfiguration.class, - NacosDiscoveryClientAutoConfiguration.class)) - .withPropertyValues("spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848") - .withPropertyValues("spring.cloud.nacos.discovery.port=18080") - .withPropertyValues("spring.cloud.nacos.discovery.service=myapp"); - - @Test - public void testProperties() { - this.contextRunner.run(context -> { - NacosDiscoveryProperties properties = context - .getBean(NacosDiscoveryProperties.class); - assertThat(properties.getPort()).isEqualTo(18080); - assertThat(properties.getServerAddr()).isEqualTo("127.0.0.1:8848"); - assertThat(properties.getService()).isEqualTo("myapp"); - }); - } - - @Test - public void nacosRegistration() { - this.contextRunner.run(context -> { - NacosRegistration nacosRegistration = context - .getBean(NacosRegistration.class); - assertThat(nacosRegistration.getPort()).isEqualTo(18080); - assertThat(nacosRegistration.getServiceId()).isEqualTo("myapp"); - assertThat(nacosRegistration.getRegisterWeight()).isEqualTo(1F); - }); - } - - @Configuration - @AutoConfigureBefore(NacosDiscoveryAutoConfiguration.class) - static class NacosDiscoveryTestConfiguration { - - @Bean - AutoServiceRegistrationProperties autoServiceRegistrationProperties() { - return new AutoServiceRegistrationProperties(); - } - - @Bean - InetUtils inetUtils() { - return new InetUtils(new InetUtilsProperties()); - } - } - -} \ No newline at end of file diff --git a/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClientTests.java b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClientTests.java new file mode 100644 index 000000000..ad0e6dbf1 --- /dev/null +++ b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClientTests.java @@ -0,0 +1,126 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.nacos; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; + +import com.alibaba.nacos.api.naming.NamingService; +import com.alibaba.nacos.api.naming.pojo.Instance; +import com.alibaba.nacos.api.naming.pojo.ListView; + +import org.junit.Test; +import org.springframework.cloud.client.ServiceInstance; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.springframework.cloud.alibaba.nacos.test.NacosMockTest.serviceInstance; + +/** + * @author xiaojing + */ +public class NacosDiscoveryClientTests { + + private String host = "123.123.123.123"; + private int port = 8888; + private String serviceName = "test-service"; + + @Test + public void testGetServers() throws Exception { + + ArrayList instances = new ArrayList<>(); + + HashMap map = new HashMap<>(); + map.put("test-key", "test-value"); + map.put("secure", "true"); + + instances.add(serviceInstance(serviceName, false, host, port, map)); + + NacosDiscoveryProperties nacosDiscoveryProperties = mock( + NacosDiscoveryProperties.class); + + NamingService namingService = mock(NamingService.class); + + when(nacosDiscoveryProperties.namingServiceInstance()).thenReturn(namingService); + when(namingService.selectInstances(eq(serviceName), eq(true))) + .thenReturn(instances); + + NacosDiscoveryClient discoveryClient = new NacosDiscoveryClient( + nacosDiscoveryProperties); + + List serviceInstances = discoveryClient + .getInstances(serviceName); + + assertThat(serviceInstances.size()).isEqualTo(1); + + ServiceInstance serviceInstance = serviceInstances.get(0); + + assertThat(serviceInstance.getServiceId()).isEqualTo(serviceName); + assertThat(serviceInstance.getHost()).isEqualTo(host); + assertThat(serviceInstance.getPort()).isEqualTo(port); + assertThat(serviceInstance.isSecure()).isEqualTo(true); + assertThat(serviceInstance.getUri().toString()) + .isEqualTo(getUri(serviceInstance)); + assertThat(serviceInstance.getMetadata().get("test-key")).isEqualTo("test-value"); + + } + + @Test + public void testGetAllService() throws Exception { + + ListView 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); + + NacosDiscoveryClient discoveryClient = new NacosDiscoveryClient( + nacosDiscoveryProperties); + + when(nacosDiscoveryProperties.namingServiceInstance()).thenReturn(namingService); + when(namingService.getServicesOfServer(eq(1), eq(Integer.MAX_VALUE))) + .thenReturn(nacosServices); + + List services = discoveryClient.getServices(); + + assertThat(services.size()).isEqualTo(3); + assertThat(services.contains(serviceName + "1")); + assertThat(services.contains(serviceName + "2")); + assertThat(services.contains(serviceName + "3")); + + } + + private String getUri(ServiceInstance instance) { + + if (instance.isSecure()) { + return "https://" + host + ":" + port; + } + + return "http://" + host + ":" + port; + } +} diff --git a/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistrationIpNetworkInterfaceTests.java b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistrationIpNetworkInterfaceTests.java new file mode 100644 index 000000000..ea1f242b1 --- /dev/null +++ b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistrationIpNetworkInterfaceTests.java @@ -0,0 +1,147 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.nacos.registry; + +import java.net.Inet4Address; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.util.Enumeration; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.alibaba.nacos.NacosDiscoveryAutoConfiguration; +import org.springframework.cloud.alibaba.nacos.NacosDiscoveryClientAutoConfiguration; +import org.springframework.cloud.alibaba.nacos.NacosDiscoveryProperties; +import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration; +import org.springframework.cloud.commons.util.InetUtils; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * @author xiaojing + */ + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = NacosAutoServiceRegistrationIpNetworkInterfaceTests.TestConfig.class, properties = { + "spring.application.name=myTestService1", + "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; + + @Test + public void contextLoads() throws Exception { + + assertNotNull("NacosRegistration was not created", registration); + assertNotNull("NacosDiscoveryProperties was not created", properties); + assertNotNull("NacosAutoServiceRegistration was not created", + nacosAutoServiceRegistration); + + checkoutNacosDiscoveryServiceIP(); + + } + + private void checkoutNacosDiscoveryServiceIP() { + assertEquals("NacosDiscoveryProperties service IP was wrong", + getIPFromNetworkInterface(TestConfig.netWorkInterfaceName), + registration.getHost()); + + } + + private String getIPFromNetworkInterface(String networkInterface) { + + if (!TestConfig.hasValidNetworkInterface) { + return inetUtils.findFirstNonLoopbackHostInfo().getIpAddress(); + } + + try { + NetworkInterface netInterface = NetworkInterface.getByName(networkInterface); + + Enumeration inetAddress = netInterface.getInetAddresses(); + while (inetAddress.hasMoreElements()) { + InetAddress currentAddress = inetAddress.nextElement(); + if (currentAddress instanceof Inet4Address + && !currentAddress.isLoopbackAddress()) { + return currentAddress.getHostAddress(); + } + } + return networkInterface; + } + catch (Exception e) { + return networkInterface; + } + } + + @Configuration + @EnableAutoConfiguration + @ImportAutoConfiguration({ AutoServiceRegistrationConfiguration.class, + NacosDiscoveryClientAutoConfiguration.class, + NacosDiscoveryAutoConfiguration.class }) + public static class TestConfig { + + static boolean hasValidNetworkInterface = false; + static String netWorkInterfaceName; + + static { + + try { + Enumeration enumeration = NetworkInterface + .getNetworkInterfaces(); + while (enumeration.hasMoreElements() && !hasValidNetworkInterface) { + NetworkInterface networkInterface = enumeration.nextElement(); + Enumeration inetAddress = networkInterface + .getInetAddresses(); + while (inetAddress.hasMoreElements()) { + InetAddress currentAddress = inetAddress.nextElement(); + if (currentAddress instanceof Inet4Address + && !currentAddress.isLoopbackAddress()) { + hasValidNetworkInterface = true; + netWorkInterfaceName = networkInterface.getName(); + System.setProperty( + "spring.cloud.nacos.discovery.network-interface", + networkInterface.getName()); + break; + } + } + } + + } + catch (Exception e) { + + } + } + } + +} diff --git a/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistrationIpTests.java b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistrationIpTests.java new file mode 100644 index 000000000..aaf72093f --- /dev/null +++ b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistrationIpTests.java @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.nacos.registry; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.alibaba.nacos.NacosDiscoveryAutoConfiguration; +import org.springframework.cloud.alibaba.nacos.NacosDiscoveryClientAutoConfiguration; +import org.springframework.cloud.alibaba.nacos.NacosDiscoveryProperties; +import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * @author xiaojing + */ + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = NacosAutoServiceRegistrationIpTests.TestConfig.class, properties = { + "spring.application.name=myTestService1", + "spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848", + "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; + + @Test + public void contextLoads() throws Exception { + + assertNotNull("NacosRegistration was not created", registration); + assertNotNull("NacosDiscoveryProperties was not created", properties); + assertNotNull("NacosAutoServiceRegistration was not created", + nacosAutoServiceRegistration); + + checkoutNacosDiscoveryServiceIP(); + + } + + private void checkoutNacosDiscoveryServiceIP() { + assertEquals("NacosDiscoveryProperties service IP was wrong", "123.123.123.123", + registration.getHost()); + + } + + @Configuration + @EnableAutoConfiguration + @ImportAutoConfiguration({ AutoServiceRegistrationConfiguration.class, + NacosDiscoveryClientAutoConfiguration.class, + NacosDiscoveryAutoConfiguration.class }) + public static class TestConfig { + } +} diff --git a/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistrationManagementPortTests.java b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistrationManagementPortTests.java new file mode 100644 index 000000000..645e1e4aa --- /dev/null +++ b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistrationManagementPortTests.java @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.nacos.registry; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; +import static org.springframework.cloud.alibaba.nacos.registry.NacosRegistration.MANAGEMENT_PORT; +import static org.springframework.cloud.alibaba.nacos.registry.NacosRegistration.MANAGEMENT_CONTEXT_PATH; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.alibaba.nacos.NacosDiscoveryAutoConfiguration; +import org.springframework.cloud.alibaba.nacos.NacosDiscoveryClientAutoConfiguration; +import org.springframework.cloud.alibaba.nacos.NacosDiscoveryProperties; +import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * @author xiaojing + */ + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = NacosAutoServiceRegistrationManagementPortTests.TestConfig.class, properties = { + "spring.application.name=myTestService1", "management.server.port=8888", + "management.server.servlet.context-path=/test-context-path", + "spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848", + "spring.cloud.nacos.discovery.port=8888" }, webEnvironment = RANDOM_PORT) +public class NacosAutoServiceRegistrationManagementPortTests { + + @Autowired + private NacosRegistration registration; + + @Autowired + private NacosAutoServiceRegistration nacosAutoServiceRegistration; + + @Autowired + private NacosDiscoveryProperties properties; + + @Test + public void contextLoads() throws Exception { + + assertNotNull("NacosRegistration was not created", registration); + assertNotNull("NacosDiscoveryProperties was not created", properties); + assertNotNull("NacosAutoServiceRegistration was not created", + nacosAutoServiceRegistration); + + checkoutNacosDiscoveryManagementData(); + + } + + private void checkoutNacosDiscoveryManagementData() { + assertEquals("NacosDiscoveryProperties management port was wrong", "8888", + properties.getMetadata().get(MANAGEMENT_PORT)); + + assertEquals("NacosDiscoveryProperties management context path was wrong", + "/test-context-path", + properties.getMetadata().get(MANAGEMENT_CONTEXT_PATH)); + + } + + @Configuration + @EnableAutoConfiguration + @ImportAutoConfiguration({ AutoServiceRegistrationConfiguration.class, + NacosDiscoveryClientAutoConfiguration.class, + NacosDiscoveryAutoConfiguration.class }) + public static class TestConfig { + } +} diff --git a/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistrationPortTests.java b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistrationPortTests.java new file mode 100644 index 000000000..eb8050851 --- /dev/null +++ b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistrationPortTests.java @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.nacos.registry; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.alibaba.nacos.NacosDiscoveryAutoConfiguration; +import org.springframework.cloud.alibaba.nacos.NacosDiscoveryClientAutoConfiguration; +import org.springframework.cloud.alibaba.nacos.NacosDiscoveryProperties; +import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * @author xiaojing + */ + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = NacosAutoServiceRegistrationPortTests.TestConfig.class, properties = { + "spring.application.name=myTestService1", + "spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848", + "spring.cloud.nacos.discovery.port=8888" }, webEnvironment = RANDOM_PORT) +public class NacosAutoServiceRegistrationPortTests { + + @Autowired + private NacosRegistration registration; + + @Autowired + private NacosAutoServiceRegistration nacosAutoServiceRegistration; + + @Autowired + private NacosDiscoveryProperties properties; + + @Test + public void contextLoads() throws Exception { + + assertNotNull("NacosRegistration was not created", registration); + assertNotNull("NacosDiscoveryProperties was not created", properties); + assertNotNull("NacosAutoServiceRegistration was not created", + nacosAutoServiceRegistration); + + checkoutNacosDiscoveryServicePort(); + + } + + private void checkoutNacosDiscoveryServicePort() { + assertEquals("NacosDiscoveryProperties service Port was wrong", 8888, + registration.getPort()); + + } + + @Configuration + @EnableAutoConfiguration + @ImportAutoConfiguration({ AutoServiceRegistrationConfiguration.class, + NacosDiscoveryClientAutoConfiguration.class, + NacosDiscoveryAutoConfiguration.class }) + public static class TestConfig { + } +} diff --git a/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistrationTests.java b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistrationTests.java new file mode 100644 index 000000000..9432c97b7 --- /dev/null +++ b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/registry/NacosAutoServiceRegistrationTests.java @@ -0,0 +1,199 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.nacos.registry; + +import java.util.Map; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.cloud.alibaba.nacos.NacosDiscoveryAutoConfiguration; +import org.springframework.cloud.alibaba.nacos.NacosDiscoveryClientAutoConfiguration; +import org.springframework.cloud.alibaba.nacos.NacosDiscoveryProperties; +import org.springframework.cloud.alibaba.nacos.endpoint.NacosDiscoveryEndpoint; +import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration; +import org.springframework.cloud.commons.util.InetUtils; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +/** + * @author xiaojing + */ + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = NacosAutoServiceRegistrationTests.TestConfig.class, properties = { + "spring.application.name=myTestService1", + "spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848", + "spring.cloud.nacos.discovery.endpoint=test-endpoint", + "spring.cloud.nacos.discovery.namespace=test-namespace", + "spring.cloud.nacos.discovery.log-name=test-logName", + "spring.cloud.nacos.discovery.weight=2", + "spring.cloud.nacos.discovery.clusterName=test-cluster", + "spring.cloud.nacos.discovery.namingLoadCacheAtStart=true", + "spring.cloud.nacos.discovery.secure=true", + "spring.cloud.nacos.discovery.accessKey=test-accessKey", + "spring.cloud.nacos.discovery.secretKey=test-secretKey" }, webEnvironment = RANDOM_PORT) +public class NacosAutoServiceRegistrationTests { + + @Autowired + private NacosRegistration registration; + + @Autowired + private NacosAutoServiceRegistration nacosAutoServiceRegistration; + + @LocalServerPort + private int port; + + @Autowired + private NacosDiscoveryProperties properties; + + @Autowired + private InetUtils inetUtils; + + @Test + public void contextLoads() throws Exception { + + assertNotNull("NacosRegistration was not created", registration); + assertNotNull("NacosDiscoveryProperties was not created", properties); + assertNotNull("NacosAutoServiceRegistration was not created", + nacosAutoServiceRegistration); + + checkoutNacosDiscoveryServerAddr(); + checkoutNacosDiscoveryEndpoint(); + checkoutNacosDiscoveryNamespace(); + checkoutNacosDiscoveryLogName(); + checkoutNacosDiscoveryWeight(); + checkoutNacosDiscoveryClusterName(); + checkoutNacosDiscoveryCache(); + checkoutNacosDiscoverySecure(); + checkoutNacosDiscoveryAccessKey(); + checkoutNacosDiscoverySecrectKey(); + + checkoutNacosDiscoveryServiceName(); + checkoutNacosDiscoveryServiceIP(); + checkoutNacosDiscoveryServicePort(); + + checkAutoRegister(); + + checkoutEndpoint(); + + } + + private void checkAutoRegister() { + assertTrue("Nacos Auto Registration was not start", + nacosAutoServiceRegistration.isRunning()); + } + + private void checkoutNacosDiscoveryServerAddr() { + assertEquals("NacosDiscoveryProperties server address was wrong", + "127.0.0.1:8848", properties.getServerAddr()); + + } + + private void checkoutNacosDiscoveryEndpoint() { + assertEquals("NacosDiscoveryProperties endpoint was wrong", "test-endpoint", + properties.getEndpoint()); + + } + + private void checkoutNacosDiscoveryNamespace() { + assertEquals("NacosDiscoveryProperties namespace was wrong", "test-namespace", + properties.getNamespace()); + + } + + private void checkoutNacosDiscoveryLogName() { + assertEquals("NacosDiscoveryProperties logName was wrong", "test-logName", + properties.getLogName()); + } + + private void checkoutNacosDiscoveryWeight() { + assertEquals(2, properties.getWeight(), 0.00000001); + } + + private void checkoutNacosDiscoveryClusterName() { + assertEquals("NacosDiscoveryProperties cluster was wrong", "test-cluster", + properties.getClusterName()); + } + + private void checkoutNacosDiscoveryCache() { + assertEquals("NacosDiscoveryProperties naming load cache was wrong", "true", + properties.getNamingLoadCacheAtStart()); + } + + private void checkoutNacosDiscoverySecure() { + assertEquals("NacosDiscoveryProperties is secure was wrong", true, + properties.isSecure()); + assertEquals("NacosDiscoveryProperties is secure was wrong", "true", + properties.getMetadata().get("secure")); + } + + private void checkoutNacosDiscoveryAccessKey() { + assertEquals("NacosDiscoveryProperties is access key was wrong", "test-accessKey", + properties.getAccessKey()); + } + + private void checkoutNacosDiscoverySecrectKey() { + assertEquals("NacosDiscoveryProperties is secret key was wrong", "test-secretKey", + properties.getSecretKey()); + } + + private void checkoutNacosDiscoveryServiceName() { + assertEquals("NacosDiscoveryProperties service name was wrong", "myTestService1", + properties.getService()); + + } + + private void checkoutNacosDiscoveryServiceIP() { + assertEquals("NacosDiscoveryProperties service IP was wrong", + inetUtils.findFirstNonLoopbackHostInfo().getIpAddress(), + registration.getHost()); + + } + + private void checkoutNacosDiscoveryServicePort() { + assertEquals("NacosDiscoveryProperties service Port was wrong", port, + registration.getPort()); + + } + + private void checkoutEndpoint() throws Exception { + NacosDiscoveryEndpoint nacosDiscoveryEndpoint = new NacosDiscoveryEndpoint( + properties); + Map map = nacosDiscoveryEndpoint.nacosDiscovery(); + assertEquals(map.get("NacosDiscoveryProperties"), properties); + assertEquals(map.get("subscribe"), + properties.namingServiceInstance().getSubscribeServices()); + } + + @Configuration + @EnableAutoConfiguration + @ImportAutoConfiguration({ AutoServiceRegistrationConfiguration.class, + NacosDiscoveryClientAutoConfiguration.class, + NacosDiscoveryAutoConfiguration.class }) + public static class TestConfig { + } +} diff --git a/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosRibbonClientConfigurationTests.java b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosRibbonClientConfigurationTests.java index ffaa79806..cc73df0e6 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosRibbonClientConfigurationTests.java +++ b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosRibbonClientConfigurationTests.java @@ -45,7 +45,6 @@ public class NacosRibbonClientConfigurationTests { @Bean IClientConfig iClientConfig() { - // return new IClientConfig.Builder().s.build(); DefaultClientConfigImpl config = new DefaultClientConfigImpl(); config.setClientName("myapp"); return config; diff --git a/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosServerListTests.java b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosServerListTests.java new file mode 100644 index 000000000..2ce5f4792 --- /dev/null +++ b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosServerListTests.java @@ -0,0 +1,159 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.nacos.ribbon; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.stream.Collectors; + +import com.alibaba.nacos.api.naming.NamingService; +import com.alibaba.nacos.api.naming.pojo.Instance; + +import com.netflix.client.config.IClientConfig; +import org.junit.Test; +import org.springframework.cloud.alibaba.nacos.NacosDiscoveryProperties; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.springframework.cloud.alibaba.nacos.test.NacosMockTest.serviceInstance; + +/** + * @author xiaojing + */ + +public class NacosServerListTests { + + @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(true))).thenReturn(null); + + NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties); + List servers = serverList.getInitialListOfServers(); + assertThat(servers).isEmpty(); + } + + @Test + @SuppressWarnings("unchecked") + public void testGetServers() throws Exception { + + ArrayList instances = new ArrayList<>(); + instances.add(serviceInstance("test-service", false, Collections.emptyMap())); + + NacosDiscoveryProperties nacosDiscoveryProperties = mock( + NacosDiscoveryProperties.class); + + NamingService namingService = mock(NamingService.class); + + when(nacosDiscoveryProperties.namingServiceInstance()).thenReturn(namingService); + when(namingService.selectInstances(eq("test-service"), eq(true))) + .thenReturn(instances); + + IClientConfig clientConfig = mock(IClientConfig.class); + when(clientConfig.getClientName()).thenReturn("test-service"); + NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties); + serverList.initWithNiwsConfig(clientConfig); + List servers = serverList.getInitialListOfServers(); + assertThat(servers).hasSize(1); + + servers = serverList.getUpdatedListOfServers(); + assertThat(servers).hasSize(1); + } + + @Test + @SuppressWarnings("unchecked") + public void testGetServersWithInstanceStatus() throws Exception { + ArrayList instances = new ArrayList<>(); + + HashMap map1 = new HashMap<>(); + map1.put("instanceNum", "1"); + HashMap map2 = new HashMap<>(); + map2.put("instanceNum", "2"); + instances.add(serviceInstance("test-service", false, map1)); + instances.add(serviceInstance("test-service", true, map2)); + + NacosDiscoveryProperties nacosDiscoveryProperties = mock( + NacosDiscoveryProperties.class); + + NamingService namingService = mock(NamingService.class); + + when(nacosDiscoveryProperties.namingServiceInstance()).thenReturn(namingService); + when(namingService.selectInstances(eq("test-service"), 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 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"); + + } + + @Test + public void testUpdateServers() throws Exception { + ArrayList instances = new ArrayList<>(); + + HashMap map = new HashMap<>(); + map.put("instanceNum", "1"); + instances.add(serviceInstance("test-service", true, map)); + + NacosDiscoveryProperties nacosDiscoveryProperties = mock( + NacosDiscoveryProperties.class); + + NamingService namingService = mock(NamingService.class); + + when(nacosDiscoveryProperties.namingServiceInstance()).thenReturn(namingService); + when(namingService.selectInstances(eq("test-service"), 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 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"); + } +} \ No newline at end of file diff --git a/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/test/CommonTestConfig.java b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/test/CommonTestConfig.java new file mode 100644 index 000000000..3f9d28604 --- /dev/null +++ b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/test/CommonTestConfig.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.nacos.test; + +import org.springframework.cloud.client.loadbalancer.LoadBalanced; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; + +/** + * @author xiaojing + */ + +@Configuration +public class CommonTestConfig { + + @Bean + @LoadBalanced + RestTemplate loadBalancedRestTemplate() { + return new RestTemplate(); + } +} \ No newline at end of file diff --git a/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/test/NacosMockTest.java b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/test/NacosMockTest.java new file mode 100644 index 000000000..33efb8ebf --- /dev/null +++ b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/test/NacosMockTest.java @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.nacos.test; + +import java.util.Map; +import java.util.UUID; + +import com.alibaba.nacos.api.naming.pojo.Instance; + +/** + * @author xiaojing + */ +public class NacosMockTest { + + public static Instance serviceInstance(String serviceName, boolean isHealthy, + Map metadata) { + Instance instance = new Instance(); + instance.setInstanceId(UUID.randomUUID().toString()); + instance.setServiceName(serviceName); + instance.setHealthy(isHealthy); + instance.setMetadata(metadata); + return instance; + } + + public static Instance serviceInstance(String serviceName, boolean isHealthy, + String host, int port, Map metadata) { + Instance instance = new Instance(); + instance.setIp(host); + instance.setPort(port); + instance.setServiceName(serviceName); + instance.setHealthy(isHealthy); + instance.setMetadata(metadata); + return instance; + } +}