This is an automated email from the ASF dual-hosted git repository. wujimin pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git
The following commit(s) were added to refs/heads/master by this push: new 35f7ea8 [SCB-1996]move registerMicroserviceMappingByEndpoints from ServiceRegistry to RegistrationManager 35f7ea8 is described below commit 35f7ea841a98a1a796adc5bdbb0f01bc6b5432e1 Author: liubao <bi...@qq.com> AuthorDate: Wed Jun 10 15:05:25 2020 +0800 [SCB-1996]move registerMicroserviceMappingByEndpoints from ServiceRegistry to RegistrationManager --- .../client/TestThirdPartyRegistration.java | 62 ++++++++++++++++++++++ .../demo/springmvc/client/ThirdPartyService.java | 37 +++++++++++++ .../servicecomb/registry/RegistrationManager.java | 60 ++++++++++++++++++++- .../thirdparty/Test3rdPartyInvocation.java | 22 ++++---- .../serviceregistry/ServiceRegistry.java | 37 +------------ .../registry/AbstractServiceRegistry.java | 27 ---------- .../servicecomb/serviceregistry/TestConsumers.java | 10 ++-- .../TestInstanceCacheCheckerWithoutMock.java | 7 +-- .../registry/EmptyMockServiceRegistry.java | 12 ----- 9 files changed, 179 insertions(+), 95 deletions(-) diff --git a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestThirdPartyRegistration.java b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestThirdPartyRegistration.java new file mode 100644 index 0000000..0ad7199 --- /dev/null +++ b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestThirdPartyRegistration.java @@ -0,0 +1,62 @@ +/* + * 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.demo.springmvc.client; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.apache.servicecomb.core.BootListener; +import org.apache.servicecomb.demo.CategorizedTestCase; +import org.apache.servicecomb.demo.TestMgr; +import org.apache.servicecomb.provider.pojo.Invoker; +import org.apache.servicecomb.registry.RegistrationManager; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Component; + +@Component +public class TestThirdPartyRegistration implements BootListener, CategorizedTestCase { + private ThirdPartyService thirdPartyService; + + @Override + public void onAfterRegistry(BootEvent event) { + List<String> endpoints = new ArrayList<>(); + endpoints.add("rest://localhost:8080"); + RegistrationManager.INSTANCE.registerMicroserviceMappingByEndpoints("testServiceName", + "1.0.1", endpoints, ThirdPartyService.class); + thirdPartyService = Invoker.createProxy("testServiceName", + "testServiceName", ThirdPartyService.class); + } + + @Override + public void testRestTransport() throws Exception { + Date date = new Date(); + ResponseEntity<Date> responseEntity = thirdPartyService.responseEntity(date); + TestMgr.check(date, responseEntity.getBody()); + // Third party invocation do not pass cse-context to the target + TestMgr.check("h1v null", responseEntity.getHeaders().getFirst("h1")); + TestMgr.check("h2v null", responseEntity.getHeaders().getFirst("h2")); + + TestMgr.check(202, responseEntity.getStatusCodeValue()); + } + + @Override + public String getMicroserviceName() { + return "testServiceName"; + } +} diff --git a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/ThirdPartyService.java b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/ThirdPartyService.java new file mode 100644 index 0000000..e8a1c93 --- /dev/null +++ b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/ThirdPartyService.java @@ -0,0 +1,37 @@ +/* + * 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.demo.springmvc.client; + +import java.util.Date; + +import org.apache.servicecomb.swagger.extend.annotations.ResponseHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestAttribute; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +import io.swagger.annotations.ResponseHeader; + +@RequestMapping(path = "/codeFirstSpringmvc", produces = MediaType.APPLICATION_JSON_VALUE) +public interface ThirdPartyService { + @ResponseHeaders({@ResponseHeader(name = "h1", response = String.class), + @ResponseHeader(name = "h2", response = String.class)}) + @RequestMapping(path = "/responseEntity", method = RequestMethod.POST) + ResponseEntity<Date> responseEntity(@RequestAttribute("date") Date date); +} diff --git a/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/RegistrationManager.java b/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/RegistrationManager.java index 2f2ae48..c052c45 100644 --- a/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/RegistrationManager.java +++ b/foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/RegistrationManager.java @@ -20,7 +20,9 @@ package org.apache.servicecomb.registry; import java.net.InetSocketAddress; import java.net.URI; import java.net.URISyntaxException; +import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.List; import java.util.stream.Collectors; @@ -28,11 +30,14 @@ import org.apache.http.client.utils.URIBuilder; import org.apache.servicecomb.foundation.common.net.IpPort; import org.apache.servicecomb.foundation.common.net.NetUtils; import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils; +import org.apache.servicecomb.registry.api.Registration; import org.apache.servicecomb.registry.api.registry.BasePath; import org.apache.servicecomb.registry.api.registry.Microservice; import org.apache.servicecomb.registry.api.registry.MicroserviceInstance; import org.apache.servicecomb.registry.api.registry.MicroserviceInstanceStatus; -import org.apache.servicecomb.registry.api.Registration; +import org.apache.servicecomb.registry.consumer.MicroserviceManager; +import org.apache.servicecomb.registry.consumer.StaticMicroserviceVersions; +import org.apache.servicecomb.registry.definition.MicroserviceNameParser; import org.apache.servicecomb.registry.swagger.SwaggerLoader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -118,6 +123,59 @@ public class RegistrationManager { registrationList.forEach(registration -> registration.init()); } + /** + * <p> + * Register a third party service if not registered before, and set it's instances into + * {@linkplain StaticMicroserviceVersions StaticMicroserviceVersions}. + * </p> + * <p> + * The registered third party service has the same {@code appId} and {@code environment} as this microservice instance has, + * and there is only one schema represented by {@code schemaIntfCls}, whose name is the same as {@code microserviceName}. + * </p> + * <em> + * This method is for initializing 3rd party service endpoint config. + * i.e. If this service has not been registered before, this service will be registered and the instances will be set; + * otherwise, NOTHING will happen. + * </em> + * + * @param microserviceName name of the 3rd party service, and this param also specifies the schemaId + * @param version version of this 3rd party service + * @param instances the instances of this 3rd party service. Users only need to specify the endpoint information, other + * necessary information will be generate and set in the implementation of this method. + * @param schemaIntfCls the producer interface of the service. This interface is used to generate swagger schema and + * can also be used for the proxy interface of RPC style invocation. + */ + public void registerMicroserviceMapping(String microserviceName, String version, List<MicroserviceInstance> instances, + Class<?> schemaIntfCls) { + MicroserviceNameParser parser = new MicroserviceNameParser(getAppId(), microserviceName); + MicroserviceManager microserviceManager = DiscoveryManager.INSTANCE.getAppManager() + .getOrCreateMicroserviceManager(parser.getAppId()); + microserviceManager.getVersionsByName() + .computeIfAbsent(microserviceName, + svcName -> new StaticMicroserviceVersions(DiscoveryManager.INSTANCE.getAppManager(), parser.getAppId(), + microserviceName) + .init(schemaIntfCls, version, instances) + ); + } + + /** + * @see #registerMicroserviceMapping(String, String, List, Class) + * @param endpoints the endpoints of 3rd party service. Each of endpoints will be treated as a separated instance. + * Format of the endpoints is the same as the endpoints that ServiceComb microservices register in service-center, + * like {@code rest://127.0.0.1:8080} + */ + public void registerMicroserviceMappingByEndpoints(String microserviceName, String version, + List<String> endpoints, Class<?> schemaIntfCls) { + ArrayList<MicroserviceInstance> microserviceInstances = new ArrayList<>(); + for (String endpoint : endpoints) { + MicroserviceInstance instance = new MicroserviceInstance(); + instance.setEndpoints(Collections.singletonList(endpoint)); + microserviceInstances.add(instance); + } + + registerMicroserviceMapping(microserviceName, version, microserviceInstances, schemaIntfCls); + } + public static String getPublishAddress() { String publicAddressSetting = DynamicPropertyFactory.getInstance().getStringProperty(PUBLISH_ADDRESS, "").get(); diff --git a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/thirdparty/Test3rdPartyInvocation.java b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/thirdparty/Test3rdPartyInvocation.java index 2934caf..7f67ecc 100644 --- a/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/thirdparty/Test3rdPartyInvocation.java +++ b/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/thirdparty/Test3rdPartyInvocation.java @@ -39,7 +39,7 @@ import org.apache.servicecomb.it.extend.engine.ITSCBRestTemplate; import org.apache.servicecomb.provider.pojo.Invoker; import org.apache.servicecomb.provider.springmvc.reference.RestTemplateBuilder; import org.apache.servicecomb.provider.springmvc.reference.async.CseAsyncRestTemplate; -import org.apache.servicecomb.serviceregistry.RegistryUtils; +import org.apache.servicecomb.registry.RegistrationManager; import org.apache.servicecomb.registry.api.registry.MicroserviceInstance; import org.hamcrest.Matchers; import org.junit.Assert; @@ -69,20 +69,18 @@ public class Test3rdPartyInvocation { public static void beforeClass() { String endpoint = ((ITSCBRestTemplate) consumersJaxrs.getSCBRestTemplate()).getAddress(Const.RESTFUL); - RegistryUtils.getServiceRegistry() - .registerMicroserviceMappingByEndpoints( - THIRD_PARTY_MICROSERVICE_NAME, "1.2.1", - Collections.singletonList(endpoint), - DataTypeJaxrsSchemaIntf.class); + RegistrationManager.INSTANCE.registerMicroserviceMappingByEndpoints( + THIRD_PARTY_MICROSERVICE_NAME, "1.2.1", + Collections.singletonList(endpoint), + DataTypeJaxrsSchemaIntf.class); MicroserviceInstance instance = new MicroserviceInstance(); instance.setEndpoints(Collections.singletonList(endpoint)); - RegistryUtils.getServiceRegistry() - .registerMicroserviceMapping( - ASYNC_THIRD_PARTY_MICROSERVICE_NAME, "1.1.1", - Collections.singletonList(instance), - DataTypeJaxrsSchemaAsyncIntf.class - ); + RegistrationManager.INSTANCE.registerMicroserviceMapping( + ASYNC_THIRD_PARTY_MICROSERVICE_NAME, "1.1.1", + Collections.singletonList(instance), + DataTypeJaxrsSchemaAsyncIntf.class + ); dataTypeJaxrsSchema = Invoker.createProxy( THIRD_PARTY_MICROSERVICE_NAME, THIRD_PARTY_MICROSERVICE_NAME, DataTypeJaxrsSchemaIntf.class); diff --git a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/ServiceRegistry.java b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/ServiceRegistry.java index 320670d..28ab9c9 100644 --- a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/ServiceRegistry.java +++ b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/ServiceRegistry.java @@ -23,9 +23,8 @@ import java.util.regex.Pattern; import org.apache.servicecomb.registry.api.registry.Microservice; import org.apache.servicecomb.registry.api.registry.MicroserviceInstance; -import org.apache.servicecomb.registry.consumer.StaticMicroserviceVersions; -import org.apache.servicecomb.serviceregistry.client.ServiceRegistryClient; import org.apache.servicecomb.registry.api.registry.MicroserviceInstances; +import org.apache.servicecomb.serviceregistry.client.ServiceRegistryClient; import org.apache.servicecomb.serviceregistry.registry.cache.MicroserviceCache; import org.apache.servicecomb.serviceregistry.registry.cache.MicroserviceCacheKey; @@ -99,38 +98,4 @@ public interface ServiceRegistry { * </p> */ Microservice getAggregatedRemoteMicroservice(String microserviceId); - - /** - * <p> - * Register a third party service if not registered before, and set it's instances into - * {@linkplain StaticMicroserviceVersions StaticMicroserviceVersions}. - * </p> - * <p> - * The registered third party service has the same {@code appId} and {@code environment} as this microservice instance has, - * and there is only one schema represented by {@code schemaIntfCls}, whose name is the same as {@code microserviceName}. - * </p> - * <em> - * This method is for initializing 3rd party service endpoint config. - * i.e. If this service has not been registered before, this service will be registered and the instances will be set; - * otherwise, NOTHING will happen. - * </em> - * - * @param microserviceName name of the 3rd party service, and this param also specifies the schemaId - * @param version version of this 3rd party service - * @param instances the instances of this 3rd party service. Users only need to specify the endpoint information, other - * necessary information will be generate and set in the implementation of this method. - * @param schemaIntfCls the producer interface of the service. This interface is used to generate swagger schema and - * can also be used for the proxy interface of RPC style invocation. - */ - void registerMicroserviceMapping(String microserviceName, String version, List<MicroserviceInstance> instances, - Class<?> schemaIntfCls); - - /** - * @see #registerMicroserviceMapping(String, String, List, Class) - * @param endpoints the endpoints of 3rd party service. Each of endpoints will be treated as a separated instance. - * Format of the endpoints is the same as the endpoints that ServiceComb microservices register in service-center, - * like {@code rest://127.0.0.1:8080} - */ - void registerMicroserviceMappingByEndpoints(String microserviceName, String version, - List<String> endpoints, Class<?> schemaIntfCls); } diff --git a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/registry/AbstractServiceRegistry.java b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/registry/AbstractServiceRegistry.java index 37a1174..d814ab9 100644 --- a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/registry/AbstractServiceRegistry.java +++ b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/registry/AbstractServiceRegistry.java @@ -266,33 +266,6 @@ public abstract class AbstractServiceRegistry implements ServiceRegistry { } @Override - public void registerMicroserviceMapping(String microserviceName, String version, - List<MicroserviceInstance> instances, Class<?> schemaIntfCls) { - MicroserviceNameParser parser = new MicroserviceNameParser(microservice.getAppId(), microserviceName); - MicroserviceManager microserviceManager = DiscoveryManager.INSTANCE.getAppManager() - .getOrCreateMicroserviceManager(parser.getAppId()); - microserviceManager.getVersionsByName() - .computeIfAbsent(microserviceName, - svcName -> new StaticMicroserviceVersions(DiscoveryManager.INSTANCE.getAppManager(), parser.getAppId(), - microserviceName) - .init(schemaIntfCls, version, instances) - ); - } - - @Override - public void registerMicroserviceMappingByEndpoints(String microserviceName, String version, - List<String> endpoints, Class<?> schemaIntfCls) { - ArrayList<MicroserviceInstance> microserviceInstances = new ArrayList<>(); - for (String endpoint : endpoints) { - MicroserviceInstance instance = new MicroserviceInstance(); - instance.setEndpoints(Collections.singletonList(endpoint)); - microserviceInstances.add(instance); - } - - registerMicroserviceMapping(microserviceName, version, microserviceInstances, schemaIntfCls); - } - - @Override public String getName() { return name; } diff --git a/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/TestConsumers.java b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/TestConsumers.java index 85e6282..12160d1 100644 --- a/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/TestConsumers.java +++ b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/TestConsumers.java @@ -21,14 +21,15 @@ import java.util.Arrays; import org.apache.servicecomb.foundation.test.scaffolding.log.LogCollector; import org.apache.servicecomb.registry.DiscoveryManager; +import org.apache.servicecomb.registry.RegistrationManager; import org.apache.servicecomb.registry.api.MicroserviceKey; +import org.apache.servicecomb.registry.api.event.MicroserviceInstanceChangedEvent; +import org.apache.servicecomb.registry.api.event.task.RecoveryEvent; import org.apache.servicecomb.registry.api.registry.Microservice; import org.apache.servicecomb.registry.api.registry.MicroserviceInstance; -import org.apache.servicecomb.registry.api.event.MicroserviceInstanceChangedEvent; import org.apache.servicecomb.registry.consumer.MicroserviceVersion; import org.apache.servicecomb.registry.consumer.MicroserviceVersionRule; import org.apache.servicecomb.registry.consumer.MicroserviceVersions; -import org.apache.servicecomb.registry.api.event.task.RecoveryEvent; import org.hamcrest.Matchers; import org.junit.After; import org.junit.Assert; @@ -141,7 +142,8 @@ public class TestConsumers extends TestRegistryBase { @Test public void registryMicroserviceMapping() { MicroserviceInstance microserviceInstance = new MicroserviceInstance(); - serviceRegistry.registerMicroserviceMapping("3rd", "1.0.0", Arrays.asList(microserviceInstance), Hello.class); + RegistrationManager.INSTANCE + .registerMicroserviceMapping("3rd", "1.0.0", Arrays.asList(microserviceInstance), Hello.class); MicroserviceVersionRule microserviceVersionRule = appManager.getOrCreateMicroserviceVersionRule(appId, "3rd", "0+"); Assert.assertThat(microserviceVersionRule.getInstances().values(), Matchers.contains(microserviceInstance)); @@ -149,7 +151,7 @@ public class TestConsumers extends TestRegistryBase { @Test public void registryMicroserviceMappingByEndpoints() { - serviceRegistry.registerMicroserviceMappingByEndpoints( + RegistrationManager.INSTANCE.registerMicroserviceMappingByEndpoints( "3rd", "1.0.0", Arrays.asList("cse://127.0.0.1:8080", "cse://127.0.0.1:8081"), diff --git a/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/diagnosis/instance/TestInstanceCacheCheckerWithoutMock.java b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/diagnosis/instance/TestInstanceCacheCheckerWithoutMock.java index 098aa51..66a6bdc 100644 --- a/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/diagnosis/instance/TestInstanceCacheCheckerWithoutMock.java +++ b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/diagnosis/instance/TestInstanceCacheCheckerWithoutMock.java @@ -23,10 +23,11 @@ import org.apache.servicecomb.foundation.common.Holder; import org.apache.servicecomb.foundation.common.testing.MockClock; import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils; import org.apache.servicecomb.registry.DiscoveryManager; -import org.apache.servicecomb.serviceregistry.RegistryUtils; -import org.apache.servicecomb.serviceregistry.ServiceRegistry; +import org.apache.servicecomb.registry.RegistrationManager; import org.apache.servicecomb.registry.consumer.MicroserviceVersionRule; import org.apache.servicecomb.registry.definition.DefinitionConst; +import org.apache.servicecomb.serviceregistry.RegistryUtils; +import org.apache.servicecomb.serviceregistry.ServiceRegistry; import org.apache.servicecomb.serviceregistry.diagnosis.Status; import org.apache.servicecomb.serviceregistry.registry.LocalServiceRegistryFactory; import org.junit.After; @@ -94,7 +95,7 @@ public class TestInstanceCacheCheckerWithoutMock { @Test public void check_StaticMicroservice() { microserviceName = appId + ":" + microserviceName; - serviceRegistry.registerMicroserviceMappingByEndpoints(microserviceName, + RegistrationManager.INSTANCE.registerMicroserviceMappingByEndpoints(microserviceName, "1", Arrays.asList("rest://localhost:8080"), ThirdPartyServiceForUT.class); diff --git a/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/registry/EmptyMockServiceRegistry.java b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/registry/EmptyMockServiceRegistry.java index 14229a8..80c3f97 100644 --- a/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/registry/EmptyMockServiceRegistry.java +++ b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/registry/EmptyMockServiceRegistry.java @@ -123,16 +123,4 @@ public class EmptyMockServiceRegistry implements ServiceRegistry { public Microservice getAggregatedRemoteMicroservice(String microserviceId) { return null; } - - @Override - public void registerMicroserviceMapping(String microserviceName, String version, List<MicroserviceInstance> instances, - Class<?> schemaIntfCls) { - - } - - @Override - public void registerMicroserviceMappingByEndpoints(String microserviceName, String version, List<String> endpoints, - Class<?> schemaIntfCls) { - - } }