Spring Boot Admin 使用

Spring Cloud Hoxton.SR4 Spring Boot 2.3.0.RELEASE

1. 简介

Spring Boot Admin 可以对 SpringBoot 应用的各项指标进行监控,可以作为微服务架构中的监控中心来使用。通过 Actuator 来暴露应用运行过程中的各项指标,Spring Boot Admin通过这些指标来监控SpringBoot应用,然后通过图形化界面呈现出来。

Spring Boot Admin除了监控单体应用,还可以结合Spring Cloud的注册中心来监控微服务应用。

2. 使用

先用IDEA创建一个Spring Boot的项目,可以随意引用一个Spring Cloud的组件,之后也会删掉。

创建完,删掉除了pom.xml以外的其他文件,再修改pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
    </parent>
    <groupId>com.shpun</groupId>
    <artifactId>spring-cloud-admin-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud-admin-test</name>
    <description>spring cloud admin test</description>
    <!--修改打包方式为pom-->
    <packaging>pom</packaging>

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

    <modules>
        <!--后续添加子模块用-->
    </modules>

    <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>
</project>

2.1 单体

2.1.1 admin-server

创建子模块 admin-server

修改pom继承

<parent>
    <groupId>com.shpun</groupId>
    <artifactId>spring-cloud-admin-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

再添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>2.3.0</version>
</dependency>

修改application.yml

server:
  port: 8200

spring:
  application:
    name: admin-server

在启动类上添加@EnableAdminServer注解来启用Admin Server功能。

2.1.2 admin-client

创建子模块 admin-client

修改pom继承

<parent>
    <groupId>com.shpun</groupId>
    <artifactId>spring-cloud-admin-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

再添加依赖

<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>
    <version>2.3.0</version>
</dependency>

修改application.yml

server:
  port: 8300

spring:
  application:
    name: admin-client
  boot:
    admin:
      client:
        #  admin-server地址
        url: http://localhost:8200

management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always

# 添加日志文件,开启admin的日志监控
logging:
  file:
    name: admin-client.log

测试

启动 admin-server 和 admin-client。

应用
查看应该监控信息,还可以查看环境变量,应用配置,日志文件等信息。

细节

2.2 结合注册中心

Spring Boot Admin 结合 Spring Cloud 注册中心使用后,Admin Server 会自动从注册中心获取服务列表,然后再获取监控信息。这里注册中心使用Eureka。

2.2.1 eureka-server

创建子模块eureka-server

修改pom继承

<parent>
    <groupId>com.shpun</groupId>
    <artifactId>spring-cloud-admin-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

再添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

修改application.yml

server:
  port: 8100

spring:
  application:
    name: eureka-server

eureka:
  instance:
    hostname: localhost
  client:
    # 是否从注册中心获取服务(注册中心不需要开启)
    register-with-eureka: false
    # 是否将服务注册到注册中心(注册中心不需要开启)
    fetch-registry: false

在启动类上添加@EnableEurekaServer注解来启用Euerka注册中心功能。

2.2.2 admin-server

添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

修改application.yml


server:
  port: 8200

spring:
  application:
    name: admin-server

eureka:
  instance:
    # 是否优先使用ip来作为主机名
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://localhost:8100/eureka

在启动类上添加@EnableDiscoveryClient注解表明是一个服务发现的客户端。

2.2.3 admin-client

添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

修改application.yml

server:
  port: 8300

spring:
  application:
    name: admin-client

management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always

eureka:
  instance:
    # 是否优先使用ip来作为主机名
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://localhost:8100/eureka

# 添加日志文件,开启admin的日志监控
logging:
  file:
    name: admin-client.log

在启动类上添加@EnableDiscoveryClient注解表明是一个服务发现的客户端。

测试

启动eureka-server,admin-server和admin-client。

admin-server和admin-client完成注册。

完成注册
admin-server也有监控信息。

监控信息

参考:
GitHub spring-boot-admin
Spring Cloud入门-Admin服务监控中心(Hoxton版本)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值