经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring Boot » 查看文章
Spring Boot Admin的使用详解(Actuator监控接口)
来源:jb51  时间:2021/5/7 9:35:44  对本文有异议

第一部分 Spring Boot Admin 简介

  •  Spring Boot Admin用来管理和监控Spring Boot应用程序。
  • 应用程序向我们的Spring Boot Admin Client注册(通过HTTP)或使用SpringCloud®(例如Eureka,Consul)发现。
  • UI是Spring Boot Actuator端点上的Vue.js应用程序。

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

Spring Boot Admin 是一个针对spring-boot的actuator接口进行UI美化封装的监控工具。他可以:在列表中浏览所有被监控spring-boot项目的基本信息,详细的Health信息、内存信息、JVM信息、垃圾回收信息、各种配置信息(比如数据源、缓存列表和命中率)等,还可以直接修改logger的level。

admin-server 服务端(admin-server)

服务端:是指Spring Boot Admin这个应用(通常就是指监控服务器),一个服务端可以监控多个客户端。

客户端

客户端是:被服务端监控的对象(通常就是指你的业务系统)。

第二部分 快速入门

本部分将为您展示SpringBoot ADMIN 的简单应用。

服务端配置(admin-server)

步骤一:搭建springboot maven项目

搭建一个基于SpringBoot的项目。注意您所使用的SpringBoot版本。

步骤二:配置pom.xml文件

  1. <dependency>
  2. <groupId>de.codecentric</groupId>
  3. <artifactId>spring-boot-admin-starter-server</artifactId>
  4. <version>2.3.1</version>
  5. </dependency>

步骤三:application.properties中配置端口号

此端口号指的是你所搭建的服务器所使用的的版本号,如果服务端和客户端在同一台机器上,注意端口号的设置,以防端口出现冲突的情况。

  1. server.port=8099

步骤四:主启动类上加注解@EnableAdminServer

  1. @SpringBootApplication
  2. @EnableAdminServer
  3. public class
  4. DemoApplication {
  5.  
  6. public static void main(String[] args) {
  7. SpringApplication.run(DemoApplication.class, args);
  8. }
  9.  
  10. }

步骤五:启动项目

访问:http://127.0.0.1:8099/applications。监控首页显示如下

在这里插入图片描述

客户端配置(admin-client)

步骤一:在客户端项目(也就是需要监控的springboot项目)中添加jar包

加入Security安全框架的jar包,加入jar需注意版本的问题。有些springboot版本,可能会自动引入失>败。如图:

在这里插入图片描述

出现这种情况需指定security的版本号,找个适合你springboot版本的security。

具体如下:

  1. <!--security-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-security</artifactId>
  5. <version>2.4.1</version>
  6. </dependency>

引入 spring-boot-admin-starter-client

  1. <!--admin server 监控-->
  2. <dependency>
  3. <groupId>de.codecentric</groupId>
  4. <artifactId>spring-boot-admin-starter-client</artifactId>
  5. <version>2.4.1</version>
  6. </dependency>

步骤二:在启动配置文件中配置如下 application.properties

  1. #开放端点用于SpringBoot Admin的监控
  2. management.endpoints.web.exposure.include=*
  3. # 给client应用取个名字
  4. spring.boot.admin.client.instance.name=zxfdemo
  5. #这里配置admin server 的地址
  6. spring.boot.admin.client.url=http://localhost:8099
  7. #这里配置admin client 的地址(客户端应用程序)
  8. spring.boot.admin.client.instance.service-url=http://localhost:8080

步骤四:测试效果

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

spring security 安全加固

SpringBoot Admin的管理后台如果没密码就能访问,那实在太不安全了,所以需要引入一个安全加固的jar包。spring-boot-starter-security

Spring Security是一个功能强大且高度可定制的身份验证和访问控制框架。提供了完善的认证机制和方法级的授权功能。是一款非常优秀的权限管理框架。它的核心是一组过滤器链,不同的功能经由不同的过滤器。此处就是想通过一个小案例将Spring Security整合到SpringBoot中去。要实现的功能就是在认证服务器上登录,然后获取Token,再访问资源服务器中的资源。

服务端配置(admin-server)

服务端配置修改

1. 服务端添加Spring Security 相关依赖

添加Spring Security 相关依赖

  1. <!-- security-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-security</artifactId>
  5. <version>2.4.1</version>
  6. </dependency>

2. 服务端设置账号密码

  1. spring.security.user.name=zxf
  2. spring.security.user.password=123456

3.添加一个Spring Security 配置类

  1. package com.example.springadmintest.config;
  2.  
  3. import de.codecentric.boot.admin.server.config.AdminServerProperties;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  6. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  7. import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
  8.  
  9. /**
  10. * 配置security验证页面指向SpringBootAdmin提供的UI界面
  11. *
  12. *
  13. */
  14. @Configuration
  15. public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
  16.  
  17. private final String contextPath;
  18.  
  19. public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
  20. this.contextPath = adminServerProperties.getContextPath();
  21. }
  22.  
  23. @Override
  24. protected void configure(HttpSecurity http) throws Exception {
  25. // 跨域设置,SpringBootAdmin客户端通过instances注册,见InstancesController
  26. http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
  27. .ignoringAntMatchers(contextPath + "/instances");
  28.  
  29. http.authorizeRequests().antMatchers(contextPath + "/assets/**").permitAll(); // 静态资源
  30. http.authorizeRequests().anyRequest().authenticated(); // 所有请求必须通过认证
  31.  
  32. // 整合spring-boot-admin-server-ui
  33. http.formLogin().loginPage("/login").permitAll();
  34. http.logout().logoutUrl("/logout").logoutSuccessUrl("/login");
  35.  
  36. // 启用basic认证,SpringBootAdmin客户端使用的是basic认证
  37. http.httpBasic();
  38. }
  39. }
  40.  

4.登录页面展示

再次访问http://localhost:8099/ ,发现需要登录

在这里插入图片描述

客户端配置(admin-client)

客户端配置

1.客户端添加Spring Security 相关依赖

  1. <!-- security-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-security</artifactId>
  5. <version>2.4.1</version>
  6. </dependency>

2. 客户端设置账号密码

  1. # 配置 admin-client 地址
  2. spring.boot.admin.client.instance.service-url=http://localhost:8080
  3. #配置 admin-server地址
  4. spring.boot.admin.client.url=http://localhost:8099
  5. # 配置 admin-server的账号
  6. spring.boot.admin.client.username=zxf
  7. # 配置 admin-server的密码
  8. spring.boot.admin.client.password=123456
  9. #配置 admin-server的密码
  10. spring.security.user.name=zxf
  11. #配置 admin-client的密码
  12. spring.security.user.password=123456
  13. #若在核心配置文件中未添加 management.security.enabled=false 配置,
  14. # 将会导致用户在访问部分监控地址时访问受限,报401未授权错误。
  15. management.security.enabled=false
  16. #监控中心配置, 允许监控所有接口
  17. management.endpoints.web.exposure.include=*
  18.  

3. 客户端添加Spring Security 配置类

  1. package com.cachedemo.controller;
  2.  
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  5. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  6.  
  7. @Configuration
  8. public class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
  9. @Override
  10. protected void configure(HttpSecurity http) throws Exception {
  11. http.authorizeRequests().anyRequest().permitAll()
  12. .and().csrf().disable();
  13. }
  14. }
  15.  

所有配置完成测试结果

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

到此这篇关于Spring Boot Admin的使用详解(Actuator监控接口)的文章就介绍到这了,更多相关Spring Boot Admin的使用 内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号