我们需要监控每一个微服务,但是如果每个微服务都集成hystrix Dashboard,这样设计并不好,所以要把hystrix Dashboard单独分离成一个微服务。
(前几次工程的代码都是基于以前的工程,这次工程完全重写)
我把springcloud的maven直接放到最外面了,子module直接引用父类的pom
pom.xml
4.0.0
com.xhx.springcloud
springcloud8-hystrix-turbine
0.0.1-SNAPSHOT
eureka-server
hystrix-dashboard
service
client
pom
springcloud8-hystrix-turbine
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.2.RELEASE
UTF-8
UTF-8
1.8
Finchley.RC2
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
pom.xml
springcloud8-hystrix-turbine
com.xhx.springcloud
0.0.1-SNAPSHOT
4.0.0
eureka-server
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
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工程就创建完毕了
pom.xml
springcloud8-hystrix-turbine
com.xhx.springcloud
0.0.1-SNAPSHOT
4.0.0
service
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
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);
}
}
pom.xml
springcloud8-hystrix-turbine
com.xhx.springcloud
0.0.1-SNAPSHOT
4.0.0
client
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
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
springcloud8-hystrix-turbine
com.xhx.springcloud
0.0.1-SNAPSHOT
4.0.0
hystrix-dashboard
org.springframework.cloud
spring-cloud-starter-netflix-hystrix-dashboard
启动类:
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集群监控