springcloud 入门(5) Hystrix Dashboard &Turbine

81 阅读3分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第10天,点击查看详情

上篇介绍了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地址: github.com/ArronSun/mi…

参考书籍:

《重新定义springcloud实战》