Search in sources :

Example 1 with InstanceHeartbeatRequest

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);
        }
    }
}
Also used : ProviderAPI(com.tencent.polaris.api.core.ProviderAPI) Configuration(com.tencent.polaris.api.config.Configuration) InstanceRegisterRequest(com.tencent.polaris.api.rpc.InstanceRegisterRequest) InstanceRegisterResponse(com.tencent.polaris.api.rpc.InstanceRegisterResponse) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) InstanceHeartbeatRequest(com.tencent.polaris.api.rpc.InstanceHeartbeatRequest) InstanceDeregisterRequest(com.tencent.polaris.api.rpc.InstanceDeregisterRequest) Test(org.junit.Test)

Example 2 with InstanceHeartbeatRequest

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;
}
Also used : ProviderAPI(com.tencent.polaris.api.core.ProviderAPI) InstanceHeartbeatRequest(com.tencent.polaris.api.rpc.InstanceHeartbeatRequest)

Example 3 with InstanceHeartbeatRequest

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);
}
Also used : InstanceHeartbeatRequest(com.tencent.polaris.api.rpc.InstanceHeartbeatRequest)

Example 4 with 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);
    }
}
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 5 with InstanceHeartbeatRequest

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);
    }
}
Also used : ProviderAPI(com.tencent.polaris.api.core.ProviderAPI) InitResult(com.tencent.polaris.discovery.example.utils.ExampleUtils.InitResult) 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