前言
本篇文章主要介紹的是Feign實(shí)現(xiàn)服務(wù)間調(diào)用,集成Hystrix熔斷器硫狞、Hystrix-Dashboard儀表盤
GitHub源碼鏈接位于文章底部。
Feign 簡介
Feign是一個(gè)http請(qǐng)求調(diào)用的輕量級(jí)框架烛卧,可以以Java接口注解的方式調(diào)用Http請(qǐng)求,而不用像Java中通過封裝HTTP請(qǐng)求報(bào)文的方式直接調(diào)用妓局。Feign通過處理注解总放,將請(qǐng)求模板化,當(dāng)實(shí)際調(diào)用的時(shí)候好爬,傳入?yún)?shù)局雄,根據(jù)參數(shù)再應(yīng)用到請(qǐng)求上,進(jìn)而轉(zhuǎn)化成真正的請(qǐng)求存炮。
在實(shí)際開發(fā)中炬搭,所有的服務(wù)都要注冊(cè)到注冊(cè)中心,然后一個(gè)服務(wù)可以在eureka注冊(cè)中心通過feign調(diào)用另一個(gè)服務(wù)穆桂。因此我們需要先建立eureka注冊(cè)中心宫盔,再建立一些普通服務(wù)作為eureka客戶端,作為被調(diào)用的服務(wù)享完,其實(shí)只是一個(gè)普通的微服務(wù)項(xiàng)目灼芭,而需要調(diào)用別的服務(wù)的,則需要加入feign般又,也就是feign客戶端彼绷,這里是沒有真正定義的feign服務(wù)端的。
一茴迁、eureka注冊(cè)中心
先創(chuàng)建springcloud-feign父工程寄悯。
和上篇文章中寫的過程一樣,這里建立一個(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è)中心所以無需注冊(cè)
register-with-eureka: false
#是否從Eureka中檢索注冊(cè)信息,本身就是注冊(cè)中心所以無需檢索
fetch-registry: false
server:
# 測試時(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/
# 心跳檢測檢測與續(xù)約時(shí)間
# 測試時(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í)間上限,單位為秒邦危,超過則剔除(客戶端告訴服務(wù)端按照此規(guī)則等待自己)
lease-expiration-duration-in-seconds: 2
feign是通過配置文件中的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ù)端洋侨,訪問 http://localhost:8100/ ,可以看到該服務(wù)已經(jīng)被注冊(cè)到注冊(cè)中心了倦蚪。
再啟動(dòng)該服務(wù)希坚,調(diào)用接口 http://localhost:8101/index?name=測試 。
三陵且、 創(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)建愛你的Feign接口注入到spring容器,寫一個(gè)接口去調(diào)用Feign中的方法殿如,經(jīng)測試后可以看到調(diào)用這個(gè)接口贡珊,其實(shí)是通過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)該程序门岔,再次訪問eureka服務(wù)端,可以看到兩個(gè)服務(wù)都被注冊(cè)到注冊(cè)中心了谨胞。
最后訪問這個(gè)服務(wù)的接口固歪,http://localhost:8102/index/test
這里顯示的值其實(shí)就是調(diào)用了另一個(gè)服務(wù)中的接口的返回值。
四胯努、Hystrix熔斷器
在微服務(wù)場景中牢裳,通常會(huì)有很多層的服務(wù)調(diào)用。如果一個(gè)底層服務(wù)出現(xià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ī)制!I纬但指!";
}
}
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棋凳,訪問這個(gè)服務(wù)的接口,http://localhost:8102/index/test
然后將FeignServer關(guān)閉连躏,再次訪問該接口剩岳,因?yàn)樵L問服務(wù)端出錯(cuò),
五入热、Hystrix Dashboard儀表盤
Hystrix-dashboard是一款針對(duì)Hystrix進(jìn)行實(shí)時(shí)監(jiān)控的工具拍棕,通過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.訪問 localhost:8102/hystrix 打開熔斷器監(jiān)控首頁
在第一個(gè)空格內(nèi)填寫 http://localhost:8102/hystrix.stream
。
下方左側(cè)是延遲時(shí)間郑气,右側(cè)是頁面標(biāo)題。
點(diǎn)擊最下方的Monitor Stream
按鈕跳轉(zhuǎn)到監(jiān)控頁面腰池。剛進(jìn)入該頁面會(huì)出現(xiàn)存在兩個(gè)loading字樣的頁面尾组,調(diào)用接口再刷新該頁面即可。
關(guān)于這些信息中說明可以用網(wǎng)上找到的一張來加以說明示弓。
本文GitHub源碼:https://github.com/lixianguo5097/springcloud/tree/master/springcloud-feign
CSDN:https://blog.csdn.net/qq_27682773
簡書:http://www.reibang.com/u/e99381e6886e
博客園:https://www.cnblogs.com/lixianguo