一、下载sentinel-dashboard控制台应用并在8080端口启动
二、项目添加springboot 和dubbo相关依赖(降级规则并未持久化,如果需要持久化,如果需要持久化降级规则,只需增加nacos相关依赖并在nacos中进行配置,然后配置application.properties或application.yml)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>dubbo-service-provider2</artifactId><version>1.0-SNAPSHOT</version><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>8</source><target>8</target></configuration></plugin></plugins></build><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><dubbo.version>3.1.8</dubbo.version><spring-boot.version>2.7.10</spring-boot.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.7.10</version></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo</artifactId><version>${dubbo.version}</version></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-dependencies-zookeeper</artifactId><type>pom</type></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-registry-nacos</artifactId></dependency><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-apache-dubbo-adapter</artifactId><version>1.8.7</version></dependency><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-transport-simple-http</artifactId><version>1.8.7</version></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-bom</artifactId><version>${dubbo.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-dependencies-zookeeper</artifactId><version>${dubbo.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>2021.0.1</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>2021.0.1.0</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement></project>
三、配置文件
server:port: 9811
dubbo:application:name: dubbo-springboot-demo-providerprotocol:name: dubboport: 28095registry:address: zookeeper://xx.xx.xx.xx:2181
四、配置jvm参数
-Djava.net.perferIPv4Stack=true
-Dcsp.sentinel.api.port=8720
-Dcsp.sentinel.dashboard.server=localhost:8080
-Dproject.name=spring-cloud-sentinel-dubbo-provider
五、定义服务接口、服务实现(其中服务实现中故意制造出了除零异常用来测试降级,handleException方法是降级时被调用的方法)
package cn.edu.tju.service;public interface DemoService {String sayHello(String name);
}
package cn.edu.tju.service;import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.google.gson.JsonObject;
import org.apache.dubbo.config.annotation.DubboService;import java.util.Date;@DubboService(loadbalance = "")
public class DemoServiceImpl implements DemoService {@SentinelResource(value = "sayHello", fallback = "handleException")public String sayHello(String name) {int t = 1/0;return "Hay " + name;}public String handleException(String name,Throwable ex) {String className = ex.getClass().getName();if(className.equals("com.alibaba.csp.sentinel.slots.block.degrade.DegradeException")){JsonObject jsonObject=new JsonObject();jsonObject.addProperty("code",500);jsonObject.addProperty("message","降级原因:"+ex.getMessage()+" "+ex.getClass().getName());return jsonObject.toString();}else {JsonObject jsonObject=new JsonObject();jsonObject.addProperty("code",500);jsonObject.addProperty("message","异常原因:"+ex.getMessage()+" "+ex.getClass().getName());return jsonObject.toString();}}}
六、在sentinel-dashboard控制台配置降级规则
其中资源名要和接口实现中方法上@SentinelResource注解上的value值相同
七、客户端调用dubbo服务sayHello接口,满足降级的条件后,将从接口获取到类似如下的响应(其中getDubbo这个接口调用了上面的sayHello接口)
可见降级规则起作用了