Spring?Feign超時(shí)設(shè)置深入了解
Feign其他功能-超時(shí)設(shè)置
- Feign 底層依賴于 Ribbon 實(shí)現(xiàn)負(fù)載均衡和遠(yuǎn)程調(diào)用。
- Ribbon默認(rèn)1秒超時(shí)。
超時(shí)配置:
ribbon:
ConnectTimeout: 1000 #連接超時(shí)時(shí)間,毫秒
ReadTimeout: 1000 #邏輯處理超時(shí)時(shí)間,毫秒
在feign-consumer中設(shè)置超時(shí)時(shí)間(具體代碼看上 Feign的快速入門)
server:
port: 9000eureka:
instance:
hostname: localhost # 主機(jī)名
client:
service-url:
defaultZone: http://localhost:8761/eureka
spring:
application:
name: feign-consumer # 設(shè)置當(dāng)前應(yīng)用的名稱。將來會(huì)在eureka中Application顯示。將來需要使用該名稱來獲取路徑#設(shè)置Ribbon的超時(shí)時(shí)間
ribbon:
ConnectTimeout: 1000 #鏈接超時(shí)時(shí)間,默認(rèn)1s
ReadTimeout: 3000 #邏輯處理的超時(shí)時(shí)間 默認(rèn)1s
provider超時(shí)2s測(cè)試
Goods goods = goodsservice.findOne(id); //當(dāng)前現(xiàn)場(chǎng)睡眠2秒 try { Thread.sleep( millis: 2000); }catch (InterruptedException e) { e.printStackTrace(); //java.net.SocketTimeoutException: Read timed out } goods.setTitle(goods.getTitle() + ":" + port);//將端口號(hào),設(shè)置
Feign其他功能-日志記錄
Feign 只能記錄 debug 級(jí)別的日志信息。
logging:
level:
com.itheima: debug //包名
定義Feign日志級(jí)別Bean
@Bean Logger.Level feignLoggerLevel() { return Logger.Level.FULL; }
啟用該Bean:
@FeignClient(configuration = XxxConfig.class)
修改consumer
#設(shè)置當(dāng)前的日志級(jí)別 debug,feign 只支持記錄debug級(jí)別的日志
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, 記錄基本的請(qǐng)求行,響應(yīng)狀態(tài)碼數(shù)據(jù) * HEADERS, 記錄基本的請(qǐng)求行,響應(yīng)狀態(tài)碼數(shù)據(jù),記錄響應(yīng)頭信息 * FULL 記錄完整的請(qǐng)求,響應(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 注入該接口對(duì)象,調(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í)設(shè)置深入了解的文章就介紹到這了,更多相關(guān)Spring Feign超時(shí)設(shè)置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用5分鐘快速搭建一個(gè)springboot項(xiàng)目的全過程
Spring Boot的監(jiān)控能夠使開發(fā)者更好地掌控應(yīng)用程序的運(yùn)行狀態(tài),下面這篇文章主要給大家介紹了關(guān)于如何利用5分鐘快速搭建一個(gè)springboot項(xiàng)目的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05Java基于websocket協(xié)議與netty實(shí)時(shí)視頻彈幕交互實(shí)現(xiàn)
本文主要介紹了Java基于websocket協(xié)議與netty實(shí)時(shí)視頻彈幕交互實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09創(chuàng)建網(wǎng)關(guān)項(xiàng)目(Spring Cloud Gateway)過程詳解
這篇文章主要介紹了創(chuàng)建網(wǎng)關(guān)項(xiàng)目(Spring Cloud Gateway)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09