前言
本篇文章主要介紹的是Feign實(shí)現(xiàn)服務(wù)間調(diào)用,集成Hystrix熔斷器、Hystrix-Dashboard儀表盤(pán)
GitHub源碼鏈接位于文章底部。
Feign 簡(jiǎn)介
Feign是一個(gè)http請(qǐng)求調(diào)用的輕量級(jí)框架,可以以Java接口注解的方式調(diào)用Http請(qǐng)求,而不用像Java中通過(guò)封裝HTTP請(qǐng)求報(bào)文的方式直接調(diào)用。Feign通過(guò)處理注解,將請(qǐng)求模板化,當(dāng)實(shí)際調(diào)用的時(shí)候,傳入?yún)?shù),根據(jù)參數(shù)再應(yīng)用到請(qǐng)求上,進(jìn)而轉(zhuǎn)化成真正的請(qǐng)求。
在實(shí)際開(kāi)發(fā)中,所有的服務(wù)都要注冊(cè)到注冊(cè)中心,然后一個(gè)服務(wù)可以在eureka注冊(cè)中心通過(guò)feign調(diào)用另一個(gè)服務(wù)。因此我們需要先建立eureka注冊(cè)中心,再建立一些普通服務(wù)作為eureka客戶端,作為被調(diào)用的服務(wù),其實(shí)只是一個(gè)普通的微服務(wù)項(xiàng)目,而需要調(diào)用別的服務(wù)的,則需要加入feign,也就是feign客戶端,這里是沒(méi)有真正定義的feign服務(wù)端的。
一、eureka注冊(cè)中心
先創(chuàng)建springcloud-feign父工程。
和上篇文章中寫(xiě)的過(guò)程一樣,這里建立一個(gè)單點(diǎn)eureka服務(wù)端。
添加eureka服務(wù)端依賴
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
配置文件
#服務(wù)端口號(hào)
server:
port: 8100
spring:
application:
name: eureka-server
#eureka基本配置信息
eureka:
client:
service-url:
#Eureka 客戶端與 Eureka 服務(wù)端進(jìn)行交互的地址
defaultZone: http://127.0.0.1:${server.port}/eureka
#是否將自己注冊(cè)到Eureka服務(wù)中,本身就是注冊(cè)中心所以無(wú)需注冊(cè)
register-with-eureka: false
#是否從Eureka中檢索注冊(cè)信息,本身就是注冊(cè)中心所以無(wú)需檢索
fetch-registry: false
server:
# 測(cè)試時(shí)關(guān)閉自我保護(hù)機(jī)制,保證不可用服務(wù)及時(shí)踢出
enable-self-preservation: false
##剔除失效服務(wù)間隔
eviction-interval-timer-in-ms: 2000
啟動(dòng)類
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApp {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApp.class, args);
}
}
二、創(chuàng)建一個(gè)普通服務(wù),作為被調(diào)用的一方
引入依賴
這個(gè)服務(wù)也是eureka客戶端,要被注冊(cè)到注冊(cè)中心,所以只需要添加eureka客戶端的依賴即可。
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
配置文件
#端口號(hào)
server:
port: 8101
spring:
application:
name: feign-server
#eureka基本配置信息
eureka:
client:
service-url:
#Eureka 客戶端與 Eureka 服務(wù)端進(jìn)行交互的地址
defaultZone: http://127.0.0.1:8100/eureka/
# 心跳檢測(cè)檢測(cè)與續(xù)約時(shí)間
# 測(cè)試時(shí)將值設(shè)置設(shè)置小些,保證服務(wù)關(guān)閉后注冊(cè)中心能及時(shí)踢出服務(wù)
instance:
# Eureka客戶端向服務(wù)端發(fā)送心跳的時(shí)間間隔,單位為秒(客戶端告訴服務(wù)端自己會(huì)按照該規(guī)則)
lease-renewal-interval-in-seconds: 1
# Eureka服務(wù)端在收到最后一次心跳之后等待的時(shí)間上限,單位為秒,超過(guò)則剔除(客戶端告訴服務(wù)端按照此規(guī)則等待自己)
lease-expiration-duration-in-seconds: 2
feign是通過(guò)配置文件中的spring : application : name : eureka-server應(yīng)用名調(diào)用的
controller層中添加一個(gè)接口
@RestController
public class ServerController {
@RequestMapping("/index")
public String index(@RequestParam String name) {
return name+"----這是被調(diào)用的服務(wù)!";
}
}
啟動(dòng)類
@SpringBootApplication
@EnableEurekaClient
public class FeignServerApp {
public static void main(String[] args) {
SpringApplication.run(FeignServerApp.class, args);
}
}
先啟動(dòng)eureka服務(wù)端,訪問(wèn) http://localhost:8100/ ,可以看到該服務(wù)已經(jīng)被注冊(cè)到注冊(cè)中心了。

再啟動(dòng)該服務(wù),調(diào)用接口 http://localhost:8101/index?name=測(cè)試 。

三、 創(chuàng)建一個(gè)Feign客戶端,去調(diào)用剛才創(chuàng)建的服務(wù)中的接口

引入依賴
該服務(wù)作為eureka客戶端和feign客戶端,所以要引入eureka客戶端和feign依賴
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
創(chuàng)建Feign接口
創(chuàng)建一個(gè)serverFeign
@FeignClient(name = "feign-server")
public interface ServerFeign {
@RequestMapping(value = "/index")
public String index(@RequestParam(value = "name") String name) ;
}
類上使用FeignClient注解,name屬性是被調(diào)用服務(wù)的名稱,在配置文件中有設(shè)置,然后將被調(diào)用的服務(wù)的那個(gè)接口復(fù)制到這里,去掉方法體即可。
controller層中添加一個(gè)接口
@RestController
public class ClientController {
@Autowired
private ServerFeign serverFeign;
@GetMapping("/index/{name}")
public String index(@PathVariable("name") String name) {
return serverFeign.index(name);
}
}
先將剛才創(chuàng)建愛(ài)你的Feign接口注入到spring容器,寫(xiě)一個(gè)接口去調(diào)用Feign中的方法,經(jīng)測(cè)試后可以看到調(diào)用這個(gè)接口,其實(shí)是通過(guò)Feign調(diào)用了另一個(gè)服務(wù)中的方法體。
啟動(dòng)類
類上要加EnableFeignClients、EnableEurekaClient注解
@SpringBootApplication
@EnableFeignClients
@EnableEurekaClient
public class FeignClientApp {
public static void main(String[] args) {
SpringApplication.run(FeignClientApp.class, args);
}
}
啟動(dòng)該程序,再次訪問(wèn)eureka服務(wù)端,可以看到兩個(gè)服務(wù)都被注冊(cè)到注冊(cè)中心了。

最后訪問(wèn)這個(gè)服務(wù)的接口,http://localhost:8102/index/test

這里顯示的值其實(shí)就是調(diào)用了另一個(gè)服務(wù)中的接口的返回值。
四、Hystrix熔斷器
在微服務(wù)場(chǎng)景中,通常會(huì)有很多層的服務(wù)調(diào)用。如果一個(gè)底層服務(wù)出現(xiàn)問(wèn)題,故障會(huì)被向上傳播給用戶。我們需要一種機(jī)制,當(dāng)?shù)讓臃?wù)不可用時(shí),可以阻斷故障的傳播。這就是熔斷器的作用。他是系統(tǒng)服務(wù)穩(wěn)定性的最后一重保障。
SpringCloud默認(rèn)已經(jīng)為Feign整合了Hystrix,只要Hystrix在項(xiàng)目的classpath中,F(xiàn)eigin 默認(rèn)就會(huì)用斷路器包裹所有的方法.
1.yml配置文件中增加
feign:
hystrix:
enabled: true
2.添加一個(gè)ServerFeignHystrix類,實(shí)現(xiàn)ServerFeign接口
@Component
public class ServerFeignHystrix implements ServerFeign {
@Override
public String index(String name) {
return name+"觸發(fā)熔斷機(jī)制!??!";
}
}
3.修改ServerFeign,在FeignClient注解中增加fallback參數(shù),值為ServerFeignHystrix.class
@FeignClient(name = "feign-server" ,fallback = ServerFeignHystrix.class)
public interface ServerFeign {
@RequestMapping(value = "/index")
public String index(@RequestParam(value = "name") String name) ;
}
4.依次重新啟動(dòng)eureka,F(xiàn)eignServer,FeignClient,訪問(wèn)這個(gè)服務(wù)的接口,http://localhost:8102/index/test

然后將FeignServer關(guān)閉,再次訪問(wèn)該接口,因?yàn)樵L問(wèn)服務(wù)端出錯(cuò),

五、Hystrix Dashboard儀表盤(pán)
Hystrix-dashboard是一款針對(duì)Hystrix進(jìn)行實(shí)時(shí)監(jiān)控的工具,通過(guò)Hystrix Dashboard可以直觀地看到各Hystrix Command的請(qǐng)求響應(yīng)時(shí)間,請(qǐng)求成功率等數(shù)據(jù)。
1.在feign-client項(xiàng)目,即Hystrix所在項(xiàng)目添加依賴:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.在yml中添加配置
management:
endpoints:
web:
exposure:
include: hystrix.stream
base-path: /
3.在啟動(dòng)類中添加注解
@SpringBootApplication
@EnableFeignClients
@EnableEurekaClient
@EnableHystrixDashboard
@EnableCircuitBreaker
public class FeignClientApp {
public static void main(String[] args) {
SpringApplication.run(FeignClientApp.class, args);
}
}
4.訪問(wèn) localhost:8102/hystrix 打開(kāi)熔斷器監(jiān)控首頁(yè)

在第一個(gè)空格內(nèi)填寫(xiě) http://localhost:8102/hystrix.stream。
下方左側(cè)是延遲時(shí)間,右側(cè)是頁(yè)面標(biāo)題。
點(diǎn)擊最下方的Monitor Stream 按鈕跳轉(zhuǎn)到監(jiān)控頁(yè)面。剛進(jìn)入該頁(yè)面會(huì)出現(xiàn)存在兩個(gè)loading字樣的頁(yè)面,調(diào)用接口再刷新該頁(yè)面即可。

關(guān)于這些信息中說(shuō)明可以用網(wǎng)上找到的一張來(lái)加以說(shuō)明。

本文GitHub源碼:https://github.com/lixianguo5097/springcloud/tree/master/springcloud-feign
CSDN:https://blog.csdn.net/qq_27682773
簡(jiǎn)書(shū):http://www.itdecent.cn/u/e99381e6886e
博客園:https://www.cnblogs.com/lixianguo