最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring?cloud?OpenFeign中動態(tài)URl、動態(tài)傳遞接口地址代碼示例

 更新時間:2024年02月04日 10:41:25   作者:Best_Liu~  
openFeign是作為微服務(wù)之間調(diào)用的解決方案,每個微服務(wù)項目是必不可少的,下面這篇文章主要給大家介紹了關(guān)于Spring?cloud?OpenFeign中動態(tài)URl、動態(tài)傳遞接口地址的相關(guān)資料,需要的朋友可以參考下

前言:

在微服務(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)用客戶的具體的目標方法是:/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和目標方法的動態(tài)傳入

1、目標方法的動態(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)文章

  • SpringMVC+Spring+Mybatis實現(xiàn)支付寶支付功能的示例代碼

    SpringMVC+Spring+Mybatis實現(xiàn)支付寶支付功能的示例代碼

    這篇文章主要介紹了SpringMVC+Spring+Mybatis實現(xiàn)支付寶支付功能的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-05-05
  • Json傳輸出現(xiàn)中文亂碼問題的解決辦法

    Json傳輸出現(xiàn)中文亂碼問題的解決辦法

    最近遇到一個問題,就是將中文消息以json格式推給微信服務(wù)器時,收到的消息是亂碼,所以下面這篇文章主要給大家介紹了關(guān)于Json傳輸出現(xiàn)中文亂碼問題的解決辦法,需要的朋友可以參考下
    2023-05-05
  • Java sha1散列算法原理及代碼實例

    Java sha1散列算法原理及代碼實例

    這篇文章主要介紹了Java sha1散列算法原理及代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-09-09
  • JAVA實現(xiàn)掃描線算法(超詳細)

    JAVA實現(xiàn)掃描線算法(超詳細)

    掃描線算法就是從Ymin開始掃描,然后構(gòu)建出NET,之后根據(jù)NET建立AET。接下來本文通過代碼給大家介紹JAVA實現(xiàn)掃描線算法,感興趣的朋友一起看看吧
    2019-10-10
  • Java常用工具類總結(jié)

    Java常用工具類總結(jié)

    今天帶大家學習Java常用工具類,文中有非常詳細的圖文解說及代碼示例,對正在學習java的小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • 如何解決springboot自動重啟問題

    如何解決springboot自動重啟問題

    這篇文章主要介紹了如何解決springboot自動重啟問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • java如何實現(xiàn)項目啟動時執(zhí)行指定方法

    java如何實現(xiàn)項目啟動時執(zhí)行指定方法

    這篇文章主要為大家詳細介紹了java項目如何啟動時執(zhí)行指定方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • 淺談java是如何做資源回收補救的

    淺談java是如何做資源回收補救的

    這篇文章主要介紹了淺談java是如何做資源回收補救的,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-06-06
  • Java 數(shù)據(jù)庫連接池 DBCP 的介紹

    Java 數(shù)據(jù)庫連接池 DBCP 的介紹

    這篇文章主要給大家分享的是 Java 數(shù)據(jù)庫連接池 DBCP 的介紹, 是 Apache 旗下 Commons 項目下的一個子項目,提供連接池功能DBCP,下面來看看文章的具體介紹內(nèi)容吧,需要的朋友可以參考一下
    2021-11-11
  • 詳解JVM 運行時內(nèi)存使用情況監(jiān)控

    詳解JVM 運行時內(nèi)存使用情況監(jiān)控

    這篇文章主要介紹了詳解JVM 運行時內(nèi)存使用情況監(jiān)控,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-09

最新評論

雷山县| 阿拉善左旗| 五河县| 姜堰市| 武宁县| 武宁县| 铜川市| 招远市| 江都市| 扶余县| 收藏| 菏泽市| 长葛市| 巨鹿县| 玉门市| 汝州市| 大姚县| 宜都市| 乌苏市| 房产| 乃东县| 历史| 塔河县| 文登市| 曲松县| 海南省| 盐城市| 增城市| 当阳市| 凉城县| 平度市| 连城县| 方正县| 柳河县| 清镇市| 廉江市| 论坛| 扎囊县| 临汾市| 桃园市| 江川县|