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

启动之后

SpringBoot+actuator和admin-UI实现监控中心_第1张图片
访问

http://localhost:8081/sys/actuator

SpringBoot+actuator和admin-UI实现监控中心_第2张图片
在这里使用的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

SpringBoot+actuator和admin-UI实现监控中心_第3张图片
使用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

SpringBoot+actuator和admin-UI实现监控中心_第4张图片
SpringBoot+actuator和admin-UI实现监控中心_第5张图片
SpringBoot+actuator和admin-UI实现监控中心_第6张图片
记录一下在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


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

你可能感兴趣的:(SpringBoot+Demo,spring,boot,后端,actuator,admin-ui)