use of com.tencent.polaris.api.pojo.DefaultServiceEventKeysProvider 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);
}
use of com.tencent.polaris.api.pojo.DefaultServiceEventKeysProvider in project polaris-java by polarismesh.
the class CircuitBreakUtils method getRuleDestinationSet.
/**
* 通过配置获取远程规则
*
* @param ruleIdentifier 规则标识
* @param extensions 插件集合
* @param controlParam 控制参数
* @return 过滤信息
*/
public static RuleDestinationResult getRuleDestinationSet(RuleIdentifier ruleIdentifier, Extensions extensions, FlowControlParam controlParam) {
Set<ServiceEventKey> svcEventKeys = new HashSet<>();
ServiceEventKey dstSvcEventKey = new ServiceEventKey(new ServiceKey(ruleIdentifier.getNamespace(), ruleIdentifier.getService()), EventType.CIRCUIT_BREAKING);
svcEventKeys.add(dstSvcEventKey);
ServiceEventKey srcSvcEventKey = null;
Service callerService = ruleIdentifier.getCallerService();
if (null != callerService && StringUtils.isNotBlank(callerService.getNamespace()) && StringUtils.isNotBlank(callerService.getService())) {
srcSvcEventKey = new ServiceEventKey(new ServiceKey(callerService.getNamespace(), callerService.getService()), EventType.CIRCUIT_BREAKING);
svcEventKeys.add(srcSvcEventKey);
}
DefaultServiceEventKeysProvider serviceEventKeysProvider = new DefaultServiceEventKeysProvider();
serviceEventKeysProvider.setSvcEventKeys(svcEventKeys);
serviceEventKeysProvider.setUseCache(true);
ResourcesResponse resourcesResponse = BaseFlow.syncGetResources(extensions, false, serviceEventKeysProvider, controlParam);
CircuitBreakerProto.CircuitBreaker dstRule = getCircuitBreakerRule(resourcesResponse.getServiceRule(dstSvcEventKey));
CircuitBreakerProto.CircuitBreaker srcRule = null;
if (null != srcSvcEventKey) {
srcRule = getCircuitBreakerRule(resourcesResponse.getServiceRule(srcSvcEventKey));
}
List<CbRule> rules = getRules(dstRule, srcRule);
if (CollectionUtils.isEmpty(rules)) {
return new RuleDestinationResult(null, false, false);
}
for (CbRule rule : rules) {
MatchSourceResult matchSourceResult = matchSource(rule, ruleIdentifier);
if (!matchSourceResult.matched) {
continue;
}
MatchDestResult matchDestResult = matchDestination(rule, ruleIdentifier, extensions.getFlowCache());
if (null == matchDestResult.destinationSet) {
continue;
}
return new RuleDestinationResult(matchDestResult.destinationSet, matchSourceResult.allSourcesMatched, matchDestResult.allMethod);
}
return new RuleDestinationResult(null, false, false);
}
use of com.tencent.polaris.api.pojo.DefaultServiceEventKeysProvider in project spring-cloud-tencent by Tencent.
the class ServiceRuleManager method getServiceRouterRule.
public List<RoutingProto.Route> getServiceRouterRule(String namespace, String sourceService, String dstService) {
Set<ServiceEventKey> routerKeys = new HashSet<>();
ServiceEventKey dstSvcEventKey = new ServiceEventKey(new ServiceKey(namespace, dstService), ServiceEventKey.EventType.ROUTING);
routerKeys.add(dstSvcEventKey);
ServiceEventKey srcSvcEventKey = new ServiceEventKey(new ServiceKey(namespace, sourceService), ServiceEventKey.EventType.ROUTING);
routerKeys.add(srcSvcEventKey);
DefaultServiceEventKeysProvider svcKeysProvider = new DefaultServiceEventKeysProvider();
svcKeysProvider.setSvcEventKeys(routerKeys);
ResourcesResponse resourcesResponse = BaseFlow.syncGetResources(sdkContext.getExtensions(), true, svcKeysProvider, controlParam);
List<RoutingProto.Route> rules = new ArrayList<>();
// get source service outbound rules.
ServiceRule sourceServiceRule = resourcesResponse.getServiceRule(srcSvcEventKey);
if (sourceServiceRule != null) {
Object rule = sourceServiceRule.getRule();
if (rule instanceof RoutingProto.Routing) {
rules.addAll(((RoutingProto.Routing) rule).getOutboundsList());
}
}
// get peer service inbound rules.
ServiceRule dstServiceRule = resourcesResponse.getServiceRule(dstSvcEventKey);
if (dstServiceRule != null) {
Object rule = dstServiceRule.getRule();
if (rule instanceof RoutingProto.Routing) {
rules.addAll(((RoutingProto.Routing) rule).getInboundsList());
}
}
return rules;
}
use of com.tencent.polaris.api.pojo.DefaultServiceEventKeysProvider in project spring-cloud-tencent by Tencent.
the class ServiceRuleManager method getServiceRateLimitRule.
public RateLimitProto.RateLimit getServiceRateLimitRule(String namespace, String service) {
ServiceEventKey serviceEventKey = new ServiceEventKey(new ServiceKey(namespace, service), ServiceEventKey.EventType.RATE_LIMITING);
DefaultServiceEventKeysProvider svcKeysProvider = new DefaultServiceEventKeysProvider();
svcKeysProvider.setSvcEventKey(serviceEventKey);
ResourcesResponse resourcesResponse = BaseFlow.syncGetResources(sdkContext.getExtensions(), true, svcKeysProvider, controlParam);
ServiceRule serviceRule = resourcesResponse.getServiceRule(serviceEventKey);
if (serviceRule != null) {
Object rule = serviceRule.getRule();
if (rule instanceof RateLimitProto.RateLimit) {
return (RateLimitProto.RateLimit) rule;
}
}
return null;
}
use of com.tencent.polaris.api.pojo.DefaultServiceEventKeysProvider in project polaris-java by polarismesh.
the class DefaultRouterAPI method processRouters.
@Override
public ProcessRoutersResponse processRouters(ProcessRoutersRequest request) throws PolarisException {
checkAvailable("RouterAPI");
RouterValidator.validateProcessRouterRequest(request);
RouterNamesGroup requestRouterGroup = request.getRouters();
List<ServiceRouter> beforeRouters;
if (null != requestRouterGroup && CollectionUtils.isNotEmpty(requestRouterGroup.getBeforeRouters())) {
beforeRouters = Extensions.loadServiceRouters(requestRouterGroup.getBeforeRouters(), extensions.getPlugins(), true);
} else {
beforeRouters = extensions.getConfigRouterChainGroup().getBeforeRouters();
}
List<ServiceRouter> afterRouters;
if (null != requestRouterGroup && CollectionUtils.isNotEmpty(requestRouterGroup.getAfterRouters())) {
afterRouters = Extensions.loadServiceRouters(requestRouterGroup.getAfterRouters(), extensions.getPlugins(), true);
} else {
afterRouters = extensions.getConfigRouterChainGroup().getAfterRouters();
}
List<ServiceRouter> coreRouters;
if (null != requestRouterGroup && CollectionUtils.isNotEmpty(requestRouterGroup.getCoreRouters())) {
coreRouters = Extensions.loadServiceRouters(requestRouterGroup.getCoreRouters(), extensions.getPlugins(), true);
} else {
coreRouters = extensions.getConfigRouterChainGroup().getCoreRouters();
}
ServiceInstances dstInstances = request.getDstInstances();
RouteInfo routeInfo = new RouteInfo(request.getSourceService(), dstInstances, request.getMethod());
// 获取路由规则
DefaultFlowControlParam engineFlowControlParam = new DefaultFlowControlParam();
BaseFlow.buildFlowControlParam(request, config, engineFlowControlParam);
Set<ServiceEventKey> routerKeys = new HashSet<>();
ServiceEventKey dstSvcEventKey = new ServiceEventKey(new ServiceKey(dstInstances.getNamespace(), dstInstances.getService()), EventType.ROUTING);
routerKeys.add(dstSvcEventKey);
ServiceEventKey srcSvcEventKey = null;
if (null != routeInfo.getSourceService() && StringUtils.isNotBlank(routeInfo.getSourceService().getNamespace()) && StringUtils.isNotBlank(routeInfo.getSourceService().getService())) {
srcSvcEventKey = new ServiceEventKey(new ServiceKey(routeInfo.getSourceService().getNamespace(), routeInfo.getSourceService().getService()), EventType.ROUTING);
routerKeys.add(srcSvcEventKey);
}
DefaultServiceEventKeysProvider svcKeysProvider = new DefaultServiceEventKeysProvider();
svcKeysProvider.setSvcEventKeys(routerKeys);
ResourcesResponse resourcesResponse = BaseFlow.syncGetResources(extensions, false, svcKeysProvider, engineFlowControlParam);
routeInfo.setDestRouteRule(resourcesResponse.getServiceRule(dstSvcEventKey));
if (null != srcSvcEventKey) {
routeInfo.setSourceRouteRule(resourcesResponse.getServiceRule(srcSvcEventKey));
}
// 执行路由
DefaultRouterChainGroup routerChainGroup = new DefaultRouterChainGroup(beforeRouters, coreRouters, afterRouters);
ServiceInstances svcInstances = BaseFlow.processServiceRouters(routeInfo, dstInstances, routerChainGroup);
return new ProcessRoutersResponse(svcInstances);
}
Aggregations