spring-cloud

微服务监控健康检查之SpringBootAdmin的简单应用

2019-04-05  本文已影响286人  KICHUN

SpringBootAdmin简介

官方文档:What is Spring Boot Admin?

codecentric’s Spring Boot Admin is a community project to manage and monitor your Spring Boot ® applications. The applications register with our Spring Boot Admin Client (via HTTP) or are discovered using Spring Cloud ® (e.g. Eureka, Consul). The UI is just a Vue.js application on top of the Spring Boot Actuator endpoints.

谷歌翻译如下:

codecentric的Spring Boot Admin是一个社区项目,用于管理和监控SpringBoot®应用程序。
应用程序向我们的Spring Boot Admin Client注册(通过HTTP)或使用SpringCloud®(例如Eureka,Consul)发现。
UI只是Spring Boot Actuator端点上的Vue.js应用程序。


普通话版
SpringBootAdmin是一个用于管理和监控SpringBoot微服务的社区项目,可以使用客户端注册或者Eureka服务发现向服务端提供监控信息。
注意,服务端相当于提供UI界面,实际的监控信息由客户端Actuator提供


通过SpringBootAdmin,你可以通过华丽大气的界面访问到整个微服务需要的监控信息,例如服务健康检查信息、CPU、内存、操作系统信息等等

本文档通过Eureka服务发现的配置方式,向大家展示SpringBootAdmin的简单使用
Talk is cheap, show me the code!

1. 父工程搭建

本示例工程父工程下共四个模块,分别为


使用了目前最新的版本,父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.kichun</groupId>
    <artifactId>cloud-demo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>eureka-server</module>
        <module>cloud-service1</module>
        <module>cloud-service2</module>
        <module>admin-server</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <!-- Spring Cloud 依赖管理 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.16.22</version>
            </dependency>
            <!-- SpringBootAdmin 依赖管理 -->
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-dependencies</artifactId>
                <version>2.1.3</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>

2. Eureka注册中心工程搭建

这个不细说,不懂出门右转,百度大把配置
模块pom文件, 注意最新版本依赖了hystrix,一定要加,否则跑不起来

<?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">
    <parent>
        <artifactId>cloud-demo</artifactId>
        <groupId>com.kichun</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>eureka-server</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
</project>

程序入口

@Slf4j
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

YML配置文件

spring:
  application:
    name: eureka-server
  main:
    allow-bean-definition-overriding: true
    
server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

3. admin-server 服务端搭建

模块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">
    <parent>
        <artifactId>cloud-demo</artifactId>
        <groupId>com.kichun</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>admin-server</artifactId>

    <dependencies>
        <!-- eureka客户端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- web服务 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 服务端作为一个客户端,也需要加actuator依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!-- springbootadmin 启动器 -->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--发邮件用的-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
    </dependencies>
</project>

应用入口, 关键注解@EnableAdminServer

@Slf4j
@SpringBootApplication
@EnableAdminServer
@EnableEurekaClient
public class AdminServerApplication {

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

YML配置文件,均有做说明

spring:
  application:
    name: admin-server
  boot:
#    此处为SpringBootAdmin配置
    admin:
      ui:
#        web界面的title
        title: 服务健康检查
#        邮件提醒
      notify:
        mail:
#          发给谁
          to: kichunwang@foxmail.com
#          谁发的
          from: 184375760@qq.com
#  spring mail邮件配置
  mail:
#    smtp主机
    host: smtp.qq.com
#   发件人账号
    username: 184375760@qq.com
#   发件人密码,如qq邮箱等使用的是授权码
    password: bogffhsssi这个请大佬改成自己密码ihb
server:
  port: 8602

eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

4. 客户端搭建

模块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">
    <parent>
        <artifactId>cloud-demo</artifactId>
        <groupId>com.kichun</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-service1</artifactId>
    <dependencies>
        <!--eureka 客户端-->
        <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>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
</project>

应用入口

@Slf4j
@SpringBootApplication
@EnableEurekaClient
public class Service1Application {
    public static void main(String[] args) {
        SpringApplication.run(Service1Application.class, args);
    }
}

关键为YML配置

spring:
  application:
    name: service1
  main:
    allow-bean-definition-overriding: true
server:
  port: 8601

eureka:
  instance:
    hostname: localhost
    leaseRenewalIntervalInSeconds: 10
#   健康检查路径
    health-check-url-path: /actuator/health
  client:
    registryFetchIntervalSeconds: 5
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
      
# 此处为SpringAdminClient配置,需要暴露Actuator节点
management:
#  暴露所有的endpoint 注意生产环境安全问题
  endpoints:
    web:
      exposure:
        include: "*"
# 展示endpoint细节信息
  endpoint:
    health:
      show-details: ALWAYS

5 . 效果展示

按启动Eureka,SpringBootAdmin服务端、各服务客户端顺序启动,访问服务端端口
如图,共两个应用启动,一个为SpringBootAdmin服务端本身共一个实例,一个Service1服务两个实例


image.png

细节信息,包括磁盘、cpu、内存使用情况


image.png

关闭一个服务时将会收到邮件提醒,邮件提醒模板可以自定义,具体请参考官方文档


image.png

服务上线时会有更具体的信息


image.png

6. 总结

SpringBootAdmin提供一个界面优美的监控界面,展示微服务运行的基本信息,通过提供邮件等提醒服务可以方便提醒管理者微服务运行情况,能与SpringCloud相关组件完美兼容,侵入性低,适合对监控要求不高的应用场景

官方文档更齐全,欢迎大佬评论打赏指出问题

上一篇下一篇

猜你喜欢

热点阅读