tencent cloud

Service Registry and Governance

Service Throttling

PDF
Focus Mode
Font Size
Last updated: 2026-05-07 18:12:23

Scenarios

This document describes how to develop a Java application locally, access Polaris (North Star) via the polaris-sdk, and implement the service-level traffic throttling feature.

Prerequisite

Before development, please ensure you have downloaded and installed Java and Maven.

Operation Steps

Step 1: Introduce Polaris Dependencies

1. Introduce the polaris sdk Dependency

Modify the pom.xml in the application's root directory to add dependencyManagement for polaris-java:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-dependencies</artifactId>
<version>${version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Note:
For detailed information about the polaris-sdk version, see: Version Information.

2. Introduce polaris starter

<dependencies>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-all</artifactId>
</dependency>
</dependencies>

Step 2: Add Polaris Configuration File polaris.yml

1. Create a polaris.yml file in the project's main/resources directory to initialize the polaris-java SDK.
2. Configure the application name, Polaris (North Star) server IP address, and other information in the polaris.yml file. For details about the server IP address, refer to: Engine Management > Client Access Address.
global:
serverConnectors:
- id: polaris
protocol: grpc
addresses:
# The IP address needs to be replaced with the client access IP address of the North Star engine you created.
- 127.0.0.1:8091
# Description: Configuration related to monitoring and log data reporting
statReporter:
# Description: Whether to Enable Reporting
enable: true
plugin:
prometheus:
type: push
# Description: Configure the pushgateway IP address, effective only when type == push
# The IP address needs to be replaced with the client access IP address of the North Star engine you created.
address: 127.0.0.1:9091
# Description: Set the execution interval for pushing metric data to pushgateway
# Range: [1s:...], Default value: 10s
pushInterval: 10s
For more polaris.yml configuration information, see default-config.yml.

Step 3: Application Development

1.Initialize the LimitAPI Instance:

import com.tencent.polaris.ratelimit.factory.LimitAPIFactory;


public static void main(String[] args) throws Exception {
LimitAPI limitAPI = LimitAPIFactory.createLimitAPI();
}

2.Request Quota

QuotaRequest quotaRequest = new QuotaRequest();
// Configure rate limiting service information: set namespace information
quotaRequest.setNamespace(String namespace);
// Configure rate limiting service information: specify service name information
quotaRequest.setService(String service);
// Set the method information for this invocation
quotaRequest.setMethod(String method);
// Set the request Tag
quotaRequest.setArguments(Set<Argument> arguments)
// Set the request quota quantity to be requested
quotaRequest.setCount(1);
Note:
If the current QuotaRequest does not yet support the AddArgument method, and the server version is >= 1.11.0, the key names corresponding to SetLabels are as follows:
Path: $path
Method: $method
Request header: $header.{Tag key}
Request parameter: $query.{Tag key}
Caller service: $caller_service
Caller IP: $caller_ip
Custom: {Tag key}

3.Initiate a Quota Application Request

After the request is received and the QuotaRequest struct is initialized, simply call the LimitAPI.GetQuota method to complete the application for this request quota.
QuotaResponse resp = limitAPI.getQuota(registerRequest);


Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback