发表于: 2018-09-17 21:15:18

1 386


spring cloud 断路器监控器和聚合监控器

一、断路器监控器

这里先用到的监控组件是 Hystrix Dashboard 组件:

在微服务架构中为例保证程序的可用性,防止程序出错导致网络阻塞,出现了断路器模型。断路器的状况反应了一个程序的可用性和健壮性,它是一个重要指标。Hystrix Dashboard是作为断路器状态的一个组件,提供了数据监控和友好的图形化界面。

这里用到两个工程,一个eureka-server,另外一个是 eureka -client。

以之前的代码为基础,eureka-server这个工程代码不变。对eureka-client进行改造:

首先是 pom文件,改为以下依赖:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.example</groupId>
   <artifactId>eurecaclient</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>eurecaclient</name>
   <description>Demo project for Spring Boot</description>

   <parent>
       <groupId>com.example</groupId>
       <artifactId>springcloudf</artifactId>
       <version>1.0-SNAPSHOT</version>
       <relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
   </parent>


   <dependencies>
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-actuator</artifactId>
       </dependency>
       <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>


   <build>
       <plugins>
           <plugin>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-maven-plugin</artifactId>
           </plugin>
       </plugins>
   </build>


</project>

接着在启动类中加上开启断路器的注解:@EnableHystrix。同时,在程序中生命断路点HystrixCommand ,还有需要开启HystrixDashboard ,即也要开启@EnableHystrixDashboard注解。

package com.example.eurecaclient;

import com.netflix.hystrix.contrib.javanica.annotation.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.*;
import org.springframework.cloud.client.discovery.*;
import org.springframework.cloud.netflix.eureka.*;
import org.springframework.cloud.netflix.hystrix.*;
import org.springframework.cloud.netflix.hystrix.dashboard.*;
import org.springframework.web.bind.annotation.*;

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@RestController
@EnableHystrix
@EnableHystrixDashboard
@EnableCircuitBreaker
public class EurecaclientApplication {

/**
   * 访问地址 http://localhost:8762/actuator/hystrix.stream
   * @param args
   */

  public static void main(String[] args) {
SpringApplication.run(EurecaclientApplication.class, args);
  }

@Value("${server.port}")
String port;

  @RequestMapping("/hi")
@HystrixCommand(fallbackMethod = "hiError")
public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) {
return "hi " + name + " ,i am from port:" + port;
  }

public String hiError(String name) {
return "hi,"+name+",sorry,error!";
  }
}

与此同时,需要在配置上做如下修改:

server:
port: 8762

spring:
application:
name: service-hi

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
management:
endpoints:
web:
exposure:
include: "*"
     cors:
allowed-origins: "*"
       allowed-method s: "*"

不加上面的配置管理信息,后面会显示不出断路的信息。

运行程序: 依次开启eureka-server 和service-client。

打开http://localhost:8762/actuator/hystrix.stream

再打开 http://localhost:8762/hi?name=forezp

显示以下信息:

可以看到一些具体的数据:

在界面依次输入:http://localhost:8762/actuator/hystrix.stream 、2000 、miya ;点确定。

结果如下:


二、断路聚合监控:Hystrix command

单个的Hystrix Dashboard的数据并没有什么多大的价值,要想看这个系统的Hystrix Dashboard数据就需要用到Hystrix Turbine。Hystrix Turbine将每个服务Hystrix Dashboard数据进行了整合。Hystrix Turbine的使用非常简单,只需要引入相应的依赖和加上注解和配置就可以了。

(1)因为我们需要多个服务的Dashboard,所以在上面基础上增加一个 eureka-client 客户端,项目建立过程与之前建立eureka-client的过程一样,引入相同的依赖,取名为service-lucy。

 (2)建立services-tuturbine项目:

引入相关的依赖,POM文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <artifactId>service-turbine</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>service-turbine</name>
   <description>Demo project for Spring Boot</description>

   <parent>
       <groupId>com.example</groupId>
       <artifactId>springcloudf</artifactId>
       <version>1.0-SNAPSHOT</version>
       <relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
   </parent>

   <dependencies>
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-actuator</artifactId>
       </dependency>
       <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>
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
       </dependency>

   </dependencies>

   <build>
       <plugins>
           <plugin>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-maven-plugin</artifactId>
           </plugin>
       </plugins>
   </build>


</project>

在启动类上加入加上注解@EnableTurbine,开启turbine,@EnableTurbine注解包含了@EnableDiscoveryClient注解,即开启了注册服务。

启动类如下:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.*;
import org.springframework.cloud.client.discovery.*;
import org.springframework.cloud.netflix.eureka.*;
import org.springframework.cloud.netflix.hystrix.*;
import org.springframework.cloud.netflix.hystrix.dashboard.*;
import org.springframework.cloud.netflix.turbine.*;
import org.springframework.web.bind.annotation.*;

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@RestController
@EnableHystrix
@EnableHystrixDashboard
@EnableCircuitBreaker
@EnableTurbine
public class ServiceTurbineApplication {

/**
   * http://localhost:8764/turbine.stream
   */
  public static void main(String[] args) {
SpringApplication.run(ServiceTurbineApplication.class, args);
  }
}

更改配置文件如下:

server:
port: 8764

spring:
application:
name: service-turbine

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
management:
endpoints:
web:
exposure:
include: "*"
     cors:
allowed-origins: "*"
       allowed-methods: "*"

turbine:
app-config: service-hi,service-lucy
aggregator:
clusterConfig: default
clusterNameExpression: new String("default")
combine-host: true
 instanceUrlSuffix:
default: actuator/hystrix.stream

(3)演示tuturbine

依次开启eureka-server、service-client、service-lucy、service-turbine工程。

输入:http://localhost:8764/turbine.stream,界面如下:

依次请求:

http://localhost:8762/hi?name=chedou

http://localhost:8763/hi?name=chedou

打开:http://localhost:8763/hystrix,输入监控流http://localhost:8764/turbine.stream



总结:

1.学到的东西:

    1.1.学习了Springcloud  监控器


      

2.明天计划的事情:

    2.1.继续准备复盘申请。

   2.2.看复盘需求。



3.遇到的问题:

 

4.收获:

       收获了上述知识点,坚持!




返回列表 返回列表
评论

    分享到