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.DiscoveryAPIFactory;public static void main(String[] args) throws Exception {ProviderAPI providerAPI = DiscoveryAPIFactory.createProviderAPI();}
public class InstanceRegisterRequest extends CommonProviderBaseEntity {// ...}
InstanceRegisterRequest struct, simply call the ProviderAPI.RegisterInstance method to complete instance registration. The RegisterInstance method automatically maintains heartbeat reporting for the instance internally.InstanceRegisterResponse registerResp = providerAPI.registerInstance(registerRequest)
import com.tencent.polaris.factory.api.DiscoveryAPIFactory;public static void main(String[] args) throws Exception {ConsumerAPI consumerAPI = DiscoveryAPIFactory.createConsumerAPI();}
GetAllInstancesRequest request = new GetAllInstancesRequest();// Set the service namespacerequest.setNamespace(String namespace);// Set the service namerequest.setService(String service);// Set the timeout periodrequest.setTimeoutMs(long timeoutMs);// Call ConsumerAPI to execute the requestconsumerAPI.getAllInstance(request);
GetInstancesRequest request = new GetInstancesRequest();// Set the service namespacerequest.setNamespace(String namespace);// Set the service namerequest.setService(String service);// Optional, set caller service information, only for routing rule matchingSourceService serviceInfo = new SourceService();// Set the caller service namespaceserviceInfo.setNamespace(String namespace);// Set the caller service nameserviceInfo.setService(String service);// Set the request Tag information for the callerserviceInfo.setArguments(Set<RouteArgument> arguments);request.setServiceInfo(serviceInfo);// Set the timeout periodrequest.setTimeoutMs(long timeoutMs);// Call ConsumerAPI to execute the requestconsumerAPI.getInstances(request);
GetOneInstanceRequest.ServiceInfo.Metadata property will trigger the custom routing process.GetOneInstanceRequest.Metadata property will trigger the metadata routing process.public class Criteria {/*** Specify the CLB policy*/private String lbPolicy;/*** key for consistent hashing*/private String hashKey;}GetOneInstanceRequest request = new GetOneInstanceRequest();// Set the service namespacerequest.setNamespace(String namespace);// Set the service namerequest.setService(String service);// Optional. The metadata, only used for filtering by the dstMetadata routing plugin.request.setMetadata(Map<String, String> metadata);// Optional. Set the fallback mechanism for metadata routing.// The currently supported fallback mechanisms for metadata routing are as follows:// - Default: no failover: METADATAFAILOVERNONE("metadataFailoverNone")// - Failover to return all nodes: METADATAFAILOVERALL("metadataFailoverAll")// - Returning nodes without the metadata routing key: METADATAFAILOVERNOTKEY("metadataFailoverNoKey")request.setMetadataFailoverType();// Optional. Corresponds to the Method in the request Tag of custom routing rulesrequest.setMethod(String method);// If you need to use Hash load balancing, it needs to be setCriteria criteria = new Criteria();request.setCriteria(criteria);// Optional, set caller service information, only for routing rule matchingSourceService serviceInfo = new SourceService();// Set the caller service namespaceserviceInfo.setNamespace(String namespace);// Set the caller service nameserviceInfo.setService(String service);// Set the request Tag information for the callerserviceInfo.setArguments(Set<RouteArgument> arguments);request.setServiceInfo(serviceInfo);// Set the timeout periodrequest.setTimeoutMs(long timeoutMs);// Call ConsumerAPI to execute the requestconsumerAPI.getOneInstance(request);
피드백