Search in sources :

Example 1 with ProviderAPI

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

the class CircuitBreakExample method main.

public static void main(String[] args) throws Throwable {
    InitResult initResult = CircuitBreakExampleUtils.initConfiguration(args);
    // 由于需要用到多个API对象,因此可以使用SDKContext使得多个API对象的资源都可以共享
    try (SDKContext sdkContext = SDKContext.initContext()) {
        ProviderAPI providerAPI = DiscoveryAPIFactory.createProviderAPIByContext(sdkContext);
        // 1. 先注册服务实例
        registerInstances(providerAPI, initResult);
        // 2. 获取可用的服务实例列表
        ConsumerAPI consumerAPI = DiscoveryAPIFactory.createConsumerAPIByContext(sdkContext);
        Instance[] availableInstances = getAvailableInstances(consumerAPI, initResult);
        System.out.println("available instances count is " + availableInstances.length);
        // 3. 针对某个服务实例上报调用结果失败,默认连续10次失败,或者1分钟错误率50%,就会对实例进行熔断
        Instance targetInstance = availableInstances[0];
        System.out.printf("target instance is %s:%d%n", targetInstance.getHost(), targetInstance.getPort());
        for (int i = 0; i < 120000; i++) {
            // 这里要进行服务调用,用户可以写自己的服务调用代码
            for (Instance instance : availableInstances) {
                ServiceCallResult serviceCallResult = new ServiceCallResult();
                serviceCallResult.setInstance(targetInstance);
                serviceCallResult.setMethod("echo");
                if (instance.getHost().equals(targetInstance.getHost()) && instance.getPort() == targetInstance.getPort()) {
                    serviceCallResult.setRetStatus(RetStatus.RetFail);
                    serviceCallResult.setDelay(2000);
                } else {
                    serviceCallResult.setRetStatus(RetStatus.RetSuccess);
                    serviceCallResult.setDelay(20);
                }
                consumerAPI.updateServiceCallResult(serviceCallResult);
            }
            System.out.printf("report call result %d%n", i);
            CircuitBreakExampleUtils.doSleep(500);
        }
        // 4. 判断服务实例是否还在可用列表中,已经熔断的实例是不会再次返回
        availableInstances = getAvailableInstances(consumerAPI, initResult);
        System.out.println("available instances count is " + availableInstances.length);
        for (Instance instance : availableInstances) {
            System.out.printf("available instance is %s:%d%n", instance.getHost(), instance.getPort());
        }
        // 4. 反注册服务实例
        System.out.println("start to deregister instances");
        deregisterInstances(providerAPI, initResult);
    }
}
Also used : SDKContext(com.tencent.polaris.client.api.SDKContext) ProviderAPI(com.tencent.polaris.api.core.ProviderAPI) InitResult(com.tencent.polaris.circuitbreak.example.CircuitBreakExampleUtils.InitResult) ServiceCallResult(com.tencent.polaris.api.rpc.ServiceCallResult) Instance(com.tencent.polaris.api.pojo.Instance) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI)

Example 2 with ProviderAPI

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

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

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

the class APIFactoryTest method testCreateProviderAPIByFile.

@Test
public void testCreateProviderAPIByFile() {
    // FIXME @thrteenwang update route config in default.yaml
    InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream("conf/default.yaml");
    ProviderAPI providerAPI = null;
    try {
        providerAPI = APIFactory.createProviderAPIByFile(resourceAsStream);
        Assert.assertNotNull(providerAPI);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        if (null != providerAPI) {
            providerAPI.destroy();
        }
    }
}
Also used : ProviderAPI(com.tencent.polaris.api.core.ProviderAPI) InputStream(java.io.InputStream) PolarisException(com.tencent.polaris.api.exception.PolarisException) Test(org.junit.Test)

Example 5 with ProviderAPI

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

the class DeregisterExample 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())) {
        InstanceDeregisterRequest instanceDeregisterRequest = new InstanceDeregisterRequest();
        instanceDeregisterRequest.setNamespace(namespace);
        instanceDeregisterRequest.setService(service);
        instanceDeregisterRequest.setHost(host);
        instanceDeregisterRequest.setPort(port);
        instanceDeregisterRequest.setToken(token);
        providerAPI.deRegister(instanceDeregisterRequest);
        System.out.println("deregister for service successfully: " + service);
    }
}
Also used : ProviderAPI(com.tencent.polaris.api.core.ProviderAPI) InitResult(com.tencent.polaris.discovery.example.utils.ExampleUtils.InitResult) 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