Search in sources :

Example 1 with MicroserviceInstance

use of org.apache.servicecomb.service.center.client.model.MicroserviceInstance in project java-chassis by ServiceComb.

the class ServiceCenterDiscovery method pullInstance.

private void pullInstance(SubscriptionKey k, SubscriptionValue v) {
    if (myselfServiceId == null) {
        // registration not ready
        return;
    }
    try {
        FindMicroserviceInstancesResponse instancesResponse = serviceCenterClient.findMicroserviceInstance(myselfServiceId, k.appId, k.serviceName, ALL_VERSION, v.revision);
        if (instancesResponse.isModified()) {
            List<MicroserviceInstance> instances = instancesResponse.getMicroserviceInstancesResponse().getInstances() == null ? Collections.emptyList() : instancesResponse.getMicroserviceInstancesResponse().getInstances();
            setMicroserviceInfo(instances);
            LOGGER.info("Instance changed event, " + "current: revision={}, instances={}; " + "origin: revision={}, instances={}; " + "appId={}, serviceName={}", instancesResponse.getRevision(), instanceToString(instances), v.revision, instanceToString(v.instancesCache), k.appId, k.serviceName);
            v.instancesCache = instances;
            v.revision = instancesResponse.getRevision();
            eventBus.post(new InstanceChangedEvent(k.appId, k.serviceName, v.instancesCache));
        }
    } catch (Exception e) {
        LOGGER.error("find service {}#{} instance failed.", k.appId, k.serviceName, e);
        failedInstances.add(k);
    }
}
Also used : InstanceChangedEvent(org.apache.servicecomb.service.center.client.DiscoveryEvents.InstanceChangedEvent) MicroserviceInstance(org.apache.servicecomb.service.center.client.model.MicroserviceInstance) FindMicroserviceInstancesResponse(org.apache.servicecomb.service.center.client.model.FindMicroserviceInstancesResponse)

Example 2 with MicroserviceInstance

use of org.apache.servicecomb.service.center.client.model.MicroserviceInstance in project java-chassis by ServiceComb.

the class ServiceCenterDiscovery method instanceToString.

private static String instanceToString(List<MicroserviceInstance> instances) {
    if (instances == null) {
        return "";
    }
    StringBuilder sb = new StringBuilder();
    for (MicroserviceInstance instance : instances) {
        for (String endpoint : instance.getEndpoints()) {
            sb.append(endpoint.length() > 64 ? endpoint.substring(0, 64) : endpoint);
            sb.append("|");
        }
        sb.append(instance.getServiceName());
        sb.append("|");
    }
    sb.append("#");
    return sb.toString();
}
Also used : MicroserviceInstance(org.apache.servicecomb.service.center.client.model.MicroserviceInstance)

Example 3 with MicroserviceInstance

use of org.apache.servicecomb.service.center.client.model.MicroserviceInstance in project java-chassis by ServiceComb.

the class ServiceCenterClientTest method TestGetServiceInstanceMessage.

@Test
public void TestGetServiceInstanceMessage() throws IOException {
    ServiceCenterRawClient serviceCenterRawClient = Mockito.mock(ServiceCenterRawClient.class);
    HttpResponse httpResponse = new HttpResponse();
    httpResponse.setStatusCode(200);
    httpResponse.setMessage("ok");
    String responseString = "{\n" + "  \"instance\": {\n" + "      \"instanceId\": \"111\",\n" + "      \"serviceId\": \"222\",\n" + "      \"version\": \"1.0\",\n" + "      \"hostName\": \"Test\",\n" + "      \"endpoints\": [\n" + "        \"string\"\n" + "      ],\n" + "      \"status\": \"UP\",\n" + "      \"properties\": {\n" + "        \"additionalProp1\": \"string\",\n" + "        \"additionalProp2\": \"string\",\n" + "        \"additionalProp3\": \"string\"\n" + "      },\n" + "      \"healthCheck\": {\n" + "        \"mode\": \"push\",\n" + "        \"port\": \"0\",\n" + "        \"interval\": \"0\",\n" + "        \"times\": \"0\"\n" + "      },\n" + "      \"dataCenterInfo\": {\n" + "        \"name\": \"string\",\n" + "        \"region\": \"string\",\n" + "        \"availableZone\": \"string\"\n" + "      },\n" + "      \"timestamp\": \"333333\",\n" + "      \"modTimestamp\": \"4444444\"\n" + "    }\n" + "}";
    httpResponse.setContent(responseString);
    Mockito.when(serviceCenterRawClient.getHttpRequest(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(httpResponse);
    ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient);
    MicroserviceInstance responseInstance = serviceCenterClient.getMicroserviceInstance("111", "222");
    Assert.assertNotNull(responseInstance);
    Assert.assertEquals("111", responseInstance.getInstanceId());
    Assert.assertEquals("Test", responseInstance.getHostName());
}
Also used : HttpResponse(org.apache.servicecomb.http.client.common.HttpResponse) MicroserviceInstance(org.apache.servicecomb.service.center.client.model.MicroserviceInstance) Test(org.junit.Test)

Example 4 with MicroserviceInstance

use of org.apache.servicecomb.service.center.client.model.MicroserviceInstance in project java-chassis by ServiceComb.

the class ServiceCenterClientTest method TestRegisterServiceInstance.

@Test
public void TestRegisterServiceInstance() throws IOException {
    ServiceCenterRawClient serviceCenterRawClient = Mockito.mock(ServiceCenterRawClient.class);
    HttpResponse httpResponse = new HttpResponse();
    httpResponse.setStatusCode(200);
    httpResponse.setMessage("ok");
    httpResponse.setContent("{\"instanceId\": \"111111\"}");
    MicroserviceInstance instance = new MicroserviceInstance();
    instance.setInstanceId("111111");
    instance.setServiceId("222222");
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
    Mockito.when(serviceCenterRawClient.postHttpRequest("/registry/microservices/222222/instances", null, mapper.writeValueAsString(instance))).thenReturn(httpResponse);
    ServiceCenterClient serviceCenterClient = new ServiceCenterClient(serviceCenterRawClient);
    RegisteredMicroserviceInstanceResponse actualResponse = serviceCenterClient.registerMicroserviceInstance(instance);
    Assert.assertNotNull(actualResponse);
    Assert.assertEquals("111111", actualResponse.getInstanceId());
}
Also used : RegisteredMicroserviceInstanceResponse(org.apache.servicecomb.service.center.client.model.RegisteredMicroserviceInstanceResponse) HttpResponse(org.apache.servicecomb.http.client.common.HttpResponse) MicroserviceInstance(org.apache.servicecomb.service.center.client.model.MicroserviceInstance) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 5 with MicroserviceInstance

use of org.apache.servicecomb.service.center.client.model.MicroserviceInstance in project java-chassis by ServiceComb.

the class RegistryClientTest method testRestTransport.

@Override
public void testRestTransport() throws Exception {
    AddressManager addressManager = new AddressManager("default", Arrays.asList("http://127.0.0.1:30100"), new EventBus());
    SSLProperties sslProperties = new SSLProperties();
    sslProperties.setEnabled(false);
    ServiceCenterClient serviceCenterClient = new ServiceCenterClient(addressManager, sslProperties, new DefaultRequestAuthHeaderProvider(), "default", null);
    EventBus eventBus = new SimpleEventBus();
    ServiceCenterConfiguration serviceCenterConfiguration = new ServiceCenterConfiguration();
    ServiceCenterRegistration serviceCenterRegistration = new ServiceCenterRegistration(serviceCenterClient, serviceCenterConfiguration, eventBus);
    Microservice microservice = new Microservice();
    microservice.setAppId("app_registry");
    microservice.setServiceName("name_registry");
    microservice.setVersion("1.0.0");
    microservice.setEnvironment("development");
    List<String> schemas = new ArrayList<>();
    schemas.add("SchemaA");
    schemas.add("SchemaB");
    microservice.setSchemas(schemas);
    MicroserviceInstance microserviceInstance = new MicroserviceInstance();
    microserviceInstance.setHostName("host_registry");
    List<String> endpoints = new ArrayList<>();
    endpoints.add("rest://127.0.0.1/");
    microserviceInstance.setEndpoints(endpoints);
    List<SchemaInfo> schemaInfos = new ArrayList<>();
    SchemaInfo schemaA = new SchemaInfo();
    schemaA.setSchemaId("SchemaA");
    schemaA.setSchema("schema contents in any format");
    schemaA.setSummary(Hashing.sha256().newHasher().putString("schema contents in any format".toString(), Charsets.UTF_8).hash().toString());
    schemaInfos.add(schemaA);
    SchemaInfo schemaB = new SchemaInfo();
    schemaB.setSchemaId("SchemaA");
    schemaB.setSchema("schema contents in any format");
    schemaB.setSummary(Hashing.sha256().newHasher().putString("schema contents in any format".toString(), Charsets.UTF_8).hash().toString());
    schemaInfos.add(schemaB);
    serviceCenterRegistration.setMicroservice(microservice);
    serviceCenterRegistration.setMicroserviceInstance(microserviceInstance);
    serviceCenterRegistration.setSchemaInfos(schemaInfos);
    eventBus.register(this);
    serviceCenterRegistration.startRegistration();
    registrationCounter.await(30000, TimeUnit.MILLISECONDS);
    if (hasRegistered) {
        TestMgr.check(events.size() >= 3, true);
        TestMgr.check(events.get(0).isSuccess(), true);
        TestMgr.check(events.get(0) instanceof MicroserviceRegistrationEvent, true);
        TestMgr.check(events.get(1).isSuccess(), true);
        TestMgr.check(events.get(1) instanceof MicroserviceInstanceRegistrationEvent, true);
        TestMgr.check(events.get(2).isSuccess(), true);
        TestMgr.check(events.get(2) instanceof HeartBeatEvent, true);
    } else {
        TestMgr.check(events.size() >= 4, true);
        TestMgr.check(events.get(0).isSuccess(), true);
        TestMgr.check(events.get(0) instanceof MicroserviceRegistrationEvent, true);
        TestMgr.check(events.get(1).isSuccess(), true);
        TestMgr.check(events.get(1) instanceof SchemaRegistrationEvent, true);
        TestMgr.check(events.get(2).isSuccess(), true);
        TestMgr.check(events.get(2) instanceof MicroserviceInstanceRegistrationEvent, true);
        TestMgr.check(events.get(3).isSuccess(), true);
        TestMgr.check(events.get(3) instanceof HeartBeatEvent, true);
    }
    ServiceCenterDiscovery discovery = new ServiceCenterDiscovery(serviceCenterClient, eventBus);
    discovery.updateMyselfServiceId(microservice.getServiceId());
    discovery.startDiscovery();
    discovery.registerIfNotPresent(new SubscriptionKey(microservice.getAppId(), microservice.getServiceName()));
    discoveryCounter.await(30000, TimeUnit.MILLISECONDS);
    TestMgr.check(instances != null, true);
    TestMgr.check(instances.size(), 1);
    discovery.stop();
    serviceCenterRegistration.stop();
    serviceCenterClient.deleteMicroserviceInstance(microservice.getServiceId(), microserviceInstance.getInstanceId());
}
Also used : SchemaRegistrationEvent(org.apache.servicecomb.service.center.client.RegistrationEvents.SchemaRegistrationEvent) AddressManager(org.apache.servicecomb.service.center.client.AddressManager) SubscriptionKey(org.apache.servicecomb.service.center.client.ServiceCenterDiscovery.SubscriptionKey) ServiceCenterConfiguration(org.apache.servicecomb.service.center.client.model.ServiceCenterConfiguration) ServiceCenterRegistration(org.apache.servicecomb.service.center.client.ServiceCenterRegistration) ServiceCenterDiscovery(org.apache.servicecomb.service.center.client.ServiceCenterDiscovery) SSLProperties(org.apache.servicecomb.http.client.common.HttpConfiguration.SSLProperties) ArrayList(java.util.ArrayList) MicroserviceInstance(org.apache.servicecomb.service.center.client.model.MicroserviceInstance) EventBus(com.google.common.eventbus.EventBus) SimpleEventBus(org.apache.servicecomb.foundation.common.event.SimpleEventBus) DefaultRequestAuthHeaderProvider(org.apache.servicecomb.http.client.auth.DefaultRequestAuthHeaderProvider) MicroserviceRegistrationEvent(org.apache.servicecomb.service.center.client.RegistrationEvents.MicroserviceRegistrationEvent) HeartBeatEvent(org.apache.servicecomb.service.center.client.RegistrationEvents.HeartBeatEvent) Microservice(org.apache.servicecomb.service.center.client.model.Microservice) ServiceCenterClient(org.apache.servicecomb.service.center.client.ServiceCenterClient) SimpleEventBus(org.apache.servicecomb.foundation.common.event.SimpleEventBus) MicroserviceInstanceRegistrationEvent(org.apache.servicecomb.service.center.client.RegistrationEvents.MicroserviceInstanceRegistrationEvent) SchemaInfo(org.apache.servicecomb.service.center.client.model.SchemaInfo)

Aggregations

MicroserviceInstance (org.apache.servicecomb.service.center.client.model.MicroserviceInstance)5 HttpResponse (org.apache.servicecomb.http.client.common.HttpResponse)2 Test (org.junit.Test)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 EventBus (com.google.common.eventbus.EventBus)1 ArrayList (java.util.ArrayList)1 SimpleEventBus (org.apache.servicecomb.foundation.common.event.SimpleEventBus)1 DefaultRequestAuthHeaderProvider (org.apache.servicecomb.http.client.auth.DefaultRequestAuthHeaderProvider)1 SSLProperties (org.apache.servicecomb.http.client.common.HttpConfiguration.SSLProperties)1 AddressManager (org.apache.servicecomb.service.center.client.AddressManager)1 InstanceChangedEvent (org.apache.servicecomb.service.center.client.DiscoveryEvents.InstanceChangedEvent)1 HeartBeatEvent (org.apache.servicecomb.service.center.client.RegistrationEvents.HeartBeatEvent)1 MicroserviceInstanceRegistrationEvent (org.apache.servicecomb.service.center.client.RegistrationEvents.MicroserviceInstanceRegistrationEvent)1 MicroserviceRegistrationEvent (org.apache.servicecomb.service.center.client.RegistrationEvents.MicroserviceRegistrationEvent)1 SchemaRegistrationEvent (org.apache.servicecomb.service.center.client.RegistrationEvents.SchemaRegistrationEvent)1 ServiceCenterClient (org.apache.servicecomb.service.center.client.ServiceCenterClient)1 ServiceCenterDiscovery (org.apache.servicecomb.service.center.client.ServiceCenterDiscovery)1 SubscriptionKey (org.apache.servicecomb.service.center.client.ServiceCenterDiscovery.SubscriptionKey)1 ServiceCenterRegistration (org.apache.servicecomb.service.center.client.ServiceCenterRegistration)1 FindMicroserviceInstancesResponse (org.apache.servicecomb.service.center.client.model.FindMicroserviceInstancesResponse)1