本章介紹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>
注意版本問題,本例使用版本
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,服務降級