Spring?Feign超時設(shè)置深入了解
Feign其他功能-超時設(shè)置
- Feign 底層依賴于 Ribbon 實現(xiàn)負(fù)載均衡和遠(yuǎn)程調(diào)用。
- Ribbon默認(rèn)1秒超時。
超時配置:
ribbon:
ConnectTimeout: 1000 #連接超時時間,毫秒
ReadTimeout: 1000 #邏輯處理超時時間,毫秒

在feign-consumer中設(shè)置超時時間(具體代碼看上 Feign的快速入門)
server:
port: 9000eureka:
instance:
hostname: localhost # 主機名
client:
service-url:
defaultZone: http://localhost:8761/eureka
spring:
application:
name: feign-consumer # 設(shè)置當(dāng)前應(yīng)用的名稱。將來會在eureka中Application顯示。將來需要使用該名稱來獲取路徑#設(shè)置Ribbon的超時時間
ribbon:
ConnectTimeout: 1000 #鏈接超時時間,默認(rèn)1s
ReadTimeout: 3000 #邏輯處理的超時時間 默認(rèn)1s
provider超時2s測試
Goods goods = goodsservice.findOne(id);
//當(dāng)前現(xiàn)場睡眠2秒
try {
Thread.sleep( millis: 2000);
}catch (InterruptedException e) {
e.printStackTrace(); //java.net.SocketTimeoutException: Read timed out
}
goods.setTitle(goods.getTitle() + ":" + port);//將端口號,設(shè)置Feign其他功能-日志記錄
Feign 只能記錄 debug 級別的日志信息。
logging:
level:
com.itheima: debug //包名
定義Feign日志級別Bean
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
啟用該Bean:
@FeignClient(configuration = XxxConfig.class)

修改consumer
#設(shè)置當(dāng)前的日志級別 debug,feign 只支持記錄debug級別的日志
logging:
level:
com.itheima: debug
package com.itheima.consumer.config;
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeignLogConfig {
/**
* NONE, 不記錄
* BASIC, 記錄基本的請求行,響應(yīng)狀態(tài)碼數(shù)據(jù)
* HEADERS, 記錄基本的請求行,響應(yīng)狀態(tài)碼數(shù)據(jù),記錄響應(yīng)頭信息
* FULL 記錄完整的請求,響應(yīng)數(shù)據(jù)
* @return
*/
@Bean
public Logger.Level level(){
return Logger.Level.FULL;
}
}
package com.itheima.consumer.feign;
import com.itheima.consumer.config.FeignLogConfig;
import com.itheima.consumer.domain.Goods;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
* feign聲明式接口 發(fā)錢遠(yuǎn)程調(diào)用的
* String url = "http://FEIGN-PROVIDER/goods/findOne/"+id;
* // 3. 調(diào)用方法
* Goods goods = restTemplate.getForObject(url, Goods.class);
*
* 1 定義接口
* 2 接口上添加注解 @FeignClient 設(shè)置value屬性為服務(wù)提供的 應(yīng)用名稱
* 3 編寫調(diào)用接口,接口的聲明規(guī)則和提供方接口保持一致
* 4 注入該接口對象,調(diào)用接口方法完成遠(yuǎn)程調(diào)用
*/
@FeignClient(value = "FEIGN-PROVIDER",configuration = FeignLogConfig.class)
public interface GoodsFeignClient {
@GetMapping("/goods/findOne/{id}")
public Goods findGoodsById(@PathVariable("id") int id);
}
到此這篇關(guān)于Spring Feign超時設(shè)置深入了解的文章就介紹到這了,更多相關(guān)Spring Feign超時設(shè)置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring boot實現(xiàn)阿里云視頻點播上傳視頻功能(復(fù)制粘貼即可)
這篇文章主要介紹了spring boot實現(xiàn)阿里云視頻點播上傳視頻功能(復(fù)制粘貼即可),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12

