前言:
我们需要监控每一个微服务,但是如果每个微服务都集成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,继续用此工程。
参考:
Spring Cloud中Hystrix仪表盘与Turbine集群监控