SpringCloud OpenFeign超詳細(xì)講解模板化遠(yuǎn)程通信的實(shí)現(xiàn)
1. openFeign實(shí)現(xiàn)
基于spring-boot-starter-parent 2.6.8,spring-cloud-dependencies 2021.0.3
,一個(gè)order服務(wù)一個(gè)user服務(wù)
1.1 pom依賴
<!--nacos服務(wù)注冊與發(fā)現(xiàn)--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2021.0.1.0</version> </dependency> <!--遠(yuǎn)程服務(wù)調(diào)用--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-loadbalancer</artifactId> </dependency> <!--服務(wù)調(diào)用feign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
1.2 yaml配置
order調(diào)用端,配置的超時(shí)設(shè)置注釋掉了只為記錄
spring:
application:
name: orderservice
cloud:
#找對應(yīng)網(wǎng)段的網(wǎng)卡 不配置內(nèi)部服務(wù)就走外網(wǎng)
inetutils:
preferred-networks: 192.168.0
nacos:
discovery:
server-addr: 192.168.0.221:8848
#feign:
# client:
# config:
# #default設(shè)置的是全局超時(shí)時(shí)間,對所有的openFeign接口服務(wù)都生效 默認(rèn)60s超時(shí)
# default:
# connectTimeout: 5000
# readTimeout: 5000
# #為某個(gè)服務(wù)設(shè)置超時(shí)時(shí)間 優(yōu)先于全局
# userservice:
# connectTimeout: 5000
# readTimeout: 5000
user服務(wù)僅需要注冊
spring:
application:
name: userservice
cloud:
#找對應(yīng)網(wǎng)段的網(wǎng)卡 不配置內(nèi)部服務(wù)就走外網(wǎng)
inetutils:
preferred-networks: 192.168.0
nacos:
discovery:
server-addr: 192.168.0.221:8848
遠(yuǎn)程調(diào)用依賴于注冊中心,這里用的是nacos,其他的eureka也可以的
1.3 客戶端調(diào)用代碼
- 啟動類上添加
@EnableFeignClients
注解 - api接口,可以單獨(dú)放在api包
@FeignClient(value = "userservice") //沒有注冊中心的服務(wù)調(diào)用使用 testFeign/隨便寫 //@FeignClient(value = "testFeign",url = "http://192.168.0.199:7540") public interface UserService { //默認(rèn)是@RequestBody注解參數(shù) //如果使用其他注解一定要帶上value 否者會報(bào)錯(cuò) RequestParam.value() was empty on parameter 1 @GetMapping("/getTime/{uuid}") String getTime(@PathVariable("uuid") String uuid, @RequestParam("name") String name); @PostMapping("/postTime") Map<String, Object> getTime(@RequestBody Map<String, Object> params); }
客戶端代碼
@Resource UserService userService; @GetMapping("/test") public String test() throws Exception { log.info("openFeign -- start"); Map<String, Object> time = userService.getTime(resMap); log.info("openFeign -- {}", time); return template + ":" + time; }
1.4.服務(wù)端暴露接口
@PostMapping("/postTime") public Map<String, Object> getTime(@RequestBody Map<String, Object> params) { params.put("time", new Date().getTime()); return params; }
1.5.測試日志
c.e.order.controller.OrderController : openFeign -- start
c.e.order.controller.OrderController : openFeign -- {aaaa=bbbb, time=1657187048104}
到此這篇關(guān)于SpringCloud OpenFeign超詳細(xì)講解模板化遠(yuǎn)程通信的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringCloud OpenFeign模板化遠(yuǎn)程通信內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringCloud?OpenFeign?服務(wù)調(diào)用傳遞?token的場景分析
- springcloud引入spring-cloud-starter-openfeign失敗的解決
- SpringCloud升級2020.0.x版之OpenFeign簡介與使用實(shí)現(xiàn)思路
- 一篇文章教你如何在SpringCloud項(xiàng)目中使用OpenFeign
- 完美解決SpringCloud-OpenFeign使用okhttp替換不生效問題
- SpringCloud OpenFeign Post請求400錯(cuò)誤解決方案
- SpringCloud OpenFeign超時(shí)控制示例詳解
相關(guān)文章
Maven項(xiàng)目引用第三方j(luò)ar包找不到類ClassNotFoundException
這篇文章主要為大家介紹了Maven項(xiàng)目引用第三方j(luò)ar包找不到類ClassNotFoundException解決及原因分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07Java高效實(shí)現(xiàn)excel轉(zhuǎn)pdf(支持帶圖片的轉(zhuǎn)換)
這篇文章主要為大家詳細(xì)介紹了如何用java實(shí)現(xiàn)excel轉(zhuǎn)pdf文件,并且支持excel單元格中帶有圖片的轉(zhuǎn)換,文中的示例代碼講解詳細(xì),需要的可以參考下2024-01-01深入學(xué)習(xí)springboot線程池的使用和擴(kuò)展
這篇文章主要介紹了深入學(xué)習(xí)springboot線程池的使用和擴(kuò)展,springboot框架提供了@Async注解,幫助我們更方便的將業(yè)務(wù)邏輯提交到線程池中異步執(zhí)行,需要的朋友可以參考下2019-06-06springboot中關(guān)于自動建表,無法更新字段的問題
這篇文章主要介紹了springboot中關(guān)于自動建表,無法更新字段的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02Java線程狀態(tài)及切換、關(guān)閉線程的正確姿勢分享
這篇文章主要給大家介紹了關(guān)于Java線程狀態(tài)及切換、關(guān)閉線程的正確姿勢,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10