H__D  

  本章介绍Sentinel 与OpenFeign整合使用,

项目框架

  

项目搭建

  1、使用上一章项目,搭建参考:【SpringCloud】Spring Cloud Alibaba 之 Sentinel @SentinelResource使用(三十三)

  2、主要是修改项目:springcloud-consumer-sentinel-order7994服务(调用者),引入openfeign依赖

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

    完整pom如下:

 1 <?xml version="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>test-springcloud</artifactId>
 7         <groupId>com.test</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11 
12     <artifactId>springcloud-consumer-sentinel-order7994</artifactId>
13 
14     <dependencies>
15 
16         <!-- alibaba nacos sentinel -->
17         <dependency>
18             <groupId>com.alibaba.cloud</groupId>
19             <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
20             <version>2.2.1.RELEASE</version>
21             <exclusions>
22                 <exclusion>
23                     <groupId>com.fasterxml.jackson.dataformat</groupId>
24                     <artifactId>jackson-dataformat-xml</artifactId>
25                 </exclusion>
26             </exclusions>
27         </dependency>
28 
29         <!-- alibaba nacos -->
30         <dependency>
31             <groupId>com.alibaba.cloud</groupId>
32             <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
33         </dependency>
34 
35         <!-- openfeign -->
36         <dependency>
37             <groupId>org.springframework.cloud</groupId>
38             <artifactId>spring-cloud-starter-openfeign</artifactId>
39         </dependency>
40 
41         <!-- spring boot -->
42         <dependency>
43             <groupId>org.springframework.boot</groupId>
44             <artifactId>spring-boot-starter-web</artifactId>
45         </dependency>
46         <dependency>
47             <groupId>org.springframework.boot</groupId>
48             <artifactId>spring-boot-starter-actuator</artifactId>
49         </dependency>
50         <dependency>
51             <groupId>org.springframework.boot</groupId>
52             <artifactId>spring-boot-devtools</artifactId>
53             <scope>runtime</scope>
54             <optional>true</optional>
55         </dependency>
56         <dependency>
57             <groupId>org.projectlombok</groupId>
58             <artifactId>lombok</artifactId>
59             <optional>true</optional>
60         </dependency>
61         <dependency>
62             <groupId>org.springframework.boot</groupId>
63             <artifactId>spring-boot-starter-test</artifactId>
64             <scope>test</scope>
65         </dependency>
66 
67     </dependencies>
68 </project>
View Code

    

    注意版本问题,本例使用版本

1 <spring-boot.version>2.2.5.RELEASE</spring-boot.version>
2 <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
3 <spring-cloud-alibaba.version>2.1.0.RELEASE</spring-cloud-alibaba.version>

    Hoxton.SR1 中,fegin.context接口方法的定义为parseAndValidatateMetadata
    Hoxton.SR3 中,fegin.context接口方法的定义为parseAndValidateMetadata

    由于com.alibaba.cloud.sentinel.feign.SentinelContractHolder类中使用了fegin.context接口方法,导致可能出现版本冲突,可能报错:

    AbstractMethodError: com.alibaba.cloud.sentinel.feign.SentinelContractHolder.parseAndValidateMetadata

    所以在项目用需要引入2.2.1.RELEASE版的spring-cloud-starter-alibaba-sentinel,并排除com.fasterxml.jackson.dataformat依赖(避免返回xml内容)

 1 <!-- alibaba nacos sentinel -->
 2 <dependency>
 3     <groupId>com.alibaba.cloud</groupId>
 4     <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
 5     <version>2.2.1.RELEASE</version>
 6     <exclusions>
 7         <exclusion>
 8             <groupId>com.fasterxml.jackson.dataformat</groupId>
 9             <artifactId>jackson-dataformat-xml</artifactId>
10         </exclusion>
11     </exclusions>
12 </dependency>

  3、application.yml文件如下,激活Sentinel对Feign的支持:

 1 # 端口
 2 server:
 3   port: 7994
 4 
 5 spring:
 6   application:
 7     name: nacos-order-consumer
 8   cloud:
 9     nacos:
10       discovery:
11         server-addr: localhost:8848
12     sentinel:
13       transport:
14         # 配置Sentinel DashBoard地址
15         dashboard: localhost:8080
16         # 应用与Sentinel控制台交互的端口,应用本地会起一个该端口占用的HttpServer
17         # 默认8719端口,假如端口被占用,依次+1,直到找到未被占用端口
18         port: 8719
19 
20 
21 # 激活Sentinel对Feign的支持
22 feign:
23   sentinel:
24     enabled: true
25 
26 management:
27   endpoints:
28     web:
29       exposure:
30         include: '*'

  4、主启动,启用OpenFeign

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

  5、定义Feign接口,如下:

1 @FeignClient(value = "nacos-payment-provider", fallback = PaymentFallbackService.class)
2 public interface PaymentService {
3 
4     @GetMapping(value = "/paymentSQL/{id}")
5     public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id);
6 }

  6、定义Feign接口实现类,用于服务降级

1 @Component
2 public class PaymentFallbackService implements PaymentService{
3 
4     public CommonResult<Payment> paymentSQL(Long id) {
5         return new CommonResult<Payment>(500, "服务降级返回,----PaymentFallbackService-paymentSQL");
6     }
7 }

  7、编写Controller,增加如下内容:

1     // =======OpenFeign
2     @Autowired
3     private PaymentService paymentService;
4 
5     @GetMapping(value = "/consumer/paymentSQL/{id}")
6     public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id){
7         return  paymentService.paymentSQL(id);
8     }

  8、测试

    1)启动项目

    2)使用地址:http://localhost:7994/consumer/paymentSQL/3,正常获取内容

      

    3)关闭服务提供者

    4)使用地址:http://localhost:7994/consumer/paymentSQL/3,服务降级

      

 

posted on 2020-05-27 01:58  H__D  阅读(2602)  评论(0编辑  收藏  举报