use of com.tencent.polaris.api.rpc.InstanceHeartbeatRequest in project polaris-java by polarismesh.
the class ProviderTest method testRoundTrip.
@Test
public void testRoundTrip() {
Configuration configuration = TestUtils.configWithEnvAddress();
try (ProviderAPI providerAPI = DiscoveryAPIFactory.createProviderAPIByConfig(configuration)) {
for (int i = 0; i < 5; i++) {
namingServer.getNamingService().addService(new ServiceKey(NAMESPACE_TEST, SERVICE_PROVIDER));
// 注册
InstanceRegisterRequest registerRequest = new InstanceRegisterRequest();
registerRequest.setNamespace(NAMESPACE_TEST);
registerRequest.setService(SERVICE_PROVIDER);
registerRequest.setHost(Consts.HOST);
registerRequest.setPort(Consts.PORT);
registerRequest.setProtocol("http");
registerRequest.setToken(Consts.PROVIDER_TOKEN);
registerRequest.setTtl(5);
InstanceRegisterResponse response = providerAPI.register(registerRequest);
Assert.assertTrue(StringUtils.isNotBlank(response.getInstanceId()));
Assert.assertFalse(response.isExists());
// 再注册
response = providerAPI.register(registerRequest);
Assert.assertTrue(StringUtils.isNotBlank(response.getInstanceId()));
Assert.assertTrue(response.isExists());
Utils.sleepUninterrupted(5000);
// 心跳上报
InstanceHeartbeatRequest heartbeatRequest = new InstanceHeartbeatRequest();
heartbeatRequest.setNamespace(NAMESPACE_TEST);
heartbeatRequest.setService(SERVICE_PROVIDER);
heartbeatRequest.setHost(Consts.HOST);
heartbeatRequest.setPort(Consts.PORT);
heartbeatRequest.setToken(Consts.PROVIDER_TOKEN);
providerAPI.heartbeat(heartbeatRequest);
// 反注册
InstanceDeregisterRequest deregisterRequest = new InstanceDeregisterRequest();
deregisterRequest.setNamespace(NAMESPACE_TEST);
deregisterRequest.setService(SERVICE_PROVIDER);
deregisterRequest.setHost(Consts.HOST);
deregisterRequest.setPort(Consts.PORT);
deregisterRequest.setToken(Consts.PROVIDER_TOKEN);
providerAPI.deRegister(deregisterRequest);
}
}
}
use of com.tencent.polaris.api.rpc.InstanceHeartbeatRequest in project polaris-java by polarismesh.
the class APIFacade method heartbeat.
public static boolean heartbeat(String namespace, String service, String host, int port, String token) {
if (!inited.get()) {
LOGGER.info("polaris not inited, heartbeat fail");
return false;
}
InstanceHeartbeatRequest heartbeatRequest = new InstanceHeartbeatRequest();
heartbeatRequest.setNamespace(namespace);
heartbeatRequest.setService(service);
heartbeatRequest.setHost(host);
heartbeatRequest.setPort(port);
heartbeatRequest.setToken(token);
((ProviderAPI) providerAPI).heartbeat(heartbeatRequest);
LOGGER.info("heartbeat instance, address is {}:{}", host, port);
return true;
}
use of com.tencent.polaris.api.rpc.InstanceHeartbeatRequest in project polaris-java-agent by polarismesh.
the class PolarisOperator method heartbeat.
private void heartbeat(String service, String host, int port) {
InstanceHeartbeatRequest instanceHeartbeatRequest = new InstanceHeartbeatRequest();
instanceHeartbeatRequest.setNamespace(polarisConfig.getNamespace());
instanceHeartbeatRequest.setService(service);
instanceHeartbeatRequest.setHost(host);
instanceHeartbeatRequest.setPort(port);
instanceHeartbeatRequest.setToken(polarisConfig.getToken());
providerAPI.heartbeat(instanceHeartbeatRequest);
}
use of com.tencent.polaris.api.rpc.InstanceHeartbeatRequest in project spring-cloud-tencent by Tencent.
the class PolarisServiceRegistry method register.
@Override
public void register(Registration registration) {
if (StringUtils.isEmpty(registration.getServiceId())) {
log.warn("No service to register for polaris client...");
return;
}
// Register instance.
InstanceRegisterRequest instanceRegisterRequest = new InstanceRegisterRequest();
instanceRegisterRequest.setNamespace(polarisDiscoveryProperties.getNamespace());
instanceRegisterRequest.setService(registration.getServiceId());
instanceRegisterRequest.setHost(registration.getHost());
instanceRegisterRequest.setPort(registration.getPort());
instanceRegisterRequest.setWeight(polarisDiscoveryProperties.getWeight());
instanceRegisterRequest.setToken(polarisDiscoveryProperties.getToken());
instanceRegisterRequest.setRegion(staticMetadataManager.getRegion());
instanceRegisterRequest.setZone(staticMetadataManager.getZone());
instanceRegisterRequest.setCampus(staticMetadataManager.getCampus());
if (null != heartbeatExecutor) {
instanceRegisterRequest.setTtl(ttl);
}
instanceRegisterRequest.setMetadata(registration.getMetadata());
instanceRegisterRequest.setProtocol(polarisDiscoveryProperties.getProtocol());
instanceRegisterRequest.setVersion(polarisDiscoveryProperties.getVersion());
try {
ProviderAPI providerClient = polarisDiscoveryHandler.getProviderAPI();
providerClient.register(instanceRegisterRequest);
log.info("polaris registry, {} {} {}:{} {} register finished", polarisDiscoveryProperties.getNamespace(), registration.getServiceId(), registration.getHost(), registration.getPort(), staticMetadataManager.getMergedStaticMetadata());
if (null != heartbeatExecutor) {
InstanceHeartbeatRequest heartbeatRequest = new InstanceHeartbeatRequest();
BeanUtils.copyProperties(instanceRegisterRequest, heartbeatRequest);
// Start the heartbeat thread after the registration is successful.
heartbeat(heartbeatRequest);
}
} catch (Exception e) {
log.error("polaris registry, {} register failed...{},", registration.getServiceId(), registration, e);
rethrowRuntimeException(e);
}
}
use of com.tencent.polaris.api.rpc.InstanceHeartbeatRequest in project polaris-java by polarismesh.
the class HeartbeatExample method main.
public static void main(String[] args) throws Exception {
InitResult initResult = ExampleUtils.initProviderConfiguration(args);
String namespace = initResult.getNamespace();
String service = initResult.getService();
String host = initResult.getHost();
int port = initResult.getPort();
String token = initResult.getToken();
try (ProviderAPI providerAPI = ExampleUtils.createProviderAPI(initResult.getConfig())) {
InstanceHeartbeatRequest instanceHeartbeatRequest = new InstanceHeartbeatRequest();
instanceHeartbeatRequest.setNamespace(namespace);
instanceHeartbeatRequest.setService(service);
instanceHeartbeatRequest.setHost(host);
instanceHeartbeatRequest.setPort(port);
instanceHeartbeatRequest.setToken(token);
providerAPI.heartbeat(instanceHeartbeatRequest);
System.out.println("heartbeat for service successfully: " + service);
}
}
Aggregations