• sentinel整合ribbon+fallback(5)


    前期准备:

    启动nacos和sentinel

    提供者9003/9004 (以9003为样本)

    新建cloudalibaba-provider-payment9003/9004

    pom文件

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <parent>
    6. <artifactId>mscloud03artifactId>
    7. <groupId>com.atguigu.springcloudgroupId>
    8. <version>1.0-SNAPSHOTversion>
    9. parent>
    10. <modelVersion>4.0.0modelVersion>
    11. <artifactId>cloudalibaba-provider-payment9003artifactId>
    12. <dependencies>
    13. <dependency>
    14. <groupId>com.alibaba.cloudgroupId>
    15. <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
    16. dependency>
    17. <dependency>
    18. <groupId>com.atguigu.springcloudgroupId>
    19. <artifactId>cloud-api-commonsartifactId>
    20. <version>${project.version}version>
    21. dependency>
    22. <dependency>
    23. <groupId>org.springframework.bootgroupId>
    24. <artifactId>spring-boot-starter-webartifactId>
    25. dependency>
    26. <dependency>
    27. <groupId>org.springframework.bootgroupId>
    28. <artifactId>spring-boot-starter-actuatorartifactId>
    29. dependency>
    30. <dependency>
    31. <groupId>org.springframework.bootgroupId>
    32. <artifactId>spring-boot-devtoolsartifactId>
    33. <scope>runtimescope>
    34. <optional>trueoptional>
    35. dependency>
    36. <dependency>
    37. <groupId>org.projectlombokgroupId>
    38. <artifactId>lombokartifactId>
    39. <optional>trueoptional>
    40. dependency>
    41. <dependency>
    42. <groupId>org.springframework.bootgroupId>
    43. <artifactId>spring-boot-starter-testartifactId>
    44. <scope>testscope>
    45. dependency>
    46. dependencies>
    47. project>

    yml文件

    1. server:
    2. port: 9003
    3. spring:
    4. application:
    5. name: nacos-payment-provider
    6. cloud:
    7. nacos:
    8. discovery:
    9. server-addr: localhost:8848 #配置Nacos地址
    10. management:
    11. endpoints:
    12. web:
    13. exposure:
    14. include: '*'

    配置9004的时候记得修改端口 

    主启动类

    1. @SpringBootApplication
    2. @EnableDiscoveryClient
    3. public class PaymentMain9003
    4. {
    5. public static void main(String[] args) {
    6. SpringApplication.run(PaymentMain9003.class, args);
    7. }
    8. }

    业务类

    1. @RestController
    2. public class PaymentController
    3. {
    4. @Value("${server.port}")
    5. private String serverPort;
    6. public static HashMap hashMap = new HashMap<>();
    7. static
    8. {
    9. hashMap.put(1L,new Payment(1L,"28a8c1e3bc2742d8848569891fb42181"));
    10. hashMap.put(2L,new Payment(2L,"bba8c1e3bc2742d8848569891ac32182"));
    11. hashMap.put(3L,new Payment(3L,"6ua8c1e3bc2742d8848569891xt92183"));
    12. }
    13. @GetMapping(value = "/paymentSQL/{id}")
    14. public CommonResult paymentSQL(@PathVariable("id") Long id)
    15. {
    16. Payment payment = hashMap.get(id);
    17. CommonResult result = new CommonResult(200,"from mysql,serverPort: "+serverPort,payment);
    18. return result;
    19. }
    20. }

    测试1

    启动9003访问localhost:9003/paymentSQL/1 

     

    消费者84 

    新建cloudalibaba-consumer-nacos-order84 

    pom文件

     

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <parent>
    6. <artifactId>mscloud03artifactId>
    7. <groupId>com.atguigu.springcloudgroupId>
    8. <version>1.0-SNAPSHOTversion>
    9. parent>
    10. <modelVersion>4.0.0modelVersion>
    11. <artifactId>cloudalibaba-consumer-nacos-order84artifactId>
    12. <dependencies>
    13. <dependency>
    14. <groupId>com.alibaba.cloudgroupId>
    15. <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
    16. dependency>
    17. <dependency>
    18. <groupId>com.alibaba.cloudgroupId>
    19. <artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
    20. dependency>
    21. <dependency>
    22. <groupId>com.atguigu.springcloudgroupId>
    23. <artifactId>cloud-api-commonsartifactId>
    24. <version>${project.version}version>
    25. dependency>
    26. <dependency>
    27. <groupId>org.springframework.bootgroupId>
    28. <artifactId>spring-boot-starter-webartifactId>
    29. dependency>
    30. <dependency>
    31. <groupId>org.springframework.bootgroupId>
    32. <artifactId>spring-boot-starter-actuatorartifactId>
    33. dependency>
    34. <dependency>
    35. <groupId>org.springframework.bootgroupId>
    36. <artifactId>spring-boot-devtoolsartifactId>
    37. <scope>runtimescope>
    38. <optional>trueoptional>
    39. dependency>
    40. <dependency>
    41. <groupId>org.projectlombokgroupId>
    42. <artifactId>lombokartifactId>
    43. <optional>trueoptional>
    44. dependency>
    45. <dependency>
    46. <groupId>org.springframework.bootgroupId>
    47. <artifactId>spring-boot-starter-testartifactId>
    48. <scope>testscope>
    49. dependency>
    50. dependencies>
    51. project>

    yml文件

    1. server:
    2. port: 84
    3. spring:
    4. application:
    5. name: nacos-order-consumer
    6. cloud:
    7. nacos:
    8. discovery:
    9. server-addr: localhost:8848
    10. sentinel:
    11. transport:
    12. #配置Sentinel dashboard地址
    13. dashboard: localhost:8080
    14. #默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
    15. port: 8719
    16. #消费者将要去访问的微服务名称(注册成功进nacos的微服务提供者)
    17. service-url:
    18. nacos-user-service: http://nacos-payment-provider

    主启动

    1. @EnableDiscoveryClient
    2. @SpringBootApplication
    3. public class OrderNacosMain84
    4. {
    5. public static void main(String[] args) {
    6. SpringApplication.run(OrderNacosMain84.class, args);
    7. }
    8. }

    配置类 

    1. @Configuration
    2. public class ApplicationContextConfig
    3. {
    4. @Bean
    5. @LoadBalanced
    6. public RestTemplate getRestTemplate()
    7. {
    8. return new RestTemplate();
    9. }
    10. }

    业务类1

    1. @RestController
    2. @Slf4j
    3. public class CircleBreakerController
    4. {
    5. public static final String SERVICE_URL = "http://nacos-payment-provider";
    6. @Resource
    7. private RestTemplate restTemplate;
    8. @RequestMapping("/consumer/fallback/{id}")
    9. @SentinelResource(value = "fallback")
    10. public CommonResult fallback(@PathVariable Long id)
    11. {
    12. CommonResult result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/"+id,CommonResult.class,id);
    13. if (id == 4) {
    14. throw new IllegalArgumentException ("IllegalArgumentException,非法参数异常....");
    15. }else if (result.getData() == null) {
    16. throw new NullPointerException ("NullPointerException,该ID没有对应记录,空指针异常");
    17. }
    18. return result;
    19. }
    20. }

    测试2 

    启动9003 9004 84

    访问http://localhost:84/consumer/fallback/1

      再访问http://localhost:84/consumer/fallback/1

     可以发现第一次是9004端口,第二次是9003端口,这里面的话,就可以就知道意见实现了ribbon中的轮训

    测试3 

     访问http://localhost:84/consumer/fallback/4http://localhost:84/consumer/fallback/4

    给客户error页面,不友好 

     修改业务类(只配置fallback)

    1. @RestController
    2. @Slf4j
    3. public class CircleBreakerController
    4. {
    5. public static final String SERVICE_URL = "http://nacos-payment-provider";
    6. @Resource
    7. private RestTemplate restTemplate;
    8. @RequestMapping("/consumer/fallback/{id}")
    9. @SentinelResource(value = "fallback",fallback = "handlerFallback") //fallback负责业务异常
    10. public CommonResult fallback(@PathVariable Long id)
    11. {
    12. CommonResult result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/"+id,CommonResult.class,id);
    13. if (id == 4) {
    14. throw new IllegalArgumentException ("IllegalArgumentException,非法参数异常....");
    15. }else if (result.getData() == null) {
    16. throw new NullPointerException ("NullPointerException,该ID没有对应记录,空指针异常");
    17. }
    18. return result;
    19. }
    20. public CommonResult handlerFallback(@PathVariable Long id,Throwable e) {
    21. Payment payment = new Payment(id,"null");
    22. return new CommonResult<>(444,"兜底异常handlerFallback,exception内容 "+e.getMessage(),payment);
    23. }
    24. }

     本例sentinel无配置

    测试4

    访问http://localhost:84/consumer/fallback/4

    这里可以发现没有出现error界面,而是我们自己配置的兜底的处理方案 

     修改业务类(只配置blockHandler )

    1. @RestController
    2. @Slf4j
    3. public class CircleBreakerController
    4. {
    5. public static final String SERVICE_URL = "http://nacos-payment-provider";
    6. @Resource
    7. private RestTemplate restTemplate;
    8. @RequestMapping("/consumer/fallback/{id}")
    9. @SentinelResource(value = "fallback",blockHandler = "blockHandler") //blockHandler负责在sentinel里面配置的降级限流
    10. public CommonResult fallback(@PathVariable Long id)
    11. {
    12. CommonResult result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/"+id,CommonResult.class,id);
    13. if (id == 4) {
    14. throw new IllegalArgumentException ("非法参数异常....");
    15. }else if (result.getData() == null) {
    16. throw new NullPointerException ("NullPointerException,该ID没有对应记录");
    17. }
    18. return result;
    19. }
    20. public CommonResult handlerFallback(@PathVariable Long id,Throwable e) {
    21. Payment payment = new Payment(id,"null");
    22. return new CommonResult<>(444,"fallback,无此流水,exception "+e.getMessage(),payment);
    23. }
    24. public CommonResult blockHandler(@PathVariable Long id,BlockException blockException) {
    25. Payment payment = new Payment(id,"null");
    26. return new CommonResult<>(445,"blockHandler-sentinel限流,无此流水: blockException "+blockException.getMessage(),payment);
    27. }
    28. }

     配置sentinel

     测试5

     访问2次以后http://localhost:84/consumer/fallback/4

      修改业务类(fallback和blockHandler都配置)

    1. @RestController
    2. @Slf4j
    3. public class CircleBreakerController
    4. {
    5. public static final String SERVICE_URL = "http://nacos-payment-provider";
    6. @Resource
    7. private RestTemplate restTemplate;
    8. @RequestMapping("/consumer/fallback/{id}")
    9. @SentinelResource(value = "fallback",fallback = "handlerFallback",blockHandler = "blockHandler")
    10. public CommonResult fallback(@PathVariable Long id)
    11. {
    12. CommonResult result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/"+id,CommonResult.class,id);
    13. if (id == 4) {
    14. throw new IllegalArgumentException ("非法参数异常....");
    15. }else if (result.getData() == null) {
    16. throw new NullPointerException ("NullPointerException,该ID没有对应记录");
    17. }
    18. return result;
    19. }
    20. public CommonResult handlerFallback(@PathVariable Long id,Throwable e) {
    21. Payment payment = new Payment(id,"null");
    22. return new CommonResult<>(444,"fallback,无此流水,exception "+e.getMessage(),payment);
    23. }
    24. public CommonResult blockHandler(@PathVariable Long id,BlockException blockException) {
    25. Payment payment = new Payment(id,"null");
    26. return new CommonResult<>(445,"blockHandler-sentinel限流,无此流水: blockException "+blockException.getMessage(),payment);
    27. }
    28. }

     

     

    测试 6

     访问http://localhost:84/consumer/fallback/4

    再访问的时候,那就意味着这个时候会被限流处理 

    若 blockHandler 和 fallback 都进行了配置,则被限流降级而抛出 BlockException 时只会进入 blockHandler 处理逻辑。 

      修改业务类(忽略属性.......) 

    1. @RestController
    2. @Slf4j
    3. public class CircleBreakerController
    4. {
    5. public static final String SERVICE_URL = "http://nacos-payment-provider";
    6. @Resource
    7. private RestTemplate restTemplate;
    8. @RequestMapping("/consumer/fallback/{id}")
    9. @SentinelResource(value = "fallback", fallback = "handlerFallback", blockHandler = "blockHandler",
    10. exceptionsToIgnore = {IllegalArgumentException.class})
    11. public CommonResult fallback(@PathVariable Long id)
    12. {
    13. CommonResult result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/"+id,CommonResult.class,id);
    14. if (id == 4) {
    15. throw new IllegalArgumentException ("非法参数异常....");
    16. }else if (result.getData() == null) {
    17. throw new NullPointerException ("NullPointerException,该ID没有对应记录");
    18. }
    19. return result;
    20. }
    21. public CommonResult handlerFallback(@PathVariable Long id,Throwable e) {
    22. Payment payment = new Payment(id,"null");
    23. return new CommonResult<>(444,"fallback,无此流水,exception "+e.getMessage(),payment);
    24. }
    25. public CommonResult blockHandler(@PathVariable Long id,BlockException blockException) {
    26. Payment payment = new Payment(id,"null");
    27. return new CommonResult<>(445,"blockHandler-sentinel限流,无此流水: blockException "+blockException.getMessage(),payment);
    28. }
    29. }

     

    测试7 

    访问http://localhost:84/consumer/fallback/4

     

  • 相关阅读:
    使用IDEA画结构图
    Promise梳理
    Vue3 SFC 和 TSX 方式自定义组件实现 v-model
    Git--原理与使用
    uniapp IOS上架AppStore因打开相机、相册提示不明确被拒
    亚马逊国际按关键字搜索商品 API 返回值说明
    Swagger2的配置
    通过cpolar实现外网ssh远程连接linux
    Fedora文件历史记录怎么开启? Fedora历史记录的显示方法
    可编程直流电源的特点都有哪些呢?
  • 原文地址:https://blog.csdn.net/m0_62436868/article/details/126460737