You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
spring-cloud-alibaba/spring-cloud-alibaba-examples/sentinel-dubbo-consumer-exa...
fangjian0423 6598768195 refactor dubbo examples & add datasource feature in example & update docs 7 years ago
..
src/main refactor dubbo examples & add datasource feature in example & update docs 7 years ago
pom.xml refactor dubbo examples & add datasource feature in example & update docs 7 years ago
readme-zh.md refactor dubbo examples & add datasource feature in example & update docs 7 years ago
readme.md refactor dubbo examples & add datasource feature in example & update docs 7 years ago

readme.md

Sentinel Dubbo Consumer Example

Project Instruction

This example illustrates how to use Sentinel starter to implement flow control for Spring Cloud applications.

Sentinel is an open-source project of Alibaba. Sentinel takes "traffic flow" as the breakthrough point, and provides solutions in areas such as flow control, concurrency, circuit breaking, and load protection to protect service stability.

Dubbo is a high-performance, java based open source RPC framework.

This example work with 'sentinel-dubbo-provider-example' module and sentinel-dubbo-provider-example module should startup firstly.

This example focus on the integration of Sentinel and Dubbo. You can see more features on sentinel-example.

Demo

Connect to Sentinel

Before we start the demo, let's learn how to connect Sentinel with Dubbo to a Spring Cloud application. Note: This section is to show you how to connect to Sentinel. The configurations have been completed in the following example, so you don't need modify the code any more.

  1. Add dependency spring-cloud-starter-sentinel and dubbo-spring-boot-starter in the pom.xml file in your Spring Cloud project.

     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-sentinel</artifactId>
     </dependency>
    
     <dependency>
         <groupId>com.alibaba.boot</groupId>
         <artifactId>dubbo-spring-boot-starter</artifactId>
     </dependency>
    
  2. Configure flow control rules

    Sentinel provide sentinel-dubbo-adapter module to support dubbo. to support dubbo. sentinel-starter integrates this feature by default.

    sentinel-dubbo-adapter will using Sentinel to handle resource by Dubbo Filter. You just need to define rules.

     FlowRule flowRule = new FlowRule();
     flowRule.setResource("dubboResource");
     flowRule.setCount(10);
     flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
     flowRule.setLimitApp("default");
     FlowRuleManager.loadRules(Collections.singletonList(flowRule));
    

Configure Rules

sentinel-dubbo-api define a service named FooService:

package org.springframework.cloud.alibaba.cloud.examples.FooService;
public interface FooService {
    String hello(String name);
}

The resource name of this service's hello method is org.springframework.cloud.alibaba.cloud.examples.dubbo.FooService:hello(java.lang.String) .

Configure rules

FlowRule flowRule = new FlowRule();
flowRule.setResource("dubboResource");
flowRule.setCount(10);
flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
flowRule.setLimitApp("default");
FlowRuleManager.loadRules(Collections.singletonList(flowRule));

Service Invocation

Using the @Reference annotation to inject service:

@Reference(version = "${foo.service.version}", application = "${dubbo.application.id}",
        url = "dubbo://localhost:12345", timeout = 30000)
private FooService fooService;

Because QPS is 10, we can see that flow control takes effect in this invocation:

FooServiceConsumer service = applicationContext.getBean(FooServiceConsumer.class);

for (int i = 0; i < 15; i++) {
    try {
        String message = service.hello("Jim");
        System.out.println((i + 1) + " -> Success: " + message);
    }
    catch (SentinelRpcException ex) {
        System.out.println("Blocked");
    }
    catch (Exception ex) {
        ex.printStackTrace();
    }
}

Start Application

Start the application in IDE or by building a fatjar.

  1. Start in IDE: Find main class SentinelDubboConsumerApp, and execute the main method.
  2. Build a fatjarExecute command mvn clean package to build a fatjarand run command java -jar sentinel-dubbo-consumer-example.jar to start the application.