feat:optimize project structure and checkstyle

pull/56/head
SkyeBeFreeman 3 years ago
parent efc8a7e555
commit baabb69be3

@ -0,0 +1,28 @@
root = true
[*.java]
indent_style = tab
indent_size = 4
continuation_indent_size = 8
[*.groovy]
indent_style = tab
indent_size = 4
continuation_indent_size = 8
[*.xml]
indent_style = tab
indent_size = 4
continuation_indent_size = 8
[*.yml]
indent_style = space
indent_size = 2
[*.yaml]
indent_style = space
indent_size = 2
[*.sh]
indent_style = space
indent_size = 4

@ -38,8 +38,14 @@
</scm>
<modules>
<module>spring-cloud-tencent-polaris-context</module>
<module>spring-cloud-tencent-commons</module>
<module>spring-cloud-tencent-metadata</module>
<module>spring-cloud-starter-tencent-polaris-discovery</module>
<module>spring-cloud-starter-tencent-polaris-ratelimit</module>
<module>spring-cloud-starter-tencent-polaris-circuitbreaker</module>
<module>spring-cloud-starter-tencent-polaris-router</module>
<module>spring-cloud-tencent-dependencies</module>
<module>spring-cloud-tencent-starters</module>
<module>spring-cloud-tencent-examples</module>
<module>spring-cloud-tencent-coverage</module>
</modules>
@ -82,19 +88,16 @@
<jacoco.version>0.8.3</jacoco.version>
<maven-source-plugin.version>3.2.0</maven-source-plugin.version>
<flatten-maven-plugin.version>1.2.7</flatten-maven-plugin.version>
<!-- Checkstyle -->
<maven-checkstyle-plugin.failsOnError>true</maven-checkstyle-plugin.failsOnError>
<maven-checkstyle-plugin.failsOnViolation>true</maven-checkstyle-plugin.failsOnViolation>
<maven-checkstyle-plugin.includeTestSourceDirectory>true
</maven-checkstyle-plugin.includeTestSourceDirectory>
</properties>
<dependencyManagement>
<dependencies>
<!-- Spring Boot Dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Spring Cloud Dependencies -->
<dependency>
<groupId>org.springframework.cloud</groupId>
@ -132,6 +135,14 @@
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>io.spring.javaformat</groupId>
<artifactId>spring-javaformat-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
@ -142,6 +153,26 @@
<parameters>true</parameters>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
@ -179,6 +210,15 @@
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin>
</plugins>
</reporting>
<profiles>
<profile>
<id>release</id>

@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-tencent</artifactId>
<groupId>com.tencent.cloud</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-starter-tencent-polaris-circuitbreaker</artifactId>
<name>Spring Cloud Starter Tencent Polaris Circuitbreaker</name>
<dependencies>
<!-- Spring Cloud Tencent dependencies start -->
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-commons</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-metadata</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-polaris-context</artifactId>
</dependency>
<!-- Spring Cloud Tencent dependencies end -->
<!-- Polaris dependencies start -->
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-discovery-factory</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-circuitbreaker-factory</artifactId>
</dependency>
<!-- Polaris dependencies end -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-openfeign-core</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

@ -17,8 +17,6 @@
package com.tencent.cloud.polaris.circuitbreaker;
import static org.springframework.core.Ordered.HIGHEST_PRECEDENCE;
import com.tencent.cloud.common.constant.ContextConstant.ModifierOrder;
import com.tencent.cloud.polaris.circuitbreaker.feign.PolarisFeignBeanPostProcessor;
import com.tencent.cloud.polaris.context.PolarisConfigModifier;
@ -27,6 +25,7 @@ import com.tencent.polaris.api.core.ConsumerAPI;
import com.tencent.polaris.client.api.SDKContext;
import com.tencent.polaris.factory.api.DiscoveryAPIFactory;
import com.tencent.polaris.factory.config.ConfigurationImpl;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@ -35,15 +34,16 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import static org.springframework.core.Ordered.HIGHEST_PRECEDENCE;
/**
* Configuration for Polaris {@link feign.Feign} which can automatically bring in the call results for reporting
* Configuration for Polaris {@link feign.Feign} which can automatically bring in the call
* results for reporting.
*
* @author Haotian Zhang
*/
@ConditionalOnProperty(
value = "spring.cloud.polaris.circuitbreaker.enabled",
havingValue = "true",
matchIfMissing = true)
@ConditionalOnProperty(value = "spring.cloud.polaris.circuitbreaker.enabled",
havingValue = "true", matchIfMissing = true)
@Configuration(proxyBeanMethods = false)
@AutoConfigureAfter(PolarisContextConfiguration.class)
@AutoConfigureBefore(FeignAutoConfiguration.class)
@ -55,8 +55,9 @@ public class PolarisFeignClientAutoConfiguration {
}
@Bean
@Order(value = HIGHEST_PRECEDENCE)
public PolarisFeignBeanPostProcessor polarisFeignBeanPostProcessor(ConsumerAPI consumerAPI) {
@Order(HIGHEST_PRECEDENCE)
public PolarisFeignBeanPostProcessor polarisFeignBeanPostProcessor(
ConsumerAPI consumerAPI) {
return new PolarisFeignBeanPostProcessor(consumerAPI);
}
@ -69,7 +70,7 @@ public class PolarisFeignClientAutoConfiguration {
@Override
public void modify(ConfigurationImpl configuration) {
//开启熔断配置
// 开启熔断配置
configuration.getConsumer().getCircuitBreaker().setEnable(true);
}
@ -77,5 +78,7 @@ public class PolarisFeignClientAutoConfiguration {
public int getOrder() {
return ModifierOrder.CIRCUIT_BREAKER_ORDER;
}
}
}

@ -0,0 +1,97 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.polaris.circuitbreaker.feign;
import com.tencent.polaris.api.core.ConsumerAPI;
import feign.Client;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient;
import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory;
import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;
/**
* Wrap Spring Bean and decorating proxy for Feign Client.
*
* @author Haotian Zhang
*/
public class PolarisFeignBeanPostProcessor
implements BeanPostProcessor, BeanFactoryAware {
private final ConsumerAPI consumerAPI;
private BeanFactory factory;
public PolarisFeignBeanPostProcessor(ConsumerAPI consumerAPI) {
this.consumerAPI = consumerAPI;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
return wrapper(bean);
}
private Object wrapper(Object bean) {
if (isNeedWrap(bean)) {
if (bean instanceof LoadBalancerFeignClient) {
LoadBalancerFeignClient client = ((LoadBalancerFeignClient) bean);
return new PolarisLoadBalancerFeignClient(
createPolarisFeignClient(client.getDelegate()), factory(),
clientFactory());
}
if (bean instanceof FeignBlockingLoadBalancerClient) {
FeignBlockingLoadBalancerClient client = (FeignBlockingLoadBalancerClient) bean;
return new PolarisFeignBlockingLoadBalancerClient(
createPolarisFeignClient(client.getDelegate()),
factory.getBean(BlockingLoadBalancerClient.class));
}
return createPolarisFeignClient((Client) bean);
}
return bean;
}
private boolean isNeedWrap(Object bean) {
return bean instanceof Client && !(bean instanceof PolarisFeignClient)
&& !(bean instanceof PolarisFeignBlockingLoadBalancerClient)
&& !(bean instanceof PolarisLoadBalancerFeignClient);
}
private PolarisFeignClient createPolarisFeignClient(Client delegate) {
return new PolarisFeignClient(delegate, consumerAPI);
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.factory = beanFactory;
}
CachingSpringLoadBalancerFactory factory() {
return this.factory.getBean(CachingSpringLoadBalancerFactory.class);
}
SpringClientFactory clientFactory() {
return this.factory.getBean(SpringClientFactory.class);
}
}

@ -18,15 +18,17 @@
package com.tencent.cloud.polaris.circuitbreaker.feign;
import feign.Client;
import org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient;
import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient;
/**
* Wrap for {@link FeignBlockingLoadBalancerClient}
* Wrap for {@link FeignBlockingLoadBalancerClient}.
*
* @author Haotian Zhang
*/
public class PolarisFeignBlockingLoadBalancerClient extends FeignBlockingLoadBalancerClient {
public class PolarisFeignBlockingLoadBalancerClient
extends FeignBlockingLoadBalancerClient {
public PolarisFeignBlockingLoadBalancerClient(Client delegate,
BlockingLoadBalancerClient loadBalancerClient) {

@ -0,0 +1,104 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.polaris.circuitbreaker.feign;
import java.io.IOException;
import java.net.URI;
import com.tencent.cloud.metadata.constant.MetadataConstant.SystemMetadataKey;
import com.tencent.cloud.metadata.context.MetadataContext;
import com.tencent.cloud.metadata.context.MetadataContextHolder;
import com.tencent.polaris.api.core.ConsumerAPI;
import com.tencent.polaris.api.pojo.RetStatus;
import com.tencent.polaris.api.pojo.ServiceKey;
import com.tencent.polaris.api.rpc.ServiceCallResult;
import feign.Client;
import feign.Request;
import feign.Request.Options;
import feign.Response;
import org.apache.commons.lang.StringUtils;
import static feign.Util.checkNotNull;
/**
* Wrap for {@link Client}.
*
* @author Haotian Zhang
*/
public class PolarisFeignClient implements Client {
private final Client delegate;
private final ConsumerAPI consumerAPI;
public PolarisFeignClient(Client target, ConsumerAPI consumerAPI) {
this.delegate = checkNotNull(target, "target");
this.consumerAPI = checkNotNull(consumerAPI, "CircuitBreakAPI");
}
@Override
public Response execute(Request request, Options options) throws IOException {
final ServiceCallResult resultRequest = createServiceCallResult(request);
try {
Response response = delegate.execute(request, options);
// HTTP code greater than 400 is an exception
if (response.status() >= 400) {
resultRequest.setRetStatus(RetStatus.RetFail);
}
return response;
}
catch (IOException origin) {
resultRequest.setRetStatus(RetStatus.RetFail);
throw origin;
}
finally {
consumerAPI.updateServiceCallResult(resultRequest);
}
}
private ServiceCallResult createServiceCallResult(final Request request) {
ServiceCallResult resultRequest = new ServiceCallResult();
MetadataContext metadataContext = MetadataContextHolder.get();
String namespace = metadataContext
.getSystemMetadata(SystemMetadataKey.PEER_NAMESPACE);
resultRequest.setNamespace(namespace);
String serviceName = metadataContext
.getSystemMetadata(SystemMetadataKey.PEER_SERVICE);
resultRequest.setService(serviceName);
String method = metadataContext.getSystemMetadata(SystemMetadataKey.PEER_PATH);
resultRequest.setMethod(method);
resultRequest.setRetStatus(RetStatus.RetSuccess);
String sourceNamespace = metadataContext
.getSystemMetadata(SystemMetadataKey.LOCAL_NAMESPACE);
String sourceService = metadataContext
.getSystemMetadata(SystemMetadataKey.LOCAL_SERVICE);
if (StringUtils.isNotBlank(sourceNamespace)
&& StringUtils.isNotBlank(sourceService)) {
resultRequest
.setCallerService(new ServiceKey(sourceNamespace, sourceService));
}
URI uri = URI.create(request.url());
resultRequest.setHost(uri.getHost());
resultRequest.setPort(uri.getPort());
return resultRequest;
}
}

@ -18,12 +18,13 @@
package com.tencent.cloud.polaris.circuitbreaker.feign;
import feign.Client;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory;
import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;
/**
* Wrap for {@link LoadBalancerFeignClient}
* Wrap for {@link LoadBalancerFeignClient}.
*
* @author Haotian Zhang
*/
@ -34,4 +35,5 @@ public class PolarisLoadBalancerFeignClient extends LoadBalancerFeignClient {
SpringClientFactory clientFactory) {
super(delegate, lbClientFactory, clientFactory);
}
}

@ -22,6 +22,7 @@ import feign.Client;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
@ -33,7 +34,7 @@ import org.springframework.test.context.junit4.SpringRunner;
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestPolarisFeignApp.class)
@ContextConfiguration(classes = {PolarisFeignClientAutoConfiguration.class})
@ContextConfiguration(classes = { PolarisFeignClientAutoConfiguration.class })
public class PolarisFeignClientTest {
@Autowired
@ -41,7 +42,8 @@ public class PolarisFeignClientTest {
@Test
public void testPolarisFeignBeanPostProcessor() {
final PolarisFeignBeanPostProcessor postProcessor = springCtx.getBean(PolarisFeignBeanPostProcessor.class);
final PolarisFeignBeanPostProcessor postProcessor = springCtx
.getBean(PolarisFeignBeanPostProcessor.class);
Assertions.assertNotNull(postProcessor, "PolarisFeignBeanPostProcessor");
}
@ -59,4 +61,5 @@ public class PolarisFeignClientTest {
}
throw new IllegalStateException("Polaris burying failed");
}
}

@ -17,7 +17,6 @@
package com.tencent.cloud.polaris.circuitbreaker.feign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@ -32,20 +31,16 @@ import org.springframework.web.bind.annotation.GetMapping;
@EnableFeignClients
public class TestPolarisFeignApp {
@Autowired
TestPolarisService service;
public static void main(String[] args) {
SpringApplication.run(TestPolarisFeignApp.class);
}
@FeignClient(name = "feign-service-polaris", fallback = TestPolarisServiceFallback.class)
@FeignClient(name = "feign-service-polaris",
fallback = TestPolarisServiceFallback.class)
public interface TestPolarisService {
/**
* B
*
* @return
* Get info of service B.
*/
@GetMapping("/example/service/b/info")
String info();
@ -59,5 +54,7 @@ public class TestPolarisFeignApp {
public String info() {
return "trigger the refuse";
}
}
}

@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-tencent-starters</artifactId>
<artifactId>spring-cloud-tencent</artifactId>
<groupId>com.tencent.cloud</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>

@ -0,0 +1,231 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.polaris;
import javax.annotation.PostConstruct;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.core.env.Environment;
/**
* Properties for Polaris.
*
* @author Haotian Zhang, Andrew Shan, Jie Cheng
*/
@ConfigurationProperties("spring.cloud.polaris.discovery")
public class PolarisProperties {
/**
* The polaris authentication token.
*/
private String token;
/**
* Namespace, separation registry of different environments.
*/
@Value("${spring.cloud.polaris.discovery.namespace:#{'default'}}")
private String namespace;
/**
* Service name to registry.
*/
@Value("${spring.cloud.polaris.discovery.service:${spring.application.name:}}")
private String service;
/**
* Load balance weight.
*/
@Value("${spring.cloud.polaris.discovery.weight:#{100}}")
private float weight;
/**
* Version number.
*/
private String version;
/**
* Protocol name such as http, https.
*/
@Value("${spring.cloud.polaris.discovery.protocol:http}")
private String protocol;
/**
* Port of instance.
*/
@Value("${server.port:}")
private int port;
/**
* Ip address to be registered.
*/
private String ipAddress;
/**
* If instance registered.
*/
@Value("${spring.cloud.polaris.discovery.register.enabled:#{true}}")
private Boolean registerEnabled;
/**
* If heartbeat enabled.
*/
@Value("${spring.cloud.polaris.discovery.heartbeat.enabled:#{true}}")
private Boolean heartbeatEnabled = true;
/**
* Custom health check url to override default.
*/
@Value("${spring.cloud.polaris.discovery.health-check-url:}")
private String healthCheckUrl;
@Autowired
private Environment environment;
public PolarisProperties(InetUtils inetUtils) {
if (inetUtils != null) {
this.ipAddress = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
}
}
/**
* Init properties.
*/
@PostConstruct
public void init() {
if (StringUtils.isEmpty(this.getNamespace())) {
this.setNamespace(environment
.resolvePlaceholders("${spring.cloud.polaris.discovery.namespace:}"));
}
if (StringUtils.isEmpty(this.getService())) {
this.setService(environment
.resolvePlaceholders("${spring.cloud.polaris.discovery.service:}"));
}
if (StringUtils.isEmpty(this.getToken())) {
this.setToken(environment
.resolvePlaceholders("${spring.cloud.polaris.discovery.token:}"));
}
}
public boolean isHeartbeatEnabled() {
if (null == heartbeatEnabled) {
return false;
}
return heartbeatEnabled;
}
public void setHeartbeatEnabled(Boolean heartbeatEnabled) {
this.heartbeatEnabled = heartbeatEnabled;
}
public String getNamespace() {
return namespace;
}
public void setNamespace(String namespace) {
this.namespace = namespace;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
public String getService() {
return service;
}
public void setService(String service) {
this.service = service;
}
public boolean isRegisterEnabled() {
return registerEnabled;
}
public void setRegisterEnabled(boolean registerEnabled) {
this.registerEnabled = registerEnabled;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getProtocol() {
return protocol;
}
public void setProtocol(String protocol) {
this.protocol = protocol;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getIpAddress() {
return ipAddress;
}
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}
public String getHealthCheckUrl() {
return healthCheckUrl;
}
public void setHealthCheckUrl(String healthCheckUrl) {
this.healthCheckUrl = healthCheckUrl;
}
@Override
public String toString() {
return "PolarisProperties{" + "token='" + token + '\'' + ", namespace='"
+ namespace + '\'' + ", service='" + service + '\'' + ", weight=" + weight
+ ", version='" + version + '\'' + ", protocol='" + protocol + '\''
+ ", port=" + port + ", ipAddress='" + ipAddress + '\''
+ ", registerEnabled=" + registerEnabled + ", heartbeatEnabled="
+ heartbeatEnabled + ", healthCheckUrl=" + healthCheckUrl
+ ", environment=" + environment + '}';
}
}

@ -36,15 +36,13 @@ import java.lang.annotation.Target;
public @interface PolarisClient {
/**
* service
*
* Service name of instance.
* @return service
*/
String service() default "";
/**
* namespace
*
* Namespace name of instance.
* @return namespace
*/
String namespace() default "";

@ -21,6 +21,7 @@ import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled;
@ -28,9 +29,10 @@ import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled;
* @author Haotian Zhang, Andrew Shan, Jie Cheng
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Target({ ElementType.TYPE, ElementType.METHOD })
@ConditionalOnDiscoveryEnabled
@ConditionalOnProperty(value = "spring.cloud.polaris.discovery.enabled", matchIfMissing = true)
@ConditionalOnProperty(value = "spring.cloud.polaris.discovery.enabled",
matchIfMissing = true)
public @interface ConditionalOnPolarisDiscoveryEnabled {
}

@ -24,6 +24,7 @@ import com.tencent.polaris.api.core.ProviderAPI;
import com.tencent.polaris.api.exception.PolarisException;
import com.tencent.polaris.client.api.SDKContext;
import com.tencent.polaris.factory.api.DiscoveryAPIFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.context.annotation.Bean;
@ -37,7 +38,8 @@ import org.springframework.context.annotation.Import;
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnPolarisDiscoveryEnabled
@Import({PolarisDiscoveryClientConfiguration.class, PolarisReactiveDiscoveryClientConfiguration.class})
@Import({ PolarisDiscoveryClientConfiguration.class,
PolarisReactiveDiscoveryClientConfiguration.class })
public class PolarisDiscoveryAutoConfiguration {
@Bean
@ -48,13 +50,15 @@ public class PolarisDiscoveryAutoConfiguration {
@Bean(name = "polarisProvider")
@ConditionalOnMissingBean
public ProviderAPI polarisProvider(SDKContext polarisContext) throws PolarisException {
public ProviderAPI polarisProvider(SDKContext polarisContext)
throws PolarisException {
return DiscoveryAPIFactory.createProviderAPIByContext(polarisContext);
}
@Bean(name = "polarisConsumer")
@ConditionalOnMissingBean
public ConsumerAPI polarisConsumer(SDKContext polarisContext) throws PolarisException {
public ConsumerAPI polarisConsumer(SDKContext polarisContext)
throws PolarisException {
return DiscoveryAPIFactory.createConsumerAPIByContext(polarisContext);
}
@ -66,7 +70,9 @@ public class PolarisDiscoveryAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public PolarisServiceDiscovery polarisServiceDiscovery(PolarisDiscoveryHandler polarisDiscoveryHandler) {
public PolarisServiceDiscovery polarisServiceDiscovery(
PolarisDiscoveryHandler polarisDiscoveryHandler) {
return new PolarisServiceDiscovery(polarisDiscoveryHandler);
}
}

@ -18,6 +18,7 @@
package com.tencent.cloud.polaris.discovery;
import java.util.List;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;

@ -33,13 +33,14 @@ import org.springframework.context.annotation.Configuration;
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnBlockingDiscoveryEnabled
@AutoConfigureBefore({SimpleDiscoveryClientAutoConfiguration.class,
CommonsClientAutoConfiguration.class})
@AutoConfigureBefore({ SimpleDiscoveryClientAutoConfiguration.class,
CommonsClientAutoConfiguration.class })
@AutoConfigureAfter(PolarisDiscoveryAutoConfiguration.class)
public class PolarisDiscoveryClientConfiguration {
@Bean
public DiscoveryClient polarisDiscoveryClient(PolarisServiceDiscovery polarisServiceDiscovery) {
public DiscoveryClient polarisDiscoveryClient(
PolarisServiceDiscovery polarisServiceDiscovery) {
return new PolarisDiscoveryClient(polarisServiceDiscovery);
}

@ -0,0 +1,113 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.polaris.discovery;
import java.util.Map;
import com.tencent.cloud.metadata.constant.MetadataConstant.SystemMetadataKey;
import com.tencent.cloud.metadata.context.MetadataContextHolder;
import com.tencent.cloud.polaris.PolarisProperties;
import com.tencent.polaris.api.core.ConsumerAPI;
import com.tencent.polaris.api.core.ProviderAPI;
import com.tencent.polaris.api.pojo.ServiceInfo;
import com.tencent.polaris.api.rpc.GetAllInstancesRequest;
import com.tencent.polaris.api.rpc.GetInstancesRequest;
import com.tencent.polaris.api.rpc.GetServicesRequest;
import com.tencent.polaris.api.rpc.InstancesResponse;
import com.tencent.polaris.api.rpc.ServicesResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* Discovery Handler for Polaris.
*
* @author Haotian Zhang, Andrew Shan, Jie Cheng
*/
@Component
public class PolarisDiscoveryHandler {
@Autowired
private PolarisProperties polarisProperties;
@Autowired
private ProviderAPI providerAPI;
@Autowired
private ConsumerAPI polarisConsumer;
/**
* Get a list of instances after service routing.
* @param service service name
* @return list of instances
*/
public InstancesResponse getFilteredInstances(String service) {
String namespace = polarisProperties.getNamespace();
GetInstancesRequest getInstancesRequest = new GetInstancesRequest();
getInstancesRequest.setNamespace(namespace);
getInstancesRequest.setService(service);
String method = MetadataContextHolder.get()
.getSystemMetadata(SystemMetadataKey.PEER_PATH);
getInstancesRequest.setMethod(method);
String localNamespace = MetadataContextHolder.get()
.getSystemMetadata(SystemMetadataKey.LOCAL_NAMESPACE);
String localService = MetadataContextHolder.get()
.getSystemMetadata(SystemMetadataKey.LOCAL_SERVICE);
Map<String, String> allTransitiveCustomMetadata = MetadataContextHolder.get()
.getAllTransitiveCustomMetadata();
if (StringUtils.isNotBlank(localNamespace) || StringUtils.isNotBlank(localService)
|| null != allTransitiveCustomMetadata) {
ServiceInfo sourceService = new ServiceInfo();
sourceService.setNamespace(localNamespace);
sourceService.setService(localService);
sourceService.setMetadata(allTransitiveCustomMetadata);
getInstancesRequest.setServiceInfo(sourceService);
}
return polarisConsumer.getInstances(getInstancesRequest);
}
/**
* Return all instances for the given service.
* @param service serviceName
* @return list of instances
*/
public InstancesResponse getInstances(String service) {
String namespace = polarisProperties.getNamespace();
GetAllInstancesRequest request = new GetAllInstancesRequest();
request.setNamespace(namespace);
request.setService(service);
return polarisConsumer.getAllInstance(request);
}
public ProviderAPI getProviderAPI() {
return providerAPI;
}
/**
* Return all service for given namespace.
* @return service list
*/
public ServicesResponse GetServices() {
String namespace = polarisProperties.getNamespace();
GetServicesRequest request = new GetServicesRequest();
request.setNamespace(namespace);
return polarisConsumer.getServices(request);
}
}

@ -0,0 +1,71 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.polaris.discovery;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import com.tencent.cloud.common.pojo.PolarisServiceInstance;
import com.tencent.polaris.api.exception.PolarisException;
import com.tencent.polaris.api.pojo.Instance;
import com.tencent.polaris.api.pojo.ServiceInfo;
import com.tencent.polaris.api.pojo.ServiceInstances;
import com.tencent.polaris.api.rpc.InstancesResponse;
import org.springframework.cloud.client.ServiceInstance;
/**
* @author Haotian Zhang, Andrew Shan, Jie Cheng
*/
public class PolarisServiceDiscovery {
private final PolarisDiscoveryHandler polarisDiscoveryHandler;
public PolarisServiceDiscovery(PolarisDiscoveryHandler polarisDiscoveryHandler) {
this.polarisDiscoveryHandler = polarisDiscoveryHandler;
}
/**
* Return all instances for the given service.
* @param serviceId id of service
* @return list of instances
* @throws PolarisException polarisException
*/
public List<ServiceInstance> getInstances(String serviceId) throws PolarisException {
List<ServiceInstance> instances = new ArrayList<>();
InstancesResponse filteredInstances = polarisDiscoveryHandler
.getFilteredInstances(serviceId);
ServiceInstances serviceInstances = filteredInstances.toServiceInstances();
for (Instance instance : serviceInstances.getInstances()) {
instances.add(new PolarisServiceInstance(instance));
}
return instances;
}
/**
* Return the names of all services.
* @return list of service names
* @throws PolarisException polarisException
*/
public List<String> getServices() throws PolarisException {
return polarisDiscoveryHandler.GetServices().getServices().stream()
.map(ServiceInfo::getService).collect(Collectors.toList());
}
}

@ -0,0 +1,88 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.polaris.discovery.reactive;
import java.util.function.Function;
import com.tencent.cloud.polaris.discovery.PolarisServiceDiscovery;
import com.tencent.polaris.api.exception.PolarisException;
import org.reactivestreams.Publisher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
/**
* Reactive Discovery Client for Polaris.
*
* @author Haotian Zhang, Andrew Shan, Jie Cheng
*/
public class PolarisReactiveDiscoveryClient implements ReactiveDiscoveryClient {
private static final Logger log = LoggerFactory
.getLogger(PolarisReactiveDiscoveryClient.class);
private PolarisServiceDiscovery polarisServiceDiscovery;
public PolarisReactiveDiscoveryClient(
PolarisServiceDiscovery polarisServiceDiscovery) {
this.polarisServiceDiscovery = polarisServiceDiscovery;
}
@Override
public String description() {
return "Spring Cloud Polaris Reactive Discovery Client";
}
@Override
public Flux<ServiceInstance> getInstances(String serviceId) {
return Mono.justOrEmpty(serviceId).flatMapMany(loadInstancesFromPolaris())
.subscribeOn(Schedulers.boundedElastic());
}
private Function<String, Publisher<ServiceInstance>> loadInstancesFromPolaris() {
return serviceId -> {
try {
return Flux.fromIterable(polarisServiceDiscovery.getInstances(serviceId));
}
catch (PolarisException e) {
log.error("get service instance[{}] from polaris error!", serviceId, e);
return Flux.empty();
}
};
}
@Override
public Flux<String> getServices() {
return Flux.defer(() -> {
try {
return Flux.fromIterable(polarisServiceDiscovery.getServices());
}
catch (Exception e) {
log.error("get services from polaris server fail,", e);
return Flux.empty();
}
}).subscribeOn(Schedulers.boundedElastic());
}
}

@ -19,6 +19,7 @@ package com.tencent.cloud.polaris.discovery.reactive;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration;
import com.tencent.cloud.polaris.discovery.PolarisServiceDiscovery;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@ -35,9 +36,9 @@ import org.springframework.context.annotation.Configuration;
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnReactiveDiscoveryEnabled
@AutoConfigureAfter({PolarisDiscoveryAutoConfiguration.class,
ReactiveCompositeDiscoveryClientAutoConfiguration.class})
@AutoConfigureBefore({ReactiveCommonsClientAutoConfiguration.class})
@AutoConfigureAfter({ PolarisDiscoveryAutoConfiguration.class,
ReactiveCompositeDiscoveryClientAutoConfiguration.class })
@AutoConfigureBefore({ ReactiveCommonsClientAutoConfiguration.class })
public class PolarisReactiveDiscoveryClientConfiguration {
@Bean

@ -0,0 +1,98 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.polaris.registry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
import org.springframework.util.StringUtils;
/**
* @author Haotian Zhang, Andrew Shan, Jie Cheng
*/
public class PolarisAutoServiceRegistration
extends AbstractAutoServiceRegistration<Registration> {
private static final Logger log = LoggerFactory
.getLogger(PolarisAutoServiceRegistration.class);
private final PolarisRegistration registration;
public PolarisAutoServiceRegistration(ServiceRegistry<Registration> serviceRegistry,
AutoServiceRegistrationProperties autoServiceRegistrationProperties,
PolarisRegistration registration) {
super(serviceRegistry, autoServiceRegistrationProperties);
this.registration = registration;
}
@Override
protected PolarisRegistration getRegistration() {
if (this.registration.getPort() <= 0) {
this.registration.setPort(this.getPort().get());
}
return this.registration;
}
@Override
protected PolarisRegistration getManagementRegistration() {
return null;
}
@Override
protected void register() {
if (!this.registration.getPolarisProperties().isRegisterEnabled()) {
log.debug("Registration disabled.");
return;
}
if (this.registration.getPort() <= 0) {
this.registration.setPort(getPort().get());
}
super.register();
}
@Override
protected void registerManagement() {
if (!this.registration.getPolarisProperties().isRegisterEnabled()) {
return;
}
super.registerManagement();
}
@Override
protected Object getConfiguration() {
return this.registration.getPolarisProperties();
}
@Override
protected boolean isEnabled() {
return this.registration.getPolarisProperties().isRegisterEnabled();
}
@Override
@SuppressWarnings("deprecation")
protected String getAppName() {
String appName = registration.getPolarisProperties().getService();
return StringUtils.isEmpty(appName) ? super.getAppName() : appName;
}
}

@ -0,0 +1,93 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.polaris.registry;
import java.net.URI;
import java.util.Map;
import com.tencent.cloud.metadata.context.MetadataContextHolder;
import com.tencent.cloud.polaris.PolarisProperties;
import com.tencent.polaris.client.api.SDKContext;
import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.serviceregistry.Registration;
/**
* @author Haotian Zhang, Andrew Shan, Jie Cheng
*/
public class PolarisRegistration implements Registration, ServiceInstance {
private final PolarisProperties polarisProperties;
private final SDKContext polarisContext;
public PolarisRegistration(PolarisProperties polarisProperties, SDKContext context) {
this.polarisProperties = polarisProperties;
this.polarisContext = context;
}
@Override
public String getServiceId() {
return polarisProperties.getService();
}
@Override
public String getHost() {
if (StringUtils.isNotBlank(polarisProperties.getIpAddress())) {
return polarisProperties.getIpAddress();
}
return polarisContext.getConfig().getGlobal().getAPI().getBindIP();
}
@Override
public int getPort() {
return polarisProperties.getPort();
}
public void setPort(int port) {
this.polarisProperties.setPort(port);
}
@Override
public boolean isSecure() {
return StringUtils.equalsIgnoreCase(polarisProperties.getProtocol(), "https");
}
@Override
public URI getUri() {
return DefaultServiceInstance.getUri(this);
}
@Override
public Map<String, String> getMetadata() {
return MetadataContextHolder.get().getAllSystemMetadata();
}
public PolarisProperties getPolarisProperties() {
return polarisProperties;
}
@Override
public String toString() {
return "PolarisRegistration{" + "polarisProperties=" + polarisProperties
+ ", polarisContext=" + polarisContext + '}';
}
}

@ -0,0 +1,213 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.polaris.registry;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import com.tencent.cloud.metadata.config.MetadataLocalProperties;
import com.tencent.cloud.polaris.PolarisProperties;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler;
import com.tencent.polaris.api.core.ProviderAPI;
import com.tencent.polaris.api.exception.PolarisException;
import com.tencent.polaris.api.pojo.Instance;
import com.tencent.polaris.api.rpc.InstanceDeregisterRequest;
import com.tencent.polaris.api.rpc.InstanceHeartbeatRequest;
import com.tencent.polaris.api.rpc.InstanceRegisterRequest;
import com.tencent.polaris.api.rpc.InstancesResponse;
import com.tencent.polaris.client.util.NamedThreadFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
import org.springframework.util.StringUtils;
import static org.springframework.util.ReflectionUtils.rethrowRuntimeException;
/**
* @author Haotian Zhang, Andrew Shan, Jie Cheng
*/
public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
private static final Logger log = LoggerFactory
.getLogger(PolarisServiceRegistry.class);
private static final int ttl = 5;
private final PolarisProperties polarisProperties;
private final PolarisDiscoveryHandler polarisDiscoveryHandler;
private final MetadataLocalProperties metadataLocalProperties;
private final ScheduledExecutorService heartbeatExecutor;
public PolarisServiceRegistry(PolarisProperties polarisProperties,
PolarisDiscoveryHandler polarisDiscoveryHandler,
MetadataLocalProperties metadataLocalProperties) {
this.polarisProperties = polarisProperties;
this.polarisDiscoveryHandler = polarisDiscoveryHandler;
this.metadataLocalProperties = metadataLocalProperties;
if (polarisProperties.isHeartbeatEnabled()) {
ScheduledThreadPoolExecutor heartbeatExecutor = new ScheduledThreadPoolExecutor(
0, new NamedThreadFactory("spring-cloud-heartbeat"));
heartbeatExecutor.setMaximumPoolSize(1);
this.heartbeatExecutor = heartbeatExecutor;
}
else {
this.heartbeatExecutor = null;
}
}
@Override
public void register(Registration registration) {
if (StringUtils.isEmpty(registration.getServiceId())) {
log.warn("No service to register for polaris client...");
return;
}
// 注册实例
InstanceRegisterRequest instanceRegisterRequest = new InstanceRegisterRequest();
instanceRegisterRequest.setNamespace(polarisProperties.getNamespace());
instanceRegisterRequest.setService(registration.getServiceId());
instanceRegisterRequest.setHost(registration.getHost());
instanceRegisterRequest.setPort(registration.getPort());
instanceRegisterRequest.setToken(polarisProperties.getToken());
if (null != heartbeatExecutor) {
instanceRegisterRequest.setTtl(ttl);
}
instanceRegisterRequest.setMetadata(metadataLocalProperties.getContent());
instanceRegisterRequest.setProtocol(polarisProperties.getProtocol());
instanceRegisterRequest.setVersion(polarisProperties.getVersion());
try {
ProviderAPI providerClient = polarisDiscoveryHandler.getProviderAPI();
providerClient.register(instanceRegisterRequest);
log.info("polaris registry, {} {} {}:{} {} register finished",
polarisProperties.getNamespace(), registration.getServiceId(),
registration.getHost(), registration.getPort(),
metadataLocalProperties.getContent());
if (null != heartbeatExecutor) {
InstanceHeartbeatRequest heartbeatRequest = new InstanceHeartbeatRequest();
BeanUtils.copyProperties(instanceRegisterRequest, heartbeatRequest);
// 注册成功后开始启动心跳线程
heartbeat(heartbeatRequest);
}
}
catch (Exception e) {
log.error("polaris registry, {} register failed...{},",
registration.getServiceId(), registration, e);
rethrowRuntimeException(e);
}
}
@Override
public void deregister(Registration registration) {
log.info("De-registering from Polaris Server now...");
if (StringUtils.isEmpty(registration.getServiceId())) {
log.warn("No dom to de-register for polaris client...");
return;
}
InstanceDeregisterRequest deRegisterRequest = new InstanceDeregisterRequest();
deRegisterRequest.setToken(polarisProperties.getToken());
deRegisterRequest.setNamespace(polarisProperties.getNamespace());
deRegisterRequest.setService(registration.getServiceId());
deRegisterRequest.setHost(registration.getHost());
deRegisterRequest.setPort(registration.getPort());
try {
ProviderAPI providerClient = polarisDiscoveryHandler.getProviderAPI();
providerClient.deRegister(deRegisterRequest);
}
catch (Exception e) {
log.error("ERR_POLARIS_DEREGISTER, de-register failed...{},", registration,
e);
}
finally {
if (null != heartbeatExecutor) {
heartbeatExecutor.shutdown();
}
}
log.info("De-registration finished.");
}
@Override
public void close() {
}
@Override
public void setStatus(Registration registration, String status) {
}
@Override
public Object getStatus(Registration registration) {
String serviceName = registration.getServiceId();
InstancesResponse instancesResponse = polarisDiscoveryHandler
.getInstances(serviceName);
Instance[] instances = instancesResponse.getInstances();
if (null == instances || instances.length == 0) {
return null;
}
for (Instance instance : instances) {
if (instance.getHost().equalsIgnoreCase(registration.getHost())
&& instance.getPort() == polarisProperties.getPort()) {
return instance.isHealthy() ? "UP" : "DOWN";
}
}
return null;
}
/**
* Start the heartbeat thread.
* @param heartbeatRequest heartbeat request
*/
public void heartbeat(InstanceHeartbeatRequest heartbeatRequest) {
heartbeatExecutor.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
try {
// String healthCheckUrl = String.format("http://%s:%s%s",
// heartbeatRequest.getHost(), heartbeatRequest.getPort(),
// polarisProperties.getHealthCheckUrl());
// //先判断是否配置了health-check-url如果配置了需要先进行服务实例健康检查如果健康检查通过则进行心跳上报如果不通过则不上报心跳
// if (Strings.isNotEmpty(healthCheckUrl) &&
// !OkHttpUtil.get(healthCheckUrl, null)) {
// log.error("polaris health check failed");
// return;
// }
polarisDiscoveryHandler.getProviderAPI().heartbeat(heartbeatRequest);
}
catch (PolarisException e) {
log.error("polaris heartbeat[{}]", e.getCode(), e);
}
catch (Exception e) {
log.error("polaris heartbeat runtime error", e);
}
}
}, 0, ttl, TimeUnit.SECONDS);
}
}

@ -19,10 +19,11 @@ package com.tencent.cloud.polaris.registry;
import com.tencent.cloud.metadata.config.MetadataLocalProperties;
import com.tencent.cloud.polaris.PolarisProperties;
import com.tencent.polaris.client.api.SDKContext;
import com.tencent.cloud.polaris.discovery.ConditionalOnPolarisDiscoveryEnabled;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler;
import com.tencent.polaris.client.api.SDKContext;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@ -39,17 +40,20 @@ import org.springframework.context.annotation.Configuration;
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties
@ConditionalOnPolarisDiscoveryEnabled
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true)
@AutoConfigureAfter({AutoServiceRegistrationConfiguration.class,
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled",
matchIfMissing = true)
@AutoConfigureAfter({ AutoServiceRegistrationConfiguration.class,
AutoServiceRegistrationAutoConfiguration.class,
PolarisDiscoveryAutoConfiguration.class})
PolarisDiscoveryAutoConfiguration.class })
public class PolarisServiceRegistryAutoConfiguration {
@Bean
public PolarisServiceRegistry polarisServiceRegistry(PolarisProperties polarisProperties,
public PolarisServiceRegistry polarisServiceRegistry(
PolarisProperties polarisProperties,
PolarisDiscoveryHandler polarisDiscoveryHandler,
MetadataLocalProperties metadataLocalProperties) {
return new PolarisServiceRegistry(polarisProperties, polarisDiscoveryHandler, metadataLocalProperties);
return new PolarisServiceRegistry(polarisProperties, polarisDiscoveryHandler,
metadataLocalProperties);
}
@Bean
@ -61,8 +65,10 @@ public class PolarisServiceRegistryAutoConfiguration {
@Bean
@ConditionalOnBean(AutoServiceRegistrationProperties.class)
public PolarisAutoServiceRegistration polarisAutoServiceRegistration(PolarisServiceRegistry registry,
AutoServiceRegistrationProperties autoServiceRegistrationProperties, PolarisRegistration registration) {
public PolarisAutoServiceRegistration polarisAutoServiceRegistration(
PolarisServiceRegistry registry,
AutoServiceRegistrationProperties autoServiceRegistrationProperties,
PolarisRegistration registration) {
return new PolarisAutoServiceRegistration(registry,
autoServiceRegistrationProperties, registration);
}

@ -18,6 +18,7 @@
package com.tencent.cloud.polaris.ribbon;
import com.tencent.cloud.polaris.discovery.ConditionalOnPolarisDiscoveryEnabled;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration;

@ -21,6 +21,7 @@ import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -33,10 +34,12 @@ public class PolarisRibbonServerListConfiguration {
@Bean
@ConditionalOnMissingBean
public ServerList<Server> ribbonServerList(PolarisDiscoveryHandler polarisDiscoveryHandler,
public ServerList<Server> ribbonServerList(
PolarisDiscoveryHandler polarisDiscoveryHandler,
IClientConfig iClientConfig) {
PolarisServerList serverList = new PolarisServerList(polarisDiscoveryHandler);
serverList.initWithNiwsConfig(iClientConfig);
return serverList;
}
}

@ -17,17 +17,17 @@
package com.tencent.cloud.polaris.ribbon;
import java.util.ArrayList;
import java.util.List;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractServerList;
import com.netflix.loadbalancer.Server;
import com.tencent.cloud.polaris.pojo.PolarisServer;
import com.tencent.cloud.common.pojo.PolarisServer;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler;
import com.tencent.polaris.api.pojo.Instance;
import com.tencent.polaris.api.pojo.ServiceInstances;
import com.tencent.polaris.api.rpc.InstancesResponse;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler;
import java.util.ArrayList;
import java.util.List;
/**
* @author Haotian Zhang, Andrew Shan, Jie Cheng
@ -53,7 +53,8 @@ public class PolarisServerList extends AbstractServerList<Server> {
}
private List<Server> getServers() {
InstancesResponse filteredInstances = polarisDiscoveryHandler.getFilteredInstances(serviceId);
InstancesResponse filteredInstances = polarisDiscoveryHandler
.getFilteredInstances(serviceId);
ServiceInstances serviceInstances = filteredInstances.toServiceInstances();
List<Server> polarisServers = new ArrayList<>();
for (Instance instance : serviceInstances.getInstances()) {

@ -0,0 +1,99 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.polaris.util;
import java.util.Map;
import java.util.Objects;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* okhttp util.
*
* @author kan peng
*/
public final class OkHttpUtil {
/**
* Logger.
*/
public final static Logger logger = LoggerFactory.getLogger(OkHttpUtil.class);
/**
* JSON format.
*/
public static final MediaType MEDIA_TYPE_JSON = MediaType
.parse("application/json; charset=utf-8");
/**
* client.
*/
private final static OkHttpClient HTTP_CLIENT = new OkHttpClient();
private OkHttpUtil() {
}
/**
* get request.
* @param url url
* @param headers headers
* @return response
*/
public static boolean get(String url, Map<String, String> headers) {
try {
Request.Builder builder = new Request.Builder();
buildHeader(builder, headers);
Request request = builder.url(url).build();
Response response = HTTP_CLIENT.newCall(request).execute();
if (response.isSuccessful() && Objects.nonNull(response.body())) {
String result = response.body().string();
logger.info("exec get request, url: {} successresponse data: {}", url,
result);
return true;
}
}
catch (Exception e) {
logger.error("exec get requesturl: {} failed!", url, e);
}
return false;
}
/**
* build header.
* @param builder builder
* @param headers headers
*/
private static void buildHeader(Request.Builder builder,
Map<String, String> headers) {
if (Objects.nonNull(headers) && headers.size() > 0) {
headers.forEach((k, v) -> {
if (Objects.nonNull(k) && Objects.nonNull(v)) {
builder.addHeader(k, v);
}
});
}
}
}

@ -2,4 +2,3 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration,\
com.tencent.cloud.polaris.ribbon.PolarisDiscoveryRibbonAutoConfiguration,\
com.tencent.cloud.polaris.registry.PolarisServiceRegistryAutoConfiguration

@ -17,21 +17,27 @@
package com.tencent.cloud.polaris;
import org.junit.Test;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.cloud.commons.util.InetUtilsProperties;
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
import org.junit.Test;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.cloud.commons.util.InetUtilsProperties;
/**
* Test for {@link PolarisProperties}
*
* @author Haotian Zhang
*/
public class PolarisPropertiesTest {
@Test
public void testInitAndGetSet() {
PolarisProperties temp = new PolarisProperties(new InetUtils(new InetUtilsProperties()));
PolarisProperties temp = new PolarisProperties(
new InetUtils(new InetUtilsProperties()));
try {
temp.setNamespace(NAMESPACE_TEST);
temp.getNamespace();
@ -44,9 +50,11 @@ public class PolarisPropertiesTest {
temp.init();
assertThat(temp).isNotNull();
} catch (Exception e) {
}
catch (Exception e) {
fail();
e.printStackTrace();
}
}
}

@ -17,10 +17,6 @@
package com.tencent.cloud.polaris.discovery;
import static com.tencent.polaris.test.common.Consts.PORT;
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
import static org.assertj.core.api.Assertions.assertThat;
import com.tencent.cloud.polaris.PolarisProperties;
import com.tencent.cloud.polaris.context.PolarisContextConfiguration;
import com.tencent.polaris.api.core.ConsumerAPI;
@ -29,19 +25,28 @@ import com.tencent.polaris.test.mock.discovery.NamingServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration;
import static com.tencent.polaris.test.common.Consts.PORT;
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test for {@link PolarisDiscoveryAutoConfiguration}
*
* @author Haotian Zhang
*/
public class PolarisDiscoveryAutoConfigurationTest {
private static NamingServer namingServer;
private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
PolarisContextConfiguration.class,
.withConfiguration(AutoConfigurations.of(PolarisContextConfiguration.class,
PolarisDiscoveryAutoConfiguration.class,
PolarisDiscoveryClientConfiguration.class))
.withPropertyValues("spring.application.name=" + SERVICE_PROVIDER)

@ -17,28 +17,33 @@
package com.tencent.cloud.polaris.discovery;
import static com.tencent.polaris.test.common.Consts.PORT;
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
import static org.assertj.core.api.Assertions.assertThat;
import com.tencent.cloud.polaris.context.PolarisContextConfiguration;
import com.tencent.polaris.test.mock.discovery.NamingServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration;
import static com.tencent.polaris.test.common.Consts.PORT;
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test for {@link PolarisDiscoveryClientConfiguration}
*
* @author Haotian Zhang
*/
public class PolarisDiscoveryClientConfigurationTest {
private static NamingServer namingServer;
private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
PolarisContextConfiguration.class,
.withConfiguration(AutoConfigurations.of(PolarisContextConfiguration.class,
PolarisDiscoveryClientConfiguration.class))
.withPropertyValues("spring.application.name=" + SERVICE_PROVIDER)
.withPropertyValues("server.port=" + PORT)
@ -58,18 +63,23 @@ public class PolarisDiscoveryClientConfigurationTest {
@Test
public void testDefaultInitialization() {
this.contextRunner.run(context -> assertThat(context).hasSingleBean(PolarisDiscoveryClient.class));
this.contextRunner.run(context -> assertThat(context)
.hasSingleBean(PolarisDiscoveryClient.class));
}
@Test
public void testDiscoveryBlockingDisabled() {
this.contextRunner.withPropertyValues("spring.cloud.discovery.blocking.enabled=false")
.run(context -> assertThat(context).doesNotHaveBean(PolarisDiscoveryClient.class));
this.contextRunner
.withPropertyValues("spring.cloud.discovery.blocking.enabled=false")
.run(context -> assertThat(context)
.doesNotHaveBean(PolarisDiscoveryClient.class));
}
@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
static class PolarisDiscoveryClientConfiguration {
}
}

@ -17,24 +17,30 @@
package com.tencent.cloud.polaris.discovery;
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.List;
import com.tencent.cloud.polaris.pojo.PolarisServiceInstance;
import com.tencent.cloud.common.pojo.PolarisServiceInstance;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.cloud.client.ServiceInstance;
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Test for {@link PolarisDiscoveryClient}
*
* @author Haotian Zhang
*/
@RunWith(PowerMockRunner.class)
@PowerMockIgnore("javax.management.*")
public class PolarisDiscoveryClientTest {
@ -48,7 +54,8 @@ public class PolarisDiscoveryClientTest {
@Test
public void testGetInstances() {
when(polarisServiceDiscovery.getInstances(anyString())).thenReturn(singletonList(mock(PolarisServiceInstance.class)));
when(polarisServiceDiscovery.getInstances(anyString()))
.thenReturn(singletonList(mock(PolarisServiceInstance.class)));
List<ServiceInstance> serviceInstances = client.getInstances(SERVICE_PROVIDER);
@ -58,11 +65,13 @@ public class PolarisDiscoveryClientTest {
@Test
public void testGetServices() {
when(polarisServiceDiscovery.getServices()).thenReturn(singletonList(SERVICE_PROVIDER));
when(polarisServiceDiscovery.getServices())
.thenReturn(singletonList(SERVICE_PROVIDER));
List<String> services = client.getServices();
assertThat(services).contains(SERVICE_PROVIDER).size().isEqualTo(1);
}
}

@ -0,0 +1,118 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.polaris.discovery;
import java.util.List;
import com.tencent.cloud.polaris.context.PolarisContextConfiguration;
import com.tencent.polaris.api.exception.PolarisException;
import com.tencent.polaris.api.pojo.ServiceKey;
import com.tencent.polaris.test.mock.discovery.NamingServer;
import com.tencent.polaris.test.mock.discovery.NamingService;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration;
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
import static com.tencent.polaris.test.common.Consts.PORT;
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test for {@link PolarisServiceDiscovery}
*
* @author Haotian Zhang
*/
public class PolarisServiceDiscoveryTest {
private static NamingServer namingServer;
private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(PolarisContextConfiguration.class,
PolarisServiceDiscoveryTest.PolarisPropertiesConfiguration.class,
PolarisDiscoveryClientConfiguration.class,
PolarisDiscoveryAutoConfiguration.class))
.withPropertyValues("spring.application.name=" + SERVICE_PROVIDER)
.withPropertyValues("server.port=" + PORT)
.withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081")
.withPropertyValues(
"spring.cloud.polaris.discovery.namespace=" + NAMESPACE_TEST)
.withPropertyValues("spring.cloud.polaris.discovery.token=xxxxxx");
@BeforeClass
public static void beforeClass() throws Exception {
namingServer = NamingServer.startNamingServer(10081);
// add service with 3 instances
NamingService.InstanceParameter instanceParameter = new NamingService.InstanceParameter();
instanceParameter.setHealthy(true);
instanceParameter.setIsolated(false);
instanceParameter.setWeight(100);
ServiceKey serviceKey = new ServiceKey(NAMESPACE_TEST, SERVICE_PROVIDER);
namingServer.getNamingService().batchAddInstances(serviceKey, PORT, 3,
instanceParameter);
}
@AfterClass
public static void afterClass() throws Exception {
if (null != namingServer) {
namingServer.terminate();
}
}
@Test
public void testGetInstances() {
this.contextRunner.run(context -> {
PolarisServiceDiscovery polarisServiceDiscovery = context
.getBean(PolarisServiceDiscovery.class);
List<ServiceInstance> serviceInstances = polarisServiceDiscovery
.getInstances(SERVICE_PROVIDER);
assertThat(serviceInstances.isEmpty()).isFalse();
assertThat(serviceInstances).hasSize(3);
assertThat(serviceInstances.get(0).getPort()).isEqualTo(PORT);
assertThat(serviceInstances.get(1).getPort()).isEqualTo(PORT + 1);
assertThat(serviceInstances.get(2).getPort()).isEqualTo(PORT + 2);
});
}
@Test
public void testGetServices() throws PolarisException {
this.contextRunner.run(context -> {
PolarisServiceDiscovery polarisServiceDiscovery = context
.getBean(PolarisServiceDiscovery.class);
List<String> services = polarisServiceDiscovery.getServices();
assertThat(services.size()).isEqualTo(1);
});
}
@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
static class PolarisPropertiesConfiguration {
}
}

@ -17,29 +17,34 @@
package com.tencent.cloud.polaris.discovery.reactive;
import static com.tencent.polaris.test.common.Consts.PORT;
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
import static org.assertj.core.api.Assertions.assertThat;
import com.tencent.cloud.polaris.context.PolarisContextConfiguration;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClientConfiguration;
import com.tencent.polaris.test.mock.discovery.NamingServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration;
import static com.tencent.polaris.test.common.Consts.PORT;
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test for {@link PolarisReactiveDiscoveryClientConfiguration}
*
* @author Haotian Zhang
*/
public class PolarisReactiveDiscoveryClientConfigurationTest {
private static NamingServer namingServer;
private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
PolarisContextConfiguration.class,
.withConfiguration(AutoConfigurations.of(PolarisContextConfiguration.class,
PolarisReactiveDiscoveryClientConfiguration.class,
PolarisDiscoveryClientConfiguration.class))
.withPropertyValues("spring.application.name=" + SERVICE_PROVIDER)
@ -64,10 +69,11 @@ public class PolarisReactiveDiscoveryClientConfigurationTest {
.hasSingleBean(PolarisReactiveDiscoveryClient.class));
}
@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
static class PolarisReactiveDiscoveryClientConfiguration {
}
}

@ -17,25 +17,30 @@
package com.tencent.cloud.polaris.discovery.reactive;
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
import static java.util.Collections.singletonList;
import static org.mockito.Mockito.when;
import com.tencent.polaris.api.exception.PolarisException;
import com.tencent.cloud.polaris.discovery.PolarisServiceDiscovery;
import java.util.Arrays;
import com.tencent.cloud.polaris.discovery.PolarisServiceDiscovery;
import com.tencent.polaris.api.exception.PolarisException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.cloud.client.ServiceInstance;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
import org.springframework.cloud.client.ServiceInstance;
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
import static java.util.Collections.singletonList;
import static org.mockito.Mockito.when;
/**
* Test for {@link PolarisReactiveDiscoveryClient}
*
* @author Haotian Zhang
*/
@RunWith(PowerMockRunner.class)
@PowerMockIgnore("javax.management.*")
public class PolarisReactiveDiscoveryClientTest {
@ -52,7 +57,8 @@ public class PolarisReactiveDiscoveryClientTest {
@Test
public void testGetInstances() throws PolarisException {
when(serviceDiscovery.getInstances(SERVICE_PROVIDER)).thenReturn(singletonList(serviceInstance));
when(serviceDiscovery.getInstances(SERVICE_PROVIDER))
.thenReturn(singletonList(serviceInstance));
Flux<ServiceInstance> instances = this.client.getInstances(SERVICE_PROVIDER);
@ -62,12 +68,14 @@ public class PolarisReactiveDiscoveryClientTest {
@Test
public void testGetServices() throws PolarisException {
when(serviceDiscovery.getServices()).thenReturn(Arrays.asList(SERVICE_PROVIDER + 1, SERVICE_PROVIDER + 2));
when(serviceDiscovery.getServices())
.thenReturn(Arrays.asList(SERVICE_PROVIDER + 1, SERVICE_PROVIDER + 2));
Flux<String> services = this.client.getServices();
StepVerifier.create(services).expectNext(SERVICE_PROVIDER + 1, SERVICE_PROVIDER + 2)
.expectComplete().verify();
StepVerifier.create(services)
.expectNext(SERVICE_PROVIDER + 1, SERVICE_PROVIDER + 2).expectComplete()
.verify();
}
}

@ -19,15 +19,12 @@ package com.tencent.cloud.polaris.registry;
import com.tencent.cloud.polaris.context.PolarisContextConfiguration;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration;
import static com.tencent.polaris.test.common.Consts.PORT;
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
import static org.assertj.core.api.Assertions.assertThat;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClientConfiguration;
import com.tencent.polaris.test.mock.discovery.NamingServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
@ -35,13 +32,21 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import static com.tencent.polaris.test.common.Consts.PORT;
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test for {@link PolarisServiceRegistryAutoConfiguration}
*
* @author Haotian Zhang
*/
public class PolarisServiceRegistryAutoConfigurationTest {
private static NamingServer namingServer;
private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
PolarisContextConfiguration.class,
.withConfiguration(AutoConfigurations.of(PolarisContextConfiguration.class,
PolarisServiceRegistryAutoConfiguration.class,
PolarisDiscoveryClientConfiguration.class))
.withPropertyValues("spring.application.name=" + SERVICE_PROVIDER)
@ -64,7 +69,8 @@ public class PolarisServiceRegistryAutoConfigurationTest {
public void testDefaultInitialization() {
this.contextRunner.run(context -> {
assertThat(context).hasSingleBean(PolarisDiscoveryAutoConfiguration.class);
assertThat(context).hasSingleBean(AutoServiceRegistrationAutoConfiguration.class);
assertThat(context)
.hasSingleBean(AutoServiceRegistrationAutoConfiguration.class);
});
}
@ -74,4 +80,5 @@ public class PolarisServiceRegistryAutoConfigurationTest {
static class PolarisServiceRegistryAutoConfiguration {
}
}

@ -0,0 +1,120 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.polaris.registry;
import com.tencent.cloud.polaris.context.PolarisContextConfiguration;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClientConfiguration;
import com.tencent.polaris.api.pojo.ServiceKey;
import com.tencent.polaris.test.mock.discovery.NamingServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration;
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
import static com.tencent.polaris.test.common.Consts.PORT;
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.when;
/**
* Test for {@link PolarisServiceRegistry}
*
* @author Haotian Zhang
*/
public class PolarisServiceRegistryTest {
private static NamingServer namingServer;
private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(PolarisContextConfiguration.class,
PolarisPropertiesConfiguration.class,
PolarisDiscoveryClientConfiguration.class,
PolarisDiscoveryAutoConfiguration.class))
.withPropertyValues("spring.application.name=" + SERVICE_PROVIDER)
.withPropertyValues("server.port=" + PORT)
.withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081")
.withPropertyValues(
"spring.cloud.polaris.discovery.namespace=" + NAMESPACE_TEST)
.withPropertyValues("spring.cloud.polaris.discovery.token=xxxxxx");
@BeforeClass
public static void beforeClass() throws Exception {
namingServer = NamingServer.startNamingServer(10081);
// add service
namingServer.getNamingService()
.addService(new ServiceKey(NAMESPACE_TEST, SERVICE_PROVIDER));
}
@AfterClass
public static void afterClass() throws Exception {
if (null != namingServer) {
namingServer.terminate();
}
}
@Test
public void testRegister() {
this.contextRunner.run(context -> {
PolarisServiceRegistry registry = context
.getBean(PolarisServiceRegistry.class);
PolarisRegistration registration = Mockito.mock(PolarisRegistration.class);
when(registration.getHost()).thenReturn("127.0.0.1");
when(registration.getPort()).thenReturn(PORT);
when(registration.getServiceId()).thenReturn(SERVICE_PROVIDER);
try {
registry.register(registration);
}
catch (Exception e) {
fail();
}
try {
assertThat(registry.getStatus(registration)).isEqualTo("DOWN");
}
catch (Exception e) {
fail();
}
try {
registry.deregister(registration);
}
catch (Exception e) {
fail();
}
});
}
@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
static class PolarisPropertiesConfiguration {
}
}

@ -17,16 +17,12 @@
package com.tencent.cloud.polaris.ribbon;
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
import static com.tencent.polaris.test.common.Consts.PORT;
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
import static org.assertj.core.api.Assertions.assertThat;
import com.netflix.client.config.DefaultClientConfigImpl;
import com.netflix.client.config.IClientConfig;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClientConfiguration;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
@ -36,22 +32,33 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
public class PolarisRibbonServerListAutoConfigurationTest {
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
import static com.tencent.polaris.test.common.Consts.PORT;
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test for {@link PolarisRibbonServerListConfiguration}
*
* @author Haotian Zhang
*/
public class PolarisRibbonServerListConfigurationTest {
private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
PolarisRibbonClientTest.class,
.withConfiguration(AutoConfigurations.of(PolarisRibbonClientTest.class,
PolarisDiscoveryClientConfiguration.class))
.withPropertyValues("spring.application.name=" + SERVICE_PROVIDER)
.withPropertyValues("server.port=" + PORT)
.withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081")
.withPropertyValues("spring.cloud.polaris.discovery.namespace=" + NAMESPACE_TEST)
.withPropertyValues(
"spring.cloud.polaris.discovery.namespace=" + NAMESPACE_TEST)
.withPropertyValues("spring.cloud.polaris.discovery.token=xxxxxx");
@Test
public void testProperties() {
this.contextRunner.run(context -> {
PolarisDiscoveryHandler discoveryHandler = context.getBean(PolarisDiscoveryHandler.class);
PolarisDiscoveryHandler discoveryHandler = context
.getBean(PolarisDiscoveryHandler.class);
PolarisServerList serverList = new PolarisServerList(discoveryHandler);
IClientConfig iClientConfig = context.getBean(IClientConfig.class);
serverList.initWithNiwsConfig(iClientConfig);

@ -0,0 +1,145 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.polaris.ribbon;
import java.util.List;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.Server;
import com.tencent.cloud.polaris.context.PolarisContextConfiguration;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClientConfiguration;
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler;
import com.tencent.polaris.api.pojo.ServiceKey;
import com.tencent.polaris.test.mock.discovery.NamingServer;
import com.tencent.polaris.test.mock.discovery.NamingService;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration;
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
import static com.tencent.polaris.test.common.Consts.PORT;
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Test for {@link PolarisServerList}
*
* @author Haotian Zhang
*/
public class PolarisServerListTest {
private static NamingServer namingServer;
private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(PolarisContextConfiguration.class,
PolarisServerListTest.PolarisPropertiesConfiguration.class,
PolarisDiscoveryClientConfiguration.class,
PolarisDiscoveryAutoConfiguration.class))
.withPropertyValues("spring.application.name=" + SERVICE_PROVIDER)
.withPropertyValues("server.port=" + PORT)
.withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081")
.withPropertyValues(
"spring.cloud.polaris.discovery.namespace=" + NAMESPACE_TEST)
.withPropertyValues("spring.cloud.polaris.discovery.token=xxxxxx");
@BeforeClass
public static void beforeClass() throws Exception {
namingServer = NamingServer.startNamingServer(10081);
// add service
namingServer.getNamingService()
.addService(new ServiceKey(NAMESPACE_TEST, SERVICE_PROVIDER));
}
@AfterClass
public static void afterClass() throws Exception {
if (null != namingServer) {
namingServer.terminate();
}
}
/**
* Test {@link PolarisServerList#getInitialListOfServers()} with empty server list.
*/
@Test
@SuppressWarnings("unchecked")
public void test1() {
this.contextRunner.run(context -> {
// mock
IClientConfig iClientConfig = mock(IClientConfig.class);
when(iClientConfig.getClientName()).thenReturn(SERVICE_PROVIDER);
PolarisDiscoveryHandler polarisDiscoveryHandler = context
.getBean(PolarisDiscoveryHandler.class);
PolarisServerList serverList = new PolarisServerList(polarisDiscoveryHandler);
serverList.initWithNiwsConfig(iClientConfig);
List<Server> servers = serverList.getInitialListOfServers();
assertThat(servers).isEmpty();
});
}
/**
* Test {@link PolarisServerList#getUpdatedListOfServers()} with server list of size
* 3.
*/
@Test
@SuppressWarnings("unchecked")
public void test2() throws Exception {
this.contextRunner.run(context -> {
// mock
IClientConfig iClientConfig = mock(IClientConfig.class);
when(iClientConfig.getClientName()).thenReturn(SERVICE_PROVIDER);
PolarisDiscoveryHandler polarisDiscoveryHandler = context
.getBean(PolarisDiscoveryHandler.class);
PolarisServerList serverList = new PolarisServerList(polarisDiscoveryHandler);
serverList.initWithNiwsConfig(iClientConfig);
// add service with 3 instances
NamingService.InstanceParameter instanceParameter = new NamingService.InstanceParameter();
instanceParameter.setHealthy(true);
instanceParameter.setIsolated(false);
instanceParameter.setWeight(100);
ServiceKey serviceKey = new ServiceKey(NAMESPACE_TEST, SERVICE_PROVIDER);
namingServer.getNamingService().batchAddInstances(serviceKey, PORT, 3,
instanceParameter);
List<Server> servers = serverList.getUpdatedListOfServers();
assertThat(servers).hasSize(3);
assertThat(servers.get(0).getPort()).isEqualTo(PORT);
assertThat(servers.get(1).getPort()).isEqualTo(PORT + 1);
assertThat(servers.get(2).getPort()).isEqualTo(PORT + 2);
});
}
@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
static class PolarisPropertiesConfiguration {
}
}

@ -0,0 +1,94 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-tencent</artifactId>
<groupId>com.tencent.cloud</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-starter-tencent-polaris-ratelimit</artifactId>
<name>Spring Cloud Starter Tencent Polaris Ratelimit</name>
<dependencies>
<!-- Spring Cloud Tencent dependencies start -->
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-commons</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-polaris-context</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-commons</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-metadata</artifactId>
</dependency>
<!-- Spring Cloud Tencent dependencies end -->
<!-- Polaris dependencies start -->
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-ratelimit-factory</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-test-common</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-test-mock-discovery</artifactId>
<scope>test</scope>
</dependency>
<!-- Polaris dependencies end -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

@ -15,18 +15,15 @@
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.polaris.ratelimit;
package com.tencent.cloud.polaris.ratelimit.config;
import static javax.servlet.DispatcherType.ASYNC;
import static javax.servlet.DispatcherType.ERROR;
import static javax.servlet.DispatcherType.FORWARD;
import static javax.servlet.DispatcherType.INCLUDE;
import static javax.servlet.DispatcherType.REQUEST;
import com.tencent.cloud.polaris.ratelimit.callee.QuotaCheckFilter;
import com.tencent.cloud.polaris.ratelimit.constant.RateLimitConstant;
import com.tencent.cloud.polaris.ratelimit.filter.QuotaCheckReactiveFilter;
import com.tencent.cloud.polaris.ratelimit.filter.QuotaCheckServletFilter;
import com.tencent.polaris.client.api.SDKContext;
import com.tencent.polaris.ratelimit.api.core.LimitAPI;
import com.tencent.polaris.ratelimit.factory.LimitAPIFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
@ -34,11 +31,18 @@ import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static javax.servlet.DispatcherType.ASYNC;
import static javax.servlet.DispatcherType.ERROR;
import static javax.servlet.DispatcherType.FORWARD;
import static javax.servlet.DispatcherType.INCLUDE;
import static javax.servlet.DispatcherType.REQUEST;
/**
* @author Haotian Zhang
*/
@Configuration
@ConditionalOnProperty(name = "spring.cloud.polaris.ratelimit.enabled", matchIfMissing = true)
@ConditionalOnProperty(name = "spring.cloud.polaris.ratelimit.enabled",
matchIfMissing = true)
public class RateLimitConfiguration {
@Bean
@ -48,7 +52,7 @@ public class RateLimitConfiguration {
}
/**
*
* Create when web application type is SERVLET.
*/
@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@ -56,20 +60,35 @@ public class RateLimitConfiguration {
@Bean
@ConditionalOnMissingBean
public QuotaCheckFilter quotaCheckFilter(LimitAPI limitAPI) {
return new QuotaCheckFilter(limitAPI);
public QuotaCheckServletFilter quotaCheckFilter(LimitAPI limitAPI) {
return new QuotaCheckServletFilter(limitAPI);
}
@Bean
public FilterRegistrationBean<QuotaCheckFilter> quotaFilterRegistrationBean(
QuotaCheckFilter quotaCheckFilter) {
FilterRegistrationBean<QuotaCheckFilter> registrationBean = new FilterRegistrationBean<>(quotaCheckFilter);
public FilterRegistrationBean<QuotaCheckServletFilter> quotaFilterRegistrationBean(
QuotaCheckServletFilter quotaCheckServletFilter) {
FilterRegistrationBean<QuotaCheckServletFilter> registrationBean = new FilterRegistrationBean<>(
quotaCheckServletFilter);
registrationBean.setDispatcherTypes(ASYNC, ERROR, FORWARD, INCLUDE, REQUEST);
registrationBean.setName("quotaFilterRegistrationBean");
registrationBean.setOrder(QuotaCheckFilter.ORDER);
registrationBean.setOrder(RateLimitConstant.FILTER_ORDER);
return registrationBean;
}
}
/**
* Create when web application type is REACTIVE.
*/
@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
static class MetadataReactiveFilterConfig {
@Bean
public QuotaCheckReactiveFilter quotaCheckReactiveFilter(LimitAPI limitAPI) {
return new QuotaCheckReactiveFilter(limitAPI);
}
}
}

@ -15,35 +15,28 @@
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.feign;
package com.tencent.cloud.polaris.ratelimit.constant;
import org.springframework.core.Ordered;
/**
* Pre plugin used by PluggableFeign.
* Constant for rate-limiter.
*
* @author Haotian Zhang
*/
public interface PluggableFeignPlugin extends Ordered {
public final class RateLimitConstant {
/**
* Get name of plugin
*
* @return
* Order of filter.
*/
String getName();
public static final int FILTER_ORDER = Ordered.HIGHEST_PRECEDENCE + 10;
/**
* Get type of plugin
* @see PluggableFeignPluginType
*
* @return
* Info of rate limit.
*/
PluggableFeignPluginType getType();
public static String QUOTA_LIMITED_INFO = "request blocked by polaris, reason is ";
/**
* Run the plugin
*/
void run(PluggableFeignContext context);
private RateLimitConstant() {
}
}

@ -0,0 +1,106 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.polaris.ratelimit.filter;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import com.tencent.cloud.metadata.constant.MetadataConstant;
import com.tencent.cloud.metadata.context.MetadataContext;
import com.tencent.cloud.metadata.context.MetadataContextHolder;
import com.tencent.cloud.polaris.ratelimit.constant.RateLimitConstant;
import com.tencent.cloud.polaris.ratelimit.utils.QuotaCheckUtils;
import com.tencent.polaris.ratelimit.api.core.LimitAPI;
import com.tencent.polaris.ratelimit.api.rpc.QuotaResponse;
import com.tencent.polaris.ratelimit.api.rpc.QuotaResultCode;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
/**
* Reactive filter to check quota.
*
* @author Haotian Zhang
*/
public class QuotaCheckReactiveFilter implements WebFilter, Ordered {
private static final Logger LOG = LoggerFactory
.getLogger(QuotaCheckReactiveFilter.class);
private final LimitAPI limitAPI;
public QuotaCheckReactiveFilter(LimitAPI limitAPI) {
this.limitAPI = limitAPI;
}
@Override
public int getOrder() {
return RateLimitConstant.FILTER_ORDER;
}
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
// get metadata of current thread
MetadataContext metadataContext = exchange
.getAttribute(MetadataConstant.HeaderName.METADATA_CONTEXT);
String localNamespace = MetadataContextHolder.get()
.getSystemMetadata(MetadataConstant.SystemMetadataKey.LOCAL_NAMESPACE);
String localService = MetadataContextHolder.get()
.getSystemMetadata(MetadataConstant.SystemMetadataKey.LOCAL_SERVICE);
String method = MetadataContextHolder.get()
.getSystemMetadata(MetadataConstant.SystemMetadataKey.LOCAL_PATH);
Map<String, String> labels = null;
if (StringUtils.isNotBlank(method)) {
labels = new HashMap<>();
labels.put("method", method);
}
try {
QuotaResponse quotaResponse = QuotaCheckUtils.getQuota(limitAPI,
localNamespace, localService, 1, labels, null);
if (quotaResponse.getCode() == QuotaResultCode.QuotaResultLimited) {
ServerHttpResponse response = exchange.getResponse();
response.setStatusCode(HttpStatus.TOO_MANY_REQUESTS);
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
DataBuffer dataBuffer = response.bufferFactory().allocateBuffer().write(
(RateLimitConstant.QUOTA_LIMITED_INFO + quotaResponse.getInfo())
.getBytes(StandardCharsets.UTF_8));
return response.writeWith(Mono.just(dataBuffer));
}
}
catch (Throwable t) {
// 限流API调用出现异常不应该影响业务流程的调用
LOG.error("fail to invoke getQuota, service is " + localService, t);
}
return chain.filter(exchange);
}
}

@ -0,0 +1,97 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.polaris.ratelimit.filter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.tencent.cloud.metadata.constant.MetadataConstant.SystemMetadataKey;
import com.tencent.cloud.metadata.context.MetadataContextHolder;
import com.tencent.cloud.polaris.ratelimit.constant.RateLimitConstant;
import com.tencent.cloud.polaris.ratelimit.utils.QuotaCheckUtils;
import com.tencent.polaris.ratelimit.api.core.LimitAPI;
import com.tencent.polaris.ratelimit.api.rpc.QuotaResponse;
import com.tencent.polaris.ratelimit.api.rpc.QuotaResultCode;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.web.filter.OncePerRequestFilter;
import static org.springframework.http.HttpStatus.TOO_MANY_REQUESTS;
/**
* Servlet filter to check quota.
*
* @author Haotian Zhang
*/
@Order(RateLimitConstant.FILTER_ORDER)
public class QuotaCheckServletFilter extends OncePerRequestFilter {
private static final Logger LOG = LoggerFactory
.getLogger(QuotaCheckServletFilter.class);
private final LimitAPI limitAPI;
public QuotaCheckServletFilter(LimitAPI limitAPI) {
this.limitAPI = limitAPI;
}
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
String localNamespace = MetadataContextHolder.get()
.getSystemMetadata(SystemMetadataKey.LOCAL_NAMESPACE);
String localService = MetadataContextHolder.get()
.getSystemMetadata(SystemMetadataKey.LOCAL_SERVICE);
String method = MetadataContextHolder.get()
.getSystemMetadata(SystemMetadataKey.LOCAL_PATH);
Map<String, String> labels = null;
if (StringUtils.isNotBlank(method)) {
labels = new HashMap<>();
labels.put("method", method);
}
try {
QuotaResponse quotaResponse = QuotaCheckUtils.getQuota(limitAPI,
localNamespace, localService, 1, labels, null);
if (quotaResponse.getCode() == QuotaResultCode.QuotaResultLimited) {
response.setStatus(TOO_MANY_REQUESTS.value());
response.getWriter().write(
RateLimitConstant.QUOTA_LIMITED_INFO + quotaResponse.getInfo());
}
else {
filterChain.doFilter(request, response);
}
}
catch (Throwable t) {
// 限流API调用出现异常不应该影响业务流程的调用
LOG.error("fail to invoke getQuota, service is " + localService, t);
filterChain.doFilter(request, response);
}
}
}

@ -16,6 +16,8 @@
*/
package com.tencent.cloud.polaris.ratelimit.utils;
import java.util.Map;
import com.tencent.polaris.api.plugin.ratelimiter.QuotaResult;
import com.tencent.polaris.ratelimit.api.core.LimitAPI;
import com.tencent.polaris.ratelimit.api.rpc.QuotaRequest;
@ -23,19 +25,20 @@ import com.tencent.polaris.ratelimit.api.rpc.QuotaResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
/**
* Utils for quota checking
* Utils for quota checking.
*
* @author Haotian Zhang
*/
public class QuotaCheckUtils {
public final class QuotaCheckUtils {
private static final Logger LOG = LoggerFactory.getLogger(QuotaCheckUtils.class);
public static QuotaResponse getQuota(LimitAPI limitAPI, String namespace, String service, int count,
Map<String, String> labels, String method) {
private QuotaCheckUtils() {
}
public static QuotaResponse getQuota(LimitAPI limitAPI, String namespace,
String service, int count, Map<String, String> labels, String method) {
// build quota request
QuotaRequest quotaRequest = new QuotaRequest();
quotaRequest.setNamespace(namespace);
@ -46,9 +49,12 @@ public class QuotaCheckUtils {
try {
return limitAPI.getQuota(quotaRequest);
} catch (Throwable throwable) {
LOG.error("fail to invoke getQuota of LimitAPI with QuotaRequest[{}].", quotaRequest, throwable);
return new QuotaResponse(new QuotaResult(QuotaResult.Code.QuotaResultOk, 0, "get quota failed"));
}
catch (Throwable throwable) {
LOG.error("fail to invoke getQuota of LimitAPI with QuotaRequest[{}].",
quotaRequest, throwable);
return new QuotaResponse(new QuotaResult(QuotaResult.Code.QuotaResultOk, 0,
"get quota failed"));
}
}

@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.tencent.cloud.polaris.ratelimit.config.RateLimitConfiguration

@ -0,0 +1,150 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.polaris.ratelimit.controller;
import com.tencent.polaris.api.pojo.ServiceKey;
import com.tencent.polaris.ratelimit.api.core.LimitAPI;
import com.tencent.polaris.ratelimit.api.rpc.QuotaResponse;
import com.tencent.polaris.ratelimit.api.rpc.QuotaResultCode;
import com.tencent.polaris.test.mock.discovery.NamingServer;
import com.tencent.polaris.test.mock.discovery.NamingService;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.HttpClientErrorException.TooManyRequests;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST;
import static com.tencent.polaris.test.common.Consts.PORT;
import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Test for rate-limit.
*
* @author Haotian Zhang
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = { CalleeControllerTests.Config.class, TestController.class },
properties = { "spring.application.name=java_provider_test",
"spring.cloud.polaris.discovery.namespace=Test",
"spring.cloud.polaris.address=grpc://127.0.0.1:10081" })
public class CalleeControllerTests {
private static NamingServer namingServer;
@LocalServerPort
private int port;
@Autowired
private RestTemplate restTemplate;
@MockBean
private LimitAPI limitAPI;
@BeforeClass
public static void beforeClass() throws Exception {
namingServer = NamingServer.startNamingServer(10081);
// add service with 3 instances
NamingService.InstanceParameter instanceParameter = new NamingService.InstanceParameter();
instanceParameter.setHealthy(true);
instanceParameter.setIsolated(false);
instanceParameter.setWeight(100);
ServiceKey serviceKey = new ServiceKey(NAMESPACE_TEST, SERVICE_PROVIDER);
namingServer.getNamingService().batchAddInstances(serviceKey, PORT, 3,
instanceParameter);
}
@AfterClass
public static void afterClass() throws Exception {
if (null != namingServer) {
namingServer.terminate();
}
}
@Before
public void setUp() {
QuotaResponse quotaResponse = mock(QuotaResponse.class);
when(quotaResponse.getCode()).thenReturn(QuotaResultCode.QuotaResultOk);
when(limitAPI.getQuota(any())).thenReturn(quotaResponse);
}
@Test
public void test1() {
String url = "http://localhost:" + port + "/test/info";
boolean hasPassed = false;
boolean hasLimited = false;
for (int i = 0; i < 30; i++) {
try {
if (i > 9) {
QuotaResponse quotaResponse = mock(QuotaResponse.class);
when(quotaResponse.getCode())
.thenReturn(QuotaResultCode.QuotaResultLimited);
when(quotaResponse.getInfo())
.thenReturn("Testing rate limit after 10 times success.");
when(limitAPI.getQuota(any())).thenReturn(quotaResponse);
}
String result = restTemplate.getForObject(url, String.class);
System.out.println(result + " [" + i + "]");
hasPassed = true;
}
catch (RestClientException e) {
if (e instanceof TooManyRequests) {
System.out.println(((TooManyRequests) e).getResponseBodyAsString());
hasLimited = true;
}
else {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
}
Assert.assertTrue(hasPassed);
Assert.assertTrue(hasLimited);
}
@Configuration
@EnableAutoConfiguration
public static class Config {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
}

@ -21,7 +21,11 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Test controller.
*
* @author Haotian Zhang
*/
@RestController
@RequestMapping("/test")
public class TestController {
@ -31,5 +35,4 @@ public class TestController {
return "hello service info";
}
}

@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-tencent-starters</artifactId>
<artifactId>spring-cloud-tencent</artifactId>
<groupId>com.tencent.cloud</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>

@ -0,0 +1,138 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.polaris.router;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.DynamicServerListLoadBalancer;
import com.netflix.loadbalancer.IPing;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.PollingServerListUpdater;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList;
import com.tencent.cloud.common.pojo.PolarisServer;
import com.tencent.cloud.metadata.constant.MetadataConstant.SystemMetadataKey;
import com.tencent.cloud.metadata.context.MetadataContextHolder;
import com.tencent.polaris.api.pojo.DefaultInstance;
import com.tencent.polaris.api.pojo.DefaultServiceInstances;
import com.tencent.polaris.api.pojo.Instance;
import com.tencent.polaris.api.pojo.ServiceInfo;
import com.tencent.polaris.api.pojo.ServiceInstances;
import com.tencent.polaris.api.pojo.ServiceKey;
import com.tencent.polaris.router.api.core.RouterAPI;
import com.tencent.polaris.router.api.rpc.ProcessRoutersRequest;
import com.tencent.polaris.router.api.rpc.ProcessRoutersResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;
/**
* Routing load balancer of polaris.
*
* @author Haotian Zhang
*/
public class PolarisRoutingLoadBalancer extends DynamicServerListLoadBalancer<Server> {
private final RouterAPI routerAPI;
public PolarisRoutingLoadBalancer(IClientConfig config, IRule rule, IPing ping,
ServerList<Server> serverList, RouterAPI routerAPI) {
super(config, rule, ping, serverList, null, new PollingServerListUpdater());
this.routerAPI = routerAPI;
}
@Override
public List<Server> getReachableServers() {
List<Server> allServers = super.getAllServers();
if (CollectionUtils.isEmpty(allServers)) {
return allServers;
}
ServiceInstances serviceInstances = null;
if (allServers.get(0) instanceof PolarisServer) {
serviceInstances = ((PolarisServer) allServers.get(0)).getServiceInstances();
}
else {
String serviceName;
// notice the difference between different service registries
if (StringUtils.isNotBlank(
allServers.get(0).getMetaInfo().getServiceIdForDiscovery())) {
serviceName = allServers.get(0).getMetaInfo().getServiceIdForDiscovery();
}
else {
serviceName = allServers.get(0).getMetaInfo().getAppName();
}
if (StringUtils.isBlank(serviceName)) {
throw new IllegalStateException(
"PolarisRoutingLoadBalancer only Server with AppName or ServiceIdForDiscovery attribute");
}
ServiceKey serviceKey = new ServiceKey(MetadataContextHolder.LOCAL_NAMESPACE,
serviceName);
List<Instance> instances = new ArrayList<>(8);
for (Server server : allServers) {
DefaultInstance instance = new DefaultInstance();
instance.setNamespace(MetadataContextHolder.LOCAL_NAMESPACE);
instance.setService(serviceName);
instance.setHealthy(server.isAlive());
instance.setProtocol(server.getScheme());
instance.setId(server.getId());
instance.setHost(server.getHost());
instance.setPort(server.getPort());
instance.setZone(server.getZone());
instance.setWeight(100);
instances.add(instance);
}
serviceInstances = new DefaultServiceInstances(serviceKey, instances);
}
ProcessRoutersRequest processRoutersRequest = new ProcessRoutersRequest();
processRoutersRequest.setDstInstances(serviceInstances);
String srcNamespace = MetadataContextHolder.get()
.getSystemMetadata(SystemMetadataKey.LOCAL_NAMESPACE);
String srcService = MetadataContextHolder.get()
.getSystemMetadata(SystemMetadataKey.LOCAL_SERVICE);
Map<String, String> transitiveCustomMetadata = MetadataContextHolder.get()
.getAllTransitiveCustomMetadata();
String method = MetadataContextHolder.get()
.getSystemMetadata(SystemMetadataKey.PEER_PATH);
processRoutersRequest.setMethod(method);
if (StringUtils.isNotBlank(srcNamespace) && StringUtils.isNotBlank(srcService)) {
ServiceInfo serviceInfo = new ServiceInfo();
serviceInfo.setNamespace(srcNamespace);
serviceInfo.setService(srcService);
serviceInfo.setMetadata(transitiveCustomMetadata);
processRoutersRequest.setSourceService(serviceInfo);
}
ProcessRoutersResponse processRoutersResponse = routerAPI
.processRouters(processRoutersRequest);
ServiceInstances filteredServiceInstances = processRoutersResponse
.getServiceInstances();
List<Server> filteredInstances = new ArrayList<>();
for (Instance instance : filteredServiceInstances.getInstances()) {
filteredInstances.add(new PolarisServer(serviceInstances, instance));
}
return filteredInstances;
}
@Override
public List<Server> getAllServers() {
return getReachableServers();
}
}

@ -21,6 +21,7 @@ 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.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@ -37,7 +38,8 @@ import org.springframework.context.annotation.Configuration;
*/
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties
@ConditionalOnProperty(value = "spring.cloud.polaris.loadbalancer.enabled", matchIfMissing = true)
@ConditionalOnProperty(value = "spring.cloud.polaris.loadbalancer.enabled",
matchIfMissing = true)
@AutoConfigureAfter(RibbonAutoConfiguration.class)
@RibbonClients(defaultConfiguration = PolarisRibbonClientConfiguration.class)
public class PolarisRibbonAutoConfiguration {
@ -53,4 +55,5 @@ public class PolarisRibbonAutoConfiguration {
public RouterAPI polarisRouter(SDKContext polarisContext) throws PolarisException {
return RouterAPIFactory.createRouterAPIByContext(polarisContext);
}
}

@ -27,6 +27,7 @@ import com.tencent.cloud.polaris.router.PolarisRoutingLoadBalancer;
import com.tencent.cloud.polaris.router.rule.PolarisLoadBalanceRule;
import com.tencent.cloud.polaris.router.rule.PolarisWeightedRandomRule;
import com.tencent.polaris.router.api.core.RouterAPI;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -40,7 +41,8 @@ public class PolarisRibbonClientConfiguration {
@Bean
@ConditionalOnMissingBean
public IRule polarisRibbonRule(PolarisRibbonProperties polarisRibbonProperties) {
switch (PolarisLoadBalanceRule.fromStrategy(polarisRibbonProperties.getPolicy())) {
switch (PolarisLoadBalanceRule
.fromStrategy(polarisRibbonProperties.getStrategy())) {
case WEIGHTED_RANDOM_RULE:
default:
return new PolarisWeightedRandomRule();
@ -49,8 +51,11 @@ public class PolarisRibbonClientConfiguration {
@Bean
@ConditionalOnMissingBean
public ILoadBalancer polarisRoutingLoadBalancer(IClientConfig iClientConfig, IRule iRule, IPing iPing,
ServerList<Server> serverList, RouterAPI polarisRouter) {
return new PolarisRoutingLoadBalancer(iClientConfig, iRule, iPing, serverList, polarisRouter);
public ILoadBalancer polarisRoutingLoadBalancer(IClientConfig iClientConfig,
IRule iRule, IPing iPing, ServerList<Server> serverList,
RouterAPI polarisRouter) {
return new PolarisRoutingLoadBalancer(iClientConfig, iRule, iPing, serverList,
polarisRouter);
}
}

@ -21,29 +21,31 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Properties of Ribbon.
*
* @author Haotian Zhang
*/
@ConfigurationProperties("spring.cloud.polaris.ribbon")
public class PolarisRibbonProperties {
/**
*
* If load-balance enabled.
*/
@Value("${spring.cloud.polaris.loadbalancer.enabled:#{true}}")
@Value("${spring.cloud.polaris.discovery.loadbalancer.enabled:#{true}}")
private Boolean loadbalancerEnabled;
/**
* loadbalnce strategy
* Load balance strategy.
*/
@Value("${spring.cloud.polaris.loadbalancer.strategy:#{'weightedRandom'}}")
private String policy;
private String strategy;
public String getPolicy() {
return policy;
public String getStrategy() {
return strategy;
}
public void setPolicy(String policy) {
this.policy = policy;
public void setStrategy(String strategy) {
this.strategy = strategy;
}
public Boolean getLoadbalancerEnabled() {
@ -56,9 +58,8 @@ public class PolarisRibbonProperties {
@Override
public String toString() {
return "PolarisRibbonProperties{" +
"loadbalancerEnabled=" + loadbalancerEnabled +
", policy='" + policy + '\'' +
'}';
return "PolarisRibbonProperties{" + "loadbalancerEnabled=" + loadbalancerEnabled
+ ", strategy='" + strategy + '\'' + '}';
}
}

@ -20,32 +20,31 @@ package com.tencent.cloud.polaris.router.rule;
import java.util.Arrays;
/**
* Load balance rule.
*
* @author Haotian Zhang
*/
public enum PolarisLoadBalanceRule {
/**
*
* Weighted random load balance rule.
*/
WEIGHTED_RANDOM_RULE("weighted_random");
/**
*
* Load balance strategy.
*/
String policy;
final String policy;
PolarisLoadBalanceRule(String strategy) {
this.policy = strategy;
}
public static PolarisLoadBalanceRule fromStrategy(String strategy) {
return Arrays.stream(values()).filter(t -> t.getPolicy().equals(strategy)).findAny()
.orElse(WEIGHTED_RANDOM_RULE);
return Arrays.stream(values()).filter(t -> t.getPolicy().equals(strategy))
.findAny().orElse(WEIGHTED_RANDOM_RULE);
}
/**
* {@link #policy}getter
*/
public String getPolicy() {
return policy;
}

@ -17,21 +17,24 @@
package com.tencent.cloud.polaris.router.rule;
import java.util.List;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.Server;
import com.tencent.cloud.polaris.pojo.PolarisServer;
import com.tencent.cloud.common.pojo.PolarisServer;
import com.tencent.polaris.api.config.consumer.LoadBalanceConfig;
import com.tencent.polaris.api.pojo.Instance;
import com.tencent.polaris.router.api.core.RouterAPI;
import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest;
import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import java.util.List;
/**
* Weighted random load balance strategy.
*
* @author Haotian Zhang
*/
public class PolarisWeightedRandomRule extends AbstractLoadBalancerRule {
@ -54,14 +57,17 @@ public class PolarisWeightedRandomRule extends AbstractLoadBalancerRule {
}
Server server = allServers.get(0);
if (!(server instanceof PolarisServer)) {
throw new IllegalStateException("PolarisDiscoveryRule only support PolarisServer instances");
throw new IllegalStateException(
"PolarisDiscoveryRule only support PolarisServer instances");
}
PolarisServer polarisServer = (PolarisServer) server;
ProcessLoadBalanceRequest request = new ProcessLoadBalanceRequest();
request.setDstInstances(polarisServer.getServiceInstances());
request.setLbPolicy(POLICY);
ProcessLoadBalanceResponse processLoadBalanceResponse = polarisRouter.processLoadBalance(request);
ProcessLoadBalanceResponse processLoadBalanceResponse = polarisRouter
.processLoadBalance(request);
Instance targetInstance = processLoadBalanceResponse.getTargetInstance();
return new PolarisServer(polarisServer.getServiceInstances(), targetInstance);
}
}

@ -19,6 +19,7 @@ package com.tencent.cloud.polaris.router.config;
import com.tencent.polaris.router.api.core.RouterAPI;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
@ -29,13 +30,14 @@ import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author skyehtzhang
* Test for {@link PolarisRibbonAutoConfiguration}
*
* @author Haotian Zhang
*/
public class PolarisRibbonAutoConfigurationTest {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
PolarisRibbonTest.class,
.withConfiguration(AutoConfigurations.of(PolarisRibbonTest.class,
PolarisRibbonAutoConfiguration.class))
.withPropertyValues("spring.application.name=" + SERVICE_PROVIDER)
.withPropertyValues("server.port=" + PORT)
@ -54,4 +56,5 @@ public class PolarisRibbonAutoConfigurationTest {
static class PolarisRibbonTest {
}
}

@ -0,0 +1,79 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-tencent</artifactId>
<groupId>com.tencent.cloud</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-tencent-commons</artifactId>
<name>Spring Cloud Tencent Commons</name>
<properties>
<commons.collections.version>3.2.2</commons.collections.version>
<commons.lang.version>2.5</commons.lang.version>
<commons.io.version>2.7</commons.io.version>
</properties>
<dependencies>
<!-- Polaris dependencies start -->
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-model</artifactId>
</dependency>
<!-- Polaris dependencies end -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
</dependency>
<dependency>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>${commons.collections.version}</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>${commons.lang.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons.io.version}</version>
</dependency>
</dependencies>
</project>

@ -22,26 +22,28 @@ package com.tencent.cloud.common.constant;
*
* @author skyehtzhang
*/
public interface ContextConstant {
public final class ContextConstant {
/**
* Order of configuration modifier.
*/
interface ModifierOrder {
public static final class ModifierOrder {
/**
* First modifier order.
*/
Integer FIRST = Integer.MIN_VALUE;
public static Integer FIRST = Integer.MIN_VALUE;
/**
* Last modifier order.
*/
Integer LAST = Integer.MAX_VALUE;
public static Integer LAST = Integer.MAX_VALUE;
/**
* Order of circuit breaker configuration modifier.
*/
Integer CIRCUIT_BREAKER_ORDER = 1;
public static Integer CIRCUIT_BREAKER_ORDER = 1;
}
}

@ -0,0 +1,111 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.common.pojo;
import java.util.Map;
import java.util.Objects;
import com.netflix.loadbalancer.Server;
import com.tencent.polaris.api.pojo.Instance;
import com.tencent.polaris.api.pojo.ServiceInstances;
import org.apache.commons.lang.StringUtils;
/**
* Polaris' implementation of {@link Server}.
*
* @author Haotian Zhang
*/
public class PolarisServer extends Server {
private final ServiceInstances serviceInstances;
private final Instance instance;
private final MetaInfo metaInfo;
public PolarisServer(ServiceInstances serviceInstances, Instance instance) {
super(instance.getHost(), instance.getPort());
if (StringUtils.equalsIgnoreCase(instance.getProtocol(), "https")) {
setSchemea("https");
}
else {
setSchemea("http");
}
this.serviceInstances = serviceInstances;
this.instance = instance;
this.metaInfo = new MetaInfo() {
@Override
public String getAppName() {
return instance.getService();
}
@Override
public String getServerGroup() {
return null;
}
@Override
public String getServiceIdForDiscovery() {
return instance.getService();
}
@Override
public String getInstanceId() {
return instance.getId();
}
};
}
public Instance getInstance() {
return instance;
}
@Override
public MetaInfo getMetaInfo() {
return metaInfo;
}
public Map<String, String> getMetadata() {
return instance.getMetadata();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!super.equals(o)) {
return false;
}
PolarisServer that = (PolarisServer) o;
return Objects.equals(instance, that.instance);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), instance);
}
public ServiceInstances getServiceInstances() {
return serviceInstances;
}
}

@ -0,0 +1,93 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.common.pojo;
import java.net.URI;
import java.util.Map;
import com.tencent.polaris.api.pojo.Instance;
import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
/**
* Polaris's implementation of {@link ServiceInstance}.
*
* @author Haotian Zhang
*/
public class PolarisServiceInstance implements ServiceInstance {
private final Instance instance;
private final boolean isSecure;
private final String scheme;
public PolarisServiceInstance(Instance instance) {
this.instance = instance;
this.isSecure = StringUtils.equalsIgnoreCase(instance.getProtocol(), "https");
if (isSecure) {
scheme = "https";
}
else {
scheme = "http";
}
}
@Override
public String getInstanceId() {
return ServiceInstance.super.getInstanceId();
}
@Override
public String getServiceId() {
return instance.getService();
}
@Override
public String getHost() {
return instance.getHost();
}
@Override
public int getPort() {
return instance.getPort();
}
@Override
public boolean isSecure() {
return this.isSecure;
}
@Override
public URI getUri() {
return DefaultServiceInstance.getUri(this);
}
@Override
public Map<String, String> getMetadata() {
return instance.getMetadata();
}
@Override
public String getScheme() {
return this.scheme;
}
}

@ -23,7 +23,7 @@ import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* Spring Context Util
* Spring Context Util.
*
* @author Hongwei Zhu
*/
@ -32,38 +32,36 @@ public class ApplicationContextAwareUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ApplicationContextAwareUtils.applicationContext = applicationContext;
}
/**
*
*
* @return Spring
* Get application context.
* @return application context
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
ApplicationContextAwareUtils.applicationContext = applicationContext;
}
/**
* Spring
*
* @param key
* @return
* Get application property.
* @param key property name
* @return property value
*/
public static String getProperties(String key) {
return applicationContext.getEnvironment().getProperty(key);
}
/**
* Spring<br>
*
*
* @param key
* @param defaultValue
* @return
* Get application property. If null, return default.
* @param key property name
* @param defaultValue default value
* @return property value
*/
public static String getProperties(String key, String defaultValue) {
return applicationContext.getEnvironment().getProperty(key, defaultValue);
}
}

@ -0,0 +1,84 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.common.util;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
/**
* Utils for Jackson.
*
* @author Haotian Zhang
*/
public final class JacksonUtils {
/**
* Object Mapper.
*/
public static final ObjectMapper OM = new ObjectMapper();
private static final Logger LOG = LoggerFactory.getLogger(JacksonUtils.class);
private JacksonUtils() {
}
/**
* Object to Json.
* @param object object to be serialized
* @param <T> type of object
* @return Json String
*/
public static <T> String serialize2Json(T object) {
try {
return OM.writeValueAsString(object);
}
catch (JsonProcessingException e) {
LOG.error("Object to Json failed. {}", object, e);
throw new RuntimeException("Object to Json failed.", e);
}
}
/**
* Json to Map.
* @param jsonStr Json String
* @return Map
*/
public static Map<String, String> deserialize2Map(String jsonStr) {
try {
if (StringUtils.hasText(jsonStr)) {
return OM.readValue(jsonStr, Map.class);
}
return new HashMap<>();
}
catch (JsonProcessingException e) {
LOG.error(
"Json to map failed. check if the format of the json string[{}] is correct.",
jsonStr, e);
throw new RuntimeException("Json to map failed.", e);
}
}
}

@ -20,21 +20,28 @@ package com.tencent.cloud.common.util;
import java.lang.reflect.Field;
/**
* Reflection Utils
* Reflection Utils.
*
* @author Haotian Zhang
*/
public class ReflectionUtils {
public final class ReflectionUtils {
private ReflectionUtils() {
}
public static Object getFieldValue(Object instance, String fieldName) {
Field field = org.springframework.util.ReflectionUtils.findField(instance.getClass(), fieldName);
Field field = org.springframework.util.ReflectionUtils
.findField(instance.getClass(), fieldName);
field.setAccessible(true);
try {
return field.get(instance);
} catch (IllegalAccessException e) {
}
catch (IllegalAccessException e) {
// ignore
} finally {
}
finally {
field.setAccessible(false);
}
return null;

@ -1,3 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.tencent.cloud.common.util.ApplicationContextAwareUtils

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-tencent</artifactId>
@ -44,11 +44,6 @@
<artifactId>spring-cloud-tencent-metadata</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-feign</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-router</artifactId>

@ -121,24 +121,12 @@
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-polaris-gateway</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-metadata</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-feign</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>

@ -25,13 +25,13 @@ import org.springframework.web.bind.annotation.GetMapping;
*
* @author Haotian Zhang
*/
@FeignClient(name = "polaris-circuitbreaker-example-b", fallback = ProviderBFallback.class)
@FeignClient(name = "polaris-circuitbreaker-example-b",
fallback = ProviderBFallback.class)
public interface ProviderB {
/**
* B
*
* @return
* Get info of service B.
* @return info of service B
*/
@GetMapping("/example/service/b/info")
String info();

@ -44,29 +44,27 @@ public class ServiceAController {
/**
*
*
* @return
* @throws Exception
*/
@GetMapping("/info")
public String info() throws Exception {
public String info() {
return "hello world ! I'am a service";
}
/**
* B
*
* @return B
* @throws Exception
*/
@GetMapping("/getBServiceInfo")
public String getBServiceInfo() throws Exception {
public String getBServiceInfo() {
return polarisServiceB.info();
}
@RequestMapping(value = "/testRest", method = RequestMethod.GET)
public String testRest() {
ResponseEntity<String> entity = restTemplate.getForEntity("http://polaris-circuitbreaker-example-b/example/service/b/info", String.class);
ResponseEntity<String> entity = restTemplate.getForEntity(
"http://polaris-circuitbreaker-example-b/example/service/b/info",
String.class);
return entity.getBody();
}

@ -264,11 +264,8 @@ consumer:
metricStatTimeWindow: 100ms
#描述:基于周期错误率的熔断策略配置
errorRate:
#描述:触发错误率熔断的阈值
#类型:double
#范围:(0:1]
#默认值:0.5
errorRateThreshold: 0.01
#描述:触发错误率熔断的阈值百分比
errorRateThreshold: 50
#描述:错误率熔断的最小统计单元数量
#类型:int
#范围:[1:...]

@ -25,13 +25,13 @@ import org.springframework.web.bind.annotation.GetMapping;
*
* @author Haotian Zhang
*/
@FeignClient(name = "polaris-circuitbreaker-example-a", fallback = ProviderAFallback.class)
@FeignClient(name = "polaris-circuitbreaker-example-a",
fallback = ProviderAFallback.class)
public interface ProviderA {
/**
* B
*
* @return
* Get info of service A.
* @return info of service A
*/
@GetMapping("/example/service/a/info")
String info();

@ -31,4 +31,5 @@ public class ProviderAFallback implements ProviderA {
public String info() {
return "trigger the refuse for service a";
}
}

@ -34,13 +34,13 @@ import org.springframework.web.client.RestTemplate;
@EnableFeignClients
public class ServiceB {
public static void main(String[] args) {
SpringApplication.run(ServiceB.class);
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ServiceB.class);
}
}

@ -44,32 +44,29 @@ public class ServiceBController {
/**
*
*
* @return
* @throws Exception
*/
@GetMapping("/info")
public String info() throws Exception {
// return "hello world ! I'am a service";
public String info() {
// return "hello world ! I'am a service";
throw new RuntimeException("failed for call my service");
}
/**
* B
*
* @return B
* @throws Exception
*/
@GetMapping("/getAServiceInfo")
public String getAServiceInfo() throws Exception {
public String getAServiceInfo() {
return polarisServiceA.info();
}
@RequestMapping(value = "/testRest", method = RequestMethod.GET)
public String testRest() {
ResponseEntity<String> entity = restTemplate.getForEntity("http://polaris-circuitbreaker-example-a/example/service/b/info", String.class);
ResponseEntity<String> entity = restTemplate.getForEntity(
"http://polaris-circuitbreaker-example-a/example/service/b/info",
String.class);
return entity.getBody();
}
}

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>polaris-discovery-example</artifactId>
@ -19,15 +19,15 @@
<groupId>com.tencent.cloud</groupId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-consul-discovery</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-consul-discovery</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>-->
<!-- </dependency>-->
</dependencies>
<build>
<plugins>

@ -17,6 +17,9 @@
package com.tencent.cloud.polaris.discovery.service.callee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@ -29,9 +32,11 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/discovery/service/callee")
public class DiscoveryCalleeController {
private static Logger logger = LoggerFactory
.getLogger(DiscoveryCalleeController.class);
/**
*
*
* @return
*/
@GetMapping("/info")
@ -41,7 +46,6 @@ public class DiscoveryCalleeController {
/**
*
*
* @param value1 1
* @param value2 2
* @return

@ -19,14 +19,17 @@ package com.tencent.cloud.polaris.discovery.service.callee;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Haotian Zhang
*/
@SpringBootApplication
@RestController
public class DiscoveryCalleeService {
public static void main(String[] args) {
SpringApplication.run(DiscoveryCalleeService.class, args);
}
}

@ -7,18 +7,20 @@ spring:
cloud:
polaris:
address: grpc://127.0.0.1:8091
consul:
port: 8500
host: 127.0.0.1
enabled: true
discovery:
register: true
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
enabled: true
service-name: ${spring.application.name}
ip-address: localhost
prefer-ip-address: true
eureka:
client:
serviceUrl:
defaultZone: http://127.0.0.1:7654/eureka/
ip-address: 127.0.0.1
# consul:
# port: 8500
# host: 127.0.0.1
# enabled: true
# discovery:
# register: true
# instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
# enabled: true
# service-name: ${spring.application.name}
# ip-address: localhost
# prefer-ip-address: true
#eureka:
# client:
# serviceUrl:
# defaultZone: http://127.0.0.1:7654/eureka/

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>polaris-discovery-example</artifactId>
@ -23,20 +23,20 @@
<groupId>com.tencent.cloud</groupId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-consul-discovery</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-consul-discovery</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-router</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.tencent.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-tencent-polaris-router</artifactId>-->
<!-- </dependency>-->
</dependencies>
<build>
<plugins>

@ -24,16 +24,17 @@ import org.springframework.web.bind.annotation.RequestParam;
/**
* @author Haotian Zhang
*/
@FeignClient(value = "DiscoveryCalleeService", fallback = DiscoveryCalleeServiceCallback.class)
@FeignClient(value = "DiscoveryCalleeService",
fallback = DiscoveryCalleeServiceCallback.class)
public interface DiscoveryCalleeService {
/**
*
*
* @param value1 1
* @param value2 2
* @return
*/
@GetMapping("/discovery/service/callee/sum")
int sum(@RequestParam("value1") final int value1, @RequestParam("value2") final int value2);
int sum(@RequestParam("value1") int value1, @RequestParam("value2") int value2);
}

@ -29,4 +29,5 @@ public class DiscoveryCalleeServiceCallback implements DiscoveryCalleeService {
public int sum(int value1, int value2) {
return 0;
}
}

@ -39,7 +39,6 @@ public class DiscoveryCallerController {
/**
*
*
* @param value1 1
* @param value2 2
* @return
@ -51,22 +50,22 @@ public class DiscoveryCallerController {
/**
*
*
* @return
*/
@GetMapping("/rest")
public String rest() {
return restTemplate.getForObject("http://DiscoveryCalleeService/discovery/service/callee/info", String.class);
return restTemplate.getForObject(
"http://DiscoveryCalleeService/discovery/service/callee/info",
String.class);
}
/**
* health check
*
* @return
*/
@GetMapping("/healthCheck")
public String healthCheck() {
return "pk ok";
}
}

@ -33,14 +33,14 @@ import org.springframework.web.client.RestTemplate;
@EnableFeignClients
public class DiscoveryCallerService {
public static void main(String[] args) {
SpringApplication.run(DiscoveryCallerService.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(DiscoveryCallerService.class, args);
}
}

@ -11,20 +11,20 @@ spring:
heartbeat:
enabled: true
health-check-url: /discovery/service/caller/healthCheck
consul:
port: 8500
host: 127.0.0.1
enabled: true
discovery:
register: true
health-check-path: /actuator/health
health-check-interval: 10s
instance-id: ${spring.application.name}:${server.port}
enabled: true
service-name: ${spring.application.name}
ip-address: localhost
prefer-ip-address: true
eureka:
client:
serviceUrl:
defaultZone: http://127.0.0.1:7654/eureka/
# consul:
# port: 8500
# host: 127.0.0.1
# enabled: true
# discovery:
# register: true
# health-check-path: /actuator/health
# health-check-interval: 10s
# instance-id: ${spring.application.name}:${server.port}
# enabled: true
# service-name: ${spring.application.name}
# ip-address: localhost
# prefer-ip-address: true
#eureka:
# client:
# serviceUrl:
# defaultZone: http://127.0.0.1:7654/eureka/

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>polaris-gateway-example</artifactId>

@ -17,16 +17,17 @@
package com.tencent.cloud.polaris.gateway.example.callee;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import com.tencent.cloud.metadata.constant.MetadataConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
/**
* @author Haotian Zhang
*/
@ -38,7 +39,6 @@ public class GatewayCalleeController {
/**
* Get info string
*
* @return
*/
@RequestMapping("/info")
@ -48,13 +48,11 @@ public class GatewayCalleeController {
/**
* Get metadata in HTTP header
*
* @param metadataStr
* @return
* @throws UnsupportedEncodingException
*/
@RequestMapping("/echo")
public String echoHeader(@RequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA) String metadataStr) throws UnsupportedEncodingException {
public String echoHeader(
@RequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA) String metadataStr)
throws UnsupportedEncodingException {
logger.info(URLDecoder.decode(metadataStr, "UTF-8"));
return URLDecoder.decode(metadataStr, "UTF-8");
}

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>polaris-gateway-example</artifactId>
@ -20,8 +20,8 @@
</dependency>
<dependency>
<artifactId>spring-cloud-starter-tencent-polaris-ratelimit</artifactId>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-polaris-gateway</artifactId>
</dependency>
<dependency>

@ -5,14 +5,14 @@ spring:
application:
name: GatewayScgService
cloud:
polaris:
address: grpc://127.0.0.1:8091
tencent:
metadata:
content:
a: 1
transitive:
- a
polaris:
address: grpc://127.0.0.1:8091
gateway:
discovery:
locator:
@ -28,4 +28,4 @@ spring:
logging:
level:
org.springframework.cloud.gateway: trace
org.springframework.cloud.gateway: info

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>polaris-gateway-example</artifactId>
@ -20,8 +20,8 @@
</dependency>
<dependency>
<artifactId>spring-cloud-starter-tencent-polaris-ratelimit</artifactId>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-polaris-gateway</artifactId>
</dependency>
<dependency>

@ -18,6 +18,7 @@
package com.tencent.cloud.ratelimit.example.service.callee;
import java.util.concurrent.atomic.AtomicInteger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
@ -35,18 +36,16 @@ import org.springframework.web.client.RestTemplate;
@RequestMapping("/business")
public class BusinessController {
private final AtomicInteger index = new AtomicInteger(0);
@Autowired
private RestTemplate restTemplate;
private final AtomicInteger index = new AtomicInteger(0);
@Value("${spring.application.name}")
private String appName;
/**
*
*
* @return
*/
@GetMapping("/info")
@ -59,18 +58,21 @@ public class BusinessController {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 30; i++) {
try {
ResponseEntity<String> entity = restTemplate
.getForEntity("http://" + appName + "/business/info", String.class);
ResponseEntity<String> entity = restTemplate.getForEntity(
"http://" + appName + "/business/info", String.class);
builder.append(entity.getBody()).append("<br/>");
} catch (RestClientException e) {
}
catch (RestClientException e) {
if (e instanceof TooManyRequests) {
builder.append(((TooManyRequests) e).getResponseBodyAsString()).append(index.incrementAndGet())
.append("<br/>");
} else {
builder.append(((TooManyRequests) e).getResponseBodyAsString())
.append(index.incrementAndGet()).append("<br/>");
}
else {
throw e;
}
}
}
return builder.toString();
}
}

@ -18,7 +18,6 @@
package com.tencent.cloud.ratelimit.example.service.callee;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
@ -28,16 +27,16 @@ import org.springframework.web.client.RestTemplate;
* @author Haotian Zhang
*/
@SpringBootApplication
@EnableAutoConfiguration
public class RateLimitCalleeService {
public static void main(String[] args) {
SpringApplication.run(RateLimitCalleeService.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(RateLimitCalleeService.class, args);
}
}

@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-tencent</artifactId>
<groupId>com.tencent.cloud</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-tencent-metadata</artifactId>
<name>Spring Cloud Tencent Metadata</name>
<dependencies>
<!-- Spring Cloud Tencent dependencies start -->
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-commons</artifactId>
</dependency>
<!-- Spring Cloud Tencent dependencies end -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- powermock-module-junit4 -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<scope>test</scope>
</dependency>
<!-- powermock-api-mockito -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save