Spring?cloud?OpenFeign中動態(tài)URl、動態(tài)傳遞接口地址代碼示例
前言:
在微服務(wù)盛行的今天,做接口開發(fā)請求第三方服務(wù)的接口,大概率會用feign做請求,而feign也是最常用的一種rpc框架;
這里主要是說明在進行feign請求的時候,第三方服務(wù)的url和接口如何動態(tài)獲取。
若是該接口是作為基礎(chǔ)服務(wù)可能會請求多個第三方使用(我們就是不同分支的代碼作為獨立項目部署,請求不同的客戶接口),不同客戶的接口地址可能不同,此時就需要做成動態(tài)方式;
若是不常改動,其實也沒必要動態(tài)了;
常用方式:
通常我們是這么請求第三方接口的:(用feign方式)
import com.zkaw.lxjtest.Dto.User; import com.zkaw.lxjtest.remoteCall.feign.factory.RemoteFeignFactory; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import java.util.List; /** * @Author: Best_Liu * @Description: * @Date Create in 11:14 2022/7/1 * @Modified By: */ @FeignClient(value = "mybatisPlus", url = "http://127.0.0.1:8090", fallbackFactory = RemoteFeignFactory.class) public interface RemoteFeignClient { @PostMapping("/user/selectListNoPage") /*@Headers({"content-type:application/json"})*/ List<User> test(@RequestBody User user); }
說明:
- 請求客戶的url是:http://127.0.0.1:8090,
- 調(diào)用客戶的具體的目標(biāo)方法是:/user/selectListNoPage 這個方法
第二種方式:配置文件傳參
import com.zkaw.lxjtest.Dto.User; import com.zkaw.lxjtest.remoteCall.feign.factory.RemoteFeignFactory; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import java.util.List; /** * @Author: Best_Liu * @Description: * @Date Create in 11:14 2022/7/1 * @Modified By: */ @FeignClient(value = "mybatisPlus", url = "${feign.client.url.TestUrl}", fallbackFactory = RemoteFeignFactory.class) public interface RemoteFeignClient { @PostMapping("/user/selectListNoPage") /*@Headers({"content-type:application/json"})*/ List<User> test(@RequestBody User user); }
然后添加配置文件,比如
在你的 application-dev.yml 文件中
feign: client: url: TestUrl: http://127.0.0.1:8088
第三種方式:調(diào)用feign時動態(tài)傳入
實現(xiàn)了url和目標(biāo)方法的動態(tài)傳入
1、目標(biāo)方法的動態(tài)傳入
利用@PathVariable注解的特性;
用于接收請求路徑中占位符的值
@PathVariable(“xxx”)
通過 @PathVariable 可以將URL中占位符參數(shù){xxx}綁定到處理器類的方法形參中
如:
@RequestMapping(value=”user/{id}/{name}”)
請求路徑:http://localhost:8080/hello/show/1/lisi
2、url動態(tài)實現(xiàn)
在創(chuàng)建feignclient時設(shè)置url地址
所以改造下我們的方法:
import com.zkaw.lxjtest.Dto.User; import com.zkaw.lxjtest.remoteCall.feign.factory.RemoteFeignFactory; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import java.util.List; /** * @Author: Best_Liu * @Description: * @Date Create in 11:14 2022/7/1 * @Modified By: */ @FeignClient(value = "mybatisPlus", fallbackFactory = RemoteFeignFactory.class) public interface RemoteFeignClient { @PostMapping("{apiName}") /*@Headers({"content-type:application/json"})*/ List<User> test(@PathVariable("apiName") String apiName, @RequestBody User user); }
feign接口調(diào)用方式,createFeignClient是Feign核心部分
import com.zkaw.lxjtest.Dto.User; import com.zkaw.lxjtest.remoteCall.feign.service.RemoteFeignClient; import feign.Feign; import feign.form.spring.SpringFormEncoder; import feign.optionals.OptionalDecoder; import org.springframework.beans.factory.ObjectFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.cloud.openfeign.support.ResponseEntityDecoder; import org.springframework.cloud.openfeign.support.SpringDecoder; import org.springframework.cloud.openfeign.support.SpringEncoder; import org.springframework.cloud.openfeign.support.SpringMvcContract; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * @Author: Best_Liu * @Description: * @Date Create in 11:20 2022/7/1 * @Modified By: */ @RestController @RequestMapping("/feign") public class feignController { @Autowired ObjectFactory<HttpMessageConverters> messageConverters; private RemoteFeignClient createFeignClient(String url) { /*1、在創(chuàng)建Feign客戶端的時候最核心的對象是decoder、encoder、contract 通過跟蹤源碼與SpringBoot自動創(chuàng)建的Feign對象比較,設(shè)置decoder、encoder、 contract為SpringBoot中自動創(chuàng)建對象相同,然后定義Feign接口的時候, 各種參數(shù)的注解和方法的注解就可以和不動態(tài)修改url的相同了 decoder解碼器,對返回的結(jié)果進行解碼*/ OptionalDecoder decoder = new OptionalDecoder(new ResponseEntityDecoder(new SpringDecoder(messageConverters))); //encoder編碼器,對輸入的數(shù)據(jù)進行編碼 SpringEncoder springEncoder = new SpringEncoder(messageConverters); SpringFormEncoder encoder = new SpringFormEncoder(springEncoder); //該對象是將接口進行解析,方便生成最后調(diào)用的網(wǎng)絡(luò)對象HttpurlConnection SpringMvcContract contract = new SpringMvcContract(); RemoteFeignClient feignClient = Feign.builder() .decoder(decoder) .encoder(encoder) .contract(contract) //這個地方的Url可以根據(jù)每次調(diào)用的時候進行改變 .target(RemoteFeignClient.class, url); return feignClient; } @PostMapping("/selectListNoPage") public List<User> selectListNoPage(@RequestBody User user){ String apiName = "user/selectListNoPage"; String url = "http://127.0.0.1:8090"; RemoteFeignClient remoteFeignClient = createFeignClient(url); List<User> users = remoteFeignClient.test(apiName,user); return users; } }
結(jié)果示例
fallback方式服務(wù)降級
import com.zkaw.lxjtest.Dto.User; import com.zkaw.lxjtest.remoteCall.feign.service.RemoteFeignClient; import feign.hystrix.FallbackFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.PathVariable; import java.util.ArrayList; import java.util.List; /** * @Author: Best_Liu * @Description: 服務(wù)降級 * @Date Create in 11:34 2022/7/1 * @Modified By: */ @Component public class RemoteFeignFactory implements FallbackFactory<RemoteFeignClient> { private static final Logger log = LoggerFactory.getLogger(RemoteFeignFactory.class); @Override public RemoteFeignClient create(Throwable throwable) { log.error("服務(wù)調(diào)用失敗:{}", throwable.getMessage()); return new RemoteFeignClient() { @Override public List<User> test(@PathVariable("apiName") String apiName, User user) { return new ArrayList<>(); } }; } }
目前我想到的是這種方式,既可以把url動態(tài)配置,請求路徑也可實現(xiàn)動態(tài),
總結(jié)
到此這篇關(guān)于Spring cloud OpenFeign中動態(tài)URl、動態(tài)傳遞接口地址的文章就介紹到這了,更多相關(guān)OpenFeign動態(tài)URl、動態(tài)傳遞接口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實戰(zhàn)角色權(quán)限后臺腳手架系統(tǒng)的實現(xiàn)流程
只學(xué)書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+Springboot+Maven+myBaits-Plus+Vue+Element-UI+Mysql實現(xiàn)一個角色權(quán)限后臺腳手架系統(tǒng),大家可以在過程中查缺補漏,提升水平2022-01-01MybatisPlus創(chuàng)建時間不想用默認(rèn)值的問題
MybatisPlus通過FieldFill注解和MpMetaObjectHandler類支持自動填充字段功能,特別地,可以設(shè)置字段在插入或更新時自動填充創(chuàng)建時間和更新時間,但在特定場景下,如導(dǎo)入數(shù)據(jù)時,可能需要自定義創(chuàng)建時間2024-09-09MybatisPlus如何調(diào)用count函數(shù)
這篇文章主要介紹了MybatisPlus如何調(diào)用count函數(shù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08使用@Transactional 設(shè)置嵌套事務(wù)不回滾
這篇文章主要介紹了使用@Transactional 設(shè)置嵌套事務(wù)不回滾問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07