feign-hystrix的使用
生活随笔
收集整理的這篇文章主要介紹了
feign-hystrix的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
如何在Feign這個組件里面,Feign這個組件里面使用Hystrix,也就是Feign如何搭配Hystrix,我們可以先來看一下Feign這個組件,看他都依賴了哪些組件,我們pom文件里面引的是spring-cloud-starter-feign,你看到這里面,這里面其實有一個feignhystrix,你看他引導了hystrix-core,說明他里面已經集成好了hystrix,我們不需要單獨的引入hystrix包,可以直接來用,那么使用的時候,怎么用呢,第一你需要配置一下,feign.hystrix,注意是沒有提示的,所以我們寫的時候得注意一下,不要寫錯了,feign.hystrix.enabled=true需要首先做這么一個配置,然后我們再來看一下productClient,這塊代碼寫到哪兒,這段代碼寫到product服務里面,這里提供一個jar包,我們只是引用而已,這里面怎么使用Hystrix呢,你不能加一個HystrixCommand注解了,這邊都是interface,不一樣了,那這里面怎么用呢,@FeignClient這里面有一個參數,可以傳,叫做fallback,fallback里面的內容要填一個class,還沒有創建的一個class@Component
public class ProductClientFallback implements ProductClient {@Overridepublic List<ProductInfo> listForOrder(@RequestBody List<String> productIdList) {return null;}}實現ProductClient,把他里面的方法都給實現了,如果產生服務降級的話呢,比如產生服務降級的話,那么他就會訪問到這兒來,返回一個null,我們先返回一個null,這個class要加一個注解,這個是要注意的一點,很容易被大家忽略掉,注意看一下他的用法,在FeignClient里面加一個fallback的字段,指定如果產生服務降級的話,那么訪問到的實際的方法,接下來我們啟動order的服務,我們怎么觀察服務降級呢,如果是出現服務降級的話,那么返回的結果應該是null,你看一下我們的ProductClient,看一下我們的client,看一下這里面的代碼@Component
public class ProductClientFallback implements ProductClient {@Overridepublic List<ProductInfo> listForOrder(@RequestBody List<String> productIdList) {return null;}}加了@Component這個注解,我們希望class載入到Spring的容器里面,那么問題來了,我問一下大家,這塊代碼到底是在哪個項目里面運行的,這塊代碼雖然寫是寫在Product項目,是order服務里面來引用,這塊其實就可以理解為是order里面的一塊代碼了,他已經在order項目里面了,這就是一片代碼,所以運行的時候呢,他肯定是在order里面來運行的,那么為什么不起作用呢,這是因為我們的啟動類里面沒有掃描到https://blog.csdn.net/huanxianglove/article/details/80914504@SpringCloudApplication
@EnableFeignClients
public class OrderApplication {public static void main(String[] args) {// Spring應用啟動起來SpringApplication.run(OrderApplication.class,args);}}我們要配置一下掃描,現在已經成功啟動了,我們使用postman下一個但試一試localhost:8010/createOrder[{productId: "00000",productName: "太陽面",productPrice: null,productStock: null,productDescription: null,productIcon: null,productStatus: null,categoryType: null,createTime: null,updateTime: null}
]可以看到productId就是"00000",我們product壓根就沒有啟動,他就是null,所以證明我們的服務降級已經起作用了,因為是null,所以報了一個空指針,他并沒有像之前一樣的,報找不到這個服務,我們再來回顧一下,Feign組件里面使用Hystrix,第一點就是配置,這一點可能很多人會忘記feign.hystrix.enabled=true這一點特別要注意,你這個地方配錯了就不生效了,還有一個地方就是啟動類,包掃描,因為我們使用的是Product服務,所以你得把它配置進來,掃描得到,;=另外就是降級fallback的這個類,這上面注解不要忘記了,這三點大家切記
<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"><modelVersion>4.0.0</modelVersion><groupId>com.learn</groupId><artifactId>order</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><parent><groupId>cn.learn</groupId><artifactId>microcloud02</artifactId><version>0.0.1</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><thymeleaf.version>3.0.9.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-stream-rabbit</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId></dependency></dependencies><!-- 這個插件,可以將應用打包成一個可執行的jar包 --><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
server.port=8010eureka.client.serviceUrl.defaultZone=http://admin:1234@10.40.8.152:8761/eurekaspring.application.name=order
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}spring.rabbitmq.host=59.110.158.145
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.port=5672spring.cloud.stream.bindings.myMessage.group=order
spring.cloud.stream.bindings.myMessage.content-type=application/jsonhystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=1000
hystrix.command.getProductInfoList.execution.isolation.thread.timeoutInMilliseconds=5000feign.hystrix.enabled=true
package com.learn.client;import java.util.List;import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;import com.learn.dto.ProductInfo;@FeignClient(name = "product",fallback = ProductClientFallback.class)
public interface ProductClient {@PostMapping("/listForOrder")public List<ProductInfo> listForOrder(@RequestBody List<String> productIdList);}
package com.learn.client;import java.util.ArrayList;
import java.util.List;import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;import com.learn.dto.ProductInfo;@Component
public class ProductClientFallback implements ProductClient {@Overridepublic List<ProductInfo> listForOrder(@RequestBody List<String> productIdList) {ProductInfo p = new ProductInfo();p.setProductId("00000");p.setProductName("太陽面");List<ProductInfo> list = new ArrayList<ProductInfo>();list.add(p);return list;}}
package com.learn.controller;import java.util.Arrays;
import java.util.List;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import com.learn.client.ProductClient;
import com.learn.dto.ProductInfo;@RestController
public class OrderFeignController {@Autowiredprivate ProductClient productClient;@GetMapping("/createOrder")public List<ProductInfo> listForOrder() {return this.productClient.listForOrder(Arrays.asList("1111"));}}
package com.learn;import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.feign.EnableFeignClients;//@SpringBootApplication
//@EnableRabbit
//@EnableEurekaClient
//@EnableCircuitBreaker
@SpringCloudApplication
@EnableFeignClients
public class OrderApplication {public static void main(String[] args) {// Spring應用啟動起來SpringApplication.run(OrderApplication.class,args);}}
?
總結
以上是生活随笔為你收集整理的feign-hystrix的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用配置项
- 下一篇: hystrix-dashboard