Search in sources :

Example 1 with HealthCheck

use of io.servicecomb.serviceregistry.api.registry.HealthCheck 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 2 with HealthCheck

use of io.servicecomb.serviceregistry.api.registry.HealthCheck 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)

Example 3 with HealthCheck

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

the class RegistryUtils method createMicroserviceInstance.

private static MicroserviceInstance createMicroserviceInstance() {
    MicroserviceInstance microserviceInstance = new MicroserviceInstance();
    microserviceInstance.setStage(DEFAULT_STAGE);
    Map<String, String> propertiesMap = InstancePropertiesLoader.INSTANCE.loadProperties();
    if (!isEmpty(propertiesMap)) {
        microserviceInstance.setProperties(propertiesMap);
    }
    // healthcheck
    if (RegistryClientFactory.getLocalModeFile().isEmpty()) {
        HealthCheck healthCheck = new HealthCheck();
        healthCheck.setMode(HealthCheckMode.HEARTBEAT);
        microserviceInstance.setHealthCheck(healthCheck);
    }
    return microserviceInstance;
}
Also used : HealthCheck(io.servicecomb.serviceregistry.api.registry.HealthCheck) MicroserviceInstance(io.servicecomb.serviceregistry.api.registry.MicroserviceInstance)

Aggregations

HealthCheck (io.servicecomb.serviceregistry.api.registry.HealthCheck)3 Microservice (io.servicecomb.serviceregistry.api.registry.Microservice)2 MicroserviceInstance (io.servicecomb.serviceregistry.api.registry.MicroserviceInstance)2 ArrayList (java.util.ArrayList)2 HealthCheckMode (io.servicecomb.serviceregistry.api.registry.HealthCheckMode)1 HeartbeatResponse (io.servicecomb.serviceregistry.api.response.HeartbeatResponse)1 ServiceRegistryClientImpl (io.servicecomb.serviceregistry.client.http.ServiceRegistryClientImpl)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1