Spring?cloud?OpenFeign中動態(tài)URl、動態(tài)傳遞接口地址代碼示例
前言:
在微服務盛行的今天,做接口開發(fā)請求第三方服務的接口,大概率會用feign做請求,而feign也是最常用的一種rpc框架;
這里主要是說明在進行feign請求的時候,第三方服務的url和接口如何動態(tài)獲取。
若是該接口是作為基礎服務可能會請求多個第三方使用(我們就是不同分支的代碼作為獨立項目部署,請求不同的客戶接口),不同客戶的接口地址可能不同,此時就需要做成動態(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,
- 調用客戶的具體的目標方法是:/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
第三種方式:調用feign時動態(tài)傳入
實現了url和目標方法的動態(tài)傳入
1、目標方法的動態(tài)傳入
利用@PathVariable注解的特性;
用于接收請求路徑中占位符的值
@PathVariable(“xxx”)
通過 @PathVariable 可以將URL中占位符參數{xxx}綁定到處理器類的方法形參中
如:
@RequestMapping(value=”user/{id}/{name}”)
請求路徑:http://localhost:8080/hello/show/1/lisi
2、url動態(tài)實現
在創(chuàng)建feignclient時設置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接口調用方式,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對象比較,設置decoder、encoder、 contract為SpringBoot中自動創(chuàng)建對象相同,然后定義Feign接口的時候, 各種參數的注解和方法的注解就可以和不動態(tài)修改url的相同了 decoder解碼器,對返回的結果進行解碼*/ OptionalDecoder decoder = new OptionalDecoder(new ResponseEntityDecoder(new SpringDecoder(messageConverters))); //encoder編碼器,對輸入的數據進行編碼 SpringEncoder springEncoder = new SpringEncoder(messageConverters); SpringFormEncoder encoder = new SpringFormEncoder(springEncoder); //該對象是將接口進行解析,方便生成最后調用的網絡對象HttpurlConnection SpringMvcContract contract = new SpringMvcContract(); RemoteFeignClient feignClient = Feign.builder() .decoder(decoder) .encoder(encoder) .contract(contract) //這個地方的Url可以根據每次調用的時候進行改變 .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; } }
結果示例
fallback方式服務降級
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: 服務降級 * @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("服務調用失敗:{}", throwable.getMessage()); return new RemoteFeignClient() { @Override public List<User> test(@PathVariable("apiName") String apiName, User user) { return new ArrayList<>(); } }; } }
目前我想到的是這種方式,既可以把url動態(tài)配置,請求路徑也可實現動態(tài),
總結
到此這篇關于Spring cloud OpenFeign中動態(tài)URl、動態(tài)傳遞接口地址的文章就介紹到這了,更多相關OpenFeign動態(tài)URl、動態(tài)傳遞接口內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java實戰(zhàn)角色權限后臺腳手架系統(tǒng)的實現流程
只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+Springboot+Maven+myBaits-Plus+Vue+Element-UI+Mysql實現一個角色權限后臺腳手架系統(tǒng),大家可以在過程中查缺補漏,提升水平2022-01-01MybatisPlus創(chuàng)建時間不想用默認值的問題
MybatisPlus通過FieldFill注解和MpMetaObjectHandler類支持自動填充字段功能,特別地,可以設置字段在插入或更新時自動填充創(chuàng)建時間和更新時間,但在特定場景下,如導入數據時,可能需要自定義創(chuàng)建時間2024-09-09