SpringBoot+actuator和admin-UI实现监控中心方式

 更新时间:2024年05月21日 10:57:39   作者:蚂蚁舞  
这篇文章主要介绍了SpringBoot+actuator和admin-UI实现监控中心方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

使用SpringBoot很久了,但是很少使用到SpringBoot的查看和监控,将来八成也不会用到,万一有机会用到呢?

所以记录一下以前学习SpringBoot+actuator和adminUI实现监控中心的方式

Springboot的版本2.0.x

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.0.5.RELEASE</version>
	<relativePath/> <!-- lookup parent from repository -->
</parent>

导入对应的包

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
	<version>2.0.5.RELEASE</version>
</dependency>

<!-- security 一旦导入就会生效 -->
<!--
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-security</artifactId>
</dependency>
 
<dependency>
	<groupId>org.springframework.security</groupId>
	<artifactId>spring-security-test</artifactId>
	<scope>test</scope>
</dependency>
 -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

application.properties


server.port=7080

# 配置用户名和密码
#spring.security.user.name=admin
#spring.security.user.password=123456

# 端点信息配置
management.server.port=8081
management.server.servlet.context-path=/sys
# 默认 never always可以显示硬盘使用情况和线程情况
management.endpoint.health.show-details=always
# 端点暴露的内容,默认["health","info"],设置"*"代表暴露所有可访问的端点
management.endpoints.web.exposure.include=*

# actuator 信息
info.actuator.name=test

启动之后

myw

访问

http://localhost:8081/sys/actuator

myw

在这里使用的Actuator是spring boot的一个附加功能,可帮助你在应用程序生产环境时监视和管理应用程序。

可以使用HTTP的各种请求来监管,审计,收集应用的运行情况.特别对于微服务管理十分有意义.

缺点:没有可视化界面

使用场景,针对微服务的服务状态监控,服务器的内存变化(堆内存,线程,日志管理等),检测服务配置连接地址是否可用(模拟访问,懒加载),统计现在有多少个bean(Spring容器中的bean) 统计接口数量,

应用场景:生产环境

  • /actuator/beans 显示应用程序中所有Spring bean的完整列表
  • /actuator/configprops 显示所有配置信息
  • /actuator/env 陈列所有的环境变量
  • /actuator/mappings 显示所有@RequestMapping的url整理列表
  • /actuator/health 显示应用程序运行状况信息 up表示成功 down失败,对于懒加载没报错的可以看到控制台报错了
  • /actuator/info 查看自定义应用信息

懒加载有个缺点,例如mysql的配置,启动的时候不会发现错误,只有运行的时候才报错

当访问

http://localhost:8081/sys/actuator/health

myw

使用adminUI的方式 client客户端导包和配置

pom.xml

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
	<version>2.0.5.RELEASE</version>
</dependency>
<dependency>
	<groupId>de.codecentric</groupId>
	<artifactId>spring-boot-admin-starter-client</artifactId>
	<version>2.0.5</version>
</dependency>

application.properties

server.port=8071

spring.application.name=boot-example-admin-client

spring.boot.admin.client.url=http://127.0.0.1:8050/myw-admin
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=123456
spring.boot.admin.client.instance.service-url=http://127.0.0.1:8071

#management.server.port=8081
#management.server.servlet.context-path=/sys
# 默认 never always可以显示硬盘使用情况和线程情况
management.endpoint.health.show-details=always
# 端点暴露的内容,默认["health","info"],设置"*"代表暴露所有可访问的端点
management.endpoints.web.exposure.include=*

使用admin-ui的方式 server服务端导包和配置

pom.xml

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

application.properties


server.port=8050
server.servlet.context-path=/myw-admin
spring.application.name=boot-example-admin-server

spring.security.user.name=admin
spring.security.user.password=123456


WebSecurityConfig.java

package boot.example.admin.server;

import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;

/**
 * SpringBootAdmin 登录鉴权使用
 *
 */
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    private final String contextPath;

    public WebSecurityConfig(AdminServerProperties adminServerProperties) {
        this.contextPath = adminServerProperties.getContextPath();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 跨域设置,SpringBootAdmin客户端通过instances注册,见InstancesController
        http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                .ignoringAntMatchers(contextPath + "/instances");

        http.authorizeRequests().antMatchers(contextPath + "/assets/**").permitAll(); // 静态资源
        http.authorizeRequests().antMatchers(contextPath + "/actuator/**").permitAll(); // 自身监控
        http.authorizeRequests().anyRequest().authenticated(); // 所有请求必须通过认证

        // 整合spring-boot-admin-server-ui
        http.formLogin().loginPage("/login").permitAll();
        http.logout().logoutUrl("/logout").logoutSuccessUrl("/login");

        // 启用basic认证,SpringBootAdmin客户端使用的是basic认证
        http.httpBasic();
    }
}

启动客户端和服务端的监控中心

http://localhost:8050/myw-admin/login#/applications

myw

myw

myw

记录一下在SpringBoot2.6.6版本使用

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.6.6</version>
	<relativePath/> <!-- lookup parent from repository -->
</parent>

client的pom.xml

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
	<version>2.6.6</version>
</dependency>
<dependency>
	<groupId>de.codecentric</groupId>
	<artifactId>spring-boot-admin-starter-client</artifactId>
	<version>2.5.6</version>
</dependency>

application.properties 1

server.port=8071

spring.application.name=boot-example-admin-client1

spring.boot.admin.client.url=http://localhost:8050
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=123456

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


application.properties 2


server.port=8072

spring.application.name=boot-example-admin-client2

spring.boot.admin.client.url=http://localhost:8050
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=123456

management.server.port=8082
management.server.base-path = /sys
management.endpoint.health.show-details=always
management.endpoints.web.exposure.include=*

application.properties 3


server.port=8073

spring.application.name=boot-example-admin-client3

spring.boot.admin.client.url=http://localhost:8050
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=123456

#spring.security.user.name=admin
#spring.security.user.password=123456

management.server.port=8083
management.server.base-path = /sys
management.endpoint.health.show-details=always
management.endpoints.web.exposure.include=*

服务端的配置

pom.xml

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

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

<dependency>
	<groupId>de.codecentric</groupId>
	<artifactId>spring-boot-admin-starter-server</artifactId>
	<version>2.5.6</version>
</dependency>

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

application.properties


server.port=8050

spring.application.name=boot-example-admin-server

spring.security.user.name=admin
spring.security.user.password=123456


总结

备注记录到这里 将来会不会用到再说了。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 详解Java阻塞队列(BlockingQueue)的实现原理

    详解Java阻塞队列(BlockingQueue)的实现原理

    这篇文章主要介绍了详解Java阻塞队列(BlockingQueue)的实现原理,阻塞队列是Java util.concurrent包下重要的数据结构,有兴趣的可以了解一下
    2017-06-06
  • SpringBoot后端接收多个文件多种实现方法

    SpringBoot后端接收多个文件多种实现方法

    SpringBoot提供了简单的方式来实现文件接收功能,可使用MultipartFile来接收上传的文件,并将其存储在服务器的指定位置,在SpringBoot中接收多个文件有多种方式,本文将详细介绍各种方法及其实现,需要的朋友可以参考下
    2025-09-09
  • SpringBoot返回对象时,如何将Long类型转换为String

    SpringBoot返回对象时,如何将Long类型转换为String

    这篇文章主要介绍了SpringBoot返回对象时,实现将Long类型转换为String,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • 基于Spring Boot的Environment源码理解实现分散配置详解

    基于Spring Boot的Environment源码理解实现分散配置详解

    这篇文章主要给大家介绍了基于Spring Boot的Environment源码理解实现分散配置的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-08-08
  • sentinel整合ribbon与fallback流程分步讲解

    sentinel整合ribbon与fallback流程分步讲解

    这篇文章主要介绍了sentinel整合ribbon与fallback分步流程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08
  • Java毕业设计实战项目之宠物商城系统的实现流程

    Java毕业设计实战项目之宠物商城系统的实现流程

    这是一个使用了java+Springboot+Maven+mybatis+Vue+mysql开发的宠物商城系统,是一个毕业设计的实战练习,具有宠物商城该有的所有功能,感兴趣的朋友快来看看吧
    2022-01-01
  • 详解Java ThreadPoolExecutor的拒绝策略

    详解Java ThreadPoolExecutor的拒绝策略

    这篇文章主要介绍了Java ThreadPoolExecutor的拒绝策略,本文对于线程的池的几种策略进行详细的讲解,在实际的生产中需要集合相关的场景来选择合适的拒绝策略,需要的朋友可以参考下
    2022-08-08
  • java实现打印日历

    java实现打印日历

    这篇文章主要为大家详细介绍了java打印日历的实现代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-01-01
  • spring boot hutool整合email的详细过程

    spring boot hutool整合email的详细过程

    这篇文章主要介绍了spring boot hutool整合email的相关知识,本文介绍两种方式发送email文件,结合实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2023-03-03
  • Java中本地缓存的4种实现方式总结

    Java中本地缓存的4种实现方式总结

    这篇文章主要介绍了Java中本地缓存的4种实现方式,分别是基础缓存实现、GuavaLoadingCache、SpringBoot整合Caffeine和JetCache,通过实例代码,详细讲解了每种缓存技术的特点和使用方法,需要的朋友可以参考下
    2025-04-04

最新评论