Search in sources :

Example 1 with APIConfig

use of com.tencent.polaris.api.config.global.APIConfig in project polaris-java by polarismesh.

the class BaseFlow method commonGetOneInstance.

/**
 * 通用获取单个服务实例的方法,用于SDK内部调用
 *
 * @param extensions 插件上下文
 * @param serviceKey 服务信息
 * @param coreRouterNames 核心路由插件链
 * @param lbPolicy 负载均衡策略
 * @param protocol 协议信息
 * @param hashKey 一致性hash的key
 * @return 过滤后的实例
 */
public static Instance commonGetOneInstance(Extensions extensions, ServiceKey serviceKey, List<String> coreRouterNames, String lbPolicy, String protocol, String hashKey) {
    ServiceEventKey svcEventKey = new ServiceEventKey(serviceKey, EventType.INSTANCE);
    LOG.info("[ConnectionManager]start to discover service {}", svcEventKey);
    DefaultServiceEventKeysProvider provider = new DefaultServiceEventKeysProvider();
    provider.setSvcEventKey(svcEventKey);
    // 为性能考虑,优先使用本地缓存
    provider.setUseCache(true);
    FlowControlParam flowControlParam = new DefaultFlowControlParam();
    APIConfig apiConfig = extensions.getConfiguration().getGlobal().getAPI();
    flowControlParam.setTimeoutMs(apiConfig.getTimeout());
    flowControlParam.setMaxRetry(apiConfig.getMaxRetryTimes());
    flowControlParam.setRetryIntervalMs(apiConfig.getRetryInterval());
    // 执行服务路由
    ServiceInfo dstSvcInfo = new ServiceInfo();
    Map<String, String> metadata = new HashMap<>();
    metadata.put("protocol", protocol);
    dstSvcInfo.setMetadata(metadata);
    RouteInfo routeInfo = new RouteInfo(null, null, dstSvcInfo, null, "");
    ResourcesResponse resourcesResponse = BaseFlow.syncGetResources(extensions, false, provider, flowControlParam);
    LOG.info("[ConnectionManager]success to discover service {}", svcEventKey);
    ServiceInstances serviceInstances = resourcesResponse.getServiceInstances(svcEventKey);
    RouterChainGroup sysRouterChainGroup = extensions.getSysRouterChainGroup();
    List<ServiceRouter> coreRouters = Extensions.loadServiceRouters(coreRouterNames, extensions.getPlugins(), false);
    RouterChainGroup routerChainGroup = new DefaultRouterChainGroup(sysRouterChainGroup.getBeforeRouters(), coreRouters, sysRouterChainGroup.getAfterRouters());
    ServiceInstances instancesAfterRoute = BaseFlow.processServiceRouters(routeInfo, serviceInstances, routerChainGroup);
    // 执行负载均衡
    LoadBalancer loadBalancer = (LoadBalancer) extensions.getPlugins().getPlugin(PluginTypes.LOAD_BALANCER.getBaseType(), lbPolicy);
    Criteria criteria = new Criteria();
    criteria.setHashKey(hashKey);
    return BaseFlow.processLoadBalance(loadBalancer, criteria, instancesAfterRoute);
}
Also used : ServiceRouter(com.tencent.polaris.api.plugin.route.ServiceRouter) DefaultServiceEventKeysProvider(com.tencent.polaris.api.pojo.DefaultServiceEventKeysProvider) HashMap(java.util.HashMap) LoadBalancer(com.tencent.polaris.api.plugin.loadbalance.LoadBalancer) Criteria(com.tencent.polaris.api.rpc.Criteria) ServiceInfo(com.tencent.polaris.api.pojo.ServiceInfo) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) APIConfig(com.tencent.polaris.api.config.global.APIConfig) DefaultRouterChainGroup(com.tencent.polaris.api.plugin.compose.DefaultRouterChainGroup) RouterChainGroup(com.tencent.polaris.api.plugin.compose.RouterChainGroup) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) RouteInfo(com.tencent.polaris.api.plugin.route.RouteInfo) DefaultRouterChainGroup(com.tencent.polaris.api.plugin.compose.DefaultRouterChainGroup)

Example 2 with APIConfig

use of com.tencent.polaris.api.config.global.APIConfig 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

APIConfig (com.tencent.polaris.api.config.global.APIConfig)2 StaticMetadataManager (com.tencent.cloud.common.metadata.StaticMetadataManager)1 DiscoveryPropertiesAutoConfiguration (com.tencent.cloud.polaris.DiscoveryPropertiesAutoConfiguration)1 PolarisDiscoveryProperties (com.tencent.cloud.polaris.PolarisDiscoveryProperties)1 Configuration (com.tencent.polaris.api.config.Configuration)1 GlobalConfig (com.tencent.polaris.api.config.global.GlobalConfig)1 DefaultRouterChainGroup (com.tencent.polaris.api.plugin.compose.DefaultRouterChainGroup)1 RouterChainGroup (com.tencent.polaris.api.plugin.compose.RouterChainGroup)1 LoadBalancer (com.tencent.polaris.api.plugin.loadbalance.LoadBalancer)1 RouteInfo (com.tencent.polaris.api.plugin.route.RouteInfo)1 ServiceRouter (com.tencent.polaris.api.plugin.route.ServiceRouter)1 DefaultServiceEventKeysProvider (com.tencent.polaris.api.pojo.DefaultServiceEventKeysProvider)1 ServiceEventKey (com.tencent.polaris.api.pojo.ServiceEventKey)1 ServiceInfo (com.tencent.polaris.api.pojo.ServiceInfo)1 ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)1 Criteria (com.tencent.polaris.api.rpc.Criteria)1 SDKContext (com.tencent.polaris.client.api.SDKContext)1 HashMap (java.util.HashMap)1 Before (org.junit.Before)1