use of io.servicecomb.serviceregistry.api.response.HeartbeatResponse 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!!!");
}
use of io.servicecomb.serviceregistry.api.response.HeartbeatResponse in project java-chassis by ServiceComb.
the class TestRegistry method testRegistryUtilsWithStub.
@Test
public void testRegistryUtilsWithStub(@Mocked final ServiceRegistryClientImpl oMockServiceRegistryClient) throws Exception {
HeartbeatResponse response = new HeartbeatResponse();
response.setOk(true);
response.setMessage("OK");
new Expectations() {
{
oMockServiceRegistryClient.init();
oMockServiceRegistryClient.registerMicroservice((Microservice) any);
result = "sampleServiceID";
oMockServiceRegistryClient.registerMicroserviceInstance((MicroserviceInstance) any);
result = "sampleInstanceID";
oMockServiceRegistryClient.unregisterMicroserviceInstance(anyString, anyString);
result = true;
}
};
RegistryUtils.setSrClient(oMockServiceRegistryClient);
RegistryUtils.init();
Assert.assertEquals(true, RegistryUtils.unregsiterInstance());
}
use of io.servicecomb.serviceregistry.api.response.HeartbeatResponse in project java-chassis by ServiceComb.
the class TestRegistry method testRegistryUtilsWithStubHeartbeatFailure.
@Test
public void testRegistryUtilsWithStubHeartbeatFailure(@Mocked final ServiceRegistryClientImpl oMockServiceRegistryClient) throws Exception {
final HeartbeatResponse response = new HeartbeatResponse();
response.setOk(false);
response.setMessage("FAIL");
new Expectations() {
{
oMockServiceRegistryClient.init();
oMockServiceRegistryClient.registerMicroservice((Microservice) any);
result = "sampleServiceID";
oMockServiceRegistryClient.registerMicroserviceInstance((MicroserviceInstance) any);
result = "sampleInstanceID";
oMockServiceRegistryClient.heartbeat(anyString, anyString);
result = response;
oMockServiceRegistryClient.unregisterMicroserviceInstance(anyString, anyString);
result = false;
}
};
RegistryUtils.setSrClient(oMockServiceRegistryClient);
RegistryUtils.init();
Assert.assertEquals(false, RegistryUtils.heartbeat().isOk());
Assert.assertEquals(false, RegistryUtils.unregsiterInstance());
}
use of io.servicecomb.serviceregistry.api.response.HeartbeatResponse in project java-chassis by ServiceComb.
the class RegistryThread method run.
@Override
public void run() {
// 本进程微服务及实例注册
this.doInit();
// 心跳
int interval = ServiceRegistryConfig.INSTANCE.getHeartbeatInterval();
while (isRunning()) {
if (!registerSuccess) {
try {
RegistryUtils.ensureRegisterMicroservice();
} catch (TimerException e) {
continue;
}
registerSuccess = RegistryUtils.regsiterInstance();
}
if (registerSuccess) {
RegistryUtils.watch();
}
try {
TimeUnit.SECONDS.sleep(interval);
} catch (InterruptedException e) {
continue;
}
// 定时心跳
HeartbeatResponse response = RegistryUtils.heartbeat();
if (response != null && !response.isOk()) {
// 心跳返回实例不存在,需要重新注册
registerSuccess = false;
}
}
}
use of io.servicecomb.serviceregistry.api.response.HeartbeatResponse in project java-chassis by ServiceComb.
the class RegistryUtils method heartbeat.
public static HeartbeatResponse heartbeat() {
Timer timer = Timer.newForeverTimer();
HeartbeatResponse response;
while (true) {
response = srClient.heartbeat(getMicroservice().getServiceId(), getMicroserviceInstance().getInstanceId());
if (response != null) {
break;
}
if (!needToWatch()) {
exception(new ClientException("could not connect to service center"));
}
try {
LOGGER.warn("Update heartbeat to service center failed, retry after {}s. service={}/{}", timer.getNextTimeout(), getMicroservice().getServiceId(), getMicroserviceInstance().getInstanceId());
timer.sleep();
} catch (TimerException e) {
LOGGER.error("Update heartbeat to service center failed, can not connect to service center. service={}/{}", getMicroservice().getServiceId(), getMicroserviceInstance().getInstanceId());
return null;
}
}
if (!response.isOk()) {
LOGGER.error("Update heartbeat to service center failed, microservice instance={}/{} does not exist", getMicroservice().getServiceId(), getMicroserviceInstance().getInstanceId());
}
return response;
}
Aggregations