Feign
OpenFeign Feign是一種聲明式、模板化的HTTP客戶端。
看了解釋過后,可以理解為他是一種 客戶端 配置實現的策略,它實現 服務間調用(FeignClient)、負載均衡(Ribbon)、容錯/降級處理(Hystrix) 也很簡單
1、引入依賴
<?xml version="1.0" encoding="UTF-8"?> <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.zjj7</groupId> <artifactId>publish</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>publish</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <!--配置倉庫--> <repositories> <repository> <id>aliRepository</id> <name>aliRepository</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <!-- cloud --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!--<dependency>--> <!--<groupId>org.springframework.boot</groupId>--> <!--<artifactId>spring-boot-starter-data-jpa</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.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!--lombok依賴--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.10</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

2、修改配置文件 (這里沒有涉及到 負載均衡的策略, 采取默認輪訓配置 openfeign ,自帶了 Ribbon 負載均衡(@LoadBalanced))
spring: application: name: publish #feign 配置 feign: hystrix: enabled: true client: config: default: connectTimeout: 5000 readTimeout: 5000 loggerLevel: full #hystrix 熔斷機制 hystrix: shareSecurityContext: true command: default: circuitBreaker: # 熔斷后的重試時間窗口,且在該時間窗口內只允許一次重試。即在熔斷開關打開后,在該時間窗口允許有一次重試,如果重試成功,則將重置Health采樣統計並閉合熔斷開關實現快速恢復,否則熔斷開關還是打開狀態,執行快速失敗, 默認為為5s。 sleepWindowInMilliseconds: 10000 # 如果在一個采樣時間窗口內,失敗率超過該配置,則自動打開熔斷開關實現降級處理,即快速失敗。默認配置下采樣周期為10s,失敗率為50%。 errorThresholdPercentage: 50 # 在熔斷開關閉合情況下,在進行失敗率判斷之前,一個采樣周期內必須進行至少N個請求才能進行采樣統計,目的是有足夠的采樣使得失敗率計算正確,默認為20。 requestVolumeThreshold: 20 # 是否強制關閉熔斷開關,如果強制關閉了熔斷開關,則請求不會被降級,一些特殊場景可以動態配置該開關,默認為false。 forceClosed: false # 是否強制打開熔斷開關,如果強制打開可熔斷開關,則請求強制降級調用getFallback處理,可以通過動態配置來打開該開關實現一些特殊需求,默認為false。 forceOpen: false execution: isolation: thread: # 是否啟用執行超時機制,默認為true timeoutEnabled: true # 執行超時時間,默認為1000毫秒,如果命令是線程隔離,且配置了executionIsolationThreadInterruptOnTimeout=true,則執行線程將執行中斷處理。如果命令是信號量隔離,則進行終止操作,因為信號量隔離與主線程是在一個線程中執行,其不會中斷線程處理,所以要根據實際情況來決定是否采用信號量隔離,尤其涉及網絡訪問的情況。 timeoutInMilliseconds: 1000 # 當隔離策略為THREAD時,當執行線程執行超時時,是否進行中斷處理,即Future#cancel(true)處理,默認為false。 interruptOnFutureCancel: true # 當隔離策略為THREAD時,當執行線程執行超時時,是否進行中斷處理,默認為true。 interruptOnTimeout: true #服務注冊中心端口號 server: port: 6121 #服務注冊中心實例的主機名、端口 #是否向服務注冊中心注冊自己 #是否檢索服務 #服務注冊中心的配置內容,指定服務注冊中心的位置 eureka: port: 6110 instance: hostname: localhost client: register-with-eureka: true fetch-registry: true serviceUrl: defaultZone: http://${eureka.instance.hostname}:${eureka.port}/eureka/ logging: level: com.zjj7.publish.feignClien: DEBUG

3、配置FeignClient,集成了Ribbon及Hystrix



4、啟動類添加注解
@EnableEurekaClient
@EnableFeignClients

這個示例中為了簡單沒有啟用 配置客戶端, 提供服務方無需提供任何配置,只需要服務調用端(客戶端)做好相關配置即可。
此時已經有了 服務間服務調用、自動負載均衡調用服務、服務熔斷/容錯 的功能
**** 擴展一下:如果一個應用Feign想要調用外部服務,並且啟用Hystrix 熔斷回調了怎么辦呢? 不需其它的配置,只需要配置一個 @FeignClient 的 url 即可,如下

