Search in sources :

Example 6 with Microservice

use of io.servicecomb.serviceregistry.api.registry.Microservice in project java-chassis by ServiceComb.

the class TestIpPortManager method testCreateServiceRegistryCacheWithInstanceCache.

@Test
public void testCreateServiceRegistryCacheWithInstanceCache() {
    boolean validAssert = true;
    List<MicroserviceInstance> list = new ArrayList<MicroserviceInstance>();
    MicroserviceInstance e1 = new MicroserviceInstance();
    list.add(e1);
    new MockUp<RegistryUtils>() {

        @Mock
        public List<MicroserviceInstance> findServiceInstance(String appId, String serviceName, String versionRule) {
            return list;
        }
    };
    Microservice microservice = new Microservice();
    new MockUp<RegistryUtils>() {

        @Mock
        private Microservice createMicroserviceFromDefinition() {
            return microservice;
        }
    };
    try {
        Deencapsulation.setField(manager, "instanceCache", Mockito.mock(InstanceCache.class));
        manager.createServiceRegistryCache();
        Assert.assertNotNull(manager.get());
        Assert.assertNull(manager.next());
    } catch (Exception e) {
        validAssert = false;
    }
    Assert.assertTrue(validAssert);
}
Also used : Microservice(io.servicecomb.serviceregistry.api.registry.Microservice) ArrayList(java.util.ArrayList) MicroserviceInstance(io.servicecomb.serviceregistry.api.registry.MicroserviceInstance) InstanceCache(io.servicecomb.serviceregistry.cache.InstanceCache) MockUp(mockit.MockUp) Test(org.junit.Test)

Example 7 with Microservice

use of io.servicecomb.serviceregistry.api.registry.Microservice in project java-chassis by ServiceComb.

the class TestPropertiesLoader method testMicroservicePropertiesLoader.

@Test
public void testMicroservicePropertiesLoader() throws Exception {
    Method method = ReflectionUtils.findMethod(RegistryUtils.class, "createMicroserviceFromDefinition");
    ReflectionUtils.makeAccessible(method);
    Microservice microservice = (Microservice) method.invoke(null);
    Map<String, String> expectedMap = new HashMap<>();
    expectedMap.put("key1", "value1");
    expectedMap.put("key2", "value2");
    Assert.assertEquals(expectedMap, microservice.getProperties());
}
Also used : Microservice(io.servicecomb.serviceregistry.api.registry.Microservice) HashMap(java.util.HashMap) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 8 with Microservice

use of io.servicecomb.serviceregistry.api.registry.Microservice in project java-chassis by ServiceComb.

the class ServiceRegistryClientDemo method main.

public static void main(String[] args) throws Exception {
    Log4jUtils.init();
    BeanUtils.init();
    //Thread.sleep(2000);
    ServiceRegistryClient client = new ServiceRegistryClientImpl();
    client.init();
    // 新增服务
    List<String> list = new ArrayList<>();
    list.add("xxxxxxx");
    Microservice service = new Microservice();
    service.setAppId(APP_NAME);
    service.setServiceName(SERVICE_NAME);
    service.setVersion(VERSION);
    service.setLevel("FRONT");
    service.setSchemas(list);
    String serviceId = client.registerMicroservice(service);
    LOG.info("create service {}", serviceId);
    List<Microservice> mss = client.getAllMicroservices();
    LOG.info("query all services {}", mss.size());
    // Watch
    client.watch(serviceId, changedEvent -> {
        if (changedEvent.succeeded()) {
            LOG.info("{} {}/{} changed", changedEvent.result().getAction(), changedEvent.result().getKey().getServiceName(), changedEvent.result().getKey().getVersion());
            for (String s : changedEvent.result().getInstance().getEndpoints()) {
                LOG.info("  -> {}", s);
            }
        } else {
            LOG.error("", changedEvent.cause());
        }
    }, open -> {
    }, close -> {
    });
    service = client.getMicroservice(serviceId);
    LOG.info("get service {}", service);
    serviceId = client.getMicroserviceId(service.getAppId(), service.getServiceName(), service.getVersion());
    LOG.info("get service id {}", serviceId);
    // 注册实例
    List<String> addresses = new ArrayList<>();
    addresses.add("grpc:127.0.0.1:8081");
    HealthCheck healthCheck = new HealthCheck();
    healthCheck.setMode(HealthCheckMode.HEARTBEAT);
    healthCheck.setInterval(10);
    healthCheck.setTimes(0);
    MicroserviceInstance instance = new MicroserviceInstance();
    instance.setServiceId(serviceId);
    instance.setHostName("TestHost");
    instance.setEndpoints(addresses);
    instance.setHealthCheck(healthCheck);
    String instanceId = client.registerMicroserviceInstance(instance);
    LOG.info("register service {} instance {}", serviceId, instanceId);
    List<MicroserviceInstance> microserviceInstances = client.getMicroserviceInstance(serviceId, serviceId);
    for (MicroserviceInstance microserviceInstance : microserviceInstances) {
        LOG.info(microserviceInstance.toString());
    }
    // 实例心跳
    HeartbeatResponse response = client.heartbeat(serviceId, instanceId);
    LOG.info("heartbeat {}", response);
    // 查询给定idl服务
    List<MicroserviceInstance> instances = client.findServiceInstance(serviceId, APP_NAME, SERVICE_NAME, "1.0+");
    for (MicroserviceInstance inst : instances) {
        LOG.info("{}", inst);
    }
    // 删除服务
    //client.deleteService(service.getProviderServiceId(), true);
    LOG.info("finish!!!");
}
Also used : HeartbeatResponse(io.servicecomb.serviceregistry.api.response.HeartbeatResponse) Microservice(io.servicecomb.serviceregistry.api.registry.Microservice) ArrayList(java.util.ArrayList) HealthCheck(io.servicecomb.serviceregistry.api.registry.HealthCheck) MicroserviceInstance(io.servicecomb.serviceregistry.api.registry.MicroserviceInstance) ServiceRegistryClientImpl(io.servicecomb.serviceregistry.client.http.ServiceRegistryClientImpl)

Example 9 with Microservice

use of io.servicecomb.serviceregistry.api.registry.Microservice in project java-chassis by ServiceComb.

the class TestIpPortManager method testCreateServiceRegistryCache.

@Test
public void testCreateServiceRegistryCache() {
    boolean validAssert = true;
    List<MicroserviceInstance> list = new ArrayList<MicroserviceInstance>();
    MicroserviceInstance e1 = new MicroserviceInstance();
    List<String> endpoints = new ArrayList<>();
    endpoints.add("rest://127.0.0.1:8080");
    e1.setEndpoints(endpoints);
    list.add(e1);
    new MockUp<RegistryUtils>() {

        @Mock
        public List<MicroserviceInstance> findServiceInstance(String appId, String serviceName, String versionRule) {
            return list;
        }
    };
    Microservice microservice = new Microservice();
    new MockUp<RegistryUtils>() {

        @Mock
        private Microservice createMicroserviceFromDefinition() {
            return microservice;
        }
    };
    try {
        manager.createServiceRegistryCache();
        Assert.assertNotNull(manager.get());
        Assert.assertNull(manager.next());
    } catch (Exception e) {
        validAssert = false;
    }
    Assert.assertTrue(validAssert);
}
Also used : Microservice(io.servicecomb.serviceregistry.api.registry.Microservice) ArrayList(java.util.ArrayList) MicroserviceInstance(io.servicecomb.serviceregistry.api.registry.MicroserviceInstance) MockUp(mockit.MockUp) Test(org.junit.Test)

Example 10 with Microservice

use of io.servicecomb.serviceregistry.api.registry.Microservice in project java-chassis by ServiceComb.

the class TestRegistry method testRegistryUtils.

@Test
public void testRegistryUtils() throws Exception {
    Microservice oInstance = RegistryUtils.getMicroservice();
    List<String> schemas = new ArrayList<>();
    schemas.add("testSchema");
    oInstance.setSchemas(schemas);
    oInstance.setServiceId("testServiceId");
    oInstance.setStatus(MicroserviceStatus.UNKNOWN.name());
    Map<String, String> properties = new HashMap<>();
    properties.put("proxy", "testPorxy");
    oInstance.setProperties(properties);
    Assert.assertEquals("default", oInstance.getServiceName());
    Assert.assertEquals("default", oInstance.getAppId());
    Assert.assertEquals("", oInstance.getDescription());
    Assert.assertEquals("FRONT", oInstance.getLevel());
    Assert.assertEquals("testPorxy", oInstance.getProperties().get("proxy"));
    Assert.assertEquals("testServiceId", oInstance.getServiceId());
    Assert.assertEquals("0.0.1", oInstance.getVersion());
    Assert.assertEquals(1, oInstance.getSchemas().size());
    Assert.assertEquals(MicroserviceStatus.UNKNOWN.name(), oInstance.getStatus());
    RegistryUtils.getMicroserviceInstance().setHostName("test");
    RegistryUtils.getMicroserviceInstance().setServiceId("testServiceID");
    RegistryUtils.getMicroserviceInstance().setInstanceId("testID");
    RegistryUtils.getMicroserviceInstance().setStage("testStage");
    List<String> endpoints = new ArrayList<>();
    endpoints.add("localhost");
    RegistryUtils.getMicroserviceInstance().setEndpoints(endpoints);
    RegistryUtils.getMicroserviceInstance().setStatus(MicroserviceInstanceStatus.STARTING);
    RegistryUtils.getMicroserviceInstance().setProperties(properties);
    HealthCheck oHealthCheck = new HealthCheck();
    oHealthCheck.setInterval(10);
    oHealthCheck.setPort(8080);
    oHealthCheck.setTimes(20);
    HealthCheckMode oHealthCheckMode = HealthCheckMode.PLATFORM;
    oHealthCheck.setMode(oHealthCheckMode);
    RegistryUtils.getMicroserviceInstance().setHealthCheck(oHealthCheck);
    Assert.assertEquals("test", RegistryUtils.getMicroserviceInstance().getHostName());
    Assert.assertEquals("testServiceID", RegistryUtils.getMicroserviceInstance().getServiceId());
    Assert.assertEquals("testID", RegistryUtils.getMicroserviceInstance().getInstanceId());
    Assert.assertEquals(endpoints, RegistryUtils.getMicroserviceInstance().getEndpoints());
    Assert.assertEquals(MicroserviceInstanceStatus.STARTING, RegistryUtils.getMicroserviceInstance().getStatus());
    Assert.assertEquals(10, RegistryUtils.getMicroserviceInstance().getHealthCheck().getInterval());
    Assert.assertEquals(8080, RegistryUtils.getMicroserviceInstance().getHealthCheck().getPort());
    Assert.assertEquals(20, RegistryUtils.getMicroserviceInstance().getHealthCheck().getTimes());
    Assert.assertEquals("pull", RegistryUtils.getMicroserviceInstance().getHealthCheck().getMode().getName());
    Assert.assertEquals(0, RegistryUtils.getMicroserviceInstance().getHealthCheck().getTTL());
    RegistryUtils.getMicroserviceInstance().getHealthCheck().setMode(HealthCheckMode.HEARTBEAT);
    Assert.assertNotEquals(0, RegistryUtils.getMicroserviceInstance().getHealthCheck().getTTL());
    Assert.assertEquals("testPorxy", RegistryUtils.getMicroserviceInstance().getProperties().get("proxy"));
    Assert.assertEquals("testStage", RegistryUtils.getMicroserviceInstance().getStage());
}
Also used : Microservice(io.servicecomb.serviceregistry.api.registry.Microservice) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HealthCheck(io.servicecomb.serviceregistry.api.registry.HealthCheck) HealthCheckMode(io.servicecomb.serviceregistry.api.registry.HealthCheckMode) Test(org.junit.Test)

Aggregations

Microservice (io.servicecomb.serviceregistry.api.registry.Microservice)22 MockUp (mockit.MockUp)10 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)5 MicroserviceInstance (io.servicecomb.serviceregistry.api.registry.MicroserviceInstance)4 BeforeClass (org.junit.BeforeClass)4 RegistryThread (io.servicecomb.serviceregistry.RegistryThread)2 HealthCheck (io.servicecomb.serviceregistry.api.registry.HealthCheck)2 ClientException (io.servicecomb.serviceregistry.client.ClientException)2 ServiceRegistryClient (io.servicecomb.serviceregistry.client.ServiceRegistryClient)2 HashMap (java.util.HashMap)2 CseContext (io.servicecomb.core.CseContext)1 Transport (io.servicecomb.core.Transport)1 MicroserviceMeta (io.servicecomb.core.definition.MicroserviceMeta)1 MicroserviceMetaManager (io.servicecomb.core.definition.MicroserviceMetaManager)1 SchemaMeta (io.servicecomb.core.definition.SchemaMeta)1 SchemaListenerManager (io.servicecomb.core.definition.loader.SchemaListenerManager)1 SchemaLoader (io.servicecomb.core.definition.loader.SchemaLoader)1 UnitTestMeta (io.servicecomb.core.unittest.UnitTestMeta)1 IpPort (io.servicecomb.foundation.common.net.IpPort)1