Spring Cloud 學(xué)習(xí)筆記08----服務(wù)消費(fèi)者(Feign)(Finchley版本)
概要
接上一篇《Spring Cloud 學(xué)習(xí)筆記06----斷路器(Hystrix)(Finchley版本)》,今天我們來(lái)學(xué)習(xí)另外一種服務(wù)調(diào)用方式(Feign),之前我們介紹了 RestTemplate+Ribbon 消費(fèi)服務(wù)的方式。
Feign簡(jiǎn)介
Feign 是一個(gè)聲明式的偽Http客戶(hù)端,它使得寫(xiě)Http 客戶(hù)端變得更簡(jiǎn)單,使用Feign,只需要?jiǎng)?chuàng)建一個(gè)接口并注解。它具有可插拔的注解特性,可使用Feign注解和JAX-RS注解,F(xiàn)eign 也支持可插拔的編碼器和解碼器,Spring Cloud 擴(kuò)展了對(duì)Spring MVC的注解支持,在Spring Web 中同樣使用HttpMessageConverters
。Feign 默認(rèn)集成了Ribbon, 并和Eureka 結(jié)合,默認(rèn)實(shí)現(xiàn)了負(fù)載均衡的效果。
簡(jiǎn)單來(lái)說(shuō):
- Feign 采用的是基于接口的注解
- Feign 整合了Ribbon, 具有負(fù)載均衡的能力
- 整合了Hystrix,具有熔斷能力。
快速入門(mén)
首先,沿用前面的服務(wù)注冊(cè)中心(eureka-server)以及服務(wù)提供者(order-provider)。然后新建一個(gè)SpringBoot 項(xiàng)目,命名為service-feign。添加依賴(lài)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--必須添加spring-boot-starter-web,否則注冊(cè)失敗-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
然后,在application.yml 加上如下配置,指定端口為8765,服務(wù)名為service-feign:
spring:
application:
name: service-feign
eureka:
client:
service-url:
# 用于指定注冊(cè)中心的地址
defaultZone: http://localhost:1111/eureka/
server:
port: 8765
接著,我們?cè)赟erviceFeignApplication 中添加@EnableFeignClients
以激活Feign。添加@EnableEurekaClient
以開(kāi)啟負(fù)載均衡,使其可以注冊(cè)到Eureka 中。
@SpringBootApplication
@EnableFeignClients
@EnableEurekaClient
public class ServiceFeignApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceFeignApplication.class, args);
}
}
接著,我們新建一個(gè)接口,用于調(diào)用服務(wù)提供者提供的服務(wù),使用@FeignClient(serviceId = "order-service")
指定調(diào)用的服務(wù)名為order-service。
@FeignClient(serviceId = "order-service")
public interface HelloService {
@GetMapping(value = "/dc")
String getOrderService(@RequestParam("name") String name);
}
最后,我們新建一個(gè)controller 調(diào)用剛剛新建接口HelloService中的方法,測(cè)試下是否可以調(diào)用成功
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping("/dc")
public String getOrderService(String name) {
return helloService.getOrderService(name);
}
}
測(cè)試結(jié)果,我們啟動(dòng)兩個(gè)order-provider 服務(wù),端口號(hào)分別為 8082,8083,
然后,啟動(dòng) service-feign, 端口號(hào)為:8765,
在Eureka面板中查看
訪(fǎng)問(wèn)http://localhost:8765/dc?name=jay
會(huì)交替出現(xiàn)如下結(jié)果:
hi: jay;port=8083
hi: jay;port=8082
參考代碼
https://github.com/XWxiaowei/SpringCloud-Learning/tree/master/2-Finchley版教程示例/Chapter6-1
參考文獻(xiàn)
http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html#netflix-feign-starter
https://blog.csdn.net/forezp/article/details/81040965
作者:碼農(nóng)飛哥
微信公眾號(hào):碼農(nóng)飛哥