spring-cloud feign的多參數傳遞方案

查看原文

一、GET請求多參數URL

1、方法一(推薦)

@FeignClient(「microservice-provider-user」)

public interface UserFeignClient {

@GetMapping(“/get”)

public User get0(@SpringQueryMap User user);

}

2、方法二(推薦)

@FeignClient(name = 「microservice-provider-user」)

public interface UserFeignClient {

@RequestMapping(value = 「/get」, method = RequestMethod.GET)

public User get1(@RequestParam(「id」) Long id, @RequestParam(「username」) String username);

}

這是最為直觀的方式,URL有幾個參數,Feign介面中的方法就有幾個參數。使用@RequestParam註解指定請求的參數是什麼

java開發工具下載地址及安裝教程大全,點這裡

更多深度技術文章,在這裡

3、方法三(不推薦)

多參數的URL也可使用Map來構建。當目標URL參數非常多的時候,可使用這種方式簡化Feign介面的編寫。

@FeignClient(name = 「microservice-provider-user」)

public interface UserFeignClient {

@RequestMapping(value = 「/get」, method = RequestMethod.GET)

public User get2(@RequestParam Map<String, Object> map);

}

在調用時,可使用類似以下的程式碼。

public User get(String username, String password) {

HashMap<String, Object> map = Maps.newHashMap();

map.put(「id」, 「1」);

map.put(「username」, 「張三」); return this.userFeignClient.get2(map);

}

注意:這種方式不建議使用。主要是因為可讀性不好,而且如果參數為空的時候會有一些問題,例如map.put(「username」, null); 會導致microservice-provider-user 服務接收到的username是”” ,而不是null。

二、POST請求多參數URL

下面來討論如何使用Feign構造包含多個參數的POST請求。假設服務提供者的Controller是這樣編寫的:

@RestController public class UserController {

@PostMapping(“/post”)

public User post(@RequestBody User user) {

}

}

我們要如何使用Feign去請求呢?答案非常簡單,示例:

@FeignClient(name = 「microservice-provider-user」)

public interface UserFeignClient {

@RequestMapping(value = 「/post」, method = RequestMethod.POST)

public User post(@RequestBody User user);

}

原創文章,轉載請註明出處。

java開發工具下載地址及安裝教程大全,點這裡

更多深度技術文章,在這裡

 
 

0人點贊

 

一、準備工作( [原文鏈接](//www.studyshare.cn/blog/details/1187/null))
前提條件:當且僅當 spring.cloud.consul.config.format=files 時,才可用git2consul管理配置,其他格式無法使用
1、在github或者gitee等建立遠程倉庫,並將consul配置文件放入遠程倉庫進行管理![在這裡插入圖片描述](//img-blog.csdnimg.cn/20200415160945970.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2RhcmVuZHU=,size_16,color_FFFFFF,t_70)java開發工具下載地址及安裝教程大全,點[這裡](//www.studyshare.cn/software/index)。更多深度技術文章,在[這裡](//www.studyshare.cn/blog/index)。2、下載並安裝node.js
(1)、下載
前往 //nodejs.org/en/download/ 可下載最新版本Node.js;前往 //nodejs.org/en/download/releases/ 可下載指定版本Node.js;根據自己的作業系統,下載安裝即可
(2)、安裝
基本都是按下一步走即可,此處省略
二、安裝git2consul
mac系統:
執行:npm install -g git2consul 如果提示無許可權
執行:sudo npm install -g git2consul 輸入密碼授權即可
windows系統:
執行:npm install -g git2consul-windows
三、配置git2consul
在遠程倉庫項目下新建git2consul.json文件![在這裡插入圖片描述](//img-blog.csdnimg.cn/20200415161010653.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2RhcmVuZHU=,size_16,color_FFFFFF,t_70)加入以下內容:
{  // 配置版本  “version”: “1.0”,  “repos”: [    {      // 名稱,指的是在consul裡面的目錄名稱      “name”: “config”,      // 要同步的Git倉庫      “url”: “你的git倉庫”,      “branches”: [        // 要同步的分支        “master”      ],      // 是否要把分支名稱作為Consul的key前綴      “include_branch_name”: false,      “hooks”: [        {          // 拉取模式          “type”: “polling”,          // 同步的間隔(分鐘)          “interval”: “1”        }      ]    }  ]}
其他選項參見://github.com/breser/git2consul四、啟動git2consul
執行如下命令,即可啟動git2consul
mac:git2consul –config-file /Users/itmuch/develop/git2consul.json
windows:git2consul-windows –config-file C:/xxxx/xxx/git2consul.json![在這裡插入圖片描述](//img-blog.csdnimg.cn/20200415161032436.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2RhcmVuZHU=,size_16,color_FFFFFF,t_70)五、查看是否成功
到consul上的key/value  tab中查看是否註冊上去了![在這裡插入圖片描述](//img-blog.csdnimg.cn/20200415161045341.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2RhcmVuZHU=,size_16,color_FFFFFF,t_70)原創文章,轉載請註明出處。java開發工具下載地址及安裝教程大全,點[這裡](//www.studyshare.cn/software/index)。更多深度技術文章,在[這裡](//www.studyshare.cn/blog/index)。