Spring?cloud?OpenFeign中動(dòng)態(tài)URl、動(dòng)態(tài)傳遞接口地址代碼示例
前言:
在微服務(wù)盛行的今天,做接口開發(fā)請(qǐng)求第三方服務(wù)的接口,大概率會(huì)用feign做請(qǐng)求,而feign也是最常用的一種rpc框架;
這里主要是說(shuō)明在進(jìn)行feign請(qǐng)求的時(shí)候,第三方服務(wù)的url和接口如何動(dòng)態(tài)獲取。
若是該接口是作為基礎(chǔ)服務(wù)可能會(huì)請(qǐng)求多個(gè)第三方使用(我們就是不同分支的代碼作為獨(dú)立項(xiàng)目部署,請(qǐng)求不同的客戶接口),不同客戶的接口地址可能不同,此時(shí)就需要做成動(dòng)態(tài)方式;
若是不常改動(dòng),其實(shí)也沒必要?jiǎng)討B(tài)了;
常用方式:
通常我們是這么請(qǐng)求第三方接口的:(用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);
}說(shuō)明:
- 請(qǐng)求客戶的url是:http://127.0.0.1:8090,
- 調(diào)用客戶的具體的目標(biāo)方法是:/user/selectListNoPage 這個(gè)方法
第二種方式:配置文件傳參
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時(shí)動(dòng)態(tài)傳入
實(shí)現(xiàn)了url和目標(biāo)方法的動(dòng)態(tài)傳入
1、目標(biāo)方法的動(dòng)態(tài)傳入
利用@PathVariable注解的特性;
用于接收請(qǐng)求路徑中占位符的值
@PathVariable(“xxx”)
通過(guò) @PathVariable 可以將URL中占位符參數(shù){xxx}綁定到處理器類的方法形參中
如:
@RequestMapping(value=”user/{id}/{name}”)
請(qǐng)求路徑:http://localhost:8080/hello/show/1/lisi
2、url動(dòng)態(tài)實(shí)現(xiàn)
在創(chuàng)建feignclient時(shí)設(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客戶端的時(shí)候最核心的對(duì)象是decoder、encoder、contract
通過(guò)跟蹤源碼與SpringBoot自動(dòng)創(chuàng)建的Feign對(duì)象比較,設(shè)置decoder、encoder、
contract為SpringBoot中自動(dòng)創(chuàng)建對(duì)象相同,然后定義Feign接口的時(shí)候,
各種參數(shù)的注解和方法的注解就可以和不動(dòng)態(tài)修改url的相同了
decoder解碼器,對(duì)返回的結(jié)果進(jìn)行解碼*/
OptionalDecoder decoder = new OptionalDecoder(new ResponseEntityDecoder(new SpringDecoder(messageConverters)));
//encoder編碼器,對(duì)輸入的數(shù)據(jù)進(jìn)行編碼
SpringEncoder springEncoder = new SpringEncoder(messageConverters);
SpringFormEncoder encoder = new SpringFormEncoder(springEncoder);
//該對(duì)象是將接口進(jìn)行解析,方便生成最后調(diào)用的網(wǎng)絡(luò)對(duì)象HttpurlConnection
SpringMvcContract contract = new SpringMvcContract();
RemoteFeignClient feignClient = Feign.builder()
.decoder(decoder)
.encoder(encoder)
.contract(contract)
//這個(gè)地方的Url可以根據(jù)每次調(diào)用的時(shí)候進(jìn)行改變
.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ù)降級(jí)
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ù)降級(jí)
* @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動(dòng)態(tài)配置,請(qǐng)求路徑也可實(shí)現(xiàn)動(dòng)態(tài),
總結(jié)
到此這篇關(guān)于Spring cloud OpenFeign中動(dòng)態(tài)URl、動(dòng)態(tài)傳遞接口地址的文章就介紹到這了,更多相關(guān)OpenFeign動(dòng)態(tài)URl、動(dòng)態(tài)傳遞接口內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)戰(zhàn)角色權(quán)限后臺(tái)腳手架系統(tǒng)的實(shí)現(xiàn)流程
只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+Springboot+Maven+myBaits-Plus+Vue+Element-UI+Mysql實(shí)現(xiàn)一個(gè)角色權(quán)限后臺(tái)腳手架系統(tǒng),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平2022-01-01
MybatisPlus創(chuàng)建時(shí)間不想用默認(rèn)值的問(wèn)題
MybatisPlus通過(guò)FieldFill注解和MpMetaObjectHandler類支持自動(dòng)填充字段功能,特別地,可以設(shè)置字段在插入或更新時(shí)自動(dòng)填充創(chuàng)建時(shí)間和更新時(shí)間,但在特定場(chǎng)景下,如導(dǎo)入數(shù)據(jù)時(shí),可能需要自定義創(chuàng)建時(shí)間2024-09-09
MybatisPlus如何調(diào)用count函數(shù)
這篇文章主要介紹了MybatisPlus如何調(diào)用count函數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
Spring Boot整合郵件發(fā)送與注意事項(xiàng)
這篇文章主要給大家介紹了關(guān)于Spring Boot整合郵件發(fā)送與注意事項(xiàng)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
使用@Transactional 設(shè)置嵌套事務(wù)不回滾
這篇文章主要介紹了使用@Transactional 設(shè)置嵌套事務(wù)不回滾問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07

