optimize ServiceRuleManager

pull/1080/head
Haotian Zhang 2 years ago
parent b2cecba273
commit 07f04174ca

@ -18,7 +18,9 @@
package com.tencent.cloud.polaris.router.endpoint; package com.tencent.cloud.polaris.router.endpoint;
import com.tencent.cloud.polaris.context.ServiceRuleManager; 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.client.api.SDKContext;
import com.tencent.polaris.factory.api.DiscoveryAPIFactory;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
@ -30,17 +32,15 @@ import static org.assertj.core.api.Assertions.assertThat;
/** /**
* test for {@link PolarisRouterEndpointAutoConfiguration}. * test for {@link PolarisRouterEndpointAutoConfiguration}.
*
* @author dongyinuo * @author dongyinuo
*/ */
public class PolarisRouterEndpointAutoConfigurationTests { public class PolarisRouterEndpointAutoConfigurationTests {
private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
TestServiceRuleManagerConfiguration.class,
PolarisRouterEndpointAutoConfiguration.class))
.withPropertyValues("endpoints.polaris-router.enabled=true");
private ServiceRuleManager serviceRuleManager; private ServiceRuleManager serviceRuleManager;
private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner().withConfiguration(AutoConfigurations.of(TestServiceRuleManagerConfiguration.class, PolarisRouterEndpointAutoConfiguration.class)).withPropertyValues("endpoints.polaris-router.enabled=true");
@Test @Test
public void polarisRouterEndpoint() { public void polarisRouterEndpoint() {
contextRunner.run(context -> { contextRunner.run(context -> {
@ -52,8 +52,13 @@ public class PolarisRouterEndpointAutoConfigurationTests {
static class TestServiceRuleManagerConfiguration { static class TestServiceRuleManagerConfiguration {
@Bean @Bean
public ServiceRuleManager serviceRuleManager(SDKContext sdkContext) { public ServiceRuleManager serviceRuleManager(SDKContext sdkContext, ConsumerAPI consumerAPI) {
return new ServiceRuleManager(sdkContext); return new ServiceRuleManager(sdkContext, consumerAPI);
}
@Bean
public ConsumerAPI consumerAPI(SDKContext sdkContext) {
return DiscoveryAPIFactory.createConsumerAPIByContext(sdkContext);
} }
@Bean @Bean

@ -46,14 +46,6 @@
<groupId>com.tencent.polaris</groupId> <groupId>com.tencent.polaris</groupId>
<artifactId>router-set</artifactId> <artifactId>router-set</artifactId>
</exclusion> </exclusion>
<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>router-isolated</artifactId>
</exclusion>
<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>router-healthy</artifactId>
</exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
@ -102,17 +94,12 @@
<groupId>com.tencent.polaris</groupId> <groupId>com.tencent.polaris</groupId>
<artifactId>flow-cache-expired</artifactId> <artifactId>flow-cache-expired</artifactId>
</dependency> </dependency>
<!-- Discovery depended on base routers -->
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>router-isolated</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.tencent.polaris</groupId> <groupId>com.tencent.polaris</groupId>
<artifactId>router-healthy</artifactId> <artifactId>polaris-router-factory</artifactId>
</dependency> </dependency>
<!-- Discovery depended on base routers -->
<dependency> <dependency>
<groupId>com.tencent.polaris</groupId> <groupId>com.tencent.polaris</groupId>

@ -19,19 +19,14 @@
package com.tencent.cloud.polaris.context; package com.tencent.cloud.polaris.context;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; 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.ServiceEventKey;
import com.tencent.polaris.api.pojo.ServiceKey;
import com.tencent.polaris.api.pojo.ServiceRule; 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.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.RateLimitProto;
import com.tencent.polaris.specification.api.v1.traffic.manage.RoutingProto; import com.tencent.polaris.specification.api.v1.traffic.manage.RoutingProto;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -46,28 +41,17 @@ public class ServiceRuleManager {
private static final Logger LOG = LoggerFactory.getLogger(ServiceRuleManager.class); private static final Logger LOG = LoggerFactory.getLogger(ServiceRuleManager.class);
private final SDKContext sdkContext; 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; this.sdkContext = sdkContext;
controlParam = new DefaultFlowControlParam(); this.consumerAPI = consumerAPI;
controlParam.setTimeoutMs(sdkContext.getConfig().getGlobal().getAPI().getTimeout());
controlParam.setMaxRetry(sdkContext.getConfig().getGlobal().getAPI().getMaxRetryTimes());
controlParam.setRetryIntervalMs(sdkContext.getConfig().getGlobal().getAPI().getRetryInterval());
} }
public RateLimitProto.RateLimit getServiceRateLimitRule(String namespace, String service) { public RateLimitProto.RateLimit getServiceRateLimitRule(String namespace, String service) {
LOG.debug("Get service rate limit rules with namespace:{} and service:{}.", namespace, 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(); ServiceRule serviceRule = getServiceRule(namespace, service, ServiceEventKey.EventType.RATE_LIMITING);
svcKeysProvider.setSvcEventKey(serviceEventKey);
ResourcesResponse resourcesResponse = BaseFlow
.syncGetResources(sdkContext.getExtensions(), true, svcKeysProvider, controlParam);
ServiceRule serviceRule = resourcesResponse.getServiceRule(serviceEventKey);
if (serviceRule != null) { if (serviceRule != null) {
Object rule = serviceRule.getRule(); Object rule = serviceRule.getRule();
if (rule instanceof RateLimitProto.RateLimit) { if (rule instanceof RateLimitProto.RateLimit) {
@ -79,29 +63,12 @@ public class ServiceRuleManager {
} }
public List<RoutingProto.Route> getServiceRouterRule(String namespace, String sourceService, String dstService) { public List<RoutingProto.Route> getServiceRouterRule(String namespace, String sourceService, String dstService) {
LOG.debug("Get service router rules with namespace:{} and sourceService:{} and dstService:{}.", LOG.debug("Get service router rules with namespace:{} and sourceService:{} and dstService:{}.", namespace, sourceService, dstService);
namespace, sourceService, 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<>(); List<RoutingProto.Route> rules = new ArrayList<>();
//get source service outbound rules. //get source service outbound rules.
ServiceRule sourceServiceRule = resourcesResponse.getServiceRule(srcSvcEventKey); ServiceRule sourceServiceRule = getServiceRule(namespace, sourceService, ServiceEventKey.EventType.ROUTING);
if (sourceServiceRule != null) { if (sourceServiceRule != null) {
Object rule = sourceServiceRule.getRule(); Object rule = sourceServiceRule.getRule();
if (rule instanceof RoutingProto.Routing) { if (rule instanceof RoutingProto.Routing) {
@ -110,7 +77,7 @@ public class ServiceRuleManager {
} }
//get peer service inbound rules. //get peer service inbound rules.
ServiceRule dstServiceRule = resourcesResponse.getServiceRule(dstSvcEventKey); ServiceRule dstServiceRule = getServiceRule(namespace, dstService, ServiceEventKey.EventType.ROUTING);
if (dstServiceRule != null) { if (dstServiceRule != null) {
Object rule = dstServiceRule.getRule(); Object rule = dstServiceRule.getRule();
if (rule instanceof RoutingProto.Routing) { if (rule instanceof RoutingProto.Routing) {
@ -120,4 +87,16 @@ public class ServiceRuleManager {
return rules; 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();
}
} }

@ -18,19 +18,25 @@
package com.tencent.cloud.polaris.context.config; package com.tencent.cloud.polaris.context.config;
import java.util.List;
import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled; import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled;
import com.tencent.cloud.polaris.context.ModifyAddress; import com.tencent.cloud.polaris.context.ModifyAddress;
import com.tencent.cloud.polaris.context.PolarisConfigModifier;
import com.tencent.cloud.polaris.context.ServiceRuleManager; import com.tencent.cloud.polaris.context.ServiceRuleManager;
import com.tencent.polaris.api.core.ConsumerAPI; import com.tencent.polaris.api.core.ConsumerAPI;
import com.tencent.polaris.api.core.ProviderAPI; import com.tencent.polaris.api.core.ProviderAPI;
import com.tencent.polaris.api.exception.PolarisException; import com.tencent.polaris.api.exception.PolarisException;
import com.tencent.polaris.client.api.SDKContext; import com.tencent.polaris.client.api.SDKContext;
import com.tencent.polaris.factory.api.DiscoveryAPIFactory; import com.tencent.polaris.factory.api.DiscoveryAPIFactory;
import com.tencent.polaris.factory.api.RouterAPIFactory;
import com.tencent.polaris.router.api.core.RouterAPI;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
/** /**
* Autoconfiguration for Polaris {@link SDKContext}. * Autoconfiguration for Polaris {@link SDKContext}.
@ -44,9 +50,9 @@ public class PolarisContextAutoConfiguration {
@Bean(name = "polarisContext", initMethod = "init", destroyMethod = "destroy") @Bean(name = "polarisContext", initMethod = "init", destroyMethod = "destroy")
@ConditionalOnMissingBean @ConditionalOnMissingBean
public SDKContext polarisContext(PolarisContextProperties properties) public SDKContext polarisContext(PolarisContextProperties properties, Environment environment, List<PolarisConfigModifier> modifierList) throws PolarisException {
throws PolarisException { return SDKContext.initContextByConfig(properties.configuration(modifierList,
return SDKContext.initContextByConfig(properties.configuration()); () -> environment.getProperty("spring.cloud.client.ip-address")));
} }
@Bean @Bean
@ -61,6 +67,11 @@ public class PolarisContextAutoConfiguration {
return DiscoveryAPIFactory.createConsumerAPIByContext(polarisContext); return DiscoveryAPIFactory.createConsumerAPIByContext(polarisContext);
} }
@Bean
public RouterAPI polarisRouter(SDKContext polarisContext) throws PolarisException {
return RouterAPIFactory.createRouterAPIByContext(polarisContext);
}
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public ModifyAddress polarisConfigModifier(PolarisContextProperties properties) { public ModifyAddress polarisConfigModifier(PolarisContextProperties properties) {
@ -69,7 +80,7 @@ public class PolarisContextAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public ServiceRuleManager serviceRuleManager(SDKContext sdkContext) { public ServiceRuleManager serviceRuleManager(SDKContext sdkContext, ConsumerAPI consumerAPI) {
return new ServiceRuleManager(sdkContext); return new ServiceRuleManager(sdkContext, consumerAPI);
} }
} }

@ -21,6 +21,7 @@ package com.tencent.cloud.polaris.context.config;
import java.util.Collection; import java.util.Collection;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.tencent.cloud.polaris.context.PolarisConfigModifier; import com.tencent.cloud.polaris.context.PolarisConfigModifier;
@ -30,9 +31,7 @@ import com.tencent.polaris.factory.ConfigAPIFactory;
import com.tencent.polaris.factory.config.ConfigurationImpl; import com.tencent.polaris.factory.config.ConfigurationImpl;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.env.Environment;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
/** /**
@ -68,19 +67,18 @@ public class PolarisContextProperties {
*/ */
private String service; private String service;
@Autowired public Configuration configuration(List<PolarisConfigModifier> modifierList, Supplier<String> ipAddressSupplier) {
private Environment environment;
@Autowired
private List<PolarisConfigModifier> modifierList;
protected Configuration configuration() {
// 1. Read user-defined polaris.yml configuration // 1. Read user-defined polaris.yml configuration
ConfigurationImpl configuration = (ConfigurationImpl) ConfigAPIFactory ConfigurationImpl configuration = (ConfigurationImpl) ConfigAPIFactory
.defaultConfig(ConfigProvider.DEFAULT_CONFIG); .defaultConfig(ConfigProvider.DEFAULT_CONFIG);
// 2. Override user-defined polaris.yml configuration with SCT configuration // 2. Override user-defined polaris.yml configuration with SCT configuration
String defaultHost = getHost(); String defaultHost = this.localIpAddress;
if (StringUtils.isBlank(localIpAddress)) {
defaultHost = ipAddressSupplier.get();
this.localIpAddress = defaultHost;
}
configuration.getGlobal().getAPI().setBindIP(defaultHost); configuration.getGlobal().getAPI().setBindIP(defaultHost);
Collection<PolarisConfigModifier> modifiers = modifierList; Collection<PolarisConfigModifier> modifiers = modifierList;
@ -96,14 +94,6 @@ public class PolarisContextProperties {
return configuration; return configuration;
} }
private String getHost() {
if (StringUtils.isNotBlank(localIpAddress)) {
return localIpAddress;
}
this.localIpAddress = environment.getProperty("spring.cloud.client.ip-address");
return this.localIpAddress;
}
public String getAddress() { public String getAddress() {
return address; return address;
} }
@ -143,5 +133,4 @@ public class PolarisContextProperties {
public void setService(String service) { public void setService(String service) {
this.service = service; this.service = service;
} }
} }

@ -22,11 +22,6 @@
<!-- Spring Cloud Tencent dependencies end --> <!-- Spring Cloud Tencent dependencies end -->
<!-- Polaris dependencies start --> <!-- Polaris dependencies start -->
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-router-factory</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.tencent.polaris</groupId> <groupId>com.tencent.polaris</groupId>
<artifactId>polaris-discovery-api</artifactId> <artifactId>polaris-discovery-api</artifactId>
@ -55,12 +50,12 @@
<artifactId>mockito-inline</artifactId> <artifactId>mockito-inline</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.tencent.polaris</groupId> <groupId>com.tencent.polaris</groupId>
<artifactId>polaris-discovery-client</artifactId> <artifactId>polaris-discovery-client</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>

@ -18,10 +18,6 @@
package com.tencent.cloud.polaris.loadbalancer.config; package com.tencent.cloud.polaris.loadbalancer.config;
import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled; import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled;
import com.tencent.polaris.api.exception.PolarisException;
import com.tencent.polaris.client.api.SDKContext;
import com.tencent.polaris.factory.api.RouterAPIFactory;
import com.tencent.polaris.router.api.core.RouterAPI;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@ -48,9 +44,4 @@ public class PolarisLoadBalancerAutoConfiguration {
public PolarisLoadBalancerProperties polarisLoadBalancerProperties() { public PolarisLoadBalancerProperties polarisLoadBalancerProperties() {
return new PolarisLoadBalancerProperties(); return new PolarisLoadBalancerProperties();
} }
@Bean
public RouterAPI routerAPI(SDKContext polarisContext) throws PolarisException {
return RouterAPIFactory.createRouterAPIByContext(polarisContext);
}
} }

Loading…
Cancel
Save