整合Alibaba Sentinel 教程
1、下载服务程序
1)下载
2、启动: java -jar ***.jar 8080端口不能被占用
3、配置进项目
pom
<!--springcloud alibaba sentinel-datasource-nacos 后续做持久化用到--> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency> <!--springcloud alibaba sentinel--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency>
yml:
spring>cloud的子级:
sentinel: eager: true transport: # 配置Sentinel dashboard地址 dashboard: localhost:8080 # 默认8719端口,假如被占用会自动从8719开始一次+1扫描,直至找到被占用的端口。 port: 8719
主启动类
package com.atguigu.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class SentinelMainApp8401 { public static void main(String[] args) { SpringApplication.run(SentinelMainApp8401.class,args); } }
测试:启动项目后,打开 Sentinel 控制台:http://127.0.0.1:8080 ,比如你有/getHello这个API,这里不用管统一前缀,添加一个限流规则:
访问这个API,只要我们一秒请求的频率超1,就会触发这个限流规则。狂点刷新:
如何自定义blockHandle
全局: 在项目中创建这么一个类
package com.zjazn.smallarea.sentinelBlockHandle; import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler; import com.alibaba.csp.sentinel.slots.block.BlockException; import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException; import com.alibaba.csp.sentinel.slots.block.flow.FlowException; import com.alibaba.fastjson.JSON; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * 自定义sentinel异常返回信息,直接代替原有的blockhandle返回 */ @Component public class ExceptionHandlerPage implements UrlBlockHandler { @Override public void blocked(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws IOException { // BlockException 异常接口,其子类为Sentinel五种规则异常的实现类 // AuthorityException 授权异常 // DegradeException 降级异常 // FlowException 限流异常 // ParamFlowException 参数限流异常 // SystemBlockException 系统负载异常 ResponseData data = new ResponseData(); if (e instanceof FlowException) { data = new ResponseData(20001, "接口被限流了。"); } else if (e instanceof DegradeException) { data = new ResponseData(20001, "接口被降级了。"); } httpServletResponse.setContentType("application/json;charset=utf-8"); httpServletResponse.getWriter().write(JSON.toJSONString(data)); } } /** * 定义返回的实体类,字段根据需要添加 */ @Data // 生成getter/setter/tostring/equals @AllArgsConstructor // 全参构造 @NoArgsConstructor // 无参构造 class ResponseData { private int code; private String message; }
局部:
在API方法上,添加一个注解配置:
@SentinelResource(value = "globalBlockHandler", blockHandlerClass = 方法所在类名.class, blockHandler = "方法名") 注意返回值需要与之对应
4、持久化配置
这里需要用到nacos
当我们重新启动项目时,之前的规则就会失效了,我们进行持久化配置。
1) yml配置:
#添加Nacos数据源配置
datasource:
ds1: # 数据源1
nacos:
server-addr: localhost:3333
dataId: ${spring.application.name}
groupId: SERVICE_GROUP
data-type: json
rule-type: flow # 流控规则
2)在nacos上添加:
3)测试:重新启动项目,再进行之前测试,发现我们配置的生效。http://127.0.0.1:53022/product/getHello