您的位置:首页 > 游戏 > 游戏 > SpringBoot学习(18)使用spring-boot-admin监控SpringBoot

SpringBoot学习(18)使用spring-boot-admin监控SpringBoot

2025/2/12 18:09:16 来源:https://blog.csdn.net/L08130421/article/details/142102961  浏览:    关键词:SpringBoot学习(18)使用spring-boot-admin监控SpringBoot

什么是 Spring Boot Admin?

Spring Boot Admin 是一个管理和监控 Spring Boot 应用程序的开源软件。每个应用都认为是一个客户端,通过 HTTP 或者使用 Eureka 注册到 admin server 中进行展示,Spring Boot Admin UI 部分使用 VueJs 将数据展示在前端。

这篇文章给大家介绍如何使用 Spring Boot Admin 对 Spring Boot 应用进行监控。

监控单体应用

这节给大家展示如何使用 Spring Boot Admin 监控单个 Spring Boot 应用。

Admin Server 端

项目依赖

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

配置文件

server.port=8000

服务端设置端口为:8000。

启动类

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

完成上面三步之后,启动服务端,浏览器访问http://localhost:8000可以看到以下界面:

Admin Client 端

项目依赖

<dependencies><dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-client</artifactId><version>2.1.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
</dependencies>

配置文件

server.port=8001
spring.application.name=Admin Client
spring.boot.admin.client.url=http://localhost:8000  
management.endpoints.web.exposure.include=*
  • spring.boot.admin.client.url 配置 Admin Server 的地址
  • management.endpoints.web.exposure.include=* 打开客户端 Actuator 的监控。

启动类

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

配置完成之后,启动 Client 端,Admin 服务端会自动检查到客户端的变化,并展示其应用

页面会展示被监控的服务列表,点击详项目名称会进入此应用的详细监控信息。

通过上图可以看出,Spring Boot Admin 以图形化的形式展示了应用的各项信息,这些信息大多都来自于 Spring Boot Actuator 提供的接口。

监控微服务

如果我们使用的是单个 Spring Boot 应用,就需要在每一个被监控的应用中配置 Admin Server 的地址信息;如果应用都注册在 Eureka 中就不需要再对每个应用进行配置,Spring Boot Admin 会自动从注册中心抓取应用的相关信息。

如果我们使用了 Spring Cloud 的服务发现功能,就不需要在单独添加 Admin Client 客户端,仅仅需要 Spring Boot Server ,其它内容会自动进行配置。

接下来我们以 Eureka 作为服务发现的示例来进行演示,实际上也可以使用 Consul 或者 Zookeeper。

1、服务端和客户端添加 spring-cloud-starter-eureka 到包依赖中

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

2、启动类添加注解

@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
@EnableAdminServer
public class SpringBootAdminApplication {public static void main(String[] args) {SpringApplication.run(SpringBootAdminApplication.class, args);}@Configurationpublic static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().anyRequest().permitAll()  .and().csrf().disable();}}
}

使用类 SecurityPermitAllConfig 关闭了安全验证。

3、在客户端中配置服务发现的地址

eureka:   instance:leaseRenewalIntervalInSeconds: 10health-check-url-path: /actuator/healthmetadata-map:startup: ${random.int}    #needed to trigger info and endpoint update after restartclient:registryFetchIntervalSeconds: 5serviceUrl:defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/management:endpoints:web:exposure:include: "*"  endpoint:health:show-details: ALWAYS

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com