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

com.tencent.polaris.circuitbreak.api.CircuitBreakAPI interface.FunctionalDecorator contains the following methods:// Create a CircuitBreakAPI instanceCircuitBreakAPI circuitBreakAPI = CircuitBreakAPIFactory.createCircuitBreakAPI();// Create a FunctionalDecorator by providing the service name (testService1) and namespace (default)FunctionalDecoratorRequest makeDecoratorRequest = new FunctionalDecoratorRequest();makeDecoratorRequest.setService(new ServiceKey("default", "testService1"));FunctionalDecorator decorator = circuitBreakAPI.makeFunctionalDecorator(makeDecoratorRequest);// Wrap the functional interfaceConsumer<Integer> integerConsumer = decorator.decorateConsumer(new Consumer<Integer>() {@Overridepublic void accept(Integer object) {// Execute the service invocation...}});// Perform service invocation by executing the functional interface// During the calling process, if circuit breaking occurs, a CallAbortedException will be thrownfor (int i = 0; i < 500; i++) {try {integerConsumer.accept(i);} catch(CallAbortedException e) {e.printStackTrace();}}
// Create a CircuitBreakAPI instanceCircuitBreakAPI circuitBreakAPI = CircuitBreakAPIFactory.createCircuitBreakAPI();// Create a FunctionalDecorator by providing the service name (testService1), namespace (default), and method name (foo)FunctionalDecoratorRequest makeDecoratorRequest = new FunctionalDecoratorRequest();makeDecoratorRequest.setService(new ServiceKey("default", "testService1"));makeDecoratorRequest.setMethod("foo");FunctionalDecorator decorator = circuitBreakAPI.makeFunctionalDecorator(makeDecoratorRequest);// Wrap the functional interfaceConsumer<Integer> integerConsumer = decorator.decorateConsumer(new Consumer<Integer>() {@Overridepublic void accept(Integer object) {// Execute the service interface invocation...}});// Perform service invocation by executing the functional interface// During the calling process, if circuit breaking occurs, a CallAbortedException will be thrownfor (int i = 0; i < 500; i++) {try {integerConsumer.accept(i);} catch(CallAbortedException e) {e.printStackTrace();}}
import com.tencent.polaris.factory.api.DiscoveryAPIFactory;public static void main(String[] args) throws Exception {ConsumerAPI consumerAPI = DiscoveryAPIFactory.createConsumerAPI();// Perform service routing to select a single instance, during which instances in circuit breaking state will be automatically excludedGetOneInstanceRequest getOneInstanceRequest = new GetOneInstanceRequest();getOneInstanceRequest.setNamespace("default");getOneInstanceRequest.setService("testService1");InstancesResponse oneInstance = consumerAPI.getOneInstance(getOneInstanceRequest);Instance targetInstance = oneInstance.getInstances()[0];// Execute the service invocation// invoke rpc call with targetInstance// Report the invocation results for circuit breaking judgmentServiceCallResult result = new ServiceCallResult();result.setNamespace(namespace);result.setService(service);result.setHost(targetInstance.getHost());result.setPort(targetInstance.getPort());// Set the return coderesult.setRetCode(code);// Set the invocation latencyresult.setDelay(delay);// Use updateServiceCallResult to report the invocation resultsconsumerAPI.updateServiceCallResult(result);}
Was this page helpful?
You can also Contact sales or Submit a Ticket for help.
Help us improve! Rate your documentation experience in 5 mins.
Feedback