小程序开发定制SpringCloud 集成Sentinel

目录


一、Seninel简介

 

二、和Hystrix的区别

三、sentinel小程序开发定制可视化界面安装

 小程序开发定制下载对应版本的sentinel的jar包,小程序开发定制通过终端命令:

java -jar jar包名

启动

 小程序开发定制访问对应路径:小程序开发定制控制台如下:

四、在springcloudalibaba中整合sentinel

(1)添加依赖

  1. <!--sentinel启动器-->
  2. <dependency>
  3. <groupId>com.alibaba.cloud</groupId>
  4. <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
  5. </dependency>

(2)配置yml

  1. server:
  2. port: 8002
  3. spring:
  4. application:
  5. name: WXL-DEV-SERVICE-2
  6. cloud:
  7. sentinel:
  8. transport:
  9. dashboard: 127.0.0.1:8080

(3)启动服务,再访问服务后,观察控制台:因为访问接口以后才会注册到sentinel当中。

五、流控规则

(1)实时监控,可用于查看接口访问情况

(2)簇点链路,可以对对应的资源流控降级

 可以设置阀值来流控:

(3)QPS流控

可以看到当每秒超过2次时被流控:

 流控文字可自定义:

  1. @GetMapping("/world")
  2. @SentinelResource(value = "helloWorld",blockHandlerClass = TestController.class,blockHandler = "helloBlock")
  3. public String helloWorld() {
  4. return "Hello world";
  5. }
  6. public static String helloBlock(BlockException e){
  7. return "你已被流控";
  8. }

value将该方法定义为sentinel的资源,blockHandlerClass指明流控处理的类,blockHandler是流控时调用的方法。

这里需要注意处理异常的方法必须是静态方法添加static, 并需要添加sentinel的异常参数BlockException。

统一异常控制处理

上面通过注解实现流控灵活性更高,对于需要统一管理的流控处理,我们可以通过统一异常处理来实现。可以自定义处理不同类型的限流。

只需实现对应接口即可,案例代码如下:

  1. package com.dragonwu.exception;
  2. import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
  3. import com.alibaba.csp.sentinel.slots.block.BlockException;
  4. import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
  5. import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
  6. import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
  7. import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
  8. import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
  9. import com.fasterxml.jackson.databind.ObjectMapper;
  10. import org.springframework.http.MediaType;
  11. import org.springframework.stereotype.Component;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. @Component
  17. public class MyBlockExceptionHandler implements BlockExceptionHandler {
  18. @Override
  19. public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws Exception {
  20. System.out.println("BlockExceptioonHandler ++++++++++++++++++++++++++"+e.getRule());
  21. Map<Integer,String> hashMap=new HashMap<>();
  22. if(e instanceof FlowException){
  23. hashMap.put(100,"接口限流了");
  24. }else if(e instanceof DegradeException){
  25. hashMap.put(101,"服务降级了");
  26. }else if(e instanceof ParamFlowException){
  27. hashMap.put(102,"热点参数限流了");
  28. }else if(e instanceof SystemBlockException){
  29. hashMap.put(103,"触发系统保护规则了");
  30. }else if(e instanceof AuthorityException){
  31. hashMap.put(104,"授权规则不通过");
  32. }
  33. //返回json数据
  34. httpServletResponse.setStatus(500);
  35. httpServletResponse.setCharacterEncoding("utf-8");
  36. httpServletResponse.setContentType(MediaType.APPLICATION_JSON_VALUE);
  37. new ObjectMapper().writeValue(httpServletResponse.getWriter(),hashMap);
  38. }
  39. }

 (4)线程流控

(5)关联限流

这里的意思是如果/hello/add接口一秒钟之内访问超过2次,则/hello/query会被限流。

(6)熔断降级

 也要设置熔断时长,熔断时长过完之后会进入半开状态,即若下一次请求为慢请求则再次熔断,直到第一次请求不是慢请求才会恢复正常状态。

六、OpenFeign整合Sentinel

(1)导入依赖:

  1. <!--OpenFeign-->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-openfeign</artifactId>
  5. </dependency>

(2)调用者开发整合配置:

  1. feign:
  2. sentinel:
  3. enabled: true #开启openFeign对sentinel的整合

(3)添加openFeign调用接口

  1. package com.wxl.feign;
  2. import org.springframework.cloud.openfeign.FeignClient;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. @FeignClient(name = "WXL-DEV-SERVICE-2", path = "/hello",fallback = ServiceFailFeign.class)
  5. public interface Service1HelloInterface {
  6. @GetMapping("/world")
  7. String helloWorld();
  8. }

并且这里添加参数fallback值为失败时回调的实现类。

实现类如下:

  1. package com.wxl.feign;
  2. import org.springframework.stereotype.Component;
  3. @Component
  4. public class ServiceFailFeign implements Service1HelloInterface{
  5. public String helloWorld() {
  6. return "降级了!!!";
  7. }
  8. }

当接口请求失败时便会调用失败类里的该方法。

这里我们为了使用效果,在服务生产者的接口里故意写入报错代码:

  1. @GetMapping("/world")
  2. public String helloWorld() {
  3. int i=1/0;
  4. return "Hello world";
  5. }

请求该消费者服务接口:

调用了失败回调方法! 

七、规则持久化

(1)引入依赖

  1. <!--sentinel持久化存储-->
  2. <dependency>
  3. <groupId>com.alibaba.csp</groupId>
  4. <artifactId>sentinel-datasource-nacos</artifactId>
  5. </dependency>

(2)为nacos添加配置

更多见:

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发