一、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開發工具下載地址及安裝教程大全,點這里。
更多深度技術文章,在這里。
一、准備工作( [原文鏈接](http://www.studyshare.cn/blog/details/1187/null))
前提條件:當且僅當 spring.cloud.consul.config.format=files 時,才可用git2consul管理配置,其他格式無法使用
1、在github或者gitee等建立遠程倉庫,並將consul配置文件放入遠程倉庫進行管理java開發工具下載地址及安裝教程大全,點[這里](http://www.studyshare.cn/software/index)。更多深度技術文章,在[這里](http://www.studyshare.cn/blog/index)。2、下載並安裝node.js
(1)、下載
前往 https://nodejs.org/en/download/ 可下載最新版本Node.js;前往 https://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文件加入以下內容:
{ // 配置版本 "version": "1.0", "repos": [ { // 名稱,指的是在consul里面的目錄名稱 "name": "config", // 要同步的Git倉庫 "url": "你的git倉庫", "branches": [ // 要同步的分支 "master" ], // 是否要把分支名稱作為Consul的key前綴 "include_branch_name": false, "hooks": [ { // 拉取模式 "type": "polling", // 同步的間隔(分鍾) "interval": "1" } ] } ]}
其他選項參見:https://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五、查看是否成功
到consul上的key/value tab中查看是否注冊上去了原創文章,轉載請注明出處。java開發工具下載地址及安裝教程大全,點[這里](http://www.studyshare.cn/software/index)。更多深度技術文章,在[這里](http://www.studyshare.cn/blog/index)。