天天看点

一起来学SpringCloud SpringBootAdmin监控

序言

​ 随着开发周期的推移,项目会不断变大,切分出的服务也会越来越多,这时一个个的微服务构成了错综复杂的系统。对于各个微服务系统的健康状态、会话数量、并发数、服务资源、延迟等度量信息的收集就成为了一个挑战。Spring Boot Admin 应运而生,它正式基于这些需求开发出的一套功能强大的监控管理系统。

此文章仅限入门 SpringCloud版本为 Greenwich

​ Spring Boot Admin 有两个角色组成,一个是 Spring Boot Admin Server,一个是 Spring Boot Admin Client。

版本管理

由于Spring Boot Admin 不受spring-cloud-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>
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-dependencies</artifactId>
                <version>2.1.6</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

           

然后就ok了

服务端

首先加入依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
        </dependency>
        <!--eureka client -->
        <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-zipkin</artifactId>
        </dependency>
    </dependencies>
           

然后是启动类

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

在然后就是yml

server:
  port: 8088

spring:
  application:
    name: spring-cloud-action-sba

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

eureka:
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}
    hostname: localhost
  client:
    service-url:
      # defaultZone 千万别写成 default-zone
      defaultZone: http://${eureka.instance.hostname}:8761/eureka/
           

application.yml

文件中配置

actuator

的相关配置,其中

info

开头的属性,就是访问

info

端点中显示的相关内容,值得注意的是

Spring Boot2.x

中,默认只开放了

info、health

两个端点,剩余的需要自己通过配置

management.endpoints.web.exposure.include

属性来加载(有

include

自然就有

exclude

,不做详细概述了)。如果想单独操作某个端点可以使用

management.endpoint.端点.enabled

属性进行启用或禁用 。

management:
  endpoints:
    web:
      exposure:
        exclude: env,beans
        include: '*'
           

在yml中有特殊的含义,所以如果想使用include或者exclude包含所有的端点时要加上引号,如下示例:

management:
  endpoints:
    web:
      exposure:
        include: '*'
           

这样一个Spring Boot Admin 的服务端就已经弄好了。在sc 中只要服务注册进 注册中心中 sba 就可以拉取到信息啦。

一起来学SpringCloud SpringBootAdmin监控

此文章仅限入门,切记啊不会用找不到多去官网看文档!

继续阅读