This is an automated email from the ASF dual-hosted git repository. yaohaishi pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git
commit 1a920ebf93cfb2dd8e5a0360693fcb12231def0b Author: yhs0092 <[email protected]> AuthorDate: Wed Jan 15 14:57:54 2020 +0800 [SCB-1691] replace RestUtils and WebsocketUtils in ServiceRegistryClientImpl --- .../client/http/ServiceRegistryClientImpl.java | 50 +++++++++++--------- .../registry/RemoteServiceRegistry.java | 2 +- .../serviceregistry/client/http/RestUtilsTest.java | 1 + .../client/http/TestClientHttp.java | 18 ++++++-- .../client/http/TestServiceRegistryClientImpl.java | 53 +++++++++------------- 5 files changed, 65 insertions(+), 59 deletions(-) diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java index ed2e369..5ab309d 100644 --- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java +++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/client/http/ServiceRegistryClientImpl.java @@ -89,8 +89,14 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient { // extract this, ServiceRegistryClient is better to be no status. private Map<String, Boolean> watchServices = new ConcurrentHashMap<>(); - public ServiceRegistryClientImpl(IpPortManager ipPortManager) { + private RestClientUtil restClientUtil; + + private WebsocketClientUtil websocketClientUtil; + + public ServiceRegistryClientImpl(IpPortManager ipPortManager, ServiceRegistryConfig serviceRegistryConfig) { this.ipPortManager = ipPortManager; + this.restClientUtil = new RestClientUtil(serviceRegistryConfig); + this.websocketClientUtil = new WebsocketClientUtil(serviceRegistryConfig); } private LoadingCache<String, Map<String, String>> schemaCache = CacheBuilder.newBuilder() @@ -113,7 +119,7 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient { LOGGER.warn("invoke service [{}] failed, retry.", requestContext.getUri()); requestContext.setIpPort(ipPortManager.getNextAvailableAddress(requestContext.getIpPort())); requestContext.incrementRetryTimes(); - RestUtils.httpDo(requestContext, responseHandler); + restClientUtil.httpDo(requestContext, responseHandler); } @VisibleForTesting @@ -197,7 +203,7 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient { } // temporary copy from syncHandler - // we will use swagger invocation to replace RestUtils later. + // we will use swagger invocation to replace restClientUtil later. private Handler<RestResponse> syncHandlerEx(CountDownLatch countDownLatch, Holder<ResponseWrapper> holder) { return restResponse -> { RequestContext requestContext = restResponse.getRequestContext(); @@ -284,7 +290,7 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient { IpPort ipPort = ipPortManager.getAvailableAddress(); CountDownLatch countDownLatch = new CountDownLatch(1); - RestUtils.get(ipPort, + restClientUtil.get(ipPort, Const.REGISTRY_API.MICROSERVICE_OPERATION_ALL, new RequestParam(), syncHandler(countDownLatch, GetAllServicesResponse.class, holder)); @@ -305,7 +311,7 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient { IpPort ipPort = ipPortManager.getAvailableAddress(); CountDownLatch countDownLatch = new CountDownLatch(1); - RestUtils.get(ipPort, + restClientUtil.get(ipPort, Const.REGISTRY_API.MICROSERVICE_EXISTENCE, new RequestParam().addQueryParam("type", "microservice") .addQueryParam("appId", appId) @@ -334,7 +340,7 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient { IpPort ipPort = ipPortManager.getAvailableAddress(); CountDownLatch countDownLatch = new CountDownLatch(1); - RestUtils.get(ipPort, + restClientUtil.get(ipPort, Const.REGISTRY_API.MICROSERVICE_EXISTENCE, new RequestParam().addQueryParam("type", "schema") .addQueryParam("serviceId", microserviceId) @@ -363,7 +369,7 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient { byte[] body = JsonUtils.writeValueAsBytes(request); CountDownLatch countDownLatch = new CountDownLatch(1); - RestUtils.put(ipPort, + restClientUtil.put(ipPort, String.format(Const.REGISTRY_API.MICROSERVICE_SCHEMA, microserviceId, schemaId), new RequestParam().setBody(body), syncHandlerEx(countDownLatch, holder)); @@ -421,7 +427,7 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient { if (global) { param.addQueryParam("global", "true"); } - RestUtils.get(ipPort, + restClientUtil.get(ipPort, String.format(Const.REGISTRY_API.MICROSERVICE_SCHEMA, microserviceId, schemaId), param, syncHandler(countDownLatch, GetSchemaResponse.class, holder)); @@ -464,7 +470,7 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient { requestParam.addQueryParam("global", "true"); } - RestUtils.get(ipPort, + restClientUtil.get(ipPort, String.format(url, microserviceId), requestParam, syncHandler(countDownLatch, GetSchemasResponse.class, holder)); @@ -500,7 +506,7 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient { } CountDownLatch countDownLatch = new CountDownLatch(1); - RestUtils.post(ipPort, + restClientUtil.post(ipPort, Const.REGISTRY_API.MICROSERVICE_OPERATION_ALL, new RequestParam().setBody(body), syncHandler(countDownLatch, CreateServiceResponse.class, holder)); @@ -532,7 +538,7 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient { if (global) { param.addQueryParam("global", "true"); } - RestUtils.get(ipPort, + restClientUtil.get(ipPort, String.format(Const.REGISTRY_API.MICROSERVICE_OPERATION_ONE, microserviceId), param, syncHandler(countDownLatch, GetServiceResponse.class, holder)); @@ -567,7 +573,7 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient { } CountDownLatch countDownLatch = new CountDownLatch(1); - RestUtils.post(ipPort, + restClientUtil.post(ipPort, String.format(Const.REGISTRY_API.MICROSERVICE_INSTANCE_OPERATION_ALL, instance.getServiceId()), new RequestParam().setBody(body), syncHandler(countDownLatch, RegisterInstanceResponse.class, holder)); @@ -587,7 +593,7 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient { IpPort ipPort = ipPortManager.getAvailableAddress(); CountDownLatch countDownLatch = new CountDownLatch(1); - RestUtils.get(ipPort, + restClientUtil.get(ipPort, String.format(Const.REGISTRY_API.MICROSERVICE_INSTANCE_OPERATION_ALL, providerId), new RequestParam().addHeader("X-ConsumerId", consumerId), syncHandler(countDownLatch, GetInstancesResponse.class, holder)); @@ -608,7 +614,7 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient { IpPort ipPort = ipPortManager.getAvailableAddress(); CountDownLatch countDownLatch = new CountDownLatch(1); - RestUtils.delete(ipPort, + restClientUtil.delete(ipPort, String.format(Const.REGISTRY_API.MICROSERVICE_INSTANCE_OPERATION_ONE, microserviceId, microserviceInstanceId), new RequestParam(), syncHandler(countDownLatch, HttpClientResponse.class, holder)); @@ -635,7 +641,7 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient { IpPort ipPort = ipPortManager.getAvailableAddress(); CountDownLatch countDownLatch = new CountDownLatch(1); - RestUtils.put(ipPort, + restClientUtil.put(ipPort, String.format(Const.REGISTRY_API.MICROSERVICE_HEARTBEAT, microserviceId, microserviceInstanceId), new RequestParam().setTimeout(ServiceRegistryConfig.INSTANCE.getHeartBeatRequestTimeout()), syncHandler(countDownLatch, HttpClientResponse.class, holder)); @@ -679,7 +685,7 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient { String url = String.format(Const.REGISTRY_API.MICROSERVICE_WATCH, selfMicroserviceId); IpPort ipPort = ipPortManager.getAvailableAddress(); - WebsocketUtils.open(ipPort, url, o -> { + websocketClientUtil.open(ipPort, url, o -> { onOpen.success(o); LOGGER.info( "watching microservice {} successfully, " @@ -753,7 +759,7 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient { requestParam.addQueryParam("rev", revision); } - RestUtils.get(ipPort, + restClientUtil.get(ipPort, Const.REGISTRY_API.MICROSERVICE_INSTANCES, requestParam, syncHandlerForInstances(countDownLatch, microserviceInstances)); @@ -805,7 +811,7 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient { } CountDownLatch countDownLatch = new CountDownLatch(1); - RestUtils.put(ipPort, + restClientUtil.put(ipPort, String.format(Const.REGISTRY_API.MICROSERVICE_PROPERTIES, microserviceId), new RequestParam().setBody(body), syncHandler(countDownLatch, HttpClientResponse.class, holder)); @@ -842,7 +848,7 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient { } CountDownLatch countDownLatch = new CountDownLatch(1); - RestUtils.put(ipPort, + restClientUtil.put(ipPort, String.format(Const.REGISTRY_API.MICROSERVICE_INSTANCE_PROPERTIES, microserviceId, microserviceInstanceId), new RequestParam().setBody(body), syncHandler(countDownLatch, HttpClientResponse.class, holder)); @@ -869,7 +875,7 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient { Holder<MicroserviceInstanceResponse> holder = new Holder<>(); IpPort ipPort = ipPortManager.getAvailableAddress(); CountDownLatch countDownLatch = new CountDownLatch(1); - RestUtils.get(ipPort, + restClientUtil.get(ipPort, String.format(Const.REGISTRY_API.MICROSERVICE_INSTANCE_OPERATION_ONE, serviceId, instanceId), new RequestParam().addHeader("X-ConsumerId", serviceId).addQueryParam("global", "true"), syncHandler(countDownLatch, MicroserviceInstanceResponse.class, holder)); @@ -890,7 +896,7 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient { IpPort ipPort = ipPortManager.getAvailableAddress(); CountDownLatch countDownLatch = new CountDownLatch(1); - RestUtils.get(ipPort, + restClientUtil.get(ipPort, Const.REGISTRY_API.SERVICECENTER_VERSION, new RequestParam(), syncHandler(countDownLatch, ServiceCenterInfo.class, holder)); @@ -920,7 +926,7 @@ public final class ServiceRegistryClientImpl implements ServiceRegistryClient { Map<String, String[]> queryParams = new HashMap<>(); queryParams.put("value", new String[] {status.toString()}); CountDownLatch countDownLatch = new CountDownLatch(1); - RestUtils.put(ipPort, url, new RequestParam().setQueryParams(queryParams), + restClientUtil.put(ipPort, url, new RequestParam().setQueryParams(queryParams), syncHandler(countDownLatch, HttpClientResponse.class, holder)); countDownLatch.await(); if (holder.value != null) { diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/RemoteServiceRegistry.java b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/RemoteServiceRegistry.java index dfeee99..b6af226 100644 --- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/RemoteServiceRegistry.java +++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/RemoteServiceRegistry.java @@ -72,7 +72,7 @@ public class RemoteServiceRegistry extends AbstractServiceRegistry { @Override protected ServiceRegistryClient createServiceRegistryClient() { - return new ServiceRegistryClientImpl(ipPortManager); + return new ServiceRegistryClientImpl(ipPortManager, serviceRegistryConfig); } @Override diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/RestUtilsTest.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/RestUtilsTest.java index ad72502..336a805 100644 --- a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/RestUtilsTest.java +++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/RestUtilsTest.java @@ -28,6 +28,7 @@ import org.junit.Test; import io.vertx.core.MultiMap; import io.vertx.core.http.HttpMethod; +@SuppressWarnings("deprecation") public class RestUtilsTest { @Test public void defaultHeadersContainServiceRegistryAndAuthentication() throws Exception { diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestClientHttp.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestClientHttp.java index b58088d..48ab807 100644 --- a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestClientHttp.java +++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestClientHttp.java @@ -63,24 +63,34 @@ public class TestClientHttp { public void await() throws InterruptedException { } }; - new MockUp<RestUtils>() { + new MockUp<RestClientUtil>() { @Mock void httpDo(RequestContext requestContext, Handler<RestResponse> responseHandler) { } }; - - new MockUp<WebsocketUtils>() { + new MockUp<WebsocketClientUtil>() { @Mock void open(IpPort ipPort, String url, Handler<Void> onOpen, Handler<Void> onClose, Handler<Buffer> onMessage, Handler<Throwable> onException, Handler<Throwable> onConnectFailed) { } }; + // mock up this two client pool, since this UT case doesn't require the client pool actually boot up. + new MockUp<HttpClientPool>() { + @Mock + void create() { + } + }; + new MockUp<WebsocketClientPool>() { + @Mock + void create() { + } + }; MicroserviceFactory microserviceFactory = new MicroserviceFactory(); Microservice microservice = microserviceFactory.create("app", "ms"); - ServiceRegistryClientImpl oClient = new ServiceRegistryClientImpl(manager); + ServiceRegistryClientImpl oClient = new ServiceRegistryClientImpl(manager, ServiceRegistryConfig.INSTANCE); oClient.init(); oClient.registerMicroservice(microservice); oClient.registerMicroserviceInstance(microservice.getInstance()); diff --git a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestServiceRegistryClientImpl.java b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestServiceRegistryClientImpl.java index 3a7662b..6e260d9 100644 --- a/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestServiceRegistryClientImpl.java +++ b/service-registry/src/test/java/org/apache/servicecomb/serviceregistry/client/http/TestServiceRegistryClientImpl.java @@ -45,6 +45,7 @@ import org.apache.servicecomb.serviceregistry.api.response.GetServiceResponse; import org.apache.servicecomb.serviceregistry.client.ClientException; import org.apache.servicecomb.serviceregistry.client.IpPortManager; import org.apache.servicecomb.serviceregistry.client.http.ServiceRegistryClientImpl.ResponseWrapper; +import org.apache.servicecomb.serviceregistry.config.ServiceRegistryConfig; import org.apache.servicecomb.serviceregistry.definition.DefinitionConst; import org.junit.After; import org.junit.Assert; @@ -60,7 +61,6 @@ import io.vertx.core.Handler; import io.vertx.core.buffer.Buffer; import io.vertx.core.http.HttpClientOptions; import io.vertx.core.http.HttpClientResponse; -import io.vertx.core.http.HttpMethod; import io.vertx.core.json.Json; import mockit.Deencapsulation; import mockit.Expectations; @@ -78,13 +78,7 @@ public class TestServiceRegistryClientImpl { @Before public void setUp() throws Exception { - oClient = new ServiceRegistryClientImpl(ipPortManager); - - new MockUp<RestUtils>() { - @Mock - void httpDo(RequestContext requestContext, Handler<RestResponse> responseHandler) { - } - }; + oClient = new ServiceRegistryClientImpl(ipPortManager, ServiceRegistryConfig.buildFromConfiguration()); new MockUp<RegistryUtils>() { @Mock @@ -216,7 +210,7 @@ public class TestServiceRegistryClientImpl { }; } }; - new MockUp<RestUtils>() { + new MockUp<RestClientUtil>() { @Mock void httpDo(RequestContext requestContext, Handler<RestResponse> responseHandler) { responseHandler.handle(null); @@ -248,7 +242,7 @@ public class TestServiceRegistryClientImpl { }; } }; - new MockUp<RestUtils>() { + new MockUp<RestClientUtil>() { @Mock void httpDo(RequestContext requestContext, Handler<RestResponse> responseHandler) { responseHandler.handle(null); @@ -304,7 +298,7 @@ public class TestServiceRegistryClientImpl { String microserviceId = "msId"; String schemaId = "schemaId"; - new MockUp<RestUtils>() { + new MockUp<RestClientUtil>() { @Mock void httpDo(RequestContext requestContext, Handler<RestResponse> responseHandler) { Holder<GetExistenceResponse> holder = Deencapsulation.getField(responseHandler, "arg$4"); @@ -317,7 +311,7 @@ public class TestServiceRegistryClientImpl { @Test public void getAggregatedMicroservice() { String microserviceId = "msId"; - new MockUp<RestUtils>() { + new MockUp<RestClientUtil>() { @Mock void httpDo(RequestContext requestContext, Handler<RestResponse> responseHandler) { Holder<GetServiceResponse> holder = Deencapsulation.getField(responseHandler, "arg$4"); @@ -342,7 +336,7 @@ public class TestServiceRegistryClientImpl { String microserviceId = "msId"; String schemaId = "schemaId"; - new MockUp<RestUtils>() { + new MockUp<RestClientUtil>() { @Mock void httpDo(RequestContext requestContext, Handler<RestResponse> responseHandler) { @@ -376,7 +370,7 @@ public class TestServiceRegistryClientImpl { public void getSchemas() { String microserviceId = "msId"; - new MockUp<RestUtils>() { + new MockUp<RestClientUtil>() { @Mock void httpDo(RequestContext requestContext, Handler<RestResponse> responseHandler) { Holder<GetSchemasResponse> holder = Deencapsulation.getField(responseHandler, "arg$4"); @@ -399,7 +393,7 @@ public class TestServiceRegistryClientImpl { public void getSchemasForNew() { String microserviceId = "msId"; - new MockUp<RestUtils>() { + new MockUp<RestClientUtil>() { @Mock void httpDo(RequestContext requestContext, Handler<RestResponse> responseHandler) { Holder<GetSchemasResponse> holder = Deencapsulation.getField(responseHandler, "arg$4"); @@ -422,7 +416,7 @@ public class TestServiceRegistryClientImpl { public void getSchemasFailed() { String microserviceId = "msId"; - new MockUp<RestUtils>() { + new MockUp<RestClientUtil>() { @Mock void httpDo(RequestContext requestContext, Handler<RestResponse> responseHandler) { Holder<GetSchemasResponse> holder = Deencapsulation.getField(responseHandler, "arg$4"); @@ -437,7 +431,7 @@ public class TestServiceRegistryClientImpl { @Test public void testFindServiceInstance() { - new MockUp<RestUtils>() { + new MockUp<RestClientUtil>() { @Mock void get(IpPort ipPort, String uri, RequestParam requestParam, Handler<RestResponse> responseHandler) { @@ -449,7 +443,7 @@ public class TestServiceRegistryClientImpl { @Test public void findServiceInstance_consumerId_null() { - new MockUp<RestUtils>() { + new MockUp<RestClientUtil>() { @Mock void get(IpPort ipPort, String uri, RequestParam requestParam, Handler<RestResponse> responseHandler) { @@ -461,7 +455,7 @@ public class TestServiceRegistryClientImpl { } @Test - public void findServiceInstances_microserviceNotExist(@Mocked RequestContext requestContext) { + public void findServiceInstances_microserviceNotExist() { HttpClientResponse response = new MockUp<HttpClientResponse>() { @Mock int statusCode() { @@ -475,18 +469,13 @@ public class TestServiceRegistryClientImpl { return null; } }.getMockInstance(); - RestResponse restResponse = new RestResponse(requestContext, response); - new MockUp<RestUtils>() { - @Mock - void get(IpPort ipPort, String uri, RequestParam requestParam, - Handler<RestResponse> responseHandler) { - Assert.assertEquals("appId=appId&global=true&serviceName=serviceName&version=0.0.0.0%2B", - requestParam.getQueryParams()); - httpDo(RestUtils.createRequestContext(HttpMethod.GET, ipPort, uri, requestParam), responseHandler); - } - + RestResponse restResponse = new RestResponse(null, response); + new MockUp<RestClientUtil>() { @Mock void httpDo(RequestContext requestContext, Handler<RestResponse> responseHandler) { + Assert.assertEquals("appId=appId&global=true&serviceName=serviceName&version=0.0.0.0%2B", + requestContext.getParams().getQueryParams()); + restResponse.setRequestContext(requestContext); responseHandler.handle(restResponse); } }; @@ -506,7 +495,7 @@ public class TestServiceRegistryClientImpl { serviceCenterInfo.setApiVersion("x.x.x"); serviceCenterInfo.setConfig(new ServiceCenterConfig()); - new MockUp<RestUtils>() { + new MockUp<RestClientUtil>() { @Mock void httpDo(RequestContext requestContext, Handler<RestResponse> responseHandler) { Holder<ServiceCenterInfo> holder = Deencapsulation.getField(responseHandler, "arg$4"); @@ -550,7 +539,7 @@ public class TestServiceRegistryClientImpl { return 200; } }.getMockInstance(); - new MockUp<RestUtils>() { + new MockUp<RestClientUtil>() { @Mock void put(IpPort ipPort, String uri, RequestParam requestParam, Handler<RestResponse> responseHandler) { Holder<HttpClientResponse> holder = Deencapsulation.getField(responseHandler, "arg$4"); @@ -571,7 +560,7 @@ public class TestServiceRegistryClientImpl { return 400; } }.getMockInstance(); - new MockUp<RestUtils>() { + new MockUp<RestClientUtil>() { @Mock void put(IpPort ipPort, String uri, RequestParam requestParam, Handler<RestResponse> responseHandler) { Holder<HttpClientResponse> holder = Deencapsulation.getField(responseHandler, "arg$4");
