快使用spring-boot-admin来监控spring-boot项目吧

/ Spring Boot / 2 条评论 / 95浏览

spring官方的spring-boot-actuator提供了对单个spring-boot的监控,暴露了rest api。但是对于运维不太友好,毕竟图形化监控界面更加合理。spring-boot-admin则基于spring-boot-actuator提供了一个ui,更加易用。

预览

下面是spring-boot-admin提供的ui界面预览。

首页

index

详细

detail

spring-boot-admin版本说明

随着spring-boot升级到2.x,spring-boot-admin也升级到了2.x。

暂时还是使用成熟的1.5.x版本。

监控spring-boot项目

我们需要一个admin-server服务,其他spring-boot项目相当于是一个client,注册到server。

admin-server

依赖

<dependencies>
    <dependency>
        <groupId>de.codecentric</groupId>
        <artifactId>spring-boot-admin-server</artifactId>
        <version>${admin.version}</version>
    </dependency>
    <dependency>
        <groupId>de.codecentric</groupId>
        <artifactId>spring-boot-admin-server-ui</artifactId>
        <version>${admin.version}</version>
    </dependency>
</dependencies>

配置

server:
  # only export server port
  port: 8081

启动类

@EnableAdminServer
@SpringBootApplication
public class AdminServerApplication {

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

}

admin-client

依赖

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
    <version>${admin.version}</version>
</dependency>

配置

server:
  # export client port
  port: 8080

spring:
  boot:
    admin:
      # admin server url
      url: http://localhost:8081
management:
  security:
    # enable security
    enabled: false

启动类

@SpringBootApplication
public class AdminClientApplication {

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

}

启动项目

打开浏览器: http://localhost:8081

监控spring-cloud项目

我们利用eureka作为注册服务,其他服务作为eureka client。

cloud-discovery

依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
    <version>${spring.cloud.version}</version>
</dependency>

配置

server:
  port: 8761

spring:
  application:
    name: cloud-discovery

eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
      defaultZone: http://localhost:${server.port}/eureka/

启动类

@EnableEurekaServer
@SpringBootApplication
public class DiscoveryApplication {

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

}

cloud-admin-server

其实也是一个eureka client

依赖

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-server</artifactId>
    <version>${admin.version}</version>
</dependency>
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-server-ui</artifactId>
    <version>${admin.version}</version>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
    <version>${spring.cloud.version}</version>
</dependency>

配置

server:
  port: 8081

spring:
  application:
    name: cloud-admin-server
eureka:
  instance:
    leaseRenewalIntervalInSeconds: 10
  client:
    registryFetchIntervalSeconds: 5
    serviceUrl:
      defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/

management:
  security:
    enabled: false

启动类

@EnableAdminServer
@SpringBootApplication
public class AdminServerApplication {

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

}

cloud-admin-client

其实也是一个eureka client

依赖

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-client</artifactId>
    <version>${admin.version}</version>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
    <version>${spring.cloud.version}</version>
</dependency>

配置

server:
  port: 8080

spring:
  application:
    name: cloud-admin-client
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
management:
  security:
    enabled: false

启动类

@SpringBootApplication
public class AdminClientApplication {

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

}

启动

打开浏览器: http://localhost:8081

预警

spring-boot-admin提供应用预警通知。

邮件预警通知

默认当admin clients下线通知。

我们需要对admin server做下改造。

依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
    <version>${spring.boot.version}</version>
</dependency>

配置

spring:
  boot:
    admin:
      notify:
        mail:
          from: ${spring.mail.username}
          to: 接收者的邮箱账号
  mail:
    host: smtp.qq.com
    # 登录账号
    username: qq账号
    # 密码(开启IMAP/SMTP服务颁发的授权码)
    password: qq授权码
    properties:
      mail.debug: false
      mail.smtp.auth: true
      mail.smtp.port: 465
      # 开启ssl加密 否则项目启动时报530error
      mail.smtp.ssl.enable: true
      mail.smtp.ssl.socketFactory: sf
  1. ymk

    很久没更新了啊

    回复
    1. @ymk

      对啊,很久没整理了

      回复