Search in sources :

Example 11 with ProviderAPI

use of com.tencent.polaris.api.core.ProviderAPI 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 12 with ProviderAPI

use of com.tencent.polaris.api.core.ProviderAPI in project polaris-java-agent by polarismesh.

the class PolarisServiceRegistry method deregister.

/**
 * 服务反注册
 *
 * @param registration
 */
@Override
public void deregister(Registration registration) {
    log.info("De-registering from Polaris Server now...");
    if (StringUtils.isEmpty(polarisRegistration.getServiceId())) {
        log.warn("No dom to de-register for polaris client...");
        return;
    }
    InstanceDeregisterRequest deRegisterRequest = new InstanceDeregisterRequest();
    deRegisterRequest.setNamespace(polarisProperties.getNamespace());
    deRegisterRequest.setService(polarisRegistration.getServiceId());
    deRegisterRequest.setHost(polarisRegistration.getHost());
    deRegisterRequest.setPort(polarisRegistration.getPort());
    try {
        ProviderAPI providerClient = PolarisAPIFactory.getProviderApi();
        providerClient.deRegister(deRegisterRequest);
    } catch (Exception e) {
        log.error("ERR_POLARIS_DEREGISTER, de-register failed...{},", polarisRegistration, e);
    } finally {
        if (null != heartbeatExecutor) {
            heartbeatExecutor.shutdown();
        }
    }
    log.info("De-registration finished.");
}
Also used : ProviderAPI(com.tencent.polaris.api.core.ProviderAPI) ReflectionUtils.rethrowRuntimeException(org.springframework.util.ReflectionUtils.rethrowRuntimeException) PolarisException(com.tencent.polaris.api.exception.PolarisException) InstanceDeregisterRequest(com.tencent.polaris.api.rpc.InstanceDeregisterRequest)

Aggregations

ProviderAPI (com.tencent.polaris.api.core.ProviderAPI)12 PolarisException (com.tencent.polaris.api.exception.PolarisException)5 InstanceHeartbeatRequest (com.tencent.polaris.api.rpc.InstanceHeartbeatRequest)5 InstanceDeregisterRequest (com.tencent.polaris.api.rpc.InstanceDeregisterRequest)4 InstanceRegisterRequest (com.tencent.polaris.api.rpc.InstanceRegisterRequest)4 ReflectionUtils.rethrowRuntimeException (org.springframework.util.ReflectionUtils.rethrowRuntimeException)4 InitResult (com.tencent.polaris.discovery.example.utils.ExampleUtils.InitResult)3 Configuration (com.tencent.polaris.api.config.Configuration)2 InstanceRegisterResponse (com.tencent.polaris.api.rpc.InstanceRegisterResponse)2 Test (org.junit.Test)2 HttpServer (com.sun.net.httpserver.HttpServer)1 ConsumerAPI (com.tencent.polaris.api.core.ConsumerAPI)1 Instance (com.tencent.polaris.api.pojo.Instance)1 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)1 ServiceCallResult (com.tencent.polaris.api.rpc.ServiceCallResult)1 InitResult (com.tencent.polaris.circuitbreak.example.CircuitBreakExampleUtils.InitResult)1 SDKContext (com.tencent.polaris.client.api.SDKContext)1 InputStream (java.io.InputStream)1 InetSocketAddress (java.net.InetSocketAddress)1