目录
一、Seninel简介
二、和Hystrix的区别
三、sentinel小程序开发定制可视化界面安装
小程序开发定制下载对应版本的sentinel的jar包,小程序开发定制通过终端命令:
java -jar jar包名
启动
小程序开发定制访问对应路径:小程序开发定制控制台如下:
四、在springcloudalibaba中整合sentinel
(1)添加依赖
- <!--sentinel启动器-->
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
- </dependency>
(2)配置yml
- server:
- port: 8002
-
- spring:
- application:
- name: WXL-DEV-SERVICE-2
- cloud:
- sentinel:
- transport:
- dashboard: 127.0.0.1:8080
(3)启动服务,再访问服务后,观察控制台:因为访问接口以后才会注册到sentinel当中。
五、流控规则
(1)实时监控,可用于查看接口访问情况
(2)簇点链路,可以对对应的资源流控降级
可以设置阀值来流控:
(3)QPS流控
可以看到当每秒超过2次时被流控:
流控文字可自定义:
- @GetMapping("/world")
- @SentinelResource(value = "helloWorld",blockHandlerClass = TestController.class,blockHandler = "helloBlock")
- public String helloWorld() {
- return "Hello world";
- }
-
- public static String helloBlock(BlockException e){
- return "你已被流控";
- }
value将该方法定义为sentinel的资源,blockHandlerClass指明流控处理的类,blockHandler是流控时调用的方法。
这里需要注意处理异常的方法必须是静态方法添加static, 并需要添加sentinel的异常参数BlockException。
统一异常控制处理
上面通过注解实现流控灵活性更高,对于需要统一管理的流控处理,我们可以通过统一异常处理来实现。可以自定义处理不同类型的限流。
只需实现对应接口即可,案例代码如下:
- package com.dragonwu.exception;
-
- import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
- import com.alibaba.csp.sentinel.slots.block.BlockException;
- import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
- import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
- import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
- import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
- import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import org.springframework.http.MediaType;
- import org.springframework.stereotype.Component;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.util.HashMap;
- import java.util.Map;
-
- @Component
- public class MyBlockExceptionHandler implements BlockExceptionHandler {
-
- @Override
- public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws Exception {
- System.out.println("BlockExceptioonHandler ++++++++++++++++++++++++++"+e.getRule());
-
- Map<Integer,String> hashMap=new HashMap<>();
-
- if(e instanceof FlowException){
- hashMap.put(100,"接口限流了");
- }else if(e instanceof DegradeException){
- hashMap.put(101,"服务降级了");
- }else if(e instanceof ParamFlowException){
- hashMap.put(102,"热点参数限流了");
- }else if(e instanceof SystemBlockException){
- hashMap.put(103,"触发系统保护规则了");
- }else if(e instanceof AuthorityException){
- hashMap.put(104,"授权规则不通过");
- }
-
- //返回json数据
- httpServletResponse.setStatus(500);
- httpServletResponse.setCharacterEncoding("utf-8");
- httpServletResponse.setContentType(MediaType.APPLICATION_JSON_VALUE);
- new ObjectMapper().writeValue(httpServletResponse.getWriter(),hashMap);
- }
- }
-
(4)线程流控
(5)关联限流
这里的意思是如果/hello/add接口一秒钟之内访问超过2次,则/hello/query会被限流。
(6)熔断降级
也要设置熔断时长,熔断时长过完之后会进入半开状态,即若下一次请求为慢请求则再次熔断,直到第一次请求不是慢请求才会恢复正常状态。
六、OpenFeign整合Sentinel
(1)导入依赖:
- <!--OpenFeign-->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-openfeign</artifactId>
- </dependency>
(2)调用者开发整合配置:
- feign:
- sentinel:
- enabled: true #开启openFeign对sentinel的整合
(3)添加openFeign调用接口
- package com.wxl.feign;
-
- import org.springframework.cloud.openfeign.FeignClient;
- import org.springframework.web.bind.annotation.GetMapping;
-
- @FeignClient(name = "WXL-DEV-SERVICE-2", path = "/hello",fallback = ServiceFailFeign.class)
- public interface Service1HelloInterface {
- @GetMapping("/world")
- String helloWorld();
- }
并且这里添加参数fallback值为失败时回调的实现类。
实现类如下:
- package com.wxl.feign;
-
- import org.springframework.stereotype.Component;
-
- @Component
- public class ServiceFailFeign implements Service1HelloInterface{
- public String helloWorld() {
- return "降级了!!!";
- }
- }
当接口请求失败时便会调用失败类里的该方法。
这里我们为了使用效果,在服务生产者的接口里故意写入报错代码:
- @GetMapping("/world")
- public String helloWorld() {
- int i=1/0;
- return "Hello world";
- }
请求该消费者服务接口:
调用了失败回调方法!
七、规则持久化
(1)引入依赖
- <!--sentinel持久化存储-->
- <dependency>
- <groupId>com.alibaba.csp</groupId>
- <artifactId>sentinel-datasource-nacos</artifactId>
- </dependency>
(2)为nacos添加配置
更多见: