Spring Cloud Tencent Guide: Discovery, Config, Rate Limiting & Circuit Breaking
This article introduces Spring Cloud Tencent, Tencent’s open‑source microservice solution built on Polaris, and walks through installing Polaris, adding service discovery, configuration management, rate limiting, routing, and circuit‑breaker features with detailed Maven dependencies and YAML configurations, plus code examples for each step.
What is Spring Cloud Tencent
Spring Cloud Tencent is Tencent’s open‑source one‑stop microservice solution. It implements the standard Spring Cloud microservice SPI, allowing developers to quickly build Spring Cloud applications. The core relies on Polaris, Tencent’s service discovery and governance platform, to support various distributed microservice scenarios.
1. Install Polaris
Polaris is Tencent’s open‑source service discovery and governance center, addressing service visibility, fault tolerance, traffic control, and security in distributed or microservice architectures. It provides a standard, multi‑language, framework‑agnostic implementation.
Polaris installation is very simple: download the zip of the response platform and run it directly.
2. Service Registration and Discovery
Add the polaris-discovery dependency:
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
</dependency>Configure the Polaris server in application.yaml:
spring:
cloud:
polaris:
address: grpc://127.0.0.1:8091Start the service and observe it in the Polaris console.
Service call example:
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Autowired
private RestTemplate restTemplate;
@GetMapping("/consumer")
public String consumer() {
return restTemplate.getForObject("http://lengleng-tencent-discovery-provider/provider/lengleng", String.class);
}3. Configuration Management
During the Bootstrap phase, Spring Cloud calls PolarisConfigFileLocator to fetch configuration files from the Polaris server and load them into the Spring context. Use standard @Value or @ConfigurationProperties to access them, and @RefreshScope for dynamic refresh.
Add the polaris-config dependency:
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-config</artifactId>
</dependency>Configure in bootstrap.yaml:
spring:
cloud:
polaris:
address: grpc://127.0.0.1:8081
config:
groups:
- name: ${spring.application.name}
files: "application"Note: This configuration must be placed in bootstrap because the current version of spring-cloud-tencent does not adapt to the latest Spring Boot file loading mechanism.
Use the configuration in code:
@Value("${name:}")
private String name;4. Service Rate Limiting
Rate limiting protects services from traffic spikes. The Spring Cloud Tencent Rate Limit module provides filters for Spring Web and WebFlux, leveraging Polaris’s rate‑limit capabilities.
Add the polaris-ratelimit dependency (also include discovery for console rule editing):
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-ratelimit</artifactId>
</dependency>Configure rate limiting in application.yaml:
spring:
cloud:
polaris:
address: grpc://127.0.0.1:8091
namespace: default
ratelimit:
reject-http-code: 403
reject-request-tips: "lengleng test rate limit"Add rate‑limit rules in the Polaris console.
5. Service Routing
Polaris supports various routing strategies such as metadata routing, proximity routing, rule routing, and custom routing. The example demonstrates metadata routing, which routes requests only to services with matching metadata.
Add the polaris-router dependency:
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-router</artifactId>
</dependency>Mark service metadata in application.yaml:
spring:
cloud:
polaris:
address: grpc://127.0.0.1:8091
tencent:
metadata:
content:
version: local6. Rate Limiting and Circuit Breaking
Circuit breaking protects services by automatically isolating instances with high error rates and periodically probing them for recovery, transitioning through half‑open to fully restored states.
Add the circuit‑breaker dependency polaris-circuitbreaker and related Spring Cloud dependencies:
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-circuitbreaker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-circuitbreaker-spring-retry</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>Define a Feign client (circuit breaking is currently supported only for Feign):
@FeignClient(contextId = "demoFeign", value = "lengleng-circuitbreaker-tencent-circuitbreaker-provider", fallback = DemoFeignFallback.class)
public interface DemoFeign {
@GetMapping("/provider")
String get(@RequestParam String name);
}Enable circuit breaking and configure rules in polaris.yml:
spring:
cloud:
polaris:
address: grpc://127.0.0.1:8091
feign:
circuitbreaker:
enabled: true
consumer:
circuitBreaker:
checkPeriod: 100ms
chain:
- errorCount
- errorRate
plugin:
errorCount:
continuousErrorThreshold: 1
metricNumBuckets: 1
errorRate:
errorRateThreshold: 100
metricStatTimeWindow: 1s
requestVolumeThreshold: 1Full source code: https://github.com/lltx/spring-cloud-tencent-demo
References
Polaris download: https://github.com/polarismesh/polaris/releases/tag/v1.9.0
Spring Cloud Tencent demo: https://github.com/lltx/spring-cloud-tencent-demo
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Architecture Diary
Committed to sharing original, high‑quality technical articles; no fluff or promotional content.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
