diff --git a/demo/demo-nacos/consumer/pom.xml b/demo/demo-nacos/consumer/pom.xml index 6cfe6a7565d..d1d46fbb5b0 100644 --- a/demo/demo-nacos/consumer/pom.xml +++ b/demo/demo-nacos/consumer/pom.xml @@ -34,10 +34,6 @@ org.apache.servicecomb java-chassis-spring-boot-starter-standalone - - org.apache.logging.log4j - log4j-core - diff --git a/demo/demo-nacos/consumer/src/main/resources/application.yml b/demo/demo-nacos/consumer/src/main/resources/application.yml index f22a361e914..0110e6e2748 100644 --- a/demo/demo-nacos/consumer/src/main/resources/application.yml +++ b/demo/demo-nacos/consumer/src/main/resources/application.yml @@ -29,8 +29,5 @@ servicecomb: rest: address: 0.0.0.0:9092 -server: - port: 9092 - diff --git a/demo/demo-nacos/gateway/pom.xml b/demo/demo-nacos/gateway/pom.xml index 82f407a3bf0..157bfa5d948 100644 --- a/demo/demo-nacos/gateway/pom.xml +++ b/demo/demo-nacos/gateway/pom.xml @@ -38,10 +38,6 @@ org.apache.servicecomb edge-core - - org.apache.logging.log4j - log4j-core - diff --git a/demo/demo-nacos/gateway/src/main/resources/application.yml b/demo/demo-nacos/gateway/src/main/resources/application.yml index e40d1f8837a..3bf90e50b73 100644 --- a/demo/demo-nacos/gateway/src/main/resources/application.yml +++ b/demo/demo-nacos/gateway/src/main/resources/application.yml @@ -15,9 +15,6 @@ ## See the License for the specific language governing permissions and ## limitations under the License. ## --------------------------------------------------------------------------- -server: - port: 9090 - servicecomb: service: application: demo-nacos @@ -27,7 +24,6 @@ servicecomb: nacos: enabled: true serverAddr: ${PAAS_CSE_NACOS_ENDPOINT:http://127.0.0.1:8848} - version: 0.0.1 rest: address: 0.0.0.0:9090?sslEnabled=false diff --git a/demo/demo-nacos/pom.xml b/demo/demo-nacos/pom.xml index a70d9a9c1af..5c82f13c086 100644 --- a/demo/demo-nacos/pom.xml +++ b/demo/demo-nacos/pom.xml @@ -51,6 +51,10 @@ org.apache.logging.log4j log4j-slf4j-impl + + org.apache.logging.log4j + log4j-core + diff --git a/demo/demo-nacos/provider/pom.xml b/demo/demo-nacos/provider/pom.xml index f7d240dea6e..9f930ab78e2 100644 --- a/demo/demo-nacos/provider/pom.xml +++ b/demo/demo-nacos/provider/pom.xml @@ -38,10 +38,6 @@ org.apache.servicecomb java-chassis-spring-boot-starter-standalone - - org.apache.logging.log4j - log4j-core - diff --git a/demo/demo-nacos/provider/src/main/resources/application.yml b/demo/demo-nacos/provider/src/main/resources/application.yml index ba3ff163ffd..b24d281cd65 100644 --- a/demo/demo-nacos/provider/src/main/resources/application.yml +++ b/demo/demo-nacos/provider/src/main/resources/application.yml @@ -31,6 +31,3 @@ servicecomb: rest: address: 0.0.0.0:9094 -server: - port: 9094 - diff --git a/demo/demo-nacos/test-client/src/main/java/org/apache/servicecomb/samples/HelloWorldIT.java b/demo/demo-nacos/test-client/src/main/java/org/apache/servicecomb/samples/HelloWorldIT.java index bce14568675..2be179a6a50 100644 --- a/demo/demo-nacos/test-client/src/main/java/org/apache/servicecomb/samples/HelloWorldIT.java +++ b/demo/demo-nacos/test-client/src/main/java/org/apache/servicecomb/samples/HelloWorldIT.java @@ -34,6 +34,6 @@ public void testRestTransport() throws Exception { private void testHelloWorld() { String result = template .getForObject(Config.GATEWAY_URL + "/sayHello?name=World", String.class); - TestMgr.check("\"Hello World\"", result); + TestMgr.check("Hello World", result); } } diff --git a/demo/demo-nacos/test-client/src/main/resources/application.yml b/demo/demo-nacos/test-client/src/main/resources/application.yml index ea1abdb996a..5caa50190ab 100644 --- a/demo/demo-nacos/test-client/src/main/resources/application.yml +++ b/demo/demo-nacos/test-client/src/main/resources/application.yml @@ -15,10 +15,6 @@ ## See the License for the specific language governing permissions and ## limitations under the License. ## --------------------------------------------------------------------------- -# spring boot configurations -server: - port: 9097 # should be same with servicecomb.rest.address to use web container - servicecomb: service: application: demo-nacos @@ -28,8 +24,6 @@ servicecomb: nacos: enabled: true serverAddr: ${PAAS_CSE_NACOS_ENDPOINT:http://127.0.0.1:8848} - serviceName: test-client - version: 0.0.1 rest: address: 0.0.0.0:9097 # should be same with server.port to use web container diff --git a/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosConfiguration.java b/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosConfiguration.java index fcdab47ae79..e1cd555a9e1 100644 --- a/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosConfiguration.java +++ b/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosConfiguration.java @@ -17,6 +17,7 @@ package org.apache.servicecomb.registry.nacos; +import org.apache.servicecomb.config.DataCenterProperties; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; @@ -33,10 +34,12 @@ public NacosDiscoveryProperties nacosDiscoveryProperties() { @Bean public NacosRegistration nacosRegistration( + DataCenterProperties dataCenterProperties, @Qualifier("nacosDiscoveryProperties") NacosDiscoveryProperties nacosDiscoveryProperties, - @Qualifier("nacosDiscovery") NacosDiscovery nacosDiscovery, Environment environment, + Environment environment, @Qualifier("instancesChangeEventListener") InstancesChangeEventListener instancesChangeEventListener) { - return new NacosRegistration(nacosDiscoveryProperties, nacosDiscovery, environment, instancesChangeEventListener); + return new NacosRegistration(dataCenterProperties, nacosDiscoveryProperties, environment, + instancesChangeEventListener); } @Bean diff --git a/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosConst.java b/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosConst.java index 08308f7084d..b3a7a5dab37 100644 --- a/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosConst.java +++ b/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosConst.java @@ -18,6 +18,24 @@ package org.apache.servicecomb.registry.nacos; public class NacosConst { + public static final String PROPERTY_VERSION = "scb-version"; + + public static final String PROPERTY_ALIAS = "scb-alias"; + + public static final String PROPERTY_DESCRIPTION = "scb-description"; + + public static final String PROPERTY_ENDPOINT = "scb-endpoint"; + + public static final String PROPERTY_REGION = "scb-region"; + + public static final String PROPERTY_ZONE = "scb-zone"; + + public static final String PROPERTY_DATACENTER = "scb-datacenter"; + + public static final String PROPERTY_SCHEMA_PREFIX = "scb-schema-"; + + public static final String ENDPOINT_PROPERTY_SEPARATOR = ","; + public static final String NACOS_REGISTRY_PREFIX = "servicecomb.registry.nacos"; public static final String SERVER_ADDR = "serverAddr"; @@ -39,6 +57,4 @@ public class NacosConst { public static final String NAMING_LOAD_CACHE_AT_START = "namingLoadCacheAtStart"; public static final String NACOS_REGISTRY_NAME = "nacos-registry"; - - public static final String SCHEMA_PREFIX = "schema_"; } diff --git a/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosDiscovery.java b/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosDiscovery.java index 7330d69b7eb..a606342386b 100644 --- a/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosDiscovery.java +++ b/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosDiscovery.java @@ -66,20 +66,20 @@ public boolean enabled(String application, String serviceName) { @Override public List findServiceInstances(String application, String serviceName) { try { - List instances = namingService.selectInstances(serviceName, application, true); + List instances = namingService.getAllInstances(serviceName, application, true); return convertServiceInstanceList(instances, application); } catch (NacosException e) { throw new IllegalStateException("updateMicroserviceInstanceStatus process is interrupted."); } } - private List convertServiceInstanceList(List isntances, String application) { - if (CollectionUtils.isEmpty(isntances)) { + private List convertServiceInstanceList(List instances, String application) { + if (CollectionUtils.isEmpty(instances)) { return Collections.emptyList(); } List result = new ArrayList<>(); - for (Instance instance : isntances) { - result.add(new NacosDiscoveryInstance(instance, nacosDiscoveryProperties, application)); + for (Instance instance : instances) { + result.add(new NacosDiscoveryInstance(instance, application, environment)); } return result; } @@ -96,7 +96,7 @@ public void onInstanceChangedEvent(InstancesChangeEvent event) { @Override public void init() { - namingService = NamingServiceManager.buildNamingService(nacosDiscoveryProperties); + namingService = NamingServiceManager.buildNamingService(environment, nacosDiscoveryProperties); } @Override diff --git a/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosDiscoveryInstance.java b/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosDiscoveryInstance.java index 67970f53193..bdcf70af27a 100644 --- a/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosDiscoveryInstance.java +++ b/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosDiscoveryInstance.java @@ -17,32 +17,39 @@ package org.apache.servicecomb.registry.nacos; -import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import org.apache.commons.lang3.StringUtils; +import org.apache.servicecomb.config.BootStrapProperties; import org.apache.servicecomb.registry.api.AbstractDiscoveryInstance; import org.apache.servicecomb.registry.api.DataCenterInfo; import org.apache.servicecomb.registry.api.MicroserviceInstanceStatus; -import org.springframework.util.CollectionUtils; +import org.springframework.core.env.Environment; import com.alibaba.nacos.api.naming.pojo.Instance; public class NacosDiscoveryInstance extends AbstractDiscoveryInstance { - private final NacosDiscoveryProperties nacosDiscoveryProperties; - private final Instance instance; - private final Map schemas = new HashMap<>(); + private final String application; + + private final Environment environment; + + private final Map schemas; - private String application; + private final List endpoints; - public NacosDiscoveryInstance(Instance instance, NacosDiscoveryProperties nacosDiscoveryProperties, - String application) { + public NacosDiscoveryInstance(Instance instance, String application, + Environment environment) { this.instance = instance; - this.nacosDiscoveryProperties = nacosDiscoveryProperties; + this.environment = environment; this.application = application; + this.endpoints = readEndpoints(instance); + this.schemas = readSchemas(instance); } @Override @@ -57,12 +64,12 @@ public String getRegistryName() { @Override public String getEnvironment() { - return nacosDiscoveryProperties.getNamespace(); + return BootStrapProperties.readServiceEnvironment(environment); } @Override public String getApplication() { - return application; + return this.application; } @Override @@ -72,25 +79,26 @@ public String getServiceName() { @Override public String getAlias() { - return instance.getMetadata().get("alias"); + return instance.getMetadata().get(NacosConst.PROPERTY_ALIAS); } @Override public String getVersion() { - return instance.getMetadata().get("version"); + return instance.getMetadata().get(NacosConst.PROPERTY_VERSION); } @Override public DataCenterInfo getDataCenterInfo() { DataCenterInfo dataCenterInfo = new DataCenterInfo(); - dataCenterInfo.setRegion(instance.getMetadata().get("region")); - dataCenterInfo.setAvailableZone(instance.getMetadata().get("zone")); + dataCenterInfo.setRegion(instance.getMetadata().get(NacosConst.PROPERTY_REGION)); + dataCenterInfo.setAvailableZone(instance.getMetadata().get(NacosConst.PROPERTY_ZONE)); + dataCenterInfo.setName(instance.getMetadata().get(NacosConst.PROPERTY_DATACENTER)); return dataCenterInfo; } @Override public String getDescription() { - return instance.getMetadata().get("description"); + return instance.getMetadata().get(NacosConst.PROPERTY_DESCRIPTION); } @Override @@ -100,31 +108,34 @@ public Map getProperties() { @Override public Map getSchemas() { - if (CollectionUtils.isEmpty(schemas)) { - Map metaData = instance.getMetadata(); - Map instanceSchemas = new HashMap<>(); - for (Map.Entry entry: metaData.entrySet()) { - if (entry.getKey().startsWith(NacosConst.SCHEMA_PREFIX)) { - instanceSchemas.put(entry.getKey().substring(NacosConst.SCHEMA_PREFIX.length()), entry.getValue()); - } + return schemas; + } + + private static Map readSchemas(Instance instance) { + Map metaData = instance.getMetadata(); + Map instanceSchemas = new HashMap<>(); + for (Map.Entry entry : metaData.entrySet()) { + if (entry.getKey().startsWith(NacosConst.PROPERTY_SCHEMA_PREFIX)) { + instanceSchemas.put(entry.getKey().substring(NacosConst.PROPERTY_SCHEMA_PREFIX.length()), + entry.getValue()); } - schemas.putAll(instanceSchemas); } - return schemas; + return instanceSchemas; } @Override public List getEndpoints() { - List endpoints = new ArrayList<>(); - StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append("rest://") - .append(instance.getIp()) - .append(":") - .append(instance.getPort()); - endpoints.add(stringBuilder.toString()); return endpoints; } + private static List readEndpoints(Instance instance) { + if (StringUtils.isEmpty(instance.getMetadata().get(NacosConst.PROPERTY_ENDPOINT))) { + return Collections.emptyList(); + } + return Arrays.asList(instance.getMetadata().get(NacosConst.PROPERTY_ENDPOINT) + .split(NacosConst.ENDPOINT_PROPERTY_SEPARATOR)); + } + @Override public String getInstanceId() { return instance.getInstanceId(); diff --git a/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosDiscoveryProperties.java b/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosDiscoveryProperties.java index 50c49dc5a84..5d27b7ec023 100644 --- a/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosDiscoveryProperties.java +++ b/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosDiscoveryProperties.java @@ -19,18 +19,12 @@ import java.util.HashMap; import java.util.Map; -import java.util.Objects; -import java.util.Properties; public class NacosDiscoveryProperties { private boolean enabled = true; private String serverAddr; - private String ip; - - private String namespace; - private Map metadata = new HashMap<>(); private boolean ephemeral = true; @@ -53,8 +47,6 @@ public class NacosDiscoveryProperties { private String logName; - private boolean secure; - public String getServerAddr() { return serverAddr; } @@ -71,22 +63,6 @@ public void setEnabled(boolean enabled) { this.enabled = enabled; } - public String getIp() { - return ip; - } - - public void setIp(String ip) { - this.ip = ip; - } - - public String getNamespace() { - return namespace; - } - - public void setNamespace(String namespace) { - this.namespace = namespace; - } - public Map getMetadata() { return metadata; } @@ -174,26 +150,4 @@ public String getLogName() { public void setLogName(String logName) { this.logName = logName; } - - public boolean isSecure() { - return secure; - } - - public void setSecure(boolean secure) { - this.secure = secure; - } - - public Properties getProperties() { - Properties properties = new Properties(); - properties.put(NacosConst.SERVER_ADDR, this.serverAddr); - properties.put(NacosConst.USERNAME, Objects.toString(this.username, "")); - properties.put(NacosConst.PASSWORD, Objects.toString(this.password, "")); - properties.put(NacosConst.NAMESPACE, Objects.toString(this.namespace, "")); - properties.put(NacosConst.NACOS_NAMING_LOG_NAME, Objects.toString(this.logName, "")); - properties.put(NacosConst.ACCESS_KEY, Objects.toString(this.accessKey, "")); - properties.put(NacosConst.SECRET_KEY, Objects.toString(this.secretKey, "")); - properties.put(NacosConst.CLUSTER_NAME, this.clusterName); - properties.put(NacosConst.NAMING_LOAD_CACHE_AT_START, this.namingLoadCacheAtStart); - return properties; - } } diff --git a/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosMicroserviceHandler.java b/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosMicroserviceHandler.java index 8f1e66ac1fd..c49b5cd69af 100644 --- a/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosMicroserviceHandler.java +++ b/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosMicroserviceHandler.java @@ -17,7 +17,6 @@ package org.apache.servicecomb.registry.nacos; -import java.lang.management.ManagementFactory; import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -26,7 +25,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.servicecomb.config.BootStrapProperties; import org.apache.servicecomb.config.ConfigUtil; -import org.apache.servicecomb.foundation.common.net.NetUtils; +import org.apache.servicecomb.config.DataCenterProperties; import org.springframework.core.env.Environment; import com.alibaba.nacos.api.naming.pojo.Instance; @@ -46,38 +45,31 @@ public class NacosMicroserviceHandler { private static final String INSTANCE_PROPS = "SERVICECOMB_INSTANCE_PROPS"; - public static Instance createMicroserviceInstance(NacosDiscoveryProperties properties, Environment environment) { + public static Instance createMicroserviceInstance( + DataCenterProperties dataCenterProperties, NacosDiscoveryProperties properties, Environment environment) { Instance instance = new Instance(); - instance.setIp(StringUtils.isEmpty(properties.getIp()) ? NetUtils.getHostName() : properties.getIp()); - instance.setPort(getEnvPort(environment)); - instance.setInstanceId(buildInstanceId()); + instance.setServiceName(BootStrapProperties.readServiceName(environment)); instance.setWeight(properties.getWeight()); instance.setEnabled(properties.isInstanceEnabled()); + instance.setClusterName(properties.getClusterName()); + instance.setEphemeral(properties.isEphemeral()); + Map metadata = properties.getMetadata(); - metadata.put("version", BootStrapProperties.readServiceVersion(environment)); - metadata.put("alias", BootStrapProperties.readServiceAlias(environment)); - metadata.put("description", BootStrapProperties.readServiceDescription(environment)); - metadata.put("secure", String.valueOf(properties.isSecure())); + metadata.put(NacosConst.PROPERTY_VERSION, BootStrapProperties.readServiceVersion(environment)); + metadata.put(NacosConst.PROPERTY_ALIAS, BootStrapProperties.readServiceAlias(environment)); + metadata.put(NacosConst.PROPERTY_DESCRIPTION, BootStrapProperties.readServiceDescription(environment)); + metadata.put(NacosConst.PROPERTY_DATACENTER, dataCenterProperties.getName()); + metadata.put(NacosConst.PROPERTY_REGION, dataCenterProperties.getRegion()); + metadata.put(NacosConst.PROPERTY_ZONE, dataCenterProperties.getAvailableZone()); if (!StringUtils.isEmpty(environment.getProperty(VERSION_MAPPING)) && !StringUtils.isEmpty(environment.getProperty(environment.getProperty(VERSION_MAPPING)))) { metadata.put("version", environment.getProperty(environment.getProperty(VERSION_MAPPING))); } metadata.putAll(genCasProperties(environment)); instance.setMetadata(metadata); - instance.setClusterName(properties.getClusterName()); - instance.setEphemeral(properties.isEphemeral()); - instance.setServiceName(BootStrapProperties.readServiceName(environment)); return instance; } - private static String buildInstanceId() { - return System.currentTimeMillis() + "-" + ManagementFactory.getRuntimeMXBean().getPid(); - } - - private static int getEnvPort(Environment environment) { - return Integer.parseInt(environment.getProperty("server.port")); - } - private static Map genCasProperties(Environment environment) { Map properties = new HashMap<>(); if (!StringUtils.isEmpty(environment.getProperty(CAS_APPLICATION_ID))) { diff --git a/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosRegistration.java b/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosRegistration.java index 754a05f709c..79cd5841185 100644 --- a/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosRegistration.java +++ b/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosRegistration.java @@ -17,10 +17,14 @@ package org.apache.servicecomb.registry.nacos; +import java.lang.management.ManagementFactory; import java.util.List; import java.util.Map; -import org.apache.servicecomb.config.BootStrapProperties; +import org.apache.servicecomb.config.DataCenterProperties; +import org.apache.servicecomb.core.Endpoint; +import org.apache.servicecomb.core.invocation.endpoint.EndpointUtils; +import org.apache.servicecomb.foundation.common.net.URIEndpointObject; import org.apache.servicecomb.registry.api.MicroserviceInstanceStatus; import org.apache.servicecomb.registry.api.Registration; import org.springframework.beans.factory.annotation.Autowired; @@ -36,68 +40,88 @@ public class NacosRegistration implements Registration { private final NacosDiscoveryProperties nacosDiscoveryProperties; - private final NacosDiscovery nacosDiscovery; - private final Environment environment; private final InstancesChangeEventListener instancesChangeEventListener; - private NacosRegistrationInstance nacosRegistrationInstance; + private final String instanceId; - private Instance instance; + private final DataCenterProperties dataCenterProperties; - private String serviceId; + private NacosRegistrationInstance nacosRegistrationInstance; - private String application; + private Instance instance; private NamingService namingService; private NamingMaintainService namingMaintainService; @Autowired - public NacosRegistration(NacosDiscoveryProperties nacosDiscoveryProperties, NacosDiscovery nacosDiscovery, + public NacosRegistration(DataCenterProperties dataCenterProperties, NacosDiscoveryProperties nacosDiscoveryProperties, Environment environment, InstancesChangeEventListener instancesChangeEventListener) { + this.instanceId = buildInstanceId(); + this.dataCenterProperties = dataCenterProperties; this.nacosDiscoveryProperties = nacosDiscoveryProperties; - this.nacosDiscovery = nacosDiscovery; this.environment = environment; this.instancesChangeEventListener = instancesChangeEventListener; } @Override public void init() { - instance = NacosMicroserviceHandler.createMicroserviceInstance(nacosDiscoveryProperties, environment); + instance = NacosMicroserviceHandler.createMicroserviceInstance(dataCenterProperties, nacosDiscoveryProperties, + environment); + instance.setInstanceId(instanceId); nacosRegistrationInstance = new NacosRegistrationInstance(instance, nacosDiscoveryProperties, environment); - serviceId = BootStrapProperties.readServiceName(environment); - application = BootStrapProperties.readApplication(environment); - namingService = NamingServiceManager.buildNamingService(nacosDiscoveryProperties); - namingMaintainService = NamingServiceManager.buildNamingMaintainService(nacosDiscoveryProperties); + + namingService = NamingServiceManager.buildNamingService(environment, nacosDiscoveryProperties); + namingMaintainService = NamingServiceManager.buildNamingMaintainService(environment, nacosDiscoveryProperties); NotifyCenter.registerSubscriber(instancesChangeEventListener); } @Override public void run() { try { - addSchemas(nacosRegistrationInstance.getSchemas(), instance.getMetadata()); - namingService.registerInstance(serviceId, application, instance); + addSchemas(nacosRegistrationInstance.getSchemas(), instance); + addEndpoints(nacosRegistrationInstance.getEndpoints(), instance); + namingService.registerInstance(nacosRegistrationInstance.getServiceName(), + nacosRegistrationInstance.getApplication(), instance); } catch (NacosException e) { throw new IllegalStateException("registry process is interrupted."); } } - private static void addSchemas(Map schemas, Map metadata) { + private static void addSchemas(Map schemas, Instance instance) { if (CollectionUtils.isEmpty(schemas)) { return; } for (Map.Entry entry : schemas.entrySet()) { - metadata.put(NacosConst.SCHEMA_PREFIX + entry.getKey(), entry.getValue()); + instance.addMetadata(NacosConst.PROPERTY_SCHEMA_PREFIX + entry.getKey(), entry.getValue()); + } + } + + private static void addEndpoints(List endpoints, Instance instance) { + if (endpoints.isEmpty()) { + return; + } + + for (String endpoint : endpoints) { + Endpoint temp = EndpointUtils.parse(endpoint); + if (temp.getAddress() instanceof URIEndpointObject) { + instance.setIp(((URIEndpointObject) temp.getAddress()).getHostOrIp()); + instance.setPort(((URIEndpointObject) temp.getAddress()).getPort()); + break; + } } + + instance.addMetadata(NacosConst.PROPERTY_ENDPOINT, String.join(NacosConst.ENDPOINT_PROPERTY_SEPARATOR, endpoints)); } @Override public void destroy() { try { - namingService.deregisterInstance(serviceId, application, instance); + namingService.deregisterInstance(nacosRegistrationInstance.getServiceName(), + nacosRegistrationInstance.getApplication(), instance); } catch (NacosException e) { throw new IllegalStateException("destroy process is interrupted."); } @@ -116,15 +140,12 @@ public NacosRegistrationInstance getMicroserviceInstance() { @Override public boolean updateMicroserviceInstanceStatus(MicroserviceInstanceStatus status) { try { - List instances = nacosDiscovery.findServiceInstances(application, serviceId); - if (CollectionUtils.isEmpty(instances)) { - return false; - } instance.setEnabled(MicroserviceInstanceStatus.DOWN != status); - namingMaintainService.updateInstance(serviceId, application, instance); + namingMaintainService.updateInstance(nacosRegistrationInstance.getServiceName(), + nacosRegistrationInstance.getApplication(), instance); return true; } catch (NacosException e) { - throw new IllegalStateException("updateMicroserviceInstanceStatus process is interrupted."); + throw new IllegalStateException(e); } } @@ -140,11 +161,15 @@ public void addEndpoint(String endpoint) { @Override public void addProperty(String key, String value) { - instance.getMetadata().put(key, value); + instance.addMetadata(key, value); } @Override public boolean enabled() { return nacosDiscoveryProperties.isEnabled(); } + + private static String buildInstanceId() { + return System.currentTimeMillis() + "-" + ManagementFactory.getRuntimeMXBean().getPid(); + } } diff --git a/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosRegistrationInstance.java b/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosRegistrationInstance.java index 9bb3013c0fd..9b5cd3a16c7 100644 --- a/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosRegistrationInstance.java +++ b/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosRegistrationInstance.java @@ -50,7 +50,7 @@ public NacosRegistrationInstance(Instance instance, NacosDiscoveryProperties nac @Override public String getEnvironment() { - return nacosDiscoveryProperties.getNamespace(); + return BootStrapProperties.readServiceEnvironment(environment); } @Override diff --git a/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NamingServiceManager.java b/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NamingServiceManager.java index 85b4d167720..2e68213b5bb 100644 --- a/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NamingServiceManager.java +++ b/service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NamingServiceManager.java @@ -18,6 +18,10 @@ package org.apache.servicecomb.registry.nacos; import java.util.Objects; +import java.util.Properties; + +import org.apache.servicecomb.config.BootStrapProperties; +import org.springframework.core.env.Environment; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.NamingMaintainService; @@ -30,12 +34,12 @@ public class NamingServiceManager { private static volatile NamingMaintainService namingMaintainService; - public static NamingService buildNamingService(NacosDiscoveryProperties properties) { + public static NamingService buildNamingService(Environment environment, NacosDiscoveryProperties properties) { if (Objects.isNull(namingService)) { synchronized (NamingServiceManager.class) { if (Objects.isNull(namingService)) { try { - namingService = new NacosNamingService(properties.getProperties()); + namingService = new NacosNamingService(getProperties(environment, properties)); } catch (NacosException e) { throw new IllegalStateException("build namingService failed."); } @@ -45,12 +49,13 @@ public static NamingService buildNamingService(NacosDiscoveryProperties properti return namingService; } - public static NamingMaintainService buildNamingMaintainService(NacosDiscoveryProperties properties) { + public static NamingMaintainService buildNamingMaintainService(Environment environment, + NacosDiscoveryProperties properties) { if (Objects.isNull(namingMaintainService)) { synchronized (NamingServiceManager.class) { if (Objects.isNull(namingMaintainService)) { try { - namingMaintainService = new NacosNamingMaintainService(properties.getProperties()); + namingMaintainService = new NacosNamingMaintainService(getProperties(environment, properties)); } catch (NacosException e) { throw new IllegalStateException("build namingMaintainService failed."); } @@ -59,4 +64,19 @@ public static NamingMaintainService buildNamingMaintainService(NacosDiscoveryPro } return namingMaintainService; } + + private static Properties getProperties(Environment environment, + NacosDiscoveryProperties nacosDiscoveryProperties) { + Properties properties = new Properties(); + properties.put(NacosConst.NAMESPACE, BootStrapProperties.readServiceEnvironment(environment)); + properties.put(NacosConst.SERVER_ADDR, nacosDiscoveryProperties.getServerAddr()); + properties.put(NacosConst.USERNAME, Objects.toString(nacosDiscoveryProperties.getUsername(), "")); + properties.put(NacosConst.PASSWORD, Objects.toString(nacosDiscoveryProperties.getPassword(), "")); + properties.put(NacosConst.NACOS_NAMING_LOG_NAME, Objects.toString(nacosDiscoveryProperties.getLogName(), "")); + properties.put(NacosConst.ACCESS_KEY, Objects.toString(nacosDiscoveryProperties.getAccessKey(), "")); + properties.put(NacosConst.SECRET_KEY, Objects.toString(nacosDiscoveryProperties.getSecretKey(), "")); + properties.put(NacosConst.CLUSTER_NAME, nacosDiscoveryProperties.getClusterName()); + properties.put(NacosConst.NAMING_LOAD_CACHE_AT_START, nacosDiscoveryProperties.getNamingLoadCacheAtStart()); + return properties; + } }