Skip to content

Commit

Permalink
update sentinel sample
Browse files Browse the repository at this point in the history
  • Loading branch information
chickenlj committed Apr 3, 2024
1 parent 3e691e6 commit e24a343
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@
public interface DemoService {

String sayHello(String name);

String sayHelloAgain(String name);

String sayHelloConsumerFlowControl(String name);

String sayHelloConsumerDowngrade(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<dubbo.version>3.2.6</dubbo.version>
<sentinel.version>1.8.6</sentinel.version>
<dubbo.version>3.3.0-beta.1</dubbo.version>
<nacos.version>2.2.0</nacos.version>
</properties>

Expand Down Expand Up @@ -80,6 +81,18 @@
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

<!-- sentinel dependency-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-apache-dubbo3-adapter</artifactId>
<version>${sentinel.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
<version>${sentinel.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@
*/
package org.apache.dubbo.samples.consumer;

import com.alibaba.csp.sentinel.adapter.dubbo3.config.DubboAdapterGlobalConfig;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.degrade.circuitbreaker.CircuitBreakerStrategy;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.dubbo.rpc.RpcResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.rpc.AsyncRpcResult;
import org.apache.dubbo.samples.sentinel.DemoService;

import org.slf4j.Logger;
Expand All @@ -26,6 +35,10 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

Expand All @@ -45,12 +58,19 @@ static class DemoTask implements CommandLineRunner {

@Override
public void run(String... args) {
// please run triggerProviderFlowControl() or triggerConsumerFlowControl() separately to verify provider and consumer side flow control.
// triggerProviderFlowControl();
triggerConsumerFlowControl();
}

private void triggerProviderFlowControl() {
Executors.newScheduledThreadPool(1)
.scheduleAtFixedRate(() -> {
logger.info("Start to call remote.");
for (int i = 0; i < 15; i++) {
try {
String result = demoService.sayHello("dubbo");
demoService.sayHelloAgain("dubbo");
logger.info("Call Count:" + i + " Dubbo Remote Return ======> " + result);
} catch (RuntimeException ex) {
if (ex.getMessage().contains("SentinelBlockException: FlowException")) {
Expand All @@ -62,5 +82,78 @@ public void run(String... args) {
}
}, 0, 5000, TimeUnit.MILLISECONDS);
}

private void triggerConsumerFlowControl() {
CountDownLatch latch = new CountDownLatch(5);
for (int i = 0; i < 8; i++) {
Executors.newFixedThreadPool(1)
.submit(() -> {
latch.await();
while(true) {
logger.info("Start to call remote.");
try {
String result = demoService.sayHelloConsumerFlowControl("dubbo");
demoService.sayHelloConsumerDowngrade("dubbo");
logger.info("Call Dubbo Remote Return ======> " + result);
}
catch (RuntimeException ex) {
if (ex.getMessage().contains("SentinelBlockException: FlowException")) {
logger.info("Call Blocked");
}
else {
logger.error("Call Request Failed.", ex);
}
}
Thread.sleep(5000);
}
});
latch.countDown();
}
}
}

@Component
static class SentinelConfig implements CommandLineRunner {
@Override
public void run(String... args) {
FlowRule flowRule = new FlowRule();
flowRule.setResource("org.apache.dubbo.samples.sentinel.DemoService:sayHelloConsumerFlowControl(java.lang.String)");
flowRule.setCount(3);
flowRule.setGrade(RuleConstant.FLOW_GRADE_THREAD);
FlowRuleManager.loadRules(Collections.singletonList(flowRule));
}
}

@Component
static class SentinelCallbackConfig implements CommandLineRunner {
@Override
public void run(String... args) {
// Register fallback handler for consumer.
// If you only want to handle degrading, you need to
// check the type of BlockException.
DubboAdapterGlobalConfig.setConsumerFallback((invoker, invocation, ex) -> {
System.out.println("Blocked by Sentinel: " + ex.getClass().getSimpleName() + ", " + invocation);
return AsyncRpcResult.newDefaultAsyncResult(ex.toRuntimeException(), invocation);
});
}
}

@Component
static class SentinelDowngradeConfig implements CommandLineRunner {
@Override
public void run(String... args) {
List<DegradeRule> rules = new ArrayList<>();
DegradeRule rule = new DegradeRule();
rule.setResource("org.apache.dubbo.samples.sentinel.DemoService:sayHelloConsumerDowngrade(java.lang.String)");
rule.setGrade(CircuitBreakerStrategy.ERROR_RATIO.getType());
rule.setCount(0.2); // Threshold is 20% error ratio
rule.setMinRequestAmount(3);
rule.setStatIntervalMs(10000); // 10s
rule.setTimeWindow(10);
rules.add(rule);
DegradeRuleManager.loadRules(rules);
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,4 @@

# Specify the application name of Dubbo
dubbo.application.name=sentinel-consumer

# Enable token verification for each invocation
dubbo.provider.token=true

# Specify the registry address
# dubbo.registry.address=nacos://localhost:8848?username=nacos&password=nacos
dubbo.registry.address=nacos://${nacos.address:localhost}:8848?username=nacos&password=nacos
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<dubbo.version>3.2.6</dubbo.version>
<dubbo.version>3.3.0-beta.1</dubbo.version>
<nacos.version>2.2.0</nacos.version>
<sentinel.version>1.8.6</sentinel.version>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,20 @@ public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
return "hello, "+ name;
}

@Override
public String sayHelloAgain(String name) {
return "hello, "+ name;
}

@Override
public String sayHelloConsumerFlowControl(String name) {
return "hello, "+ name;
}

@Override
public String sayHelloConsumerDowngrade(String name) {
return "hello, "+ name;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
*/
package org.apache.dubbo.samples.provider;

import com.alibaba.csp.sentinel.adapter.dubbo3.config.DubboAdapterGlobalConfig;
import com.alibaba.dubbo.rpc.RpcResult;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.apache.dubbo.rpc.AsyncRpcResult;
import org.apache.dubbo.samples.sentinel.DemoService;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
Expand All @@ -37,15 +40,48 @@ public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}

// Service level QOS flow control
@Component
static class SentinelConfig implements CommandLineRunner {
static class SentinelServiceConfig implements CommandLineRunner {
@Override
public void run(String... args) {
// Limit DemoService to 10 QPS
FlowRule flowRule = new FlowRule(DemoService.class.getName())
.setCount(10)
.setGrade(RuleConstant.FLOW_GRADE_QPS);
FlowRule flowRule = new FlowRule();
// Note: the resource name here is the interface name.
flowRule.setResource(DemoService.class.getName());
flowRule.setCount(10);
flowRule.setLimitApp("default");
flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
FlowRuleManager.loadRules(Collections.singletonList(flowRule));
}
}

// Method level QOS flow control
@Component
static class SentinelMethodConfig implements CommandLineRunner {
@Override
public void run(String... args) {
// Limit DemoService.sayHelloAgain() method to 5 QPS.
FlowRule flowRule = new FlowRule();
// Note: the resource name here includes the method signature.
flowRule.setResource(DemoService.class.getName() + ":sayHelloAgain(java.lang.String)");
flowRule.setCount(5);
// Note: this will take effect only for the specific consumer whose app name is "sentinel-consumer".
flowRule.setLimitApp("sentinel-consumer");
flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
FlowRuleManager.loadRules(Collections.singletonList(flowRule));
}
}

// Set method gets executed when flow control happens.
@Component
static class SentinelCallbackConfig implements CommandLineRunner {
@Override
public void run(String... args) {
DubboAdapterGlobalConfig.setProviderFallback((invoker, invocation, ex) -> {
System.out.println("Blocked by Sentinel: " + ex.getClass().getSimpleName() + ", " + invocation);
return AsyncRpcResult.newDefaultAsyncResult(ex.toRuntimeException(), invocation);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@

# Specify the application name of Dubbo
dubbo.application.name=sentinel-provider

# Enable token verification for each invocation
dubbo.provider.token=true

# Specify the registry address
# dubbo.registry.address=nacos://localhost:8848?username=nacos&password=nacos
dubbo.protocol.name=tri
dubbo.protocol.port=50051
dubbo.registry.address=nacos://${nacos.address:localhost}:8848?username=nacos&password=nacos

0 comments on commit e24a343

Please sign in to comment.