在微服务架构中为例保证程序的可用性,防止程序出错导致网络阻塞,出现了断路器模型。断路器的状况反应了一个程序的可用性和健壮性,它是一个重要指标。Hystrix Dashboard是作为断路器状态的一个组件,提供了数据监控和友好的图形化界面。
我只想说一句 因为版本的原因真是各种坑 但是还是爱你的
在之前的feign-server服务基础上改造
由于是spring boot 2.1.X版本问题 所以
<!--监控检查依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--hystrix监控面板 相关依赖--> <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-javanica</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-hystrix-dashboard</artifactId> </dependency>
代替
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency>
package com.li.feignserver;
import com.li.feignserver.server.EurekaCclientService;import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.ServletRegistrationBean;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.netflix.hystrix.EnableHystrix;import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;import org.springframework.cloud.openfeign.EnableFeignClients;import org.springframework.context.annotation.Bean;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@EnableHystrix // 开启Hystrix @EnableHystrixDashboard // 开启Hystrix面板@RestController@EnableFeignClients // 开启Feign功能@EnableEurekaClient@SpringBootApplicationpublic class FeignServerApplication {
public static void main(String[] args) { SpringApplication.run(FeignServerApplication.class, args); }
@Resource private EurekaCclientService eurekaCclientService;
@GetMapping("/hello") public String hello(@RequestParam(value = "name") String name) { return eurekaCclientService.hello(name); }}
# 端点放开management.endpoints.web.exposure.include=*
然后依次启动服务 eureka-server-> eureka-client -> feign-server
eureka-server : 8761端口
eureka-client: 8763端口
feign-server : 8766端口
打开浏览器输入 http://localhost:8761/
服务已经注册进来了
然后访问仪表盘服务地址:http://localhost:8766/hystrix
图中会有一些提示:
Cluster via Turbine (default cluster): http://turbine-hostname:port/turbine.stream Cluster via Turbine (custom cluster): http://turbine-hostname:port/turbine.stream?cluster=[clusterName] Single Hystrix App: http://hystrix-app:port/actuator/hystrix.stream 大概意思就是如果查看默认集群使用第一个url,查看指定集群使用第二个url,单个应用的监控使用最后一个,我们暂时只演示单个应用的所以在输入框中输入: http://localhost:8766/actuator/hystrix.stream ,输入之后点击 monitor,进入页面
然后使用feign调用服务 浏览器输入 http://localhost:8766/hello?name=lhd
指标有变化
(1)Deplay 该参数用来控制服务器上轮询监控信息的延迟时间,默认是2000毫秒,可以通过配置该属性来降低客户端的网络和cpu消耗。 (2)Title该参数对应了头部标题Hystrix Stream之后的内容,默认会使用哦具体监控实例的URL,可疑通过配置该信息来展示更合适的标题。
源码下载: https://github.com/LiHaodong888/SpringCloudLearn