文章目錄
- Hystrix Dashboard
- Turbine
上篇介紹了hystrix的簡單使用
springcloud多子產品項目一步一步搭建(4)Hystrix
這篇學一學對Hystrix的監控。
Hystrix Dashboard 儀表盤是根據系統一段時間内發生的請求情況來展示的可視化面闆,這些資訊是每個HystrixCommand執行過程中的資訊,這些資訊是一個名額集合和具體的系統運作情況。
Hystrix Dashboard
Hystrix Dashboard隻能對某個單一的服務進行監控,它的主要功能某一項微服務進行監控。
先建立一個hystrix-dashboard springboot項目,依賴如下:
<dependencies>
<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>
</dependencies>
啟動類添加@EnableHystrixDashboard注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@EnableHystrixDashboard
@SpringBootApplication
public class HystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardApplication.class, args);
}
}
application.properties配置檔案
server.port=10001
# 執行個體名稱 名字可以自己定
spring.application.name=hystrix-dashboard
啟動HystrixDashboardApplication,浏覽器通路http://localhost:10001/hystrix,出現如下界面則建立成功
紅框中寫的三個位址對應着三種監控方式:
預設叢集監控: http://turbine-hostname:port/turbine.stream
指定叢集監控: http://turbine-hostname:port/turbine.stream?cluster=[clusterName]
單個應用監控: http://hystrix-app:port/actuator/hystrix.stream
接下來就是對服務進行監控:
1、在項目eureka-provider端添加actuator依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2、修改eureka-provider配置檔案,對外暴露監控位址
server.port=9001
# 生産者應用名稱 -
spring.application.name=PROVIDER
# 生産者執行個體名,同一個spring.application.name 名稱唯一
eureka.instance.instance-id=provider
eureka.client.register-with-eureka=true
# 和eureka伺服器通訊的URL
eureka.client.service-url.defaultZone=http://localhost:8001/eureka
# 設定心跳的時間間隔(預設是30秒)
eureka.instance.lease-renewal-interval-in-seconds=5
# eureka server 最後一次收到心跳時等待的時間,逾時将會移除client(預設是90秒)
eureka.instance.lease-expiration-duration-in-seconds=3
# actuator 監控
management.endpoints.web.exposure.include=*
啟動eureka-provider,在hystix dashboard監控頁面輸入http://localhost:9001/actuator/hystrix.stream
點選Monitor Stream出現如下頁面表示監控成功,如果出現loading,調用一下eureka-provider帶有HystrixCommand的接口即可
這樣Hystrix dashboard對某個服務的監控就算完成了。
Turbine
Hystrix dashboard本身依賴隻能對單個服務進行監控,要想對叢集進行監控就要使用Turbine。
建立hystrix-dashbord-turbine springboot項目,依賴如下:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
</dependencies>
啟動類加上@EnableTurbine注解:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
@EnableTurbine
@SpringBootApplication
public class HystrixDashbordTurbineApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashbordTurbineApplication.class, args);
}
}
配置檔案application.properties
server.port=11001
spring.application.name=hystrix-turbine
# 執行個體名,同一個spring.application.name 名稱唯一
eureka.instance.instance-id=hystrix-turbine
# 此用戶端是否應該從eureka server 擷取eureka注冊資訊
eureka.client.register-with-eureka=false
# 和eureka伺服器通訊的URL
eureka.client.service-url.defaultZone=http://localhost:8001/eureka
turbine.app-config=PROVIDERS,USER-PROVIDERS
turbine.cluster-name-expression=new String("default")
turbine.app-config其實是從配置在eureka中的服務進行監控的。
再建立一個springboot項目 user-provider,依賴之前的eureka-provider相同
配置檔案application.properties
server.port=9101
# 生産者應用名稱 -
spring.application.name=USER-PROVIDERS
# 生産者執行個體名,同一個spring.application.name 名稱唯一
eureka.instance.instance-id=userProvider
eureka.client.register-with-eureka=true
# 和eureka伺服器通訊的URL
eureka.client.service-url.defaultZone=http://localhost:8001/eureka
# 設定心跳的時間間隔(預設是30秒)
eureka.instance.lease-renewal-interval-in-seconds=5
# eureka server 最後一次收到心跳時等待的時間,逾時将會移除client(預設是90秒)
eureka.instance.lease-expiration-duration-in-seconds=3
# actuator 監控
management.endpoints.web.exposure.include=*
分别建立一個類UserService和UserController
UserServiceImpl.java
@Service
public class UserServiceImpl implements UserService {
@Override
public String getUsername(String username) throws Exception {
if ("username".equals(username)) {
throw new Exception();
}
return "username="+username;
}
}
UserController.java
@RequestMapping("/user")
@RestController
public class UserController {
@Autowired
private UserService userService;
@HystrixCommand
@GetMapping("/getUserName/{username}")
public String getUserName(@PathVariable String username) throws Exception {
return userService.getUsername(username);
}
}
啟動user-provider 和 eureka-provider以及turbine項目,在Hystrix dashboard 監控位址欄輸入http://localhost:11001/turbine.stream
浏覽器位址欄通路http://localhost:9001/provider/getName/yy和http://localhost:9101/user/getUserName/username=fsd
可以看到turbine對服務provider的ProviderController和userProvider的UserController的監控
這樣對服務的叢集監控就完成了。
下篇将會介紹springcloud zuul配置中心的使用
GitHub位址:
https://github.com/ArronSun/micro-services-practice.git
參考書籍:
《重新定義springcloud實戰》