springcloud 入门(5) 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实战》

能力一般,水平有限,如有错误,请多指出。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

索码理

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值