From 5fd7ace354e6a5bcc4fff8c7c23035973d555f4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E8=B7=AF=E8=B7=AF?= <499319126@qq.com> Date: Thu, 2 Mar 2023 16:55:56 +0800 Subject: [PATCH] optimize ServiceRuleManager (#869) --- CHANGELOG.md | 1 + ...sRouterEndpointAutoConfigurationTests.java | 12 +++- .../polaris/context/ServiceRuleManager.java | 65 +++++++------------ .../PolarisContextAutoConfiguration.java | 8 +-- 4 files changed, 36 insertions(+), 50 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 67ea7a641..5d8e0fa19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,4 +7,5 @@ - [refactor:move loadbalancer to discovery module.](https://github.com/Tencent/spring-cloud-tencent/pull/844) - [fix:fix the error capture of rate limit exception.](https://github.com/Tencent/spring-cloud-tencent/pull/854) - [feat:enable stat reporting as default.](https://github.com/Tencent/spring-cloud-tencent/pull/862) +- [optimize:optimize ServiceRuleManager.](https://github.com/Tencent/spring-cloud-tencent/pull/869) - [refactor:update to junit 5.](https://github.com/Tencent/spring-cloud-tencent/pull/865) diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/endpoint/PolarisRouterEndpointAutoConfigurationTests.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/endpoint/PolarisRouterEndpointAutoConfigurationTests.java index 22234487b..e2ec00e1e 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/endpoint/PolarisRouterEndpointAutoConfigurationTests.java +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/endpoint/PolarisRouterEndpointAutoConfigurationTests.java @@ -18,7 +18,9 @@ package com.tencent.cloud.polaris.router.endpoint; import com.tencent.cloud.polaris.context.ServiceRuleManager; +import com.tencent.polaris.api.core.ConsumerAPI; import com.tencent.polaris.client.api.SDKContext; +import com.tencent.polaris.factory.api.DiscoveryAPIFactory; import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; @@ -30,6 +32,7 @@ import static org.assertj.core.api.Assertions.assertThat; /** * test for {@link PolarisRouterEndpointAutoConfiguration}. + * * @author dongyinuo */ public class PolarisRouterEndpointAutoConfigurationTests { @@ -52,8 +55,13 @@ public class PolarisRouterEndpointAutoConfigurationTests { static class TestServiceRuleManagerConfiguration { @Bean - public ServiceRuleManager serviceRuleManager(SDKContext sdkContext) { - return new ServiceRuleManager(sdkContext); + public ServiceRuleManager serviceRuleManager(SDKContext sdkContext, ConsumerAPI consumerAPI) { + return new ServiceRuleManager(sdkContext, consumerAPI); + } + + @Bean + public ConsumerAPI consumerAPI(SDKContext sdkContext) { + return DiscoveryAPIFactory.createConsumerAPIByContext(sdkContext); } @Bean diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/ServiceRuleManager.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/ServiceRuleManager.java index 8294fb0eb..128899491 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/ServiceRuleManager.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/ServiceRuleManager.java @@ -19,19 +19,14 @@ package com.tencent.cloud.polaris.context; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; -import java.util.Set; -import com.tencent.polaris.api.pojo.DefaultServiceEventKeysProvider; +import com.tencent.polaris.api.core.ConsumerAPI; import com.tencent.polaris.api.pojo.ServiceEventKey; -import com.tencent.polaris.api.pojo.ServiceKey; import com.tencent.polaris.api.pojo.ServiceRule; +import com.tencent.polaris.api.rpc.GetServiceRuleRequest; +import com.tencent.polaris.api.rpc.ServiceRuleResponse; import com.tencent.polaris.client.api.SDKContext; -import com.tencent.polaris.client.flow.BaseFlow; -import com.tencent.polaris.client.flow.DefaultFlowControlParam; -import com.tencent.polaris.client.flow.FlowControlParam; -import com.tencent.polaris.client.flow.ResourcesResponse; import com.tencent.polaris.specification.api.v1.traffic.manage.RateLimitProto; import com.tencent.polaris.specification.api.v1.traffic.manage.RoutingProto; import org.slf4j.Logger; @@ -46,28 +41,17 @@ public class ServiceRuleManager { private static final Logger LOG = LoggerFactory.getLogger(ServiceRuleManager.class); private final SDKContext sdkContext; - private final FlowControlParam controlParam; + private final ConsumerAPI consumerAPI; - public ServiceRuleManager(SDKContext sdkContext) { + public ServiceRuleManager(SDKContext sdkContext, ConsumerAPI consumerAPI) { this.sdkContext = sdkContext; - controlParam = new DefaultFlowControlParam(); - controlParam.setTimeoutMs(sdkContext.getConfig().getGlobal().getAPI().getTimeout()); - controlParam.setMaxRetry(sdkContext.getConfig().getGlobal().getAPI().getMaxRetryTimes()); - controlParam.setRetryIntervalMs(sdkContext.getConfig().getGlobal().getAPI().getRetryInterval()); + this.consumerAPI = consumerAPI; } public RateLimitProto.RateLimit getServiceRateLimitRule(String namespace, String service) { LOG.debug("Get service rate limit rules with namespace:{} and service:{}.", namespace, 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); + ServiceRule serviceRule = getServiceRule(namespace, service, ServiceEventKey.EventType.RATE_LIMITING); if (serviceRule != null) { Object rule = serviceRule.getRule(); if (rule instanceof RateLimitProto.RateLimit) { @@ -79,29 +63,12 @@ public class ServiceRuleManager { } public List getServiceRouterRule(String namespace, String sourceService, String dstService) { - LOG.debug("Get service router rules with namespace:{} and sourceService:{} and dstService:{}.", - namespace, sourceService, dstService); - Set 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); + LOG.debug("Get service router rules with namespace:{} and sourceService:{} and dstService:{}.", namespace, sourceService, dstService); List rules = new ArrayList<>(); //get source service outbound rules. - ServiceRule sourceServiceRule = resourcesResponse.getServiceRule(srcSvcEventKey); + ServiceRule sourceServiceRule = getServiceRule(namespace, sourceService, ServiceEventKey.EventType.ROUTING); if (sourceServiceRule != null) { Object rule = sourceServiceRule.getRule(); if (rule instanceof RoutingProto.Routing) { @@ -110,7 +77,7 @@ public class ServiceRuleManager { } //get peer service inbound rules. - ServiceRule dstServiceRule = resourcesResponse.getServiceRule(dstSvcEventKey); + ServiceRule dstServiceRule = getServiceRule(namespace, dstService, ServiceEventKey.EventType.ROUTING); if (dstServiceRule != null) { Object rule = dstServiceRule.getRule(); if (rule instanceof RoutingProto.Routing) { @@ -120,4 +87,16 @@ public class ServiceRuleManager { return rules; } + + private ServiceRule getServiceRule(String namespace, String service, ServiceEventKey.EventType eventType) { + GetServiceRuleRequest getServiceRuleRequest = new GetServiceRuleRequest(); + getServiceRuleRequest.setRuleType(eventType); + getServiceRuleRequest.setService(service); + getServiceRuleRequest.setTimeoutMs(sdkContext.getConfig().getGlobal().getAPI().getTimeout()); + getServiceRuleRequest.setNamespace(namespace); + + ServiceRuleResponse res = consumerAPI.getServiceRule(getServiceRuleRequest); + return res.getServiceRule(); + } + } diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextAutoConfiguration.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextAutoConfiguration.java index 6db635efe..f0f143cad 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextAutoConfiguration.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextAutoConfiguration.java @@ -48,9 +48,7 @@ public class PolarisContextAutoConfiguration { @Bean(name = "polarisContext", initMethod = "init", destroyMethod = "destroy") @ConditionalOnMissingBean - public SDKContext polarisContext(PolarisContextProperties properties, Environment environment, - List modifierList) - throws PolarisException { + public SDKContext polarisContext(PolarisContextProperties properties, Environment environment, List modifierList) throws PolarisException { return SDKContext.initContextByConfig(properties.configuration(modifierList, () -> { return environment.getProperty("spring.cloud.client.ip-address"); })); @@ -80,7 +78,7 @@ public class PolarisContextAutoConfiguration { } @Bean - public ServiceRuleManager serviceRuleManager(SDKContext sdkContext) { - return new ServiceRuleManager(sdkContext); + public ServiceRuleManager serviceRuleManager(SDKContext sdkContext, ConsumerAPI consumerAPI) { + return new ServiceRuleManager(sdkContext, consumerAPI); } }