Search in sources :

Example 6 with InstanceHeartbeatRequest

use of com.tencent.polaris.api.rpc.InstanceHeartbeatRequest in project polaris-java by polarismesh.

the class Provider method heartbeat.

// do the instance heartbeat
private static void heartbeat(String namespace, String service, String host, int port, ProviderAPI providerAPI) {
    // do heartbeat
    InstanceHeartbeatRequest heartbeatRequest = new InstanceHeartbeatRequest();
    heartbeatRequest.setNamespace(namespace);
    heartbeatRequest.setService(service);
    heartbeatRequest.setHost(host);
    heartbeatRequest.setPort(port);
    providerAPI.heartbeat(heartbeatRequest);
    System.out.printf("heartbeat instance, address is %s:%d%n", host, port);
}
Also used : InstanceHeartbeatRequest(com.tencent.polaris.api.rpc.InstanceHeartbeatRequest)

Example 7 with InstanceHeartbeatRequest

use of com.tencent.polaris.api.rpc.InstanceHeartbeatRequest in project sofa-rpc by sofastack.

the class PolarisRegistry method heartbeatPolaris.

private void heartbeatPolaris(InstanceRegisterRequest service) {
    try {
        InstanceHeartbeatRequest instanceHeartbeatRequest = new InstanceHeartbeatRequest();
        instanceHeartbeatRequest.setNamespace(service.getNamespace());
        instanceHeartbeatRequest.setService(service.getService());
        instanceHeartbeatRequest.setHost(service.getHost());
        instanceHeartbeatRequest.setPort(service.getPort());
        providerAPI.heartbeat(instanceHeartbeatRequest);
    } catch (Exception e) {
        LOGGER.error(LogCodes.getLog(LogCodes.ERROR_CHECK_PASS, "Polaris"), e);
    }
}
Also used : SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) InstanceHeartbeatRequest(com.tencent.polaris.api.rpc.InstanceHeartbeatRequest)

Example 8 with InstanceHeartbeatRequest

use of com.tencent.polaris.api.rpc.InstanceHeartbeatRequest in project polaris-java-agent by polarismesh.

the class PolarisServiceRegistry method register.

/**
 * 服务注册
 *
 * @param registration
 */
@Override
public void register(Registration registration) {
    if (StringUtils.isEmpty(polarisRegistration.getServiceId())) {
        log.warn("No service to register for polaris client...");
        return;
    }
    // 注册实例
    InstanceRegisterRequest instanceRegisterRequest = new InstanceRegisterRequest();
    instanceRegisterRequest.setNamespace(polarisProperties.getNamespace());
    instanceRegisterRequest.setService(polarisRegistration.getServiceId());
    instanceRegisterRequest.setHost(polarisRegistration.getHost());
    instanceRegisterRequest.setPort(polarisRegistration.getPort());
    instanceRegisterRequest.setProtocol(polarisProperties.getProtocol());
    if (null != heartbeatExecutor) {
        instanceRegisterRequest.setTtl(ttl);
    }
    try {
        ProviderAPI providerClient = PolarisAPIFactory.getProviderApi();
        providerClient.register(instanceRegisterRequest);
        log.info("Polaris register success with address host:{}, port:{}", polarisRegistration.getHost(), polarisRegistration.getPort());
        if (null != heartbeatExecutor) {
            InstanceHeartbeatRequest heartbeatRequest = new InstanceHeartbeatRequest();
            BeanUtils.copyProperties(instanceRegisterRequest, heartbeatRequest);
            // 添加反注册Hook
            // 注册成功后开始启动心跳线程
            heartbeat();
        }
    } catch (Exception e) {
        log.error("polaris registry, {} register failed...{},", polarisRegistration.getServiceId(), polarisRegistration, e);
        rethrowRuntimeException(e);
    }
}
Also used : ProviderAPI(com.tencent.polaris.api.core.ProviderAPI) InstanceRegisterRequest(com.tencent.polaris.api.rpc.InstanceRegisterRequest) ReflectionUtils.rethrowRuntimeException(org.springframework.util.ReflectionUtils.rethrowRuntimeException) PolarisException(com.tencent.polaris.api.exception.PolarisException) InstanceHeartbeatRequest(com.tencent.polaris.api.rpc.InstanceHeartbeatRequest)

Example 9 with InstanceHeartbeatRequest

use of com.tencent.polaris.api.rpc.InstanceHeartbeatRequest in project polaris-java-agent by polarismesh.

the class PolarisServiceRegistry method heartbeat.

private void heartbeat() {
    String host = polarisProperties.getHost();
    Integer port = polarisProperties.getPort();
    InstanceHeartbeatRequest heartbeatRequest = new InstanceHeartbeatRequest();
    heartbeatRequest.setNamespace(polarisProperties.getNamespace());
    heartbeatRequest.setService(polarisProperties.getService());
    heartbeatRequest.setHost(host);
    heartbeatRequest.setPort(port);
    try {
        heartbeatExecutor.scheduleWithFixedDelay(() -> {
            PolarisAPIFactory.getProviderApi().heartbeat(heartbeatRequest);
            log.info("heartbeat instance, address is {}:{}", host, port);
        }, ttl, ttl, TimeUnit.SECONDS);
    } catch (PolarisException e) {
        log.error(e.getMessage());
    }
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) InstanceHeartbeatRequest(com.tencent.polaris.api.rpc.InstanceHeartbeatRequest)

Aggregations

InstanceHeartbeatRequest (com.tencent.polaris.api.rpc.InstanceHeartbeatRequest)9 ProviderAPI (com.tencent.polaris.api.core.ProviderAPI)5 PolarisException (com.tencent.polaris.api.exception.PolarisException)3 InstanceRegisterRequest (com.tencent.polaris.api.rpc.InstanceRegisterRequest)3 ReflectionUtils.rethrowRuntimeException (org.springframework.util.ReflectionUtils.rethrowRuntimeException)2 SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)1 Configuration (com.tencent.polaris.api.config.Configuration)1 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)1 InstanceDeregisterRequest (com.tencent.polaris.api.rpc.InstanceDeregisterRequest)1 InstanceRegisterResponse (com.tencent.polaris.api.rpc.InstanceRegisterResponse)1 InitResult (com.tencent.polaris.discovery.example.utils.ExampleUtils.InitResult)1 Test (org.junit.Test)1