学习目标
模拟限流降级
下载控制台地址
https://github.com/alibaba/Sentinel/releases
下载的1.8.3版本
启动
java -jar .\sentinel-dashboard-1.8.3.jar
访问
http://localhost:8080/#/login
账号密码均为sentinel
搭建项目
sentinel-8401
pom
<dependencies>
<!--SpringCloud ailibaba nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--SpringCloud ailibaba sentinel-datasource-nacos 后续做持久化用到-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<!--SpringCloud ailibaba sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!--openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- SpringBoot整合Web组件+actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--日常通用jar包配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.6.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
application.yml
server:
port: 8401
spring:
application:
name: sentinel-service
cloud:
nacos:
discovery:
#Nacos服务注册中心地址
server-addr: localhost:8848
sentinel:
transport:
#配置Sentinel dashboard地址
dashboard: localhost:8080
#默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
port: 8719
management:
endpoints:
web:
exposure:
include: '*'
启动类
package com.xiong;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class Sentinel8401 {
public static void main(String[] args) {
SpringApplication.run(Sentinel8401.class,args);
}
}
业务类
package com.xiong.controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
// 测试自定义异常
@GetMapping("/testA")
//testAName 断流配置用的名称、handleException返回的自定义异常
@SentinelResource(value = "testAName",blockHandler = "handleException")
public String testA()
{
return "------testA";
}
// 自定义异常
public String handleException(BlockException exception)
{
return "自定义异常!!!!";
}
// 测试全局自定义异常
@GetMapping("/rateLimit/customerBlockHandler")
@SentinelResource(value = "customerBlockHandler", blockHandlerClass = CustomerBlockHandler.class, blockHandler = "handleException2")
public String customerBlockHandler()
{
return "正常访问~~";
}
}
全局异常管理
package com.xiong.controller;
import com.alibaba.csp.sentinel.slots.block.BlockException;
public class CustomerBlockHandler
{
public static String handleException2(BlockException exception){
return "自定义全局异常";
}
}
启动及测试
启动
启动 nacos
启动 sentinel
启动 8401
测试
先访问
http://localhost:8401/rateLimit/customerBlockHandler
访问成功之后,sentinel中出现此资源的列表
点击其中的按钮可以进行配置
配置完成后再次点击进行测试
733

被折叠的 条评论
为什么被折叠?



