tencent cloud

Service Registry and Governance

CLB

PDF
Modo Foco
Tamanho da Fonte
Última atualização: 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 CLB 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 polaris-sdk version information, 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.yaml

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 RouterAPI Instance

import com.tencent.polaris.factory.api.RouterAPIFactory;

public static void main(String[] args) throws Exception {
RouterAPI routerAPI = RouterAPIFactory.createRouterAPI();
}

2. Register the Request Body and Set the Execution Load Balancing Instance List and Load Balancing Policy Information.

public class Criteria {
// key for consistent hashing
private String hashKey;
}

ProcessLoadBalanceRequest request = new ProcessLoadBalanceRequest();
// Set the service instances that need to participate in load balancing
request.setDstInstances(ServiceInstances dstInstances);
// Set the load balancing policy
// Currently supported load balancing policies are as follows
// - Weighted random load balancing: weightedRandom
// - Weighted consistent load balancing: ringHash
request.setLbPolicy(String lbPolicy);

// If you need to use Hash load balancing, it needs to be set
Criteria criteria = new Criteria();
request.setCriteria(criteria);

3. Load Balancing

When you use ConsumerAPI.getAllInstances or ConsumerAPI.getInstances to obtain the service instance list, after completing the initialization of ProcessLoadBalanceRequest, you only need to call the RouterAPI.processLoadBalance method to complete load balancing.
ProcessLoadBalanceResponse resp = routerAPI.processLoadBalance(request)


Ajuda e Suporte

Esta página foi útil?

comentários