如何通过Spring Boot Admin详细监控我们的微服务架构?

2026-05-21 08:105阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1217个文字,预计阅读时间需要5分钟。

如何通过Spring Boot Admin详细监控我们的微服务架构?

1. 介绍:Spring Boot Admin是一个Web应用程序,用于管理和监控Spring Boot应用程序。每个应用程序都被视为客户端,并注册到管理服务器。底层能力由Spring Boot Actuator端点提供。

1.概述

Spring Boot Admin是一个Web应用程序,用于管理和监视Spring Boot应用程序。每个应用程序都被视为客户端,并注册到管理服务器。底层能力是由Spring Boot Actuator端点提供的。

在本文中,我们将介绍配置Spring Boot Admin服务器的步骤以及应用程序如何集成客户端。

2.管理服务器配置

由于Spring Boot Admin Server可以作为servlet或webflux应用程序运行,根据需要,选择一种并添加相应的Spring Boot Starter。在此示例中,我们使用Servlet Web Starter。

如何通过Spring Boot Admin详细监控我们的微服务架构?

首先,创建一个简单的Spring Boot Web应用程序,并添加以下Maven依赖项:

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

之后,@ EnableAdminServer将可用,因此我们将其添加到主类中,如下例所示:

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

至此,服务端就配置完了。

3.设置客户端

要在Spring Boot Admin Server服务器上注册应用程序,可以包括Spring Boot Admin客户端或使用Spring Cloud Discovery(例如Eureka,Consul等)。

下面的例子使用Spring Boot Admin客户端进行注册,为了保护端点,还需要添加spring-boot-starter-security,添加以下Maven依赖项:

<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.2.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>

接下来,我们需要配置客户端说明管理服务器的URL。为此,只需添加以下属性:

spring.boot.admin.client.url=localhost:8080

从Spring Boot 2开始,默认情况下不公开运行状况和信息以外的端点,对于生产环境,应该仔细选择要公开的端点。

management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always

使执行器端点可访问:

@Configuration public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity localhost:8761}/eureka/ management: endpoints: web: exposure: include: "*" endpoint: health: show-details: ALWAYS

4.安全配置

Spring Boot Admin服务器可以访问应用程序的敏感端点,因此建议为admin 服务和客户端应用程序添加一些安全配置。
由于有多种方法可以解决分布式Web应用程序中的身份验证和授权,因此Spring Boot Admin不会提供默认方法。默认情况下spring-boot-admin-server-ui提供登录页面和注销按钮。

服务器的Spring Security配置如下所示:

@Configuration(proxyBeanMethods = false) public class SecuritySecureConfig extends WebSecurityConfigurerAdapter { private final AdminServerProperties adminServer; public SecuritySecureConfig(AdminServerProperties adminServer) { this.adminServer = adminServer; } @Override protected void configure(HttpSecurity localhost:8080 instance: metadata: user.name: ${spring.security.user.name} user.password: ${spring.security.user.password}

使用Eureka提交凭据:

eureka: instance: metadata-map: user.name: ${spring.security.user.name} user.password: ${spring.security.user.password}

5.日志文件查看器

默认情况下,日志文件无法通过执行器端点访问,因此在Spring Boot Admin中不可见。为了启用日志文件执行器端点,需要通过设置logging.file.path或将Spring Boot配置为写入日志文件 logging.file.name。

Spring Boot Admin将检测所有看起来像URL的内容,并将其呈现为超链接。
还支持ANSI颜色转义。因为Spring Boot的默认格式不使用颜色,可以设置一个自定义日志格式支持颜色。

logging.file.name=/var/log/sample-boot-application.log logging.pattern.file=%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx

6. 通知事项

邮件通知

邮件通知将作为使用Thymeleaf模板呈现的HTML电子邮件进行传递。要启用邮件通知,请配置JavaMailSender使用spring-boot-starter-mail并设置收件人。

将spring-boot-starter-mail添加到依赖项中:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>

配置一个JavaMailSender

spring.mail.username=smtp_user spring.mail.password=smtp_password spring.boot.admin.notify.mail.to=admin@example.com

无论何时注册客户端将其状态从“ UP”更改为“ OFFLINE”,都会将电子邮件发送到上面配置的地址。

自定义通知程序

可以通过添加实现Notifier接口的Spring Bean来添加自己的通知程序,最好通过扩展 AbstractEventNotifier或AbstractStatusChangeNotifier来实现。

public class CustomNotifier extends AbstractEventNotifier { private static final Logger LOGGER = LoggerFactory.getLogger(LoggingNotifier.class); public CustomNotifier(InstanceRepository repository) { super(repository); } @Override protected Mono<Void> doNotify(InstanceEvent event, Instance instance) { return Mono.fromRunnable(() -> { if (event instanceof InstanceStatusChangedEvent) { LOGGER.info("Instance {} ({}) is {}", instance.getRegistration().getName(), event.getInstance(), ((InstanceStatusChangedEvent) event).getStatusInfo().getStatus()); } else { LOGGER.info("Instance {} ({}) {}", instance.getRegistration().getName(), event.getInstance(), event.getType()); } }); } }

其他的一些配置参数和属性可以通过官方文档来了解。

到此这篇关于详解用Spring Boot Admin来监控我们的微服务的文章就介绍到这了,更多相关Spring Boot Admin监控微服务内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

本文共计1217个文字,预计阅读时间需要5分钟。

如何通过Spring Boot Admin详细监控我们的微服务架构?

1. 介绍:Spring Boot Admin是一个Web应用程序,用于管理和监控Spring Boot应用程序。每个应用程序都被视为客户端,并注册到管理服务器。底层能力由Spring Boot Actuator端点提供。

1.概述

Spring Boot Admin是一个Web应用程序,用于管理和监视Spring Boot应用程序。每个应用程序都被视为客户端,并注册到管理服务器。底层能力是由Spring Boot Actuator端点提供的。

在本文中,我们将介绍配置Spring Boot Admin服务器的步骤以及应用程序如何集成客户端。

2.管理服务器配置

由于Spring Boot Admin Server可以作为servlet或webflux应用程序运行,根据需要,选择一种并添加相应的Spring Boot Starter。在此示例中,我们使用Servlet Web Starter。

如何通过Spring Boot Admin详细监控我们的微服务架构?

首先,创建一个简单的Spring Boot Web应用程序,并添加以下Maven依赖项:

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

之后,@ EnableAdminServer将可用,因此我们将其添加到主类中,如下例所示:

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

至此,服务端就配置完了。

3.设置客户端

要在Spring Boot Admin Server服务器上注册应用程序,可以包括Spring Boot Admin客户端或使用Spring Cloud Discovery(例如Eureka,Consul等)。

下面的例子使用Spring Boot Admin客户端进行注册,为了保护端点,还需要添加spring-boot-starter-security,添加以下Maven依赖项:

<dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.2.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>

接下来,我们需要配置客户端说明管理服务器的URL。为此,只需添加以下属性:

spring.boot.admin.client.url=localhost:8080

从Spring Boot 2开始,默认情况下不公开运行状况和信息以外的端点,对于生产环境,应该仔细选择要公开的端点。

management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always

使执行器端点可访问:

@Configuration public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity localhost:8761}/eureka/ management: endpoints: web: exposure: include: "*" endpoint: health: show-details: ALWAYS

4.安全配置

Spring Boot Admin服务器可以访问应用程序的敏感端点,因此建议为admin 服务和客户端应用程序添加一些安全配置。
由于有多种方法可以解决分布式Web应用程序中的身份验证和授权,因此Spring Boot Admin不会提供默认方法。默认情况下spring-boot-admin-server-ui提供登录页面和注销按钮。

服务器的Spring Security配置如下所示:

@Configuration(proxyBeanMethods = false) public class SecuritySecureConfig extends WebSecurityConfigurerAdapter { private final AdminServerProperties adminServer; public SecuritySecureConfig(AdminServerProperties adminServer) { this.adminServer = adminServer; } @Override protected void configure(HttpSecurity localhost:8080 instance: metadata: user.name: ${spring.security.user.name} user.password: ${spring.security.user.password}

使用Eureka提交凭据:

eureka: instance: metadata-map: user.name: ${spring.security.user.name} user.password: ${spring.security.user.password}

5.日志文件查看器

默认情况下,日志文件无法通过执行器端点访问,因此在Spring Boot Admin中不可见。为了启用日志文件执行器端点,需要通过设置logging.file.path或将Spring Boot配置为写入日志文件 logging.file.name。

Spring Boot Admin将检测所有看起来像URL的内容,并将其呈现为超链接。
还支持ANSI颜色转义。因为Spring Boot的默认格式不使用颜色,可以设置一个自定义日志格式支持颜色。

logging.file.name=/var/log/sample-boot-application.log logging.pattern.file=%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID}){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx

6. 通知事项

邮件通知

邮件通知将作为使用Thymeleaf模板呈现的HTML电子邮件进行传递。要启用邮件通知,请配置JavaMailSender使用spring-boot-starter-mail并设置收件人。

将spring-boot-starter-mail添加到依赖项中:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>

配置一个JavaMailSender

spring.mail.username=smtp_user spring.mail.password=smtp_password spring.boot.admin.notify.mail.to=admin@example.com

无论何时注册客户端将其状态从“ UP”更改为“ OFFLINE”,都会将电子邮件发送到上面配置的地址。

自定义通知程序

可以通过添加实现Notifier接口的Spring Bean来添加自己的通知程序,最好通过扩展 AbstractEventNotifier或AbstractStatusChangeNotifier来实现。

public class CustomNotifier extends AbstractEventNotifier { private static final Logger LOGGER = LoggerFactory.getLogger(LoggingNotifier.class); public CustomNotifier(InstanceRepository repository) { super(repository); } @Override protected Mono<Void> doNotify(InstanceEvent event, Instance instance) { return Mono.fromRunnable(() -> { if (event instanceof InstanceStatusChangedEvent) { LOGGER.info("Instance {} ({}) is {}", instance.getRegistration().getName(), event.getInstance(), ((InstanceStatusChangedEvent) event).getStatusInfo().getStatus()); } else { LOGGER.info("Instance {} ({}) {}", instance.getRegistration().getName(), event.getInstance(), event.getType()); } }); } }

其他的一些配置参数和属性可以通过官方文档来了解。

到此这篇关于详解用Spring Boot Admin来监控我们的微服务的文章就介绍到这了,更多相关Spring Boot Admin监控微服务内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!