SpringCloud Turbine

来源:互联网 发布:淘宝月销量 编辑:程序博客网 时间:2024/06/02 07:22
前提:SpringCloud解决方案下,存在一个EurekaServer项目,外加两个子项目,并在一个项目中使用hystrix方法调用另外一个项目中的接口

我这里有三个子项目分别为:eureka,gateway,user


创建子项目turbine,pom.xml中加入依赖:

<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-turbine</artifactId></dependency><dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId></dependency>

application.yml中加入配置:

turbine:  aggregator:    clusterConfig: default # 指定聚合哪些集群,多个使用","分割,默认为default  appConfig: user,gateway,blog  # 配置Eureka中的serviceId列表,表明监控哪些服务  clusterNameExpression: new String("default")

启动类:

@SpringBootApplication@EnableTurbine@EnableHystrixDashboardpublic class TurbineApplication {    public static void main(String[] args) {        SpringApplication.run(TurbineApplication.class, args);    }}

启动turbine项目,浏览器输入:http://localhost:9997/hystrix,这里9997是我配置文件的端口号


输入http://localhost:9997/turbine.stream,点击"Monitor Stream",进入监控页面,调用子项目中带有hystrix断路器的接口,就可以看到监控图了
我这里在gateway中使用RestTemplate调用了user中的hi接口,调用代码如下:

@AutowiredRestTemplate restTemplate;@HystrixCommand(fallbackMethod = "hystrixMsg")public Result hiUser() {    // return restTemplate.getForObject("http://user-manage/user/hi", Result.class);    // getForObject 无法序列化返回结果为制定的类型    Result result = restTemplate        .exchange("http://user/user/hi", HttpMethod.GET, null, new ParameterizedTypeReference<Result>() {})        .getBody();    return result;}public Result hystrixMsg() {    return new Result("该服务已断开连接!");}

在浏览器中不停调用gateway的hiUser接口,在user项目启动并能成功调用的时候,监控图如下:


断掉user项目,再次不停调用gatew的hiUser接口,监控图如下:


可以看到红色的百分比数字100%表示全部调用失败了,同时浏览器显示了断路函数中返回的信息


监控图中各项数字的含义,在网上找了张图:



阅读全文
0 0
原创粉丝点击