This is an automated email from the ASF dual-hosted git repository. liubao pushed a commit to branch liubao_master in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git
commit 6fa467752891d887ca3098b505a858b6cac0e9fd Author: liubao <bi...@qq.com> AuthorDate: Wed May 22 16:16:08 2024 +0800 [SCB-2882]add services discovery api and support global instance id bean --- .../servicecomb/registry/RegistrationId.java | 41 ++++++++++++++++++++++ .../registry/RegistryConfiguration.java | 5 +++ .../apache/servicecomb/registry/api/Discovery.java | 8 +++++ .../servicecomb/registry/TestDiscoveryManager.java | 5 +++ .../servicecomb/registry/lightweight/Self.java | 16 +++++---- .../registry/lightweight/StoreServiceTest.java | 6 ++-- .../servicecomb/localregistry/LocalDiscovery.java | 5 +++ .../localregistry/LocalRegistrationInstance.java | 12 +++---- .../localregistry/LocalRegistryConfiguration.java | 7 ++-- .../localregistry/LocalRegistryStore.java | 7 ++++ .../registry/nacos/NacosConfiguration.java | 5 +-- .../servicecomb/registry/nacos/NacosDiscovery.java | 10 ++++++ .../registry/nacos/NacosRegistration.java | 10 ++---- .../servicecomb/registry/sc/SCDiscovery.java | 9 +++++ .../servicecomb/registry/sc/SCRegistration.java | 9 +++++ .../zeroconfig/ZeroConfigDiscovery.java | 9 +++++ .../registry/zookeeper/ZookeeperDiscovery.java | 28 +++++++++++++++ .../registry/zookeeper/ZookeeperRegistration.java | 15 ++++---- 18 files changed, 174 insertions(+), 33 deletions(-) diff --git a/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/RegistrationId.java b/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/RegistrationId.java new file mode 100644 index 000000000..9932f5fdf --- /dev/null +++ b/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/RegistrationId.java @@ -0,0 +1,41 @@ +/* + * 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; + +import java.util.UUID; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RegistrationId { + private static final Logger LOGGER = LoggerFactory.getLogger(RegistrationId.class); + + private final String instanceId; + + public RegistrationId() { + this.instanceId = buildInstanceId(); + LOGGER.info("initialized global registration id {}", this.instanceId); + } + + public String getInstanceId() { + return instanceId; + } + + private static String buildInstanceId() { + return UUID.randomUUID().toString(); + } +} diff --git a/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/RegistryConfiguration.java b/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/RegistryConfiguration.java index b3c83b64a..6581d266b 100644 --- a/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/RegistryConfiguration.java +++ b/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/RegistryConfiguration.java @@ -66,4 +66,9 @@ public class RegistryConfiguration { public MicroserviceInstanceCache microserviceInstanceCache(DiscoveryManager discoveryManager) { return new MicroserviceInstanceCache(discoveryManager); } + + @Bean + public RegistrationId registrationId() { + return new RegistrationId(); + } } diff --git a/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/api/Discovery.java b/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/api/Discovery.java index 6fb382835..0c57ed6b6 100644 --- a/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/api/Discovery.java +++ b/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/api/Discovery.java @@ -55,6 +55,14 @@ public interface Discovery<D extends DiscoveryInstance> extends SPIEnabled, SPIO */ List<D> findServiceInstances(String application, String serviceName); + /** + * Find all services of this application. + * + * @param application application + * @return all services match the criteria. + */ + List<String> findServices(String application); + /** * Discovery can call InstanceChangedListener when instance get changed. */ diff --git a/foundations/foundation-registry/src/test/java/org/apache/servicecomb/registry/TestDiscoveryManager.java b/foundations/foundation-registry/src/test/java/org/apache/servicecomb/registry/TestDiscoveryManager.java index 77762b0fc..bd50cc857 100644 --- a/foundations/foundation-registry/src/test/java/org/apache/servicecomb/registry/TestDiscoveryManager.java +++ b/foundations/foundation-registry/src/test/java/org/apache/servicecomb/registry/TestDiscoveryManager.java @@ -112,6 +112,11 @@ public class TestDiscoveryManager { return null; } + @Override + public List<String> findServices(String application) { + return null; + } + @Override public void setInstanceChangedListener(InstanceChangedListener<MyDiscoveryInstance> instanceChangedListener) { diff --git a/service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/Self.java b/service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/Self.java index d79aa19f2..d6acccbf7 100644 --- a/service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/Self.java +++ b/service-registry/registry-lightweight/src/main/java/org/apache/servicecomb/registry/lightweight/Self.java @@ -17,9 +17,8 @@ package org.apache.servicecomb.registry.lightweight; -import java.util.UUID; - import org.apache.servicecomb.core.BootListener; +import org.apache.servicecomb.registry.RegistrationId; import org.apache.servicecomb.registry.lightweight.model.Microservice; import org.apache.servicecomb.registry.lightweight.model.MicroserviceFactory; import org.apache.servicecomb.registry.lightweight.model.MicroserviceInstance; @@ -41,11 +40,18 @@ public class Self implements InitializingBean, BootListener { private Environment environment; + private RegistrationId registrationId; + @Autowired public void setEnvironment(Environment environment) { this.environment = environment; } + @Autowired + public void setRegistrationId(RegistrationId registrationId) { + this.registrationId = registrationId; + } + @Override public void afterPropertiesSet() { init(environment); @@ -61,7 +67,7 @@ public class Self implements InitializingBean, BootListener { microservice.getVersion())); instance = microservice.getInstance() - .instanceId(buildInstanceId()) + .instanceId(registrationId.getInstanceId()) .serviceId(microservice.getServiceId()); microserviceInfo @@ -72,10 +78,6 @@ public class Self implements InitializingBean, BootListener { return this; } - private static String buildInstanceId() { - return UUID.randomUUID().toString(); - } - @Override public int getOrder() { return Integer.MAX_VALUE; diff --git a/service-registry/registry-lightweight/src/test/java/org/apache/servicecomb/registry/lightweight/StoreServiceTest.java b/service-registry/registry-lightweight/src/test/java/org/apache/servicecomb/registry/lightweight/StoreServiceTest.java index bab02249c..efaf9665d 100644 --- a/service-registry/registry-lightweight/src/test/java/org/apache/servicecomb/registry/lightweight/StoreServiceTest.java +++ b/service-registry/registry-lightweight/src/test/java/org/apache/servicecomb/registry/lightweight/StoreServiceTest.java @@ -23,6 +23,7 @@ import java.util.concurrent.CompletableFuture; import org.apache.servicecomb.core.Endpoint; import org.apache.servicecomb.foundation.test.scaffolding.time.MockTicker; +import org.apache.servicecomb.registry.RegistrationId; import org.apache.servicecomb.registry.api.MicroserviceInstanceStatus; import org.apache.servicecomb.registry.lightweight.model.Microservice; import org.apache.servicecomb.registry.lightweight.store.InstanceStore; @@ -80,8 +81,9 @@ public class StoreServiceTest { protected RegisterRequest createRegisterRequest() { return new MockRegisterRequest(); } - } - .init(environment) + }; + self.setRegistrationId(new RegistrationId()); + self.init(environment) .addSchema("schema-1", "s1") .addEndpoint("rest://1.1.1.1:80"); diff --git a/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/LocalDiscovery.java b/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/LocalDiscovery.java index a439974d3..4d3a0a6e8 100644 --- a/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/LocalDiscovery.java +++ b/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/LocalDiscovery.java @@ -62,6 +62,11 @@ public class LocalDiscovery implements Discovery<LocalDiscoveryInstance> { return this.localRegistryStore.findServiceInstances(application, serviceName); } + @Override + public List<String> findServices(String application) { + return this.localRegistryStore.findServices(application); + } + @Override public void setInstanceChangedListener(InstanceChangedListener<LocalDiscoveryInstance> instanceChangedListener) { diff --git a/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/LocalRegistrationInstance.java b/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/LocalRegistrationInstance.java index 4a45dcfcc..2262beab4 100644 --- a/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/LocalRegistrationInstance.java +++ b/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/LocalRegistrationInstance.java @@ -20,11 +20,11 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.UUID; import org.apache.servicecomb.config.BootStrapProperties; import org.apache.servicecomb.config.DataCenterProperties; import org.apache.servicecomb.core.provider.LocalOpenAPIRegistry; +import org.apache.servicecomb.registry.RegistrationId; import org.apache.servicecomb.registry.api.DataCenterInfo; import org.apache.servicecomb.registry.api.MicroserviceInstanceStatus; import org.apache.servicecomb.registry.api.RegistrationInstance; @@ -47,7 +47,9 @@ public class LocalRegistrationInstance implements RegistrationInstance { public LocalRegistrationInstance( Environment environment, - DataCenterProperties dataCenterProperties, LocalOpenAPIRegistry localOpenAPIRegistry) { + DataCenterProperties dataCenterProperties, + LocalOpenAPIRegistry localOpenAPIRegistry, + RegistrationId registrationId) { this.environment = environment; this.localOpenAPIRegistry = localOpenAPIRegistry; @@ -58,11 +60,7 @@ public class LocalRegistrationInstance implements RegistrationInstance { this.properties.putAll(BootStrapProperties.readServiceProperties(environment)); - this.instanceId = buildInstanceId(); - } - - private static String buildInstanceId() { - return UUID.randomUUID().toString(); + this.instanceId = registrationId.getInstanceId(); } @Override diff --git a/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/LocalRegistryConfiguration.java b/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/LocalRegistryConfiguration.java index 5a98249da..9ebdcca36 100644 --- a/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/LocalRegistryConfiguration.java +++ b/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/LocalRegistryConfiguration.java @@ -18,6 +18,7 @@ package org.apache.servicecomb.localregistry; import org.apache.servicecomb.config.DataCenterProperties; import org.apache.servicecomb.core.provider.LocalOpenAPIRegistry; +import org.apache.servicecomb.registry.RegistrationId; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; @@ -27,8 +28,10 @@ public class LocalRegistryConfiguration { @Bean public LocalRegistrationInstance localRegistrationInstance( Environment environment, - DataCenterProperties dataCenterProperties, LocalOpenAPIRegistry localOpenAPIRegistry) { - return new LocalRegistrationInstance(environment, dataCenterProperties, localOpenAPIRegistry); + DataCenterProperties dataCenterProperties, + LocalOpenAPIRegistry localOpenAPIRegistry, + RegistrationId registrationId) { + return new LocalRegistrationInstance(environment, dataCenterProperties, localOpenAPIRegistry, registrationId); } @Bean diff --git a/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/LocalRegistryStore.java b/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/LocalRegistryStore.java index 36218068c..3af782e59 100644 --- a/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/LocalRegistryStore.java +++ b/service-registry/registry-local/src/main/java/org/apache/servicecomb/localregistry/LocalRegistryStore.java @@ -148,4 +148,11 @@ public class LocalRegistryStore { } return instances; } + + public List<String> findServices(String application) { + if (microserviceInstanceMap.get(application) == null) { + return Collections.emptyList(); + } + return microserviceInstanceMap.get(application).keySet().stream().toList(); + } } 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 2a1adbaae..24eaf50a3 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 @@ -18,6 +18,7 @@ package org.apache.servicecomb.registry.nacos; import org.apache.servicecomb.config.DataCenterProperties; +import org.apache.servicecomb.registry.RegistrationId; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; @@ -36,8 +37,8 @@ public class NacosConfiguration { public NacosRegistration nacosRegistration( DataCenterProperties dataCenterProperties, @Qualifier("nacosDiscoveryProperties") NacosDiscoveryProperties nacosDiscoveryProperties, - Environment environment) { - return new NacosRegistration(dataCenterProperties, nacosDiscoveryProperties, environment); + Environment environment, RegistrationId registrationId) { + return new NacosRegistration(dataCenterProperties, nacosDiscoveryProperties, environment, registrationId); } @Bean 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 87d03fa46..8d04e8200 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 @@ -98,6 +98,16 @@ public class NacosDiscovery implements Discovery<NacosDiscoveryInstance> { } } + @Override + public List<String> findServices(String application) { + try { + return namingService.getServicesOfServer(0, Integer.MAX_VALUE, application) + .getData(); + } catch (Exception e) { + throw new IllegalStateException(e); + } + } + private List<NacosDiscoveryInstance> convertServiceInstanceList( List<Instance> instances, String application, String serviceName) { if (CollectionUtils.isEmpty(instances)) { 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 097b22d87..0b962152e 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 @@ -19,12 +19,12 @@ package org.apache.servicecomb.registry.nacos; import java.util.List; import java.util.Map; -import java.util.UUID; 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.RegistrationId; import org.apache.servicecomb.registry.api.MicroserviceInstanceStatus; import org.apache.servicecomb.registry.api.Registration; import org.springframework.beans.factory.annotation.Autowired; @@ -52,8 +52,8 @@ public class NacosRegistration implements Registration<NacosRegistrationInstance @Autowired public NacosRegistration(DataCenterProperties dataCenterProperties, NacosDiscoveryProperties nacosDiscoveryProperties, - Environment environment) { - this.instanceId = buildInstanceId(); + Environment environment, RegistrationId registrationId) { + this.instanceId = registrationId.getInstanceId(); this.dataCenterProperties = dataCenterProperties; this.nacosDiscoveryProperties = nacosDiscoveryProperties; this.environment = environment; @@ -157,8 +157,4 @@ public class NacosRegistration implements Registration<NacosRegistrationInstance public boolean enabled() { return nacosDiscoveryProperties.isEnabled(); } - - private static String buildInstanceId() { - return UUID.randomUUID().toString(); - } } diff --git a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/registry/sc/SCDiscovery.java b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/registry/sc/SCDiscovery.java index 471d02429..8cffa5047 100644 --- a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/registry/sc/SCDiscovery.java +++ b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/registry/sc/SCDiscovery.java @@ -21,6 +21,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import org.apache.servicecomb.foundation.common.event.SimpleEventBus; import org.apache.servicecomb.registry.api.Discovery; @@ -28,6 +29,7 @@ import org.apache.servicecomb.service.center.client.DiscoveryEvents.InstanceChan import org.apache.servicecomb.service.center.client.ServiceCenterClient; import org.apache.servicecomb.service.center.client.ServiceCenterDiscovery; import org.apache.servicecomb.service.center.client.ServiceCenterDiscovery.SubscriptionKey; +import org.apache.servicecomb.service.center.client.model.Microservice; import org.apache.servicecomb.service.center.client.model.MicroserviceInstance; import org.apache.servicecomb.service.center.client.model.SchemaInfo; import org.springframework.beans.factory.annotation.Autowired; @@ -96,6 +98,13 @@ public class SCDiscovery implements Discovery<SCDiscoveryInstance> { return toDiscoveryInstances(instances); } + @Override + public List<String> findServices(String application) { + return serviceCenterClient.getMicroserviceList().getServices().stream() + .filter(e -> e.getAppId().equals(application)).map(Microservice::getServiceName) + .collect(Collectors.toList()); + } + private List<SCDiscoveryInstance> toDiscoveryInstances(List<MicroserviceInstance> instances) { if (CollectionUtils.isEmpty(instances)) { return Collections.emptyList(); diff --git a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/registry/sc/SCRegistration.java b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/registry/sc/SCRegistration.java index c0ccec52d..e37f54b67 100644 --- a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/registry/sc/SCRegistration.java +++ b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/registry/sc/SCRegistration.java @@ -21,6 +21,7 @@ import java.util.concurrent.TimeUnit; import org.apache.servicecomb.config.DataCenterProperties; import org.apache.servicecomb.foundation.common.event.SimpleEventBus; +import org.apache.servicecomb.registry.RegistrationId; import org.apache.servicecomb.registry.api.MicroserviceInstanceStatus; import org.apache.servicecomb.registry.api.Registration; import org.apache.servicecomb.service.center.client.RegistrationEvents.MicroserviceInstanceRegistrationEvent; @@ -60,6 +61,8 @@ public class SCRegistration implements Registration<SCRegistrationInstance> { private Environment environment; + private RegistrationId registrationId; + private CountDownLatch readyWaiter = new CountDownLatch(1); @Autowired @@ -75,6 +78,11 @@ public class SCRegistration implements Registration<SCRegistrationInstance> { this.dataCenterProperties = dataCenterProperties; } + @Autowired + public void setRegistrationId(RegistrationId registrationId) { + this.registrationId = registrationId; + } + @Autowired public void setEnvironment(Environment environment) { this.environment = environment; @@ -89,6 +97,7 @@ public class SCRegistration implements Registration<SCRegistrationInstance> { environment, configurationProperties, dataCenterProperties); + microserviceInstance.setInstanceId(registrationId.getInstanceId()); serviceCenterRegistration = new ServiceCenterRegistration(serviceCenterClient, new ServiceCenterConfiguration().setCanOverwriteSwagger( this.configurationProperties.isCanOverwriteSwagger()) diff --git a/service-registry/registry-zero-config/src/main/java/org/apache/servicecomb/zeroconfig/ZeroConfigDiscovery.java b/service-registry/registry-zero-config/src/main/java/org/apache/servicecomb/zeroconfig/ZeroConfigDiscovery.java index 9615d8ec3..70b54f8a0 100644 --- a/service-registry/registry-zero-config/src/main/java/org/apache/servicecomb/zeroconfig/ZeroConfigDiscovery.java +++ b/service-registry/registry-zero-config/src/main/java/org/apache/servicecomb/zeroconfig/ZeroConfigDiscovery.java @@ -21,8 +21,10 @@ import static org.apache.servicecomb.zeroconfig.ZeroConfigConst.ORDER; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; import org.apache.servicecomb.registry.lightweight.AbstractLightweightDiscovery; +import org.apache.servicecomb.registry.lightweight.model.Microservice; import org.apache.servicecomb.registry.lightweight.model.MicroserviceInstance; import org.apache.servicecomb.registry.lightweight.model.MicroserviceInstances; import org.springframework.beans.factory.annotation.Autowired; @@ -74,4 +76,11 @@ public class ZeroConfigDiscovery extends AbstractLightweightDiscovery<ZeroConfig } return result; } + + @Override + public List<String> findServices(String application) { + List<Microservice> microservices = store.getAllMicroservices(); + return microservices.stream().filter(e -> e.getAppId().equals(application)) + .map(Microservice::getServiceName).collect(Collectors.toList()); + } } diff --git a/service-registry/registry-zookeeper/src/main/java/org/apache/servicecomb/registry/zookeeper/ZookeeperDiscovery.java b/service-registry/registry-zookeeper/src/main/java/org/apache/servicecomb/registry/zookeeper/ZookeeperDiscovery.java index c421a7022..a77084116 100644 --- a/service-registry/registry-zookeeper/src/main/java/org/apache/servicecomb/registry/zookeeper/ZookeeperDiscovery.java +++ b/service-registry/registry-zookeeper/src/main/java/org/apache/servicecomb/registry/zookeeper/ZookeeperDiscovery.java @@ -76,6 +76,9 @@ public class ZookeeperDiscovery implements Discovery<ZookeeperDiscoveryInstance> private final Map<String, Map<String, ServiceCache<ZookeeperInstance>>> serviceDiscoveries = new ConcurrentHashMapEx<>(); + private final Map<String, ServiceDiscovery<ZookeeperInstance>> serviceNameDiscoveries = + new ConcurrentHashMapEx<>(); + private Environment environment; private ZookeeperRegistryProperties zookeeperRegistryProperties; @@ -152,6 +155,31 @@ public class ZookeeperDiscovery implements Discovery<ZookeeperDiscoveryInstance> } } + @Override + public List<String> findServices(String application) { + try { + ServiceDiscovery<ZookeeperInstance> discovery = serviceNameDiscoveries.computeIfAbsent(application, app -> + { + JsonInstanceSerializer<ZookeeperInstance> serializer = + new JsonInstanceSerializer<>(ZookeeperInstance.class); + ServiceDiscovery<ZookeeperInstance> dis = ServiceDiscoveryBuilder.builder(ZookeeperInstance.class) + .client(client) + .basePath(basePath + "/" + application) + .serializer(serializer) + .build(); + try { + dis.start(); + } catch (Exception e) { + throw new IllegalStateException(e); + } + return dis; + }); + return discovery.queryForNames().stream().toList(); + } catch (Exception e) { + throw new IllegalStateException(e); + } + } + private List<ZookeeperDiscoveryInstance> toDiscoveryInstances( List<ServiceInstance<ZookeeperInstance>> instances) { return instances.stream().map(instance -> diff --git a/service-registry/registry-zookeeper/src/main/java/org/apache/servicecomb/registry/zookeeper/ZookeeperRegistration.java b/service-registry/registry-zookeeper/src/main/java/org/apache/servicecomb/registry/zookeeper/ZookeeperRegistration.java index 1e819926a..c799cc047 100644 --- a/service-registry/registry-zookeeper/src/main/java/org/apache/servicecomb/registry/zookeeper/ZookeeperRegistration.java +++ b/service-registry/registry-zookeeper/src/main/java/org/apache/servicecomb/registry/zookeeper/ZookeeperRegistration.java @@ -18,7 +18,6 @@ package org.apache.servicecomb.registry.zookeeper; import java.util.HashMap; import java.util.Map; -import java.util.UUID; import javax.security.auth.login.AppConfigurationEntry; import javax.security.auth.login.Configuration; @@ -34,6 +33,7 @@ import org.apache.curator.x.discovery.ServiceInstance; import org.apache.curator.x.discovery.details.JsonInstanceSerializer; import org.apache.servicecomb.config.BootStrapProperties; import org.apache.servicecomb.config.DataCenterProperties; +import org.apache.servicecomb.registry.RegistrationId; import org.apache.servicecomb.registry.api.DataCenterInfo; import org.apache.servicecomb.registry.api.MicroserviceInstanceStatus; import org.apache.servicecomb.registry.api.Registration; @@ -77,6 +77,8 @@ public class ZookeeperRegistration implements Registration<ZookeeperRegistration private ServiceInstance<ZookeeperInstance> instance; + private RegistrationId registrationId; + @Autowired @SuppressWarnings("unused") public void setEnvironment(Environment environment) { @@ -89,6 +91,11 @@ public class ZookeeperRegistration implements Registration<ZookeeperRegistration this.zookeeperRegistryProperties = zookeeperRegistryProperties; } + @Autowired + public void setRegistrationId(RegistrationId registrationId) { + this.registrationId = registrationId; + } + @Autowired @SuppressWarnings("unused") public void setDataCenterProperties(DataCenterProperties dataCenterProperties) { @@ -103,7 +110,7 @@ public class ZookeeperRegistration implements Registration<ZookeeperRegistration } basePath = String.format(ZookeeperConst.ZOOKEEPER_DISCOVERY_ROOT, env); ZookeeperInstance zookeeperInstance = new ZookeeperInstance(); - zookeeperInstance.setInstanceId(buildInstanceId()); + zookeeperInstance.setInstanceId(registrationId.getInstanceId()); zookeeperInstance.setEnvironment(env); zookeeperInstance.setApplication(BootStrapProperties.readApplication(environment)); zookeeperInstance.setServiceName(BootStrapProperties.readServiceName(environment)); @@ -205,8 +212,4 @@ public class ZookeeperRegistration implements Registration<ZookeeperRegistration public boolean enabled() { return zookeeperRegistryProperties.isEnabled(); } - - private static String buildInstanceId() { - return UUID.randomUUID().toString(); - } }