This is an automated email from the ASF dual-hosted git repository. liubao pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git
commit 80bece2682b98f55b20c4ec06b0b727b1ddb746f Author: liubao <[email protected]> AuthorDate: Tue May 12 14:30:14 2020 +0800 [SCB-1876] part-5: add registration and discovery implementation --- .../servicecomb/core/CseApplicationListener.java | 4 + .../org/apache/servicecomb/core/SCBEngine.java | 2 +- .../core/definition/ServiceRegistryListener.java | 15 +++- .../provider/producer/ProducerBootListener.java | 3 +- .../servicecomb/serviceregistry/Discovery.java | 2 - .../serviceregistry/DiscoveryManager.java | 16 +++- .../servicecomb/serviceregistry/LifeCycle.java | 2 + .../servicecomb/serviceregistry/Registration.java | 2 + .../serviceregistry/RegistrationManager.java | 5 ++ .../consumer/MicroserviceVersions.java | 3 +- .../definition/DefinitionConst.java | 4 + .../event/MicroserviceInstanceRegisteredEvent.java | 5 +- .../core/TestCseApplicationListener.java | 6 ++ .../servicecomb/serviceregistry/RegistryUtils.java | 11 +-- .../serviceregistry/ServiceCenterDiscovery.java | 90 ++++++++++++++++++++ .../serviceregistry/ServiceCenterRegistration.java | 99 ++++++++++++++++++++++ ...rg.apache.servicecomb.serviceregistry.Discovery | 18 ++++ ...apache.servicecomb.serviceregistry.Registration | 18 ++++ .../servicecomb/serviceregistry/TestConsumers.java | 13 +++ .../serviceregistry/TestRegistryBase.java | 24 ++---- .../discovery/TestDiscoveryTree.java | 22 ++++- 21 files changed, 318 insertions(+), 46 deletions(-) diff --git a/core/src/main/java/org/apache/servicecomb/core/CseApplicationListener.java b/core/src/main/java/org/apache/servicecomb/core/CseApplicationListener.java index 7beb65e..6a3f6c4 100644 --- a/core/src/main/java/org/apache/servicecomb/core/CseApplicationListener.java +++ b/core/src/main/java/org/apache/servicecomb/core/CseApplicationListener.java @@ -19,6 +19,8 @@ package org.apache.servicecomb.core; import org.apache.servicecomb.foundation.common.utils.BeanUtils; import org.apache.servicecomb.foundation.vertx.client.http.HttpClients; +import org.apache.servicecomb.serviceregistry.DiscoveryManager; +import org.apache.servicecomb.serviceregistry.RegistrationManager; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; @@ -44,6 +46,8 @@ public class CseApplicationListener this.applicationContext = applicationContext; BeanUtils.setContext(applicationContext); HttpClients.load(); + RegistrationManager.INSTANCE.init(); + DiscoveryManager.INSTANCE.init(); } public void setInitEventClass(Class<?> initEventClass) { diff --git a/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java b/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java index da03752..ff226e0 100644 --- a/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java +++ b/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java @@ -322,7 +322,7 @@ public class SCBEngine { } catch (TimeoutException e) { LOGGER.warn("{}", e.getMessage()); } catch (Throwable e) { - LOGGER.error("Failed to start ServiceComb due to errors and close: {}", e.getMessage()); + LOGGER.error("Failed to start ServiceComb due to errors and close", e); try { destroy(); } catch (Exception exception) { diff --git a/core/src/main/java/org/apache/servicecomb/core/definition/ServiceRegistryListener.java b/core/src/main/java/org/apache/servicecomb/core/definition/ServiceRegistryListener.java index ca9dfe5..b6c4c8e 100644 --- a/core/src/main/java/org/apache/servicecomb/core/definition/ServiceRegistryListener.java +++ b/core/src/main/java/org/apache/servicecomb/core/definition/ServiceRegistryListener.java @@ -30,6 +30,7 @@ import org.apache.servicecomb.foundation.common.event.SubscriberOrder; import org.apache.servicecomb.serviceregistry.api.registry.Microservice; import org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersion; import org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersions; +import org.apache.servicecomb.serviceregistry.definition.DefinitionConst; import org.apache.servicecomb.serviceregistry.event.CreateMicroserviceEvent; import org.apache.servicecomb.serviceregistry.event.CreateMicroserviceVersionEvent; import org.apache.servicecomb.serviceregistry.event.DestroyMicroserviceEvent; @@ -87,10 +88,16 @@ public class ServiceRegistryListener { MicroserviceVersions microserviceVersions = microserviceVersion.getMicroserviceVersions(); microserviceMeta.setMicroserviceVersionsMeta(getMicroserviceVersionsMeta(microserviceVersions)); - // TODO: service center do not have schema. But this logic expected to work. Deleted old code and comments. - for (String schemaId : microservice.getSchemas()) { - Swagger swagger = scbEngine.getSwaggerLoader().loadSwagger(microservice, schemaId); - microserviceMeta.registerSchemaMeta(schemaId, swagger); + boolean isServiceCenter = DefinitionConst.REGISTRY_APP_ID.equals(microservice.getAppId()) + && DefinitionConst.REGISTRY_SERVICE_NAME.equals(microservice.getServiceName()); + // do not load service center schemas, because service center did not provide swagger,but can get schema ids.... + // service center better to resolve the problem. + if (!isServiceCenter) { + // TODO: get schemas from instance + for (String schemaId : microservice.getSchemas()) { + Swagger swagger = scbEngine.getSwaggerLoader().loadSwagger(microservice, schemaId); + microserviceMeta.registerSchemaMeta(schemaId, swagger); + } } microserviceMeta.putExtData(CORE_MICROSERVICE_VERSION, microserviceVersion); diff --git a/core/src/main/java/org/apache/servicecomb/core/provider/producer/ProducerBootListener.java b/core/src/main/java/org/apache/servicecomb/core/provider/producer/ProducerBootListener.java index 0aac744..e663e5d 100644 --- a/core/src/main/java/org/apache/servicecomb/core/provider/producer/ProducerBootListener.java +++ b/core/src/main/java/org/apache/servicecomb/core/provider/producer/ProducerBootListener.java @@ -67,11 +67,10 @@ public class ProducerBootListener implements BootListener { microserviceMeta.getMicroserviceName(), schemaMeta.getSchemaId(), content); - // TODO: don't forget to implement this in registry + RegistrationManager.INSTANCE.addSchema(schemaMeta.getSchemaId(), content); } - // TODO: don't forget to implement this in registry saveBasePaths(microserviceMeta); } diff --git a/core/src/main/java/org/apache/servicecomb/serviceregistry/Discovery.java b/core/src/main/java/org/apache/servicecomb/serviceregistry/Discovery.java index 4517c51..ed914b7 100644 --- a/core/src/main/java/org/apache/servicecomb/serviceregistry/Discovery.java +++ b/core/src/main/java/org/apache/servicecomb/serviceregistry/Discovery.java @@ -35,8 +35,6 @@ public interface Discovery extends SPIOrder, LifeCycle { MicroserviceInstances findServiceInstances(String appId, String serviceName, String versionRule); - MicroserviceVersions getOrCreateMicroserviceVersions(String appId, String microserviceName); - String getRevision(); void setRevision(String revision); diff --git a/core/src/main/java/org/apache/servicecomb/serviceregistry/DiscoveryManager.java b/core/src/main/java/org/apache/servicecomb/serviceregistry/DiscoveryManager.java index 3323baa..39a0009 100644 --- a/core/src/main/java/org/apache/servicecomb/serviceregistry/DiscoveryManager.java +++ b/core/src/main/java/org/apache/servicecomb/serviceregistry/DiscoveryManager.java @@ -34,21 +34,25 @@ import org.apache.servicecomb.serviceregistry.definition.MicroserviceDefinition; public class DiscoveryManager { public static DiscoveryManager INSTANCE = new DiscoveryManager(); - private List<Discovery> discoveryList = SPIServiceUtils.getOrLoadSortedService(Discovery.class); + private final List<Discovery> discoveryList; - private final AppManager appManager = new AppManager(); + private final AppManager appManager; - private InstanceCacheManager instanceCacheManager = new InstanceCacheManagerNew(appManager); + private final InstanceCacheManager instanceCacheManager; private final MicroserviceDefinition microserviceDefinition; public DiscoveryManager() { + appManager = new AppManager(); + instanceCacheManager = new InstanceCacheManagerNew(appManager); + discoveryList = SPIServiceUtils.getOrLoadSortedService(Discovery.class); + MicroserviceConfigLoader loader = ConfigUtil.getMicroserviceConfigLoader(); microserviceDefinition = new MicroserviceDefinition(loader.getConfigModels()); } public MicroserviceInstances findServiceInstances(String appId, String serviceName, - String versionRule, String revision) { + String versionRule) { MicroserviceInstances result = new MicroserviceInstances(); discoveryList @@ -114,4 +118,8 @@ public class DiscoveryManager { public void run() { discoveryList.forEach(discovery -> discovery.run()); } + + public void init() { + discoveryList.forEach(discovery -> discovery.init()); + } } diff --git a/core/src/main/java/org/apache/servicecomb/serviceregistry/LifeCycle.java b/core/src/main/java/org/apache/servicecomb/serviceregistry/LifeCycle.java index 70bbccb..2e0b4af 100644 --- a/core/src/main/java/org/apache/servicecomb/serviceregistry/LifeCycle.java +++ b/core/src/main/java/org/apache/servicecomb/serviceregistry/LifeCycle.java @@ -18,6 +18,8 @@ package org.apache.servicecomb.serviceregistry; public interface LifeCycle { + void init(); + void run(); void destroy(); diff --git a/core/src/main/java/org/apache/servicecomb/serviceregistry/Registration.java b/core/src/main/java/org/apache/servicecomb/serviceregistry/Registration.java index fa6d4c4..7a17916 100644 --- a/core/src/main/java/org/apache/servicecomb/serviceregistry/Registration.java +++ b/core/src/main/java/org/apache/servicecomb/serviceregistry/Registration.java @@ -26,6 +26,8 @@ import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance; import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstanceStatus; public interface Registration extends SPIOrder, LifeCycle { + String name(); + MicroserviceInstance getMicroserviceInstance(); Microservice getMicroservice(); diff --git a/core/src/main/java/org/apache/servicecomb/serviceregistry/RegistrationManager.java b/core/src/main/java/org/apache/servicecomb/serviceregistry/RegistrationManager.java index 12aefe6..b89add2 100644 --- a/core/src/main/java/org/apache/servicecomb/serviceregistry/RegistrationManager.java +++ b/core/src/main/java/org/apache/servicecomb/serviceregistry/RegistrationManager.java @@ -94,6 +94,11 @@ public class RegistrationManager { registrationList.forEach(registration -> registration.run()); } + + public void init() { + registrationList.forEach(discovery -> discovery.init()); + } + public static String getPublishAddress() { String publicAddressSetting = DynamicPropertyFactory.getInstance().getStringProperty(PUBLISH_ADDRESS, "").get(); diff --git a/core/src/main/java/org/apache/servicecomb/serviceregistry/consumer/MicroserviceVersions.java b/core/src/main/java/org/apache/servicecomb/serviceregistry/consumer/MicroserviceVersions.java index b254838..c314506 100644 --- a/core/src/main/java/org/apache/servicecomb/serviceregistry/consumer/MicroserviceVersions.java +++ b/core/src/main/java/org/apache/servicecomb/serviceregistry/consumer/MicroserviceVersions.java @@ -179,8 +179,7 @@ public class MicroserviceVersions { protected MicroserviceInstances findServiceInstances() { return DiscoveryManager.INSTANCE.findServiceInstances(appId, microserviceName, - DefinitionConst.VERSION_RULE_ALL, - revision); + DefinitionConst.VERSION_RULE_ALL); } protected void safeSetInstances(List<MicroserviceInstance> pulledInstances, String rev) { diff --git a/core/src/main/java/org/apache/servicecomb/serviceregistry/definition/DefinitionConst.java b/core/src/main/java/org/apache/servicecomb/serviceregistry/definition/DefinitionConst.java index 69a9ded..69c7012 100644 --- a/core/src/main/java/org/apache/servicecomb/serviceregistry/definition/DefinitionConst.java +++ b/core/src/main/java/org/apache/servicecomb/serviceregistry/definition/DefinitionConst.java @@ -50,4 +50,8 @@ public interface DefinitionConst { public static final String REGISTER_URL_PREFIX = "servicecomb.service.registry.registerUrlPrefix"; public static final String REGISTER_SERVICE_PATH = "servicecomb.service.registry.registerPath"; + + public static final String REGISTRY_APP_ID = "default"; + + public static final String REGISTRY_SERVICE_NAME = "SERVICECENTER"; } diff --git a/core/src/main/java/org/apache/servicecomb/serviceregistry/event/MicroserviceInstanceRegisteredEvent.java b/core/src/main/java/org/apache/servicecomb/serviceregistry/event/MicroserviceInstanceRegisteredEvent.java index 8149a76..1526213 100644 --- a/core/src/main/java/org/apache/servicecomb/serviceregistry/event/MicroserviceInstanceRegisteredEvent.java +++ b/core/src/main/java/org/apache/servicecomb/serviceregistry/event/MicroserviceInstanceRegisteredEvent.java @@ -17,6 +17,9 @@ package org.apache.servicecomb.serviceregistry.event; +/** + * when registration is ready, should post this event. + */ public class MicroserviceInstanceRegisteredEvent { - // TODO: registry need publish this event instead of MicroserviceInstanceRegisterTask + } diff --git a/core/src/test/java/org/apache/servicecomb/core/TestCseApplicationListener.java b/core/src/test/java/org/apache/servicecomb/core/TestCseApplicationListener.java index 33b71af..315b199 100644 --- a/core/src/test/java/org/apache/servicecomb/core/TestCseApplicationListener.java +++ b/core/src/test/java/org/apache/servicecomb/core/TestCseApplicationListener.java @@ -16,17 +16,23 @@ */ package org.apache.servicecomb.core; +import org.apache.servicecomb.config.ConfigUtil; import org.apache.servicecomb.core.CseApplicationListener; import org.apache.servicecomb.core.SCBEngine; import org.apache.servicecomb.core.SCBStatus; import org.apache.servicecomb.core.bootstrap.SCBBootstrap; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; import org.springframework.context.event.ContextClosedEvent; import mockit.Mocked; public class TestCseApplicationListener { + @Before + public void before() { + + } @Test public void onApplicationEvent_close(@Mocked ContextClosedEvent contextClosedEvent) { SCBEngine scbEngine = new SCBBootstrap().useLocalRegistry().createSCBEngineForTest(); diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/RegistryUtils.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/RegistryUtils.java index 9fc917b..2474ee8 100644 --- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/RegistryUtils.java +++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/RegistryUtils.java @@ -40,6 +40,7 @@ import org.apache.servicecomb.serviceregistry.client.ServiceRegistryClient; import org.apache.servicecomb.serviceregistry.client.http.MicroserviceInstances; import org.apache.servicecomb.serviceregistry.config.ServiceRegistryConfig; import org.apache.servicecomb.serviceregistry.definition.MicroserviceDefinition; +import org.apache.servicecomb.serviceregistry.event.MicroserviceInstanceRegisteredEvent; import org.apache.servicecomb.serviceregistry.registry.ServiceRegistryFactory; import org.apache.servicecomb.serviceregistry.registry.cache.AggregateServiceRegistryCache; import org.apache.servicecomb.serviceregistry.registry.cache.MicroserviceCache; @@ -62,10 +63,6 @@ public final class RegistryUtils { */ private static volatile ServiceRegistry serviceRegistry; - - private static InstanceCacheManager instanceCacheManager = new InstanceCacheManagerNew( - DiscoveryManager.INSTANCE.getAppManager()); - private static final Map<String, ServiceRegistryConfig> EXTRA_SERVICE_REGISTRY_CONFIGS = new LinkedHashMap<>(); private static final Map<String, ServiceRegistry> EXTRA_SERVICE_REGISTRIES = new LinkedHashMap<>(); @@ -141,10 +138,6 @@ public final class RegistryUtils { return serviceRegistry.getServiceRegistryClient(); } - public static InstanceCacheManager getInstanceCacheManager() { - return instanceCacheManager; - } - public static String getAppId() { return serviceRegistry.getAppId(); } @@ -317,7 +310,7 @@ public final class RegistryUtils { if (instanceRegisterCounter.decrementAndGet() > 0) { return; } - EventManager.getEventBus().post(microserviceInstanceRegisterTask); + EventManager.getEventBus().post(new MicroserviceInstanceRegisteredEvent()); } } } diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/ServiceCenterDiscovery.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/ServiceCenterDiscovery.java new file mode 100644 index 0000000..eef2a82 --- /dev/null +++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/ServiceCenterDiscovery.java @@ -0,0 +1,90 @@ +/* + * 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.serviceregistry; + +import org.apache.servicecomb.serviceregistry.api.registry.Microservice; +import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance; +import org.apache.servicecomb.serviceregistry.client.http.MicroserviceInstances; +import org.apache.servicecomb.serviceregistry.definition.DefinitionConst; + +public class ServiceCenterDiscovery implements Discovery { + public static final String NAME = "service center discovery"; + + private String revision; + + @Override + public void init() { + // ServiceCenterRegistration has already done it + } + + @Override + public void run() { + // ServiceCenterRegistration has already done it + } + + @Override + public void destroy() { + // ServiceCenterRegistration has already done it + } + + @Override + public int getOrder() { + return 0; + } + + @Override + public Microservice getMicroservice(String microserviceId) { + return RegistryUtils.getMicroservice(microserviceId); + } + + @Override + public String getSchema(String microserviceId, String schemaId) { + ; + return RegistryUtils + .getAggregatedSchema(microserviceId, schemaId); + } + + @Override + public MicroserviceInstance findMicroserviceInstance(String serviceId, String instanceId) { + return RegistryUtils.getResultFromFirstValidServiceRegistry( + sr -> sr.getServiceRegistryClient().findServiceInstance(serviceId, instanceId)); + } + + @Override + public MicroserviceInstances findServiceInstances(String appId, String serviceName, String versionRule) { + return RegistryUtils.findServiceInstances(appId, + serviceName, + DefinitionConst.VERSION_RULE_ALL, + revision); + } + + @Override + public String getRevision() { + return revision; + } + + @Override + public void setRevision(String revision) { + this.revision = revision; + } + + @Override + public String name() { + return NAME; + } +} diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/ServiceCenterRegistration.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/ServiceCenterRegistration.java new file mode 100644 index 0000000..ec0f69d --- /dev/null +++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/ServiceCenterRegistration.java @@ -0,0 +1,99 @@ +/* + * 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.serviceregistry; + +import java.util.Collection; + +import org.apache.servicecomb.foundation.common.concurrency.SuppressedRunnableWrapper; +import org.apache.servicecomb.serviceregistry.api.registry.BasePath; +import org.apache.servicecomb.serviceregistry.api.registry.Microservice; +import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance; +import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstanceStatus; + +public class ServiceCenterRegistration implements Registration { + public static final String NAME = "service center registration"; + + @Override + public void init() { + RegistryUtils.init(); + } + + @Override + public void run() { + RegistryUtils.run(); + } + + @Override + public void destroy() { + RegistryUtils.destroy(); + } + + @Override + public int getOrder() { + return 0; + } + + @Override + public String name() { + return NAME; + } + + @Override + public MicroserviceInstance getMicroserviceInstance() { + return RegistryUtils.getMicroserviceInstance(); + } + + @Override + public Microservice getMicroservice() { + return RegistryUtils.getMicroservice(); + } + + @Override + public boolean updateMicroserviceInstanceStatus(MicroserviceInstanceStatus status) { + RegistryUtils.executeOnEachServiceRegistry(sr -> new SuppressedRunnableWrapper(() -> { + MicroserviceInstance selfInstance = sr.getMicroserviceInstance(); + sr.getServiceRegistryClient().updateMicroserviceInstanceStatus( + selfInstance.getServiceId(), + selfInstance.getInstanceId(), + MicroserviceInstanceStatus.DOWN); + }).run()); + return true; + } + + @Override + public void addSchema(String schemaId, String content) { + RegistryUtils.executeOnEachServiceRegistry(sr -> { + sr.getMicroservice().addSchema(schemaId, content); + }); + } + + @Override + public void addEndpoint(String endpoint) { + RegistryUtils.executeOnEachServiceRegistry(sr -> { + Microservice microservice = sr.getMicroservice(); + microservice.getInstance().getEndpoints().add(endpoint); + }); + } + + @Override + public void addBasePath(Collection<BasePath> basePaths) { + RegistryUtils.executeOnEachServiceRegistry(sr -> { + sr.getMicroservice().getPaths().addAll(basePaths); + }); + } +} diff --git a/service-registry/src/main/resources/META-INF/services/org.apache.servicecomb.serviceregistry.Discovery b/service-registry/src/main/resources/META-INF/services/org.apache.servicecomb.serviceregistry.Discovery new file mode 100644 index 0000000..7d152e0 --- /dev/null +++ b/service-registry/src/main/resources/META-INF/services/org.apache.servicecomb.serviceregistry.Discovery @@ -0,0 +1,18 @@ +# +# 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. +# + +org.apache.servicecomb.serviceregistry.ServiceCenterDiscovery \ No newline at end of file diff --git a/service-registry/src/main/resources/META-INF/services/org.apache.servicecomb.serviceregistry.Registration b/service-registry/src/main/resources/META-INF/services/org.apache.servicecomb.serviceregistry.Registration new file mode 100644 index 0000000..b869661 --- /dev/null +++ b/service-registry/src/main/resources/META-INF/services/org.apache.servicecomb.serviceregistry.Registration @@ -0,0 +1,18 @@ +# +# 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. +# + +org.apache.servicecomb.serviceregistry.ServiceCenterRegistration \ No newline at end of file diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/TestConsumers.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/TestConsumers.java index 0aead31..ec0f8d4 100644 --- a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/TestConsumers.java +++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/TestConsumers.java @@ -19,6 +19,8 @@ package org.apache.servicecomb.serviceregistry; import java.util.Arrays; +import org.apache.servicecomb.config.ConfigUtil; +import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils; import org.apache.servicecomb.foundation.test.scaffolding.log.LogCollector; import org.apache.servicecomb.serviceregistry.api.MicroserviceKey; import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance; @@ -28,12 +30,23 @@ import org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersionRule; import org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersions; import org.apache.servicecomb.serviceregistry.task.event.RecoveryEvent; import org.hamcrest.Matchers; +import org.junit.After; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; import mockit.Expectations; public class TestConsumers extends TestRegistryBase { + @Before + public void before() { + + } + + @After + public void tearDown() { + } + @Test public void getOrCreateMicroserviceVersionRule() { MicroserviceVersionRule microserviceVersionRule = appManager diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/TestRegistryBase.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/TestRegistryBase.java index 31535af..0cfeda5 100644 --- a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/TestRegistryBase.java +++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/TestRegistryBase.java @@ -18,24 +18,21 @@ package org.apache.servicecomb.serviceregistry; import org.apache.log4j.Level; import org.apache.log4j.Logger; +import org.apache.servicecomb.config.ConfigUtil; import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils; import org.apache.servicecomb.serviceregistry.client.http.MicroserviceInstances; import org.apache.servicecomb.serviceregistry.consumer.AppManager; import org.apache.servicecomb.serviceregistry.consumer.MicroserviceManager; import org.apache.servicecomb.serviceregistry.registry.ServiceRegistryFactory; -import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import com.google.common.eventbus.EventBus; -import mockit.Deencapsulation; import mockit.Mock; import mockit.MockUp; public class TestRegistryBase { - private AppManager originalAppManager = DiscoveryManager.INSTANCE.getAppManager(); - protected ServiceRegistry serviceRegistry; protected AppManager appManager; @@ -62,16 +59,14 @@ public class TestRegistryBase { @Before public void setup() { + ConfigUtil.installDynamicConfig(); + // avoid write too many logs Logger.getRootLogger().setLevel(Level.OFF); - ArchaiusUtils.resetConfig(); - serviceRegistry = ServiceRegistryFactory.createLocal("registry.yaml"); serviceRegistry.init(); - Deencapsulation.setField(RegistryUtils.class, "appManager", new AppManager()); - appManager = DiscoveryManager.INSTANCE.getAppManager(); microserviceManager = appManager.getOrCreateMicroserviceManager(appId); eventBus = serviceRegistry.getEventBus(); @@ -83,11 +78,6 @@ public class TestRegistryBase { Logger.getRootLogger().setLevel(Level.INFO); } - @After - public void tearDown() { - Deencapsulation.setField(RegistryUtils.class, "appManager", originalAppManager); - } - @AfterClass public static void classTeardown() { RegistryUtils.setServiceRegistry(null); @@ -97,20 +87,20 @@ public class TestRegistryBase { protected void mockNotExist() { MicroserviceInstances microserviceInstances = new MicroserviceInstances(); microserviceInstances.setMicroserviceNotExist(true); - new MockUp<RegistryUtils>() { + new MockUp<DiscoveryManager>() { @Mock MicroserviceInstances findServiceInstances(String appId, String serviceName, - String versionRule, String revision) { + String versionRule) { return microserviceInstances; } }; } protected void mockDisconnect() { - new MockUp<RegistryUtils>() { + new MockUp<DiscoveryManager>() { @Mock MicroserviceInstances findServiceInstances(String appId, String serviceName, - String versionRule, String revision) { + String versionRule) { return null; } }; diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/discovery/TestDiscoveryTree.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/discovery/TestDiscoveryTree.java index 0940270..a6523bb 100644 --- a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/discovery/TestDiscoveryTree.java +++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/discovery/TestDiscoveryTree.java @@ -20,13 +20,18 @@ package org.apache.servicecomb.serviceregistry.discovery; import java.util.Arrays; import java.util.List; +import org.apache.servicecomb.config.ConfigUtil; import org.apache.servicecomb.foundation.common.cache.VersionedCache; import org.apache.servicecomb.foundation.common.exceptions.ServiceCombException; import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils; +import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils; +import org.apache.servicecomb.serviceregistry.DiscoveryManager; import org.apache.servicecomb.serviceregistry.RegistryUtils; import org.apache.servicecomb.serviceregistry.cache.InstanceCacheManager; import org.hamcrest.Matchers; +import org.junit.After; import org.junit.Assert; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -36,6 +41,15 @@ import mockit.Expectations; import mockit.Mocked; public class TestDiscoveryTree { + @Before + public void before() { + ConfigUtil.installDynamicConfig(); + } + @After + public void tearDown() { + ArchaiusUtils.resetConfig(); + } + DiscoveryTree discoveryTree = new DiscoveryTree(); List<DiscoveryFilter> filters = Deencapsulation.getField(discoveryTree, "filters"); @@ -167,9 +181,9 @@ public class TestDiscoveryTree { @Test public void easyDiscovery(@Mocked InstanceCacheManager instanceCacheManager) { - new Expectations(RegistryUtils.class) { + new Expectations(DiscoveryManager.class) { { - RegistryUtils.getInstanceCacheManager(); + DiscoveryManager.INSTANCE.getInstanceCacheManager(); result = instanceCacheManager; instanceCacheManager.getOrCreateVersionedCache(anyString, anyString, anyString); result = parent; @@ -183,9 +197,9 @@ public class TestDiscoveryTree { @Test public void discovery_filterReturnNull(@Mocked InstanceCacheManager instanceCacheManager) { - new Expectations(RegistryUtils.class) { + new Expectations(DiscoveryManager.class) { { - RegistryUtils.getInstanceCacheManager(); + DiscoveryManager.INSTANCE.getInstanceCacheManager(); result = instanceCacheManager; instanceCacheManager.getOrCreateVersionedCache(anyString, anyString, anyString); result = parent;
