SpringCloud之Feign代理,聲明式服務(wù)調(diào)用方式
將其他微服務(wù)中的服務(wù)接口,用feign在本項(xiàng)目中進(jìn)行調(diào)用。
Spring Cloud Feign是一套基于Netflix Feign實(shí)現(xiàn)的聲明式服務(wù)調(diào)用客戶端。它使得編寫(xiě)Web服務(wù)客戶端變得更加簡(jiǎn)單。我們只需要通過(guò)創(chuàng)建接口并用注解來(lái)配置它既可完成對(duì)Web服務(wù)接口的綁定。它具備可插拔的注解支持,包括Feign注解、JAX-RS注解。它也支持可插拔的編碼器和解碼器。Spring Cloud Feign還擴(kuò)展了對(duì)Spring MVC注解的支持,同時(shí)還整合了Ribbon和Hystrix來(lái)提供均衡負(fù)載的HTTP客戶端實(shí)現(xiàn)。
對(duì)于Feign來(lái)講,其實(shí)就是一個(gè)WEB接口而已,它內(nèi)部自集成了Spring Ribbon 和Spring Hystrix 斷路器功能,也就是說(shuō)可以支持自動(dòng)降級(jí)和負(fù)載均衡功能??梢哉f(shuō),在內(nèi)部服務(wù)與服務(wù)之間的相互數(shù)據(jù)通信橋梁就是通過(guò)Feign來(lái)實(shí)現(xiàn)的。也就是說(shuō),我們可以像使用web service OR dubbo 一樣對(duì)其進(jìn)行聲明式的配置,這非常棒~(yú) 在我之前的工作中,就大量的使用了Feign代理,可以說(shuō)使用spring cloud 就必須要學(xué)會(huì)如何深入使用Feign代理,當(dāng)然它也非常的簡(jiǎn)單。
引入相關(guān)依賴然后再主入口啟用注解
@EnableFeignClients //啟用代理服務(wù) @EnableCircuitBreaker //啟用斷路器
引入相關(guān)依賴然后再主入口啟用注解:@Enabl
注意:我們feign在第四個(gè)版本后需要手工的開(kāi)啟斷路器功能才可以生效。
了解完Feign的基礎(chǔ)配置之后,我們當(dāng)然要開(kāi)始代碼實(shí)現(xiàn)了。首先我們需要編寫(xiě)一個(gè)interface,并且這個(gè)interface一定是已知的服務(wù)(也就是注冊(cè)到了Eureka上的接口服務(wù),我們?cè)谶@里需要使用interface的方式進(jìn)行聲明)
@FeignClient注解就是Feign的注解聲明了,里面name屬性表示了當(dāng)前代理的服務(wù)APP NAME; fallback屬性當(dāng)然就是我們調(diào)用服務(wù)失敗的降級(jí)策略了,也就是當(dāng)網(wǎng)絡(luò)閃段、異常等調(diào)用代理服務(wù)失敗時(shí),會(huì)根據(jù)斷路器的超時(shí)時(shí)間降級(jí)到指定的fallback所賦予的HelloServiceHystrix類(lèi)中,我們可以進(jìn)行降級(jí)處理。
我們的Feign底層默認(rèn)提供了重試機(jī)制,也就是底層使用Retryer類(lèi)對(duì)調(diào)用服務(wù)進(jìn)行重試調(diào)用操作,通過(guò)底層代碼我們知道默認(rèn)是每100ms去進(jìn)行調(diào)用,調(diào)用次數(shù)是5次。既然Feign集成了Ribbon 與 Hystrix ,那么必然會(huì)使用兩個(gè)超時(shí)機(jī)制,一個(gè)是Ribbon的超時(shí)時(shí)間,一個(gè)是Hystrix的超時(shí)時(shí)間.這兩個(gè)超時(shí)時(shí)間的含義截然不同,千萬(wàn)要注意配置。
經(jīng)驗(yàn)小結(jié): 我們可以配置Hystrix的超時(shí)時(shí)間大于Ribbon的超時(shí)時(shí)間。并且如果想進(jìn)行重試最好是Hystrix的超時(shí)時(shí)間設(shè)置為Ribbon的超時(shí)時(shí)間的倍數(shù)。
這樣我們可以進(jìn)行重試策略,如果Hystrix的超時(shí)時(shí)間小于Ribbon的超時(shí)時(shí)間,則不會(huì)重試,直接被斷路器組件對(duì)調(diào)用請(qǐng)求執(zhí)行請(qǐng)求段熔機(jī)制,服務(wù)降級(jí)。
Feign配合Ribbon、Hystrix的超時(shí)策略配置如下
1.pom
<?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"> ? ? <parent> ? ? ? ? <artifactId>com.zx.dt2b.erp</artifactId> ? ? ? ? <groupId>com.zx.dt2b.erp</groupId> ? ? ? ? <version>1.0-SNAPSHOT</version> ? ? </parent> ? ? <modelVersion>4.0.0</modelVersion> ? ? ? <artifactId>feign</artifactId> ? ? <dependencies> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? ? ? <artifactId>spring-boot-starter-web</artifactId> ? ? ? ? </dependency> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? ? ? <artifactId>spring-boot-starter-actuator</artifactId> ? ? ? ? </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-netflix-hystrix</artifactId> ? ? ? ? </dependency> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.springframework.cloud</groupId> ? ? ? ? ? ? <artifactId>spring-cloud-starter-feign</artifactId> ? ? ? ? </dependency> ? ? </dependencies> ? ? ? <dependencyManagement> ? ? ? ? <dependencies> ? ? ? ? ? ? <dependency> ? ? ? ? ? ? ? ? <groupId>org.springframework.cloud</groupId> ? ? ? ? ? ? ? ? <artifactId>spring-cloud-dependencies</artifactId> ? ? ? ? ? ? ? ? <!-- <version>Dalston.SR5</version> ?--> ? ? ? ? ? ? ? ? <version>Edgware.SR4</version> ? ? ? ? ? ? ? ? <!-- <version>Finchley.SR1</version> ?--> ? ? ? ? ? ? ? ? <type>pom</type> ? ? ? ? ? ? ? ? <scope>import</scope> ? ? ? ? ? ? </dependency> ? ? ? ? </dependencies> ? ? </dependencyManagement>? ? ? ? <build> ? ? ? ? <finalName>feign</finalName> ? ? ? ? <plugins> ? ? ? ? ? ? <plugin> ? ? ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? ? ? ? ? <artifactId>spring-boot-maven-plugin</artifactId> ? ? ? ? ? ? ? ? <configuration> ? ? ? ? ? ? ? ? ? ? <mainClass>com.zx.dt2b.FeignApplication</mainClass> ? ? ? ? ? ? ? ? </configuration> ? ? ? ? ? ? </plugin> ? ? ? ? </plugins> ? ? </build> ? </project>
2.主入口
package com.zx.dt2b;? import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; ? @EnableFeignClients?? ??? ?//啟用代理服務(wù) @EnableCircuitBreaker?? ?//啟用斷路器 @EnableDiscoveryClient?? ?//標(biāo)識(shí)具體的一個(gè)服務(wù),需要向注冊(cè)中心注冊(cè) @SpringBootApplication?? ?//SpringBoot 核心配置 public class FeignApplication { ? ?? ?public static void main(String[] args) {? ?? ??? ?SpringApplication.run(FeignApplication.class, args); ?? ?}? }
3.配置文件
feign: ? hystrix: ? ? enabled: true ?#開(kāi)啟降級(jí) ? compression: ? ? request: ? ? ? min-request-size: 2048 ? ? ? mime-types: ? ? ? ? - text/html, application/xml, application/json spring: ? application: ? ? name: feign-consumer ? cloud: ? ? loadbalancer: ? ? ? retry: ? ? ? ? enabled: true ? server: ? context-path: / ? port: 7005 ? eureka: ? client: ? ? service-url: ? ? ? defaultZone: http://eureka1:8001/eureka ? feign: ? hystrix: ? ? enabled: true ? compression: ? ? request: ? ? ? min-request-size: 2048 ? ? ? mime-types: ? ? ? ? - text/html, application/xml, application/json ? ##設(shè)置斷路器的超時(shí)時(shí)間 hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 10000 ? ##微服務(wù)的請(qǐng)求配置 customer-service: ? ConnectTimeout: 10000 ? ReadTimeout: 3000 ? ribbon: ? ? OkToRetryOnAllOperations: true ##對(duì)所有的請(qǐng)求都進(jìn)行重試 ? ? MaxAutoRetriesNextServer: 1 ##切換實(shí)例的次數(shù) ? ? MaxAutoRetries: 2 ? ?##對(duì)當(dāng)前實(shí)例重試的次數(shù)
4.業(yè)務(wù)代碼與實(shí)現(xiàn)
@FeignClient(name="provider-service", fallback= IndexFeignFailback.class)
name表示微服務(wù)名稱:前端直接從本項(xiàng)目訪問(wèn)其他項(xiàng)目服務(wù)接口
失敗后IndexFeignFailback中對(duì)應(yīng)的接口,進(jìn)行降級(jí)。
代理服務(wù)中異常等要保持一致
package com.zx.dt2b.feign;? import com.zx.dt2b.feign.hystrix.IndexFeignFailback; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; ? @FeignClient(name="customer-service", fallback= IndexFeignFailback.class) public interface IndexFeignClient { ? ?? ?@RequestMapping(value="/customerservice/index", method = {RequestMethod.GET}) ?? ?public String hello() throws Exception; ? ?? ?@RequestMapping(value="/customerservice/hi", method = {RequestMethod.GET}) ?? ?public String hi() throws InterruptedException; }
package com.zx.dt2b.feign.hystrix;? import com.zx.dt2b.feign.IndexFeignClient; import org.springframework.stereotype.Component; ? @Component public class IndexFeignFailback implements IndexFeignClient { ? ?? ?@Override ?? ?public String hello() throws Exception { ?? ??? ?return "-----hello接口的降級(jí)方法!--------"; ?? ?} ? ?? ?@Override ?? ?public String hi() throws InterruptedException { ?? ??? ?return "-----hi接口的降級(jí)方法!--------"; ?? ?}? }
5.controller測(cè)試
package com.zx.dt2b.api;? import com.zx.dt2b.feign.IndexFeignClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; ? @RestController public class ConsumerController { ? ?? ?@Autowired ?? ?private IndexFeignClient indexFeignClient; ? ?? ?@RequestMapping(value="/feign-hello") ?? ?public String hello() throws Exception { ?? ??? ?return indexFeignClient.hello(); ?? ?} ? ?? ?@RequestMapping(value="/feign-hi") ?? ?public String hi() throws InterruptedException { ?? ??? ?return indexFeignClient.hi(); ?? ?} }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java構(gòu)造器與傳值學(xué)習(xí)總結(jié)
這篇文章主要為大家詳細(xì)介紹了Java構(gòu)造器與傳值學(xué)習(xí)總結(jié),文中示例介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01Java通過(guò)動(dòng)態(tài)規(guī)劃設(shè)計(jì)股票買(mǎi)賣(mài)最佳時(shí)機(jī)
動(dòng)態(tài)規(guī)劃可謂是大名鼎鼎,筆試面試中的高頻考點(diǎn),也是重點(diǎn)難點(diǎn),動(dòng)態(tài)規(guī)劃類(lèi)型題目靈活多變,難度系數(shù)也相對(duì)較高,往往我們做不好動(dòng)態(tài)規(guī)劃的題目就會(huì)與心儀的offer失之交臂,本篇文章我們就一起來(lái)研究一下動(dòng)態(tài)規(guī)劃設(shè)計(jì)股票買(mǎi)賣(mài)最佳時(shí)機(jī)2022-10-10如何用Java的swing編寫(xiě)簡(jiǎn)單計(jì)算器
這篇文章主要給大家介紹了關(guān)于如何用Java的swing編寫(xiě)簡(jiǎn)單計(jì)算器的相關(guān)資料,通過(guò)本文可以設(shè)計(jì)一個(gè)圖形界面的簡(jiǎn)易計(jì)算器,完成簡(jiǎn)單的算術(shù)運(yùn)算符,可以完成加法、減法、乘法、除法和取余運(yùn)算,需要的朋友可以參考下2023-12-12Spring Bean生命周期之Bean元信息的配置與解析階段詳解
這篇文章主要為大家詳細(xì)介紹了Spring Bean生命周期之Bean元信息的配置與解析階段,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03SpringCloud feign微服務(wù)調(diào)用之間的異常處理方式
這篇文章主要介紹了SpringCloud feign微服務(wù)調(diào)用之間的異常處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06Java練習(xí)之潛艇小游戲的實(shí)現(xiàn)
這篇文章主要和大家分享一個(gè)Java小練習(xí)——利用Java編寫(xiě)一個(gè)潛艇小游戲,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-03-03