Search in sources :

Example 1 with SDKContext

use of com.tencent.polaris.client.api.SDKContext 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 SDKContext

use of com.tencent.polaris.client.api.SDKContext in project polaris-java by polarismesh.

the class APIFactoryTest method testInitContextByConfig.

@Test
public void testInitContextByConfig() {
    SDKContext sdkContext = null;
    try {
        sdkContext = APIFactory.initContextByConfig(TestUtils.createSimpleConfiguration(8888));
        Supplier plugins = sdkContext.getPlugins();
        Plugin plugin = plugins.getPlugin(PluginTypes.LOAD_BALANCER.getBaseType(), LoadBalanceConfig.LOAD_BALANCE_WEIGHTED_RANDOM);
        Assert.assertNotNull(plugin);
    } catch (PolarisException e) {
        Assert.fail(e.getMessage());
    } finally {
        if (null != sdkContext) {
            sdkContext.destroy();
        }
    }
}
Also used : SDKContext(com.tencent.polaris.client.api.SDKContext) PolarisException(com.tencent.polaris.api.exception.PolarisException) Supplier(com.tencent.polaris.api.plugin.Supplier) Plugin(com.tencent.polaris.api.plugin.Plugin) Test(org.junit.Test)

Example 3 with SDKContext

use of com.tencent.polaris.client.api.SDKContext in project polaris-java by polarismesh.

the class APIFactoryTest method testInitContextByFile.

@Test
public void testInitContextByFile() {
    InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream("conf/default.yaml");
    SDKContext sdkContext = null;
    try {
        sdkContext = APIFactory.initContextByFile(resourceAsStream);
        Supplier plugins = sdkContext.getPlugins();
        Plugin plugin = plugins.getPlugin(PluginTypes.CIRCUIT_BREAKER.getBaseType(), DefaultPlugins.CIRCUIT_BREAKER_ERROR_COUNT);
        Assert.assertNotNull(plugin);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        if (null != sdkContext) {
            sdkContext.destroy();
        }
    }
}
Also used : SDKContext(com.tencent.polaris.client.api.SDKContext) InputStream(java.io.InputStream) Supplier(com.tencent.polaris.api.plugin.Supplier) PolarisException(com.tencent.polaris.api.exception.PolarisException) Plugin(com.tencent.polaris.api.plugin.Plugin) Test(org.junit.Test)

Example 4 with SDKContext

use of com.tencent.polaris.client.api.SDKContext in project spring-cloud-tencent by Tencent.

the class PolarisContextConfigurationTest method testProperties.

@Test
public void testProperties() {
    contextRunner.run(context -> {
        final SDKContext sdkContext = context.getBean(SDKContext.class);
        Assert.assertNotNull(sdkContext);
    });
}
Also used : SDKContext(com.tencent.polaris.client.api.SDKContext) Test(org.junit.Test)

Example 5 with SDKContext

use of com.tencent.polaris.client.api.SDKContext in project spring-cloud-tencent by Tencent.

the class PolarisRegistrationTest method setUp.

@Before
public void setUp() {
    // mock DiscoveryPropertiesAutoConfiguration
    DiscoveryPropertiesAutoConfiguration discoveryPropertiesAutoConfiguration = mock(DiscoveryPropertiesAutoConfiguration.class);
    doReturn(true).when(discoveryPropertiesAutoConfiguration).isRegisterEnabled();
    // mock PolarisDiscoveryProperties
    PolarisDiscoveryProperties polarisDiscoveryProperties = mock(PolarisDiscoveryProperties.class);
    doReturn(SERVICE_PROVIDER).when(polarisDiscoveryProperties).getService();
    doReturn(PORT).when(polarisDiscoveryProperties).getPort();
    doReturn("http").when(polarisDiscoveryProperties).getProtocol();
    // mock SDKContext
    APIConfig apiConfig = mock(APIConfig.class);
    doReturn(HOST).when(apiConfig).getBindIP();
    GlobalConfig globalConfig = mock(GlobalConfig.class);
    doReturn(apiConfig).when(globalConfig).getAPI();
    Configuration configuration = mock(Configuration.class);
    doReturn(globalConfig).when(configuration).getGlobal();
    SDKContext polarisContext = mock(SDKContext.class);
    doReturn(configuration).when(polarisContext).getConfig();
    // mock StaticMetadataManager
    StaticMetadataManager staticMetadataManager = mock(StaticMetadataManager.class);
    doReturn(Collections.singletonMap("key1", "value1")).when(staticMetadataManager).getMergedStaticMetadata();
    doReturn(Collections.singletonMap("key2", "value2")).when(staticMetadataManager).getLocationMetadata();
    polarisRegistration = new PolarisRegistration(discoveryPropertiesAutoConfiguration, polarisDiscoveryProperties, polarisContext, staticMetadataManager);
}
Also used : SDKContext(com.tencent.polaris.client.api.SDKContext) DiscoveryPropertiesAutoConfiguration(com.tencent.cloud.polaris.DiscoveryPropertiesAutoConfiguration) Configuration(com.tencent.polaris.api.config.Configuration) PolarisDiscoveryProperties(com.tencent.cloud.polaris.PolarisDiscoveryProperties) APIConfig(com.tencent.polaris.api.config.global.APIConfig) GlobalConfig(com.tencent.polaris.api.config.global.GlobalConfig) StaticMetadataManager(com.tencent.cloud.common.metadata.StaticMetadataManager) DiscoveryPropertiesAutoConfiguration(com.tencent.cloud.polaris.DiscoveryPropertiesAutoConfiguration) Before(org.junit.Before)

Aggregations

SDKContext (com.tencent.polaris.client.api.SDKContext)10 Test (org.junit.Test)6 ConsumerAPI (com.tencent.polaris.api.core.ConsumerAPI)2 ErrorCode (com.tencent.polaris.api.exception.ErrorCode)2 PolarisException (com.tencent.polaris.api.exception.PolarisException)2 RetriableException (com.tencent.polaris.api.exception.RetriableException)2 ServerCodes (com.tencent.polaris.api.exception.ServerCodes)2 Plugin (com.tencent.polaris.api.plugin.Plugin)2 Supplier (com.tencent.polaris.api.plugin.Supplier)2 ConfigFile (com.tencent.polaris.api.plugin.configuration.ConfigFile)2 ConfigFileConnector (com.tencent.polaris.api.plugin.configuration.ConfigFileConnector)2 ConfigFileResponse (com.tencent.polaris.api.plugin.configuration.ConfigFileResponse)2 ConfigFileMetadata (com.tencent.polaris.configuration.api.core.ConfigFileMetadata)2 ConfigFileTestUtils (com.tencent.polaris.configuration.client.ConfigFileTestUtils)2 TimeUnit (java.util.concurrent.TimeUnit)2 RunWith (org.junit.runner.RunWith)2 ArgumentMatchers.anyList (org.mockito.ArgumentMatchers.anyList)2 ArgumentMatchers.anyLong (org.mockito.ArgumentMatchers.anyLong)2 Mock (org.mockito.Mock)2 Mockito.mock (org.mockito.Mockito.mock)2