1.pom中增加spring cloud alibaba相关依赖。
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.edu.tju</groupId>
<artifactId>spring-boot-sentinel-degrade</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.0.0.RELEASE</version>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.配置application.properties:
server.port=8819
spring.application.name=myDegrade
spring.datasource.url=jdbc:mysql://139.198.xx.xx:3306/test
spring.datasource.username=root
spring.datasource.password=xx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
management.endpoints.web.exposure.include=*
3.创建降级规则配置类:
package cn.edu.tju.config;
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 org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
@Configuration
public class DegradeConfig {
@PostConstruct
public void initDegradeRule(){
List<DegradeRule> degradeRuleList=new ArrayList<>();
//定义降级规则
DegradeRule degradeRule=new DegradeRule();
//设置降级规则所对应的资源名称
degradeRule.setResource("helloWorld");
//根据发生异常的次数进行降级
degradeRule.setGrade(RuleConstant.DEGRADE_GRADE_EXCEPTION_COUNT);
//至少要有两次请求才会降级,第一次请求时发生异常也不会降级
degradeRule.setMinRequestAmount(2);
//发生异常的次数达到1后开始降级
degradeRule.setCount(1);
//降级的时间窗口,在10秒内,所有的请求都直接降级
degradeRule.setTimeWindow(10);
//把降级规则添加到降级规则列表
degradeRuleList.add(degradeRule);
//把降级列表添加到降级规则管理器
DegradeRuleManager.loadRules(degradeRuleList);
}
}
4.编写controller类,并对相应的接口使用@SentinelResource注解进行相关配置
package cn.edu.tju.controller;
import com.alibaba.csp.sentinel.EntryType;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@RestController
public class Demo {
@RequestMapping("/hi")
//value制定资源名称,brockHandler属性指定降级之后调用的方法
@SentinelResource(value="helloWorld",entryType = EntryType.IN,blockHandler = "myBlockHandler")
public String hi(){
int i=1/0;
return "hi:"+new Date().toLocaleString();
}
//降级之后调用的方法,参数和返回类型与源方法一致,但参数要增加一个BlockException类型的参数
public String myBlockHandler(BlockException ex){
return "服务被降级了";
}
}
5.创建启动类,并启动程序
package cn.edu.tju;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Start {
public static void main(String[] args) {
SpringApplication.run(Start.class,args);
}
}
6.在浏览器中快速刷新访问:http://localhost:8819/hi,结果如下: