Spring?Feign超時設(shè)置深入了解
Feign其他功能-超時設(shè)置
- Feign 底層依賴于 Ribbon 實現(xiàn)負載均衡和遠程調(diào)用。
- Ribbon默認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è)置當前應用的名稱。將來會在eureka中Application顯示。將來需要使用該名稱來獲取路徑#設(shè)置Ribbon的超時時間
ribbon:
ConnectTimeout: 1000 #鏈接超時時間,默認1s
ReadTimeout: 3000 #邏輯處理的超時時間 默認1s
provider超時2s測試
Goods goods = goodsservice.findOne(id); //當前現(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è)置當前的日志級別 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, 記錄基本的請求行,響應狀態(tài)碼數(shù)據(jù) * HEADERS, 記錄基本的請求行,響應狀態(tài)碼數(shù)據(jù),記錄響應頭信息 * FULL 記錄完整的請求,響應數(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ā)錢遠程調(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ù)提供的 應用名稱 * 3 編寫調(diào)用接口,接口的聲明規(guī)則和提供方接口保持一致 * 4 注入該接口對象,調(diào)用接口方法完成遠程調(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)文章
Java基于websocket協(xié)議與netty實時視頻彈幕交互實現(xiàn)
本文主要介紹了Java基于websocket協(xié)議與netty實時視頻彈幕交互實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09創(chuàng)建網(wǎng)關(guān)項目(Spring Cloud Gateway)過程詳解
這篇文章主要介紹了創(chuàng)建網(wǎng)關(guān)項目(Spring Cloud Gateway)過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-09-09