chengyouling commented on code in PR #3942: URL: https://github.com/apache/servicecomb-java-chassis/pull/3942#discussion_r1329731390
########## service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosDiscovery.java: ########## @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.servicecomb.registry.nacos; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.apache.servicecomb.config.MicroserviceProperties; +import org.apache.servicecomb.registry.api.Discovery; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.util.CollectionUtils; + +import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.api.naming.NamingService; +import com.alibaba.nacos.api.naming.pojo.Instance; + +public class NacosDiscovery implements Discovery<NacosDiscoveryInstance> { + public static final String NACOS_DISCOVERY_ENABLED = "servicecomb.registry.nacos.%s.%s.enabled"; + + private final NacosDiscoveryProperties nacosDiscoveryProperties; + + private Environment environment; + + private MicroserviceProperties microserviceProperties; + + private NamingService namingService; + + @Autowired + public NacosDiscovery(NacosDiscoveryProperties nacosDiscoveryProperties) { + this.nacosDiscoveryProperties = nacosDiscoveryProperties; + } + + @Autowired + public void setMicroserviceProperties(MicroserviceProperties microserviceProperties) { + this.microserviceProperties = microserviceProperties; + } + + @Autowired + public void setEnvironment(Environment environment) { + this.environment = environment; + } + + @Override + public String name() { + return NacosConst.NACOS_REGISTRY_NAME; + } + + @Override + public boolean enabled(String application, String serviceName) { + return environment.getProperty(String.format(NACOS_DISCOVERY_ENABLED, application, serviceName), + boolean.class, true); + } + + @Override + public List<NacosDiscoveryInstance> findServiceInstances(String application, String serviceName) { + try { + List<Instance> instances = namingService.selectInstances(serviceName, application, true); + return convertServiceInstanceList(instances, application); + } catch (NacosException e) { + throw new IllegalStateException("updateMicroserviceInstanceStatus process is interrupted."); + } + } + + private List<NacosDiscoveryInstance> convertServiceInstanceList(List<Instance> isntances, String application) { + if (CollectionUtils.isEmpty(isntances)) { + return Collections.emptyList(); + } + List<NacosDiscoveryInstance> result = new ArrayList<>(); + for (Instance instance : isntances) { + result.add(new NacosDiscoveryInstance(instance, nacosDiscoveryProperties, microserviceProperties, application)); + } + return result; + } + + @Override + public void setInstanceChangedListener(InstanceChangedListener<NacosDiscoveryInstance> instanceChangedListener) { Review Comment: fixed ########## service-registry/registry-nacos/src/main/java/org/apache/servicecomb/registry/nacos/NacosDiscoveryInstance.java: ########## @@ -0,0 +1,136 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.servicecomb.registry.nacos; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.servicecomb.config.MicroserviceProperties; +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 com.alibaba.nacos.api.naming.pojo.Instance; + +public class NacosDiscoveryInstance extends AbstractDiscoveryInstance { + private final NacosDiscoveryProperties nacosDiscoveryProperties; + + private final Instance instance; + + private final MicroserviceProperties microserviceProperties; + + private final Map<String, String> schemas = new HashMap<>(); + + private String application; + + public NacosDiscoveryInstance(Instance instance, NacosDiscoveryProperties nacosDiscoveryProperties, + MicroserviceProperties microserviceProperties, String application) { + this.instance = instance; + this.nacosDiscoveryProperties = nacosDiscoveryProperties; + this.microserviceProperties = microserviceProperties; + this.application = application; + } + + @Override + public MicroserviceInstanceStatus getStatus() { + return instance.isEnabled() ? MicroserviceInstanceStatus.UP : MicroserviceInstanceStatus.DOWN; + } + + @Override + public String getRegistryName() { + return NacosConst.NACOS_REGISTRY_NAME; + } + + @Override + public String getEnvironment() { + return nacosDiscoveryProperties.getNamespace(); + } + + @Override + public String getApplication() { + return application; + } + + @Override + public String getServiceName() { + return instance.getServiceName(); + } + + @Override + public String getAlias() { + return instance.getMetadata().get("alias"); + } + + @Override + public String getVersion() { + return instance.getMetadata().get("version"); + } + + @Override + public DataCenterInfo getDataCenterInfo() { + DataCenterInfo dataCenterInfo = new DataCenterInfo(); + dataCenterInfo.setRegion(instance.getMetadata().get("region")); + dataCenterInfo.setAvailableZone(instance.getMetadata().get("zone")); + return dataCenterInfo; + } + + @Override + public String getDescription() { + return instance.getMetadata().get("description"); + } + + @Override + public Map<String, String> getProperties() { + return instance.getMetadata(); + } + + @Override + public Map<String, String> getSchemas() { + if (CollectionUtils.isEmpty(schemas)) { + Map<String, String> metaData = instance.getMetadata(); + Map<String, String> instanceSchemas = new HashMap<>(); + for (Map.Entry<String, String> entry: metaData.entrySet()) { + if (entry.getKey().startsWith("schema_")) { Review Comment: fixed -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
