SpringBoot項(xiàng)目中feignClient使用方式
SpringBoot項(xiàng)目中feignClient使用
1.在application.yml中加入兩個(gè)配置
# feign client 配置
hystrix:
threadpool:
default:
coreSize: 500
maxQueueSize: -1
queueSizeRejectionThreshold: 10000
command:
default:
circuitBreaker:
requestVolumeThreshold: 1500
execution:
#(超時(shí)時(shí)間)
timeout:
enabled: false
#(上下文傳遞)
isolation:
strategy: SEMAPHORE
semaphore:
maxConcurrentRequests: 1500
maxSemaphores: 1500
ribbon:
ConnectTimeout: 50000000
ReadTimeout: 5000000002.在主類上加入
scanBasePackageClasses = FeignInterceptor.class, scanBasePackages = {"com.example"}FeignInterceptor用來掃描 傳遞上下文的攔截器
{"com.example"}用來掃描自己的包
@EnableFeignClients(主類上)掃描被@FeignClient注解的接口
@EnableFeignClients
@SpringBootApplication(scanBasePackageClasses = FeignInterceptor.class, scanBasePackages = {"com.example"})
3.將xxxRestContextInterceptor加入到攔截器中
@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
//注冊自定義攔截器,添加攔截路徑和排除攔截路徑
registry.addInterceptor(new XsyRestContextInterceptor());
}
}4.接口調(diào)用說明
聲明一個(gè)接口比如:PaasAggFeignClient,在其上顯式加上@FeignClient注解
內(nèi)部填入Eureka上已注冊服務(wù)名稱比如:
@FeignClient("manager-service", configuration = FeignInterceptor.class)其中configuration = FeignInterceptor.class是要加上的,保證FeignInterceptor生效
自定義方法,請求路徑為該服務(wù)上對應(yīng)的rest請求路徑,如下
@FeignClient("manager-service")
public interface PaasAggFeignClient {
@RequestMapping(value = "/api/xxxx/test/description", method = RequestMethod.GET)
String testDescription();
@RequestMapping(value = "/api/xxxx/test/{id}", method = RequestMethod.GET)
String testInfo(@PathVariable("id") Long id);
@RequestMapping(value = "/api/xxxx/test/", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_JSON})
String testCreate(@RequestBody JSONObject jsonObject);
@RequestMapping(value = "/api/xxxx/test/{id}", method = RequestMethod.DELETE)
String testDelete(@PathVariable("id") Long id);
@RequestMapping(value = "/api/xxxx/test/{id}", method = RequestMethod.PATCH, consumes = {MediaType.APPLICATION_JSON})
String testUpdate(@PathVariable("id") Long id, @RequestBody JSONObject jsonObject);
}5.在對應(yīng)controller內(nèi)引入自定義接口即可使用
@Resource private PaasAggFeignClient paasAggFeignClient; String paasAggResult = paasAggFeignClient.testDescription(); String paasAggResult = paasAggFeignClient.testCreate(entity); String paasAggResult = paasAggFeignClient.testDelete(id); .....
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java 中的進(jìn)制轉(zhuǎn)換與編碼機(jī)制詳解
在 Java 編程領(lǐng)域,進(jìn)制轉(zhuǎn)換是一項(xiàng)極為基礎(chǔ)且重要的技能,下面給大家介紹Java 中的進(jìn)制轉(zhuǎn)換與編碼機(jī)制,感興趣的朋友一起看看吧2025-04-04
springboot接收別人上傳的本地視頻實(shí)例代碼
本文通過實(shí)例代碼給大家介紹了springboot接收別人上傳的本地視頻,代碼簡單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-07-07
Java 關(guān)鍵字static詳解及實(shí)例代碼
這篇文章主要介紹了Java 關(guān)鍵字static詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-04-04
Java實(shí)現(xiàn)獲取小程序帶參二維碼并保存到本地
這篇文章主要介紹了Java實(shí)現(xiàn)獲取小程序帶參二維碼并保存到本地,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
解析ConcurrentHashMap: 紅黑樹的代理類(TreeBin)
ConcurrentHashMap是由Segment數(shù)組結(jié)構(gòu)和HashEntry數(shù)組結(jié)構(gòu)組成。Segment的結(jié)構(gòu)和HashMap類似,是一種數(shù)組和鏈表結(jié)構(gòu),今天給大家普及java面試常見問題---ConcurrentHashMap知識,一起看看吧2021-06-06
SpringCloud消息總線Bus配置中心實(shí)現(xiàn)過程解析
這篇文章主要介紹了SpringCloud消息總線Bus配置中心實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
springboot中實(shí)現(xiàn)通過后臺創(chuàng)建臨時(shí)表
這篇文章主要介紹了springboot中實(shí)現(xiàn)通過后臺創(chuàng)建臨時(shí)表操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
解決SpringBoot的@DeleteMapping注解的方法不被調(diào)用問題
這篇文章主要介紹了解決SpringBoot的@DeleteMapping注解的方法不被調(diào)用問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01

