Spring Cloud Feign高級(jí)應(yīng)用實(shí)例詳解
這篇文章主要介紹了Spring Cloud Feign高級(jí)應(yīng)用實(shí)例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
1.使用feign進(jìn)行服務(wù)間的調(diào)用
Spring boot2X Consul如何使用Feign實(shí)現(xiàn)服務(wù)調(diào)用
2.開(kāi)啟gzip壓縮
Feign支持對(duì)請(qǐng)求與響應(yīng)的壓縮,以提高通信效率,需要在服務(wù)消費(fèi)者配置文件開(kāi)啟壓縮支持和壓縮文件的類(lèi)型
添加配置
feign.compression.request.enabled=true feign.compression.response.enabled=true feign.compression.request.mime-types=text/xml,application/xml,application/json feign.compression.request.min-request-size=2048
3.開(kāi)啟日志
配置
logging.level.com.xyz.comsumer.feign.RemoteHelloService=debug
添加FeignLogConfig類(lèi)
package com.xyz.comsumer.configure;
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeignLogConfig {
@Bean
Logger.Level feignLogger(){
return Logger.Level.FULL;
}
}
輸出
2019-12-07 23:23:03.630 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] <--- HTTP/1.1 200 (671ms) 2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] content-length: 14 2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] content-type: text/plain;charset=UTF-8 2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] date: Sat, 07 Dec 2019 15:23:03 GMT 2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] vary: Origin 2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] vary: Access-Control-Request-Method 2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] vary: Access-Control-Request-Headers 2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] 2019-12-07 23:23:03.632 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] hello,provider 2019-12-07 23:23:03.632 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] <--- END HTTP (14-byte body)
說(shuō)明:
Feign日志記錄只能響應(yīng)DEBUG日志級(jí)別
對(duì)每一個(gè)Feign客戶(hù)端,可以配置一個(gè)Logger.Level對(duì)象,通過(guò)該對(duì)象控制日志輸出內(nèi)容。
Logger.Level有如下幾種選擇:
NONE, 不記錄日志 (默認(rèn))。
BASIC, 只記錄請(qǐng)求方法和URL以及響應(yīng)狀態(tài)代碼和執(zhí)行時(shí)間。
HEADERS, 記錄請(qǐng)求和應(yīng)答的頭的基本信息。
FULL, 記錄請(qǐng)求和響應(yīng)的頭信息,正文和元數(shù)據(jù)。
4.替換JDK默認(rèn)的URLConnection為okhttp
在默認(rèn)情況下 spring cloud feign在進(jìn)行各個(gè)子服務(wù)之間的調(diào)用時(shí),http組件使用的是jdk的HttpURLConnection
服務(wù)之間調(diào)用使用的HttpURLConnection,效率非常低
為了提高效率,可以通過(guò)連接池提高效率
使用okhttp,能提高qps,因?yàn)閛khttp有連接池和超時(shí)時(shí)間進(jìn)行調(diào)優(yōu)
添加依賴(lài)
<dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-okhttp</artifactId> </dependency>
修改配置
禁用默認(rèn)的http,啟用okhttp
feign.okhttp.enabled=true feign.httpclient.enabled=false
添加FeignOkHttpConfig類(lèi)
package com.xyz.comsumer.configure;
import feign.Feign;
import okhttp3.ConnectionPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.TimeUnit;
@Configuration
@ConditionalOnClass(Feign.class)
@AutoConfigureBefore(FeignAutoConfiguration.class)
public class FeignOkHttpConfig {
@Bean
public okhttp3.OkHttpClient okHttpClient(){
return new okhttp3.OkHttpClient.Builder()
.readTimeout(10, TimeUnit.SECONDS)
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.retryOnConnectionFailure(true)
.connectionPool(new ConnectionPool())
.build();
}
}
5.超時(shí)設(shè)置
Feign調(diào)用服務(wù)的默認(rèn)時(shí)長(zhǎng)是1秒鐘
hystrix的超時(shí)時(shí)間
hystrix.command.default.execution.timeout.enabled=true hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000
說(shuō)明:
hystrix.command.default.execution.timeout.enabled 執(zhí)行是否啟用超時(shí),默認(rèn)啟用true
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds 命令執(zhí)行超時(shí)時(shí)間,默認(rèn)1000ms
常用的配置還有
hystrix.command.default.execution.isolation.strategy 隔離策略,默認(rèn)是Thread, 可選THREAD|SEMAPHORE,建議選擇SEMAPHORE
Feign 的負(fù)載均衡底層用的就是 Ribbon
ribbon的超時(shí)時(shí)間
ribbon.ReadTimeout=10000 ribbon.ConnectTimeout=10000
6.使用hystrix進(jìn)行熔斷、降級(jí)處理
feign啟用hystrix,才能熔斷、降級(jí)
feign.hystrix.enabled=true
hystrix服務(wù)降級(jí)處理
RemoteHelloServiceFallbackImpl
package com.xyz.comsumer.feign.fallback;
import com.xyz.comsumer.feign.RemoteHelloService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
public class RemoteHelloServiceFallbackImpl implements RemoteHelloService {
private final Logger logger = LoggerFactory.getLogger(RemoteHelloServiceFallbackImpl.class);
@Override
public String hello() {
logger.error("feign 查詢(xún)信息失敗:{}");
return null;
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Django實(shí)現(xiàn)支付寶付款和微信支付的示例代碼
支付寶支付和微信支付是當(dāng)今互聯(lián)網(wǎng)產(chǎn)品常用的功能,這篇文章主要介紹了Django實(shí)現(xiàn)支付寶付款和微信支付的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
實(shí)現(xiàn)python版本的按任意鍵繼續(xù)/退出
本文給大家簡(jiǎn)單介紹了在windows以及l(fā)inux下實(shí)現(xiàn)python版本的按任意鍵繼續(xù)/退出功能,非常的簡(jiǎn)單實(shí)用,linux下稍微復(fù)雜些,有需要的小伙伴可以參考下2016-09-09
Pytorch基本變量類(lèi)型FloatTensor與Variable用法
今天小編就為大家分享一篇Pytorch基本變量類(lèi)型FloatTensor與Variable用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
python對(duì)指定目錄下文件進(jìn)行批量重命名的方法
這篇文章主要介紹了python對(duì)指定目錄下文件進(jìn)行批量重命名的方法,涉及Python中replace及join方法的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
python 用lambda函數(shù)替換for循環(huán)的方法
今天小編就為大家分享一篇python 用lambda函數(shù)替換for循環(huán)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
用python實(shí)現(xiàn)爬取奧特曼圖片實(shí)例
大家好,本篇文章主要講的是用python實(shí)現(xiàn)爬取奧特曼圖片實(shí)例,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-02-02
基于Python數(shù)據(jù)結(jié)構(gòu)之遞歸與回溯搜索
今天小編就為大家分享一篇基于Python數(shù)據(jù)結(jié)構(gòu)之遞歸與回溯搜索,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02

