Search in sources :

Example 1 with ServiceRegistryClientImpl

use of io.servicecomb.serviceregistry.client.http.ServiceRegistryClientImpl 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 ServiceRegistryClientImpl

use of io.servicecomb.serviceregistry.client.http.ServiceRegistryClientImpl in project java-chassis by ServiceComb.

the class RegistryClientFactory method getRegistryClient.

public static ServiceRegistryClient getRegistryClient() {
    if (registryClient != null) {
        return registryClient;
    }
    synchronized (LOCK) {
        if (registryClient != null) {
            return registryClient;
        }
        ServiceRegistryClient client = null;
        if (localModeFile.isEmpty()) {
            LOGGER.info("It is running in the normal mode, a separated service registry is required");
            client = new ServiceRegistryClientImpl();
        } else {
            LOGGER.info("It is running in the local development mode, the local file {} is using as the local registry", localModeFile);
            client = new LocalServiceRegistryClientImpl();
        }
        try {
            client.init();
        } catch (Exception e) {
            LOGGER.error("init registry client failed.", e);
            return null;
        }
        registryClient = client;
        return registryClient;
    }
}
Also used : ServiceRegistryClientImpl(io.servicecomb.serviceregistry.client.http.ServiceRegistryClientImpl)

Aggregations

ServiceRegistryClientImpl (io.servicecomb.serviceregistry.client.http.ServiceRegistryClientImpl)2 HealthCheck (io.servicecomb.serviceregistry.api.registry.HealthCheck)1 Microservice (io.servicecomb.serviceregistry.api.registry.Microservice)1 MicroserviceInstance (io.servicecomb.serviceregistry.api.registry.MicroserviceInstance)1 HeartbeatResponse (io.servicecomb.serviceregistry.api.response.HeartbeatResponse)1 ArrayList (java.util.ArrayList)1