Cloud Native 10 min read

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.

Java Architecture Diary
Java Architecture Diary
Java Architecture Diary
Spring Cloud Tencent Guide: Discovery, Config, Rate Limiting & Circuit Breaking

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:

<code>&lt;dependency&gt;
    &lt;groupId&gt;com.tencent.cloud&lt;/groupId&gt;
    &lt;artifactId&gt;spring-cloud-starter-tencent-polaris-discovery&lt;/artifactId&gt;
&lt;/dependency&gt;</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-config

dependency:

<code>&lt;dependency&gt;
    &lt;groupId&gt;com.tencent.cloud&lt;/groupId&gt;
    &lt;artifactId&gt;spring-cloud-starter-tencent-polaris-config&lt;/artifactId&gt;
&lt;/dependency&gt;</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-ratelimit

dependency (also include

discovery

for console rule editing):

<code>&lt;dependency&gt;
    &lt;groupId&gt;com.tencent.cloud&lt;/groupId&gt;
    &lt;artifactId&gt;spring-cloud-starter-tencent-polaris-discovery&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupId&gt;com.tencent.cloud&lt;/groupId&gt;
    &lt;artifactId&gt;spring-cloud-starter-tencent-polaris-ratelimit&lt;/artifactId&gt;
&lt;/dependency&gt;</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-router

dependency:

<code>&lt;dependency&gt;
    &lt;groupId&gt;com.tencent.cloud&lt;/groupId&gt;
    &lt;artifactId&gt;spring-cloud-starter-tencent-polaris-router&lt;/artifactId&gt;
&lt;/dependency&gt;</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-circuitbreaker

and related Spring Cloud dependencies:

<code>&lt;dependency&gt;
    &lt;groupId&gt;com.tencent.cloud&lt;/groupId&gt;
    &lt;artifactId&gt;spring-cloud-starter-tencent-polaris-circuitbreaker&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
    &lt;artifactId&gt;spring-cloud-starter-loadbalancer&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
    &lt;artifactId&gt;spring-cloud-circuitbreaker-spring-retry&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
    &lt;artifactId&gt;spring-cloud-starter-openfeign&lt;/artifactId&gt;
&lt;/dependency&gt;</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

Microservicesservice discoveryconfiguration managementRate LimitingSpring Cloudcircuit-breakerPolaris
Java Architecture Diary
Written by

Java Architecture Diary

Committed to sharing original, high‑quality technical articles; no fluff or promotional content.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.