SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(13)-- SpringCloud Alibaba Sentinel 服务熔断功能

亦凉 2023-01-04 10:26 259阅读 0赞

一、sentinel整合ribbon+openFeign+fallback

二、Ribbon系列

1、启动nacos和sentinel

2、提供者9003/9004

①、新建cloudalibaba-provider-payment9003/9004
②、POM
  1. <dependencies>
  2. <!--SpringCloud ailibaba nacos -->
  3. <dependency>
  4. <groupId>com.alibaba.cloud</groupId>
  5. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  6. </dependency>
  7. <dependency><!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
  8. <groupId>com.atguigu.springcloud</groupId>
  9. <artifactId>cloud-api-commons</artifactId>
  10. <version>${project.version}</version>
  11. </dependency>
  12. <!-- SpringBoot整合Web组件 -->
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-web</artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-actuator</artifactId>
  20. </dependency>
  21. <!--日常通用jar包配置-->
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-devtools</artifactId>
  25. <scope>runtime</scope>
  26. <optional>true</optional>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.projectlombok</groupId>
  30. <artifactId>lombok</artifactId>
  31. <optional>true</optional>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-test</artifactId>
  36. <scope>test</scope>
  37. </dependency>
  38. </dependencies>
③、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: '*'

记得修改不同的端口号

④、主启动
  1. package com.atguigu.springcloud.alibaba;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. @SpringBootApplication
  6. @EnableDiscoveryClient
  7. public class PaymentMain9003
  8. {
  9. public static void main(String[] args) {
  10. SpringApplication.run(PaymentMain9003.class, args);
  11. }
  12. }
⑤、业务类
  1. package com.atguigu.springcloud.alibaba.controller;
  2. import com.atguigu.springcloud.alibaba.entities.CommonResult;
  3. import com.atguigu.springcloud.alibaba.entities.Payment;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.PathVariable;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import java.util.HashMap;
  9. @RestController
  10. public class PaymentController
  11. {
  12. @Value("${server.port}")
  13. private String serverPort;
  14. public static HashMap<Long, Payment> hashMap = new HashMap<>();
  15. static{
  16. hashMap.put(1L,new Payment(1L,"28a8c1e3bc2742d8848569891fb42181"));
  17. hashMap.put(2L,new Payment(2L,"bba8c1e3bc2742d8848569891ac32182"));
  18. hashMap.put(3L,new Payment(3L,"6ua8c1e3bc2742d8848569891xt92183"));
  19. }
  20. @GetMapping(value = "/paymentSQL/{id}")
  21. public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id){
  22. Payment payment = hashMap.get(id);
  23. CommonResult<Payment> result = new CommonResult(200,"from mysql,serverPort: "+serverPort,payment);
  24. return result;
  25. }
  26. }
⑥、测试地址

http://localhost:9003/paymentSQL/1

3、消费者84

①、新建cloudalibaba-consumer-nacos-order84
②、POM
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <parent>
  4. <artifactId>cloud2020</artifactId>
  5. <groupId>com.atguigu.springcloud</groupId>
  6. <version>1.0-SNAPSHOT</version>
  7. </parent>
  8. <modelVersion>4.0.0</modelVersion>
  9. <artifactId>cloudalibaba-consumer-nacos-order84</artifactId>
  10. <dependencies>
  11. <dependency>
  12. <groupId>org.springframework.cloud</groupId>
  13. <artifactId>spring-cloud-starter-openfeign</artifactId>
  14. </dependency>
  15. <dependency>
  16. <groupId>com.alibaba.cloud</groupId>
  17. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  18. </dependency>
  19. <dependency>
  20. <groupId>com.alibaba.cloud</groupId>
  21. <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
  22. </dependency>
  23. <dependency>
  24. <groupId>com.atguigu.springcloud</groupId>
  25. <artifactId>cloud-api-commons</artifactId>
  26. <version>${project.version}</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-web</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-actuator</artifactId>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-devtools</artifactId>
  39. <scope>runtime</scope>
  40. <optional>true</optional>
  41. </dependency>
  42. <dependency>
  43. <groupId>org.projectlombok</groupId>
  44. <artifactId>lombok</artifactId>
  45. <optional>true</optional>
  46. </dependency>
  47. <dependency>
  48. <groupId>org.springframework.boot</groupId>
  49. <artifactId>spring-boot-starter-test</artifactId>
  50. <scope>test</scope>
  51. </dependency>
  52. </dependencies>
  53. </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. dashboard: localhost:8080
  13. port: 8719
  14. service-url:
  15. nacos-user-service: http://nacos-payment-provider
④、主启动
  1. package com.atguigu.springcloud.alibaba;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. import org.springframework.cloud.openfeign.EnableFeignClients;
  6. @EnableDiscoveryClient
  7. @SpringBootApplication
  8. @EnableFeignClients
  9. public class OrderNacosMain84
  10. {
  11. public static void main(String[] args) {
  12. SpringApplication.run(OrderNacosMain84.class, args);
  13. }
  14. }
⑤、业务类

ApplicationContextConfig

  1. package com.atguigu.springcloud.alibaba.config;
  2. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.web.client.RestTemplate;
  6. @Configuration
  7. public class ApplicationContextConfig
  8. {
  9. @Bean
  10. @LoadBalanced
  11. public RestTemplate getRestTemplate()
  12. {
  13. return new RestTemplate();
  14. }
  15. }

CircleBreakerController的全部源码

  1. package com.atguigu.springcloud.alibaba.controller;
  2. import com.alibaba.csp.sentinel.annotation.SentinelResource;
  3. import com.alibaba.csp.sentinel.slots.block.BlockException;
  4. import com.atguigu.springcloud.alibaba.entities.CommonResult;
  5. import com.atguigu.springcloud.alibaba.entities.Payment;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.PathVariable;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11. import org.springframework.web.client.RestTemplate;
  12. import javax.annotation.Resource;
  13. @RestController
  14. @Slf4j
  15. public class CircleBreakerController {
  16. public static final String SERVICE_URL = "http://nacos-payment-provider";
  17. @Resource
  18. private RestTemplate restTemplate;
  19. @RequestMapping("/consumer/fallback/{id}")
  20. //@SentinelResource(value = "fallback") //没有配置
  21. //@SentinelResource(value = "fallback",fallback = "handlerFallback") //fallback只负责业务异常
  22. //@SentinelResource(value = "fallback",blockHandler = "blockHandler") //blockHandler只负责sentinel控制台配置违规
  23. @SentinelResource(value = "fallback",fallback = "handlerFallback",blockHandler = "blockHandler",
  24. exceptionsToIgnore = { IllegalArgumentException.class})
  25. public CommonResult<Payment> fallback(@PathVariable Long id) {
  26. CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/"+id, CommonResult.class,id);
  27. if (id == 4) {
  28. throw new IllegalArgumentException ("IllegalArgumentException,非法参数异常....");
  29. }else if (result.getData() == null) {
  30. throw new NullPointerException ("NullPointerException,该ID没有对应记录,空指针异常");
  31. }
  32. return result;
  33. }
  34. //fallback
  35. public CommonResult handlerFallback(@PathVariable Long id,Throwable e) {
  36. Payment payment = new Payment(id,"null");
  37. return new CommonResult<>(444,"兜底异常handlerFallback,exception内容 "+e.getMessage(),payment);
  38. }
  39. //blockHandler
  40. public CommonResult blockHandler(@PathVariable Long id,BlockException blockException) {
  41. Payment payment = new Payment(id,"null");
  42. return new CommonResult<>(445,"blockHandler-sentinel限流,无此流水: blockException "+blockException.getMessage(),payment);
  43. }
  44. }

在这里插入图片描述
没有任何配置

  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<Payment> fallback(@PathVariable Long id)
  11. {
  12. CommonResult<Payment> 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. }

给客户error页面,不友好
在这里插入图片描述
结果
在这里插入图片描述
图说
在这里插入图片描述

三、Feign系列

1、修改84模块

2、POM

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

3、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. dashboard: localhost:8080
  13. port: 8719
  14. service-url:
  15. nacos-user-service: http://nacos-payment-provider
  16. #对Feign的支持
  17. feign:
  18. sentinel:
  19. enabled: true

4、业务类

①、带@FeignClient注解的业务接口
  1. package com.atguigu.springcloud.alibaba.service;
  2. import com.atguigu.springcloud.alibaba.entities.CommonResult;
  3. import com.atguigu.springcloud.alibaba.entities.Payment;
  4. import org.springframework.cloud.openfeign.FeignClient;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.PathVariable;
  7. @FeignClient(value = "nacos-payment-provider",fallback = PaymentFallbackService.class)
  8. public interface PaymentService
  9. {
  10. @GetMapping(value = "/paymentSQL/{id}")
  11. public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id);
  12. }
②、fallback = PaymentFallbackService.class
③、PaymentFallbackService实现类
  1. package com.atguigu.springcloud.alibaba.service;
  2. import com.atguigu.springcloud.alibaba.entities.CommonResult;
  3. import com.atguigu.springcloud.alibaba.entities.Payment;
  4. import org.springframework.stereotype.Component;
  5. @Component
  6. public class PaymentFallbackService implements PaymentService
  7. {
  8. @Override
  9. public CommonResult<Payment> paymentSQL(Long id)
  10. {
  11. return new CommonResult<>(44444,"服务降级返回,---PaymentFallbackService",new Payment(id,"errorSerial"));
  12. }
  13. }
④、Controller
  1. // OpenFeign
  2. @Resource
  3. private PaymentService paymentService;
  4. @GetMapping(value = "/consumer/paymentSQL/{id}")
  5. public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id) {
  6. return paymentService.paymentSQL(id);
  7. }

5、主启动

添加@EnableFeignClients启动Feign的功能

  1. package com.atguigu.springcloud.alibaba;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. import org.springframework.cloud.openfeign.EnableFeignClients;
  6. @EnableDiscoveryClient
  7. @SpringBootApplication
  8. @EnableFeignClients
  9. public class OrderNacosMain84
  10. {
  11. public static void main(String[] args) {
  12. SpringApplication.run(OrderNacosMain84.class, args);
  13. }
  14. }

6、http://lcoalhost:84/consumer/openfeign/1

7、http://lcoalhost:84/consumer/paymentSQL/1

8、测试84调用9003,此时故意关闭9003微服务提供者,看84消费侧自动降级,不会被耗死

四、熔断框架比较

在这里插入图片描述
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,259人围观)

还没有评论,来说两句吧...

相关阅读