13.Spring Boot Admin:微服务应用监控

Spring Boot Admin:微服务应用监控

Spring Boot Admin 可以对SpringBoot应用的各项指标进行监控,可以作为微服务架构中的监控中心来使用,本文将对其用法进行详细介绍。

1.Spring Boot Admin 简介

SpringBoot应用可以通过Actuator来暴露应用运行过程中的各项指标,Spring Boot Admin通过这些指标来监控SpringBoot应用,然后通过图形化界面呈现出来。Spring Boot Admin不仅可以监控单体应用,还可以和Spring Cloud的注册中心相结合来监控微服务应用。

Spring Boot Admin 可以提供应用的以下监控信息:

  • 监控应用运行过程中的概览信息;
  • 度量指标信息,比如JVM、Tomcat及进程信息;
  • 环境变量信息,比如系统属性、系统环境变量以及应用配置信息;
  • 查看所有创建的Bean信息;
  • 查看应用中的所有配置信息;
  • 查看应用运行日志信息;
  • 查看JVM信息;
  • 查看可以访问的Web端点;
  • 查看HTTP跟踪信息。

2.创建admin-server模块

这里我们创建一个admin-server模块来作为监控中心演示其功能。

2.1 在pom.xml中添加相关依赖:

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

2.2 在application.yml中进行配置:

spring:
  application:
    name: admin-server
server:
  port: 9301

2.3 在启动类上添加@EnableAdminServer来启用admin-server功能:

@EnableAdminServer
@SpringBootApplication
public class AdminServerApplication {

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

}

3.创建admin-client模块

这里我们创建一个admin-client模块作为客户端注册到admin-server。

3.1 在pom.xml中添加相关依赖:

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

3.2 在application.yml中进行配置:

spring:
  application:
    name: admin-client
  boot:
    admin:
      client:
        url: http://localhost:9301 #配置admin-server地址
server:
  port: 9305
management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always
logging:
  file: admin-client.log #添加开启admin的日志监控

3.3 启动admin-server和admin-client服务。

4.监控信息演示

4.1 访问如下地址打开Spring Boot Admin的主页:http://localhost:9301

在这里插入图片描述

4.2 点击wallboard按钮,选择admin-client查看监控信息;

4.3 监控信息概览;

有截图
在这里插入图片描述

4.4 度量指标信息,比如JVM、Tomcat及进程信息;

有截图

4.5 查看所有创建的Bean信息;

在这里插入图片描述

4.6 查看应用中的所有配置信息;

有截图

4.7 环境变量信息,比如系统属性、系统环境变量以及应用配置信息;

在这里插入图片描述

4.8 查看日志信息,需要添加以下配置才能开启;

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

这里可以看到日志信息。

4.9 查看JVM信息;

有截图

4.10 查看可以访问的Web端点;

有截图

4.11 查看HTTP跟踪信息;

执行http://localhost:9305/user/1之后,查看http traces请求信息。
有截图

5.结合注册中心使用

Spring Boot Admin结合Spring Cloud 注册中心使用,只需将admin-server和注册中心整合即可,admin-server 会自动从注册中心获取服务列表,然后挨个获取监控信息。这里以Eureka注册中心为例来介绍下该功能。

5.1 修改admin-server

5.1.1 在pom.xml中添加相关依赖:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
5.1.2 在application-eureka.yml中进行配置,只需添加注册中心配置即可:
spring:
  application:
    name: admin-server
server:
  port: 9301
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8001/eureka/
5.1.3 在启动类上添加@EnableDiscoveryClient来启用服务注册功能:
@EnableDiscoveryClient
@EnableAdminServer
@SpringBootApplication
public class AdminServerApplication {

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

}

5.2 修改admin-client

5.2.1 在pom.xml中添加相关依赖:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
5.2.2 在application-eureka.yml中进行配置,删除原来的admin-server地址配置,添加注册中心配置即可:
spring:
  application:
    name: admin-client
server:
  port: 9305
management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always
logging:
  file: admin-client.log #添加开启admin的日志监控
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8001/eureka/
5.2.3 在启动类上添加@EnableDiscoveryClient来启用服务注册功能:
@EnableDiscoveryClient
@SpringBootApplication
public class AdminClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(AdminClientApplication.class, args);
    }
}

5.3 功能演示

  • 启动eureka-server,使用application-eureka.yml配置启动admin-server,admin-client;

    如果不知道如何使用application-eureka.yml配置admin-server,admin-client,请参考https://blog.csdn.net/MissOfSpring/article/details/101446531的4.2 运行Eureka注册中心集群一节。

  • 查看注册中心发现服务均已注册:http://localhost:8001/
    在这里插入图片描述

  • 查看Spring Boot Admin 主页发现可以看到服务信息:http://localhost:9301
    在这里插入图片描述

6.添加登录认证

我们可以通过给admin-server添加Spring Security支持来获得登录认证功能。

6.1 创建admin-security-server模块

6.1.1 在pom.xml中添加相关依赖:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>2.1.5</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
6.1.2 在application.yml中进行配置,配置登录用户名和密码,忽略admin-security-server的监控信息:
spring:
  application:
    name: admin-security-server
  security: # 配置登录用户名和密码
    user:
      name: allan
      password: 123456
  boot:  # 不显示admin-security-server的监控信息
    admin:
      discovery:
        ignored-services: ${spring.application.name}
server:
  port: 9301
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8001/eureka/
6.1.3 对SpringSecurity进行配置,以便admin-client可以注册:
/**
 * Created by allan on 2020/1/11.
 */
@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
    private final String adminContextPath;

    public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
        this.adminContextPath = adminServerProperties.getContextPath();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");
        successHandler.setDefaultTargetUrl(adminContextPath + "/");

        http.authorizeRequests()
                //1.配置所有静态资源和登录页可以公开访问
                .antMatchers(adminContextPath + "/assets/**").permitAll()
                .antMatchers(adminContextPath + "/login").permitAll()
                .anyRequest().authenticated()
                .and()
                //2.配置登录和登出路径
                .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
                .logout().logoutUrl(adminContextPath + "/logout").and()
                //3.开启http basic支持,admin-client注册时需要使用
                .httpBasic().and()
                .csrf()
                //4.开启基于cookie的csrf保护
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                //5.忽略这些路径的csrf保护以便admin-client注册
                .ignoringAntMatchers(
                        adminContextPath + "/instances",
                        adminContextPath + "/actuator/**"
                );
    }
}
6.1.4 修改启动类,开启AdminServer及注册发现功能:
@EnableDiscoveryClient
@EnableAdminServer
@SpringBootApplication
public class AdminSecurityServerApplication {

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

}
6.1.5 启动eureka-server,admin-security-server,访问Spring Boot Admin 主页发现需要登录才能访问:http://localhost:9301

在这里插入图片描述

7.使用到的模块

springcloud-learning[需要修改]
├── eureka-server – eureka注册中心
├── admin-server – admin监控中心服务
├── admin-client – admin监控中心监控的应用服务
└── admin-security-server – 带登录认证的admin监控中心服务

8.项目源码地址

https://github.com/yanglin1501804006/spring-cloud-vGreenwich.SR3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值