tencent cloud

Service Registry and Governance

Dynamic Routing

PDF
フォーカスモード
フォントサイズ
最終更新日: 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 routing 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 server IP address details, 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 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: Set the Routing Policy, Instance List, Metadata, and Other Information.

// ProcessRoutersRequest: Struct for executing routing requests
type ProcessRoutersRequest struct {
// Optional parameter to set the routing plugin expected for this routing request
// The currently supported routing plugins are as follows
// - Custom Routing: ruleBasedRouter
// - Nearby Routing: nearbyBasedRouter
// - Metadata Routing: dstMetaRouter
Routers []string
// Optional parameter. Caller service information. You can set the traffic tag information for this request via ServiceInfo.Metadata
SourceService ServiceInfo
// Required parameter. The list of instances to be routed for services.
// 1. InstancesResponse, returned from ConsumerAPI.GetAllInstances.
// 2. DefaultServiceInstances, for user to construct manually.
DstInstances ServiceInstances
// Optional parameter, corresponding to the method($method) Tag in routing rules
Method string
// Optional. Timeout for a single query. Defaults to the global timeout configuration if not specified
// Total maximum timeout duration is (1+RetryCount) * Timeout
Timeout *time.Duration
// Optional. Retry count. Defaults to the global timeout configuration if not specified
RetryCount *int
}
Note:
If currently ProcessRoutersRequest does not yet support the AddArgument method, and the server version >= 1.12.0, the corresponding key names for SourceService.Metadata are as follows:
Path: $path
Method: $method
Request header: $header.{Tag key}
Request parameter: $query.{Tag key}
Request COOKIE: $cookie.{Tag key}
Caller IP: $caller_ip
Custom: {Tag key}

3. Perform Service Routing

After initializing the ProcessRoutersRequest struct, you simply need to call the RouterAPI.processRouters method to complete service routing.
ProcessRoutersResponse resp = routerAPI.processRouters(registerRequest)


ヘルプとサポート

この記事はお役に立ちましたか?

フィードバック