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>
<dependencies><dependency><groupId>com.tencent.polaris</groupId><artifactId>polaris-all</artifactId></dependency></dependencies>
global:serverConnectors:- id: polarisprotocol: grpcaddresses:# 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 reportingstatReporter:# Description: Whether to Enable Reportingenable: trueplugin: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: 10spushInterval: 10s
import com.tencent.polaris.factory.api.RouterAPIFactory;public static void main(String[] args) throws Exception {RouterAPI routerAPI = RouterAPIFactory.createRouterAPI();}
public class Criteria {// key for consistent hashingprivate String hashKey;}ProcessLoadBalanceRequest request = new ProcessLoadBalanceRequest();// Set the service instances that need to participate in load balancingrequest.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: ringHashrequest.setLbPolicy(String lbPolicy);// If you need to use Hash load balancing, it needs to be setCriteria criteria = new Criteria();request.setCriteria(criteria);
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)
피드백