前言:
我們需要監控每一個微服務,但是如果每個微服務都內建hystrix Dashboard,這樣設計并不好,是以要把hystrix Dashboard單獨分離成一個微服務。
(前幾次工程的代碼都是基于以前的工程,這次工程完全重寫)
代碼:
首先看一下工程的目錄
我把springcloud的maven直接放到最外面了,子module直接引用父類的pom
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xhx.springcloud</groupId>
<artifactId>springcloud8-hystrix-turbine</artifactId>
<version>0.0.1-SNAPSHOT</version>
<modules>
<module>eureka-server</module>
<module>hystrix-dashboard</module>
<module>service</module>
<module>client</module>
</modules>
<packaging>pom</packaging>
<name>springcloud8-hystrix-turbine</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RC2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
子module eureka
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud8-hystrix-turbine</artifactId>
<groupId>com.xhx.springcloud</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
spring配置檔案:
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
fetch-registry: false
register-with-eureka: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
啟動類:
package com.xhx.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* xuhaixing
* 2018/6/7 14:22
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaserverApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaserverApplication.class, args);
}
}
至此,eureka工程就建立完畢了
子module 微服務的服務端
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud8-hystrix-turbine</artifactId>
<groupId>com.xhx.springcloud</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>service</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
spring配置檔案:
server:
port: 8083
spring:
application:
name: application-service
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
controller類,一會用用戶端直接調用它
package com.xhx.springcloud.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* xuhaixing
* 2018/6/7 15:04
*/
@RestController
@RequestMapping(value = "hello")
public class HelloController {
@RequestMapping(value = "getWord")
public String getWord(@RequestParam(value = "name") String name){
return name;
}
}
啟動類:
package com.xhx.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
/**
* xuhaixing
* 2018/6/7 14:56
*/
@SpringBootApplication
@EnableEurekaClient
public class ServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class,args);
}
}
子module 微服務的用戶端:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud8-hystrix-turbine</artifactId>
<groupId>com.xhx.springcloud</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>client</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
openfeign: 新的feign maven依賴包,老的不推薦使用了
actuator: 程式監控與管理
hystrix: 熔斷
spring配置檔案:
server:
port: 8085
spring:
application:
name: application-client
management:
endpoints:
web:
exposure:
include: "*"
feign:
hystrix:
enabled: true
在springboot2.0以上,management...必須有,否則不會暴露actuator端點
feign.hystrix.enabled為開啟feign熔斷
feign接口類:
package com.xhx.springcloud.api;
import com.xhx.springcloud.hystrix.HelloHystrix;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* xuhaixing
* 2018/6/7 15:28
*/
@FeignClient(value = "APPLICATION-SERVICE",path = "hello",fallback =HelloHystrix.class)
public interface HelloApi {
@RequestMapping(value = "getWord")
String getWord(@RequestParam(value = "name") String name);
}
hystrix類:
package com.xhx.springcloud.hystrix;
import com.xhx.springcloud.api.HelloApi;
import org.springframework.stereotype.Component;
/**
* xuhaixing
* 2018/6/7 15:37
*/
@Component
public class HelloHystrix implements HelloApi {
@Override
public String getWord(String name) {
return "調用第三方api錯誤";
}
}
controller類:
package com.xhx.springcloud.controller;
import com.netflix.discovery.converters.Auto;
import com.xhx.springcloud.api.HelloApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* xuhaixing
* 2018/6/7 15:44
*/
@RestController
@RequestMapping(value = "userInfo")
public class UserController {
@Autowired
private HelloApi helloApi;
@RequestMapping(value = "getName",method = RequestMethod.POST)
public String getName(@RequestParam(value = "name") String name){
return helloApi.getWord(name);
}
}
啟動類:
package com.xhx.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* xuhaixing
* 2018/6/7 15:13
*/
@SpringBootApplication
@EnableCircuitBreaker
@EnableFeignClients
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class,args);
}
}
@EnableCircuitBreaker 為啟用熔斷,給hystrix dashboard用
@EnableFeignClients 為啟用feign用戶端
啟動這三個工程:
下面建立hystrix dashboard
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud8-hystrix-turbine</artifactId>
<groupId>com.xhx.springcloud</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>hystrix-dashboard</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
</dependencies>
</project>
啟動類:
package com.xhx.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
/**
* xuhaixing
* 2018/6/7 14:22
*/
@SpringBootApplication
@EnableHystrixDashboard
public class HystrixdashboardApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixdashboardApplication.class, args);
}
}
spring配置檔案:
server:
port: 8081
spring:
application:
name: hystrix-dashboard
啟動,然後通路:http://localhost:8081/hystrix
然後點選monitor Stream
首先要通路一次接口,否則 直接通路http://localhost:8085/actuator/hystrix.stream 會一直ping ping ping...
進去後是這樣的:
在監控的界面有兩個重要的圖形資訊:一個實心圓和一條曲線。
▪實心圓:1、通過顔色的變化代表了執行個體的健康程度,健康程度從綠色、黃色、橙色、紅色遞減。2、通過大小表示請求流量發生變化,流量越大該實心圓就越大。是以可以在大量的執行個體中快速發現故障執行個體和高壓執行個體。
▪曲線:用來記錄2分鐘内流浪的相對變化,可以通過它來觀察流量的上升和下降趨勢。
下面這張圖是引用的别人的:
将在下節引用turbine,繼續用此工程。
我的github位址
參考:
Hystrix-Dashboard儀表盤
Spring Cloud中Hystrix儀表盤與Turbine叢集監控