基于Hystrix Dashboard和Turbine构建服务监控(SpringBoot 2.0.x)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhaoenweiex/article/details/81517612

前言

最近这段时间想把集群的监控搭起来,总是不成功,最后发现是SpringBoot版本不兼容导致的。本文将介绍基于SpringBoot 2.0.x的 Dashboard,Turbine构建服务监控中遇到的问题和经验,并以对Zuul的监控为例搭建简单的的服务监控。

服务搭建

版本介绍

SpringBoot—————->2.0.4.RELEASE
SpringCloud—————>Finchley SR1
对了,下面是版本对应表
这里写图片描述

整体思路

部署一个单独的监控服务,利用这个服务来监控Zuul服务,从而返回Zuul代理的服务的状态,实现后台接口监控的目的。全部的服务都注册到Eureka上。
在本文中,为了方便我们将turbine和Dashboard集成到一个服务上了,实际中也可以分开部署。本文中被监控的服务实际上只有Zuul一个,但实际上多个服务通过Zuul代理,所以监控面板上有很多其他的服务。采用Eureka作为注册中心。

监控应用的构建

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>

    <groupId>com.zw.se2.pub</groupId>
    <artifactId>dashboard</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>dashboard</name>
    <description>HystrixDashboard</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix-stream</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</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-turbine</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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


</project>

配置

bootstrap.yaml,注意格式,不知道为啥,贴过来格式就不对了

eureka:
  client:
    service-url:
        defaultZone: http://eureka:8797/eureka
spring:  
  application:
    name: dashboard
turbine:
  app-config: gateway-server
  combine-host-port: true
  cluster-name-expression: new String("default")

入口类

package com.zw.se2.pub.dashboard;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;

/**
 * @author zew
 */
@EnableHystrixDashboard
@SpringBootApplication
@EnableEurekaClient
@EnableTurbine
public class DashboardApplication {

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

Zuul的构建

POM

配置

application.properties

server.port=8988
management.endpoints.web.exposure.include=hystrix.stream
eureka.instance.prefer-ip-address=true

bootstrap.yaml

eureka:
  client:
    service-url:
        defaultZone: http://eureka:8797/eureka

入口类

package com.zw.se2.hy.uav.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

/**
 * @author zew
 */
@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
@EnableZuulProxy
public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}

Eureka的构建

注册中心的构建我就不再赘述了,不太清楚的可以翻看我以前的文章有过介绍。

需要注意的问题

1.SpringBoot的1.5.x和2.0.x的服务监控接口是不同的
SpringBoot 1.5.x的监控接口是ip:port/hystrix.stream
SpringBoot 2.0.x的监控是ip:port/actuator/hystrix.stream
因此被监控的服务和Turbine的版本要统一,不一致的版本是无法实现监控信息聚合的也就无法实现监控
2.若监控面板始终处于loading的状态,但服务都已经正常启动了,多半是需要访问以下要监控的服务,ps有时是由于前台的缓存导致始终处于loading状态,清一下缓存就好了
3.Zuul是自然集成Hystrix的因此不需要额外在引入Hystrix的依赖,
4.被监控的服务一定要通过feginClient调用或是服务接口有@HystrixCommand的注解才能通过通过hystrixDashboard进行服务监控

总结

服务监控是微服务体系中不可或缺的一环,没有了服务监控,微服务完全就是睁眼瞎,没有实际的应用价值,不过感谢开源社区,借助SpirngCould体系以及K8S等框架可以较为方便的搭建自己的简便监控服务。
下一步就是服务追踪了。

猜你喜欢

转载自blog.csdn.net/zhaoenweiex/article/details/81517612