Spring Cloud【Finchley】-06服務(wù)消費(fèi)者整合Feign
文章目錄

概述
實(shí)例
新建工程
增加maven依賴
創(chuàng)建一個(gè)Feign接口,并添加@FeignClient注解
修改Controller層,將RestTemplate改為調(diào)用Feign接口
啟動(dòng)類增加@EnableFeiginClients注解
測(cè)試
源碼
概述
回想下我們?cè)谑褂肊ureka 和 Ribbon的時(shí)候是怎么調(diào)用注冊(cè)在Eureka Server上的微服務(wù)的地址呢?
可以看到其實(shí)是通過(guò)拼接的方式,當(dāng)然了我們上面的這個(gè)例子只有一個(gè)參數(shù) id,看起來(lái)沒(méi)有這麻煩。
設(shè)想下如果有多個(gè)參數(shù)呢?
假設(shè)URL如下
http://localhost:8080/search?name=小工匠&age=20&username=artisan
那我們用RestTemplate如何調(diào)用對(duì)方的微服務(wù)呢? 可以采用如下方式
@GetMapping("/searchUser") public User searchUser(String name ,String age ,String username) { Map
1
2
3
4
5
6
7
8
9
10
11
12
13
是不是已經(jīng)很麻煩了?
spring Cloud為我們整合了Fegin解決上述苦惱。
Feign官方文檔: https://cloud.spring.io/spring-cloud-static/Finchley.SR2/single/spring-cloud.html#_spring_cloud_openfeign
Feign是Netflix開發(fā)的聲明模板化的HTTP客戶端。 在Spring Cloud中使用Feign,只需要?jiǎng)?chuàng)建一個(gè)接口,并在接口上添加一些注解即可。 Spring Cloud對(duì)Feign進(jìn)行了增強(qiáng),使Feign支持了SpringMVC的總結(jié),并整合了Ribbon和Eureka。
實(shí)例
新建工程
在父工程上右鍵,新建Maven Module ,如下
下面根據(jù)官方文檔操作即可
增加maven依賴
1
2
3
4
創(chuàng)建一個(gè)Feign接口,并添加@FeignClient注解
package com.artisan.micorservice.feignclient; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.artisan.micorservice.model.User; @FeignClient("microservice-provider-user") public interface UserFeignClient { @RequestMapping(method = RequestMethod.GET, value = "/user/{id}") public User findById(@PathVariable Long id); }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
FeignClient中的microservice-provider-user是要調(diào)用的微服務(wù)的名稱,用于創(chuàng)建Ribbon負(fù)載均衡器。
因?yàn)槲覀冞@里使用了Eureka,所以Ribbon會(huì)把microservice-provider-user解析成Eureka Server中注冊(cè)的服務(wù)。
另外,也可以通過(guò)url屬性指定請(qǐng)求的URL ,比如 @FeignClient("microservice-provider-user", url="http://localhost:8900/")
修改Controller層,將RestTemplate改為調(diào)用Feign接口
package com.artisan.micorservice.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import com.artisan.micorservice.feignclient.UserFeignClient; import com.artisan.micorservice.model.User; @RestController public class MovieController { @Autowired private UserFeignClient userClient; @GetMapping("/movie/{id}") public User findById(@PathVariable Long id) { return userClient.findById(id); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
啟動(dòng)類增加@EnableFeiginClients注解
package com.artisan.micorservice; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @EnableDiscoveryClient @SpringBootApplication @EnableFeignClients public class MicorserviceConsumerFeginApplication { public static void main(String[] args) { SpringApplication.run(MicorserviceConsumerFeginApplication.class, args); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
測(cè)試
啟動(dòng)eureka server微服務(wù)
啟動(dòng)2個(gè) provider-user微服務(wù)
啟動(dòng)該微服務(wù)
2次請(qǐng)求http://localhost:7901/movie/1 ,觀察 provider-user微服務(wù)的日志打印情況。
8900端口
8901端口
通過(guò)日志可以看到不僅實(shí)現(xiàn)了聲明式的REST API調(diào)用,同時(shí)也實(shí)現(xiàn)了客戶端的負(fù)載均衡。
源碼
https://github.com/yangshangwei/SpringCloudMaster
Spring Spring Cloud
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。