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-discoverydependency:
<code><dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
</dependency></code>Configure the Polaris server in
application.yaml:
<code>spring:
cloud:
polaris:
address: grpc://127.0.0.1:8091</code>Start the service and observe it in the Polaris console.
Service call example:
<code>@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);
}</code>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-configdependency:
<code><dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-config</artifactId>
</dependency></code>Configure in
bootstrap.yaml:
<code>spring:
cloud:
polaris:
address: grpc://127.0.0.1:8081
config:
groups:
- name: ${spring.application.name}
files: "application"</code>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:
<code>@Value("${name:}")
private String name;</code>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-ratelimitdependency (also include
discoveryfor console rule editing):
<code><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></code>Configure rate limiting in
application.yaml:
<code>spring:
cloud:
polaris:
address: grpc://127.0.0.1:8091
namespace: default
ratelimit:
reject-http-code: 403
reject-request-tips: "lengleng test rate limit"</code>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-routerdependency:
<code><dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-router</artifactId>
</dependency></code>Mark service metadata in
application.yaml:
<code>spring:
cloud:
polaris:
address: grpc://127.0.0.1:8091
tencent:
metadata:
content:
version: local</code>6. 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-circuitbreakerand related Spring Cloud dependencies:
<code><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></code>Define a Feign client (circuit breaking is currently supported only for Feign):
<code>@FeignClient(contextId = "demoFeign", value = "lengleng-circuitbreaker-tencent-circuitbreaker-provider", fallback = DemoFeignFallback.class)
public interface DemoFeign {
@GetMapping("/provider")
String get(@RequestParam String name);
}</code>Enable circuit breaking and configure rules in
polaris.yml:
<code>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: 1</code>Full 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
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.