Skip to content

Commit

Permalink
[#4676] optimized the method of building ServiceCenterClient (#4677)
Browse files Browse the repository at this point in the history
  • Loading branch information
chengyouling authored Jan 15, 2025
1 parent 0668f63 commit 5ee33ed
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ public class ServiceCenterClient implements ServiceCenterOperation {

private EventBus eventBus;

private ServiceCenterAddressManager addressManager;
private final ServiceCenterAddressManager addressManager;

public ServiceCenterClient(ServiceCenterRawClient httpClient) {
public ServiceCenterClient(ServiceCenterRawClient httpClient, ServiceCenterAddressManager addressManager) {
this.httpClient = httpClient;
this.addressManager = addressManager;
}

public ServiceCenterClient setEventBus(EventBus eventBus) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

Expand All @@ -41,11 +42,18 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.google.common.eventbus.EventBus;

/**
* Created by on 2019/10/17.
*/
public class ServiceCenterClientTest {
private final ServiceCenterAddressManager addressManager;

public ServiceCenterClientTest() {
this.addressManager = new ServiceCenterAddressManager("default", Arrays.asList("http://127.0.0.1:30100"),
new EventBus());
}

@Test
public void TestGetServiceCenterInstances() throws IOException {
Expand Down Expand Up @@ -92,7 +100,7 @@ public void TestGetServiceCenterInstances() throws IOException {

Mockito.when(serviceCenterRawClient.getHttpRequest("/registry/health", null, null)).thenReturn(httpResponse);

ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient);
ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient, addressManager);
MicroserviceInstancesResponse serviceCenterInstances = serviceCenterClient.getServiceCenterInstances();

Assertions.assertNotNull(serviceCenterInstances);
Expand Down Expand Up @@ -121,7 +129,7 @@ public void TestRegistryService() throws IOException {
Mockito.anyString()))
.thenReturn(httpResponse);

ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient);
ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient, addressManager);
RegisteredMicroserviceResponse actualResponse = serviceCenterClient.registerMicroservice(microservice);

Assertions.assertNotNull(actualResponse);
Expand Down Expand Up @@ -179,7 +187,7 @@ public void TestGetServiceMessage() throws IOException {
Mockito.when(serviceCenterRawClient.getHttpRequest("/registry/microservices/111111", null, null))
.thenReturn(httpResponse);

ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient);
ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient, addressManager);
Microservice microservices = serviceCenterClient.getMicroserviceByServiceId("111111");

Assertions.assertNotNull(microservices);
Expand Down Expand Up @@ -207,7 +215,7 @@ public void TestGetServiceList() throws IOException {
Mockito.when(serviceCenterRawClient.getHttpRequest(Mockito.any(), Mockito.any(), Mockito.any()))
.thenReturn(httpResponse);

ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient);
ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient, addressManager);
MicroservicesResponse actualMicroservicesResponse = serviceCenterClient.getMicroserviceList();

Assertions.assertNotNull(actualMicroservicesResponse);
Expand All @@ -228,7 +236,7 @@ public void TestQueryServiceId() throws IOException {
Mockito.when(serviceCenterRawClient.getHttpRequest(Mockito.any(), Mockito.any(), Mockito.any()))
.thenReturn(httpResponse);

ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient);
ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient, addressManager);
Microservice microservice = new Microservice("Test111");
RegisteredMicroserviceResponse actualServiceId = serviceCenterClient.queryServiceId(microservice);

Expand Down Expand Up @@ -257,7 +265,7 @@ public void TestRegisterServiceInstance() throws IOException {
Mockito.anyString()))
.thenReturn(httpResponse);

ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient);
ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient, addressManager);
RegisteredMicroserviceInstanceResponse actualResponse = serviceCenterClient.registerMicroserviceInstance(instance);

Assertions.assertNotNull(actualResponse);
Expand All @@ -276,7 +284,7 @@ public void TestDeleteServiceInstance() throws IOException {
Mockito.when(serviceCenterRawClient.deleteHttpRequest(Mockito.any(), Mockito.any(), Mockito.any()))
.thenReturn(httpResponse);

ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient);
ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient, addressManager);
serviceCenterClient.deleteMicroserviceInstance("111", "222");
}

Expand Down Expand Up @@ -310,7 +318,7 @@ public void TestGetServiceInstanceList() throws IOException {
Mockito.when(serviceCenterRawClient.getHttpRequest("/registry/microservices/222222/instances", null, null))
.thenReturn(httpResponse);

ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient);
ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient, addressManager);
MicroserviceInstancesResponse serviceCenterInstances = serviceCenterClient
.getMicroserviceInstanceList("222222");

Expand Down Expand Up @@ -364,7 +372,7 @@ public void TestGetServiceInstanceMessage() throws IOException {
Mockito.when(serviceCenterRawClient.getHttpRequest(Mockito.any(), Mockito.any(), Mockito.any()))
.thenReturn(httpResponse);

ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient);
ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient, addressManager);
MicroserviceInstance responseInstance = serviceCenterClient
.getMicroserviceInstance("111", "222");

Expand Down Expand Up @@ -394,7 +402,7 @@ public void TestSendHeartBeats() throws IOException {
Mockito.anyString()))
.thenReturn(httpResponse);

ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient);
ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient, addressManager);
serviceCenterClient.sendHeartBeats(heartbeatsRequest);
}

Expand All @@ -410,7 +418,7 @@ public void TestUpdateServicesInstanceStatus() throws IOException {
Mockito.when(serviceCenterRawClient.putHttpRequest(Mockito.any(), Mockito.any(), Mockito.any()))
.thenReturn(httpResponse);

ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient);
ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient, addressManager);
Boolean result = serviceCenterClient
.updateMicroserviceInstanceStatus("111", "222", MicroserviceInstanceStatus.UP);

Expand Down Expand Up @@ -440,7 +448,7 @@ public void TestGetServiceSchemas() throws IOException {
Mockito.when(serviceCenterRawClient.getHttpRequest(Mockito.any(), Mockito.any(), Mockito.any()))
.thenReturn(httpResponse);

ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient);
ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient, addressManager);
List<SchemaInfo> schemaResponse = serviceCenterClient
.getServiceSchemasList("111", false);

Expand Down Expand Up @@ -468,7 +476,7 @@ public void TestGetServiceSchemasContext() throws IOException {
Mockito.when(serviceCenterRawClient.getHttpRequest(Mockito.any(), Mockito.any(), Mockito.any()))
.thenReturn(httpResponse);

ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient);
ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient, addressManager);
String schemaContext = serviceCenterClient
.getServiceSchemaContext("111", "222");

Expand All @@ -488,7 +496,7 @@ public void TestUpdateServiceSchema() throws IOException {
Mockito.when(serviceCenterRawClient.putHttpRequest(Mockito.any(), Mockito.any(), Mockito.any()))
.thenReturn(httpResponse);

ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient);
ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient, addressManager);
boolean result = serviceCenterClient
.updateServiceSchemaContext("111", new SchemaInfo());

Expand All @@ -506,7 +514,7 @@ public void testUpdateMicroserviceProperties() throws IOException {
Mockito.when(serviceCenterRawClient.putHttpRequest(Mockito.any(), Mockito.any(), Mockito.any()))
.thenReturn(httpResponse);

ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient);
ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient, addressManager);
boolean result = serviceCenterClient
.updateMicroserviceProperties("111", new HashMap<String, String>());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void startup(Environment environment) {
new ServiceCenterClient(new ServiceCenterRawClient.Builder()
.setTenantName("default")
.setAddressManager(addressManager)
.setHttpTransport(createHttpTransport(environment, sslProperties)).build());
.setHttpTransport(createHttpTransport(environment, sslProperties)).build(), addressManager);

Map<String, ServiceCenterClient> clients = new HashMap<>(1);
clients.put(DEFAULT_REGISTRY_NAME, serviceCenterClient);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static ServiceCenterClient serviceCenterClient(SCConfigurationProperties
return new ServiceCenterClient(new ServiceCenterRawClient.Builder()
.setTenantName("default")
.setAddressManager(addressManager)
.setHttpTransport(createHttpTransport(environment, sslProperties)).build());
.setHttpTransport(createHttpTransport(environment, sslProperties)).build(), addressManager);
}

private static HttpTransport createHttpTransport(Environment environment, SSLProperties sslProperties) {
Expand Down

0 comments on commit 5ee33ed

Please sign in to comment.