SpringBoot监控Acturator
1.加入对应的maven
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.在配置文件上加入对应的监控信息管理
management:
endpoints:
web:
exposure:
include: '*' #可以打开所有的监控点 监控点有什么可以再百度搜查“Actuator端点说明”
endpoint:
health:
show-details: always #展示细节,除了always之外还有when-authorized、never,默认值是never
shutdown:
enabled: true
info:
env:
enabled: true
3.启动项目,你可以查询
actuator
http://localhost:8080/actuator/info 这个是查找配置文件中以info开头的配置信息 注意这个链接生效需要配置management.info.env.enabled=true
http://localhost:8080/actuator/beans 展示了bean的别名,类型,是否单例,类的地址,依赖等信息
http://localhost:8080/actuator/conditions
自动配置很好,但是需要看哪个配置是否生效,用这个链接
http://localhost:8080/actuator/heapdump
自动生成内存快照
http://localhost:8080/actuator/mappings
描述全部的URI路径,以及他们和控制器的映射关系
http://localhost:8080/actuator/threaddump
接口会生成当前线程活动的快照。主要展示了线程名、线程ID、线程的状态、是否等待锁资源信息。
打开cmd执行
curl -X POST http://localhost:8080/actuator/shutdown
关闭SpringBoot应用
5.添加监控的管理平台Spring Boot Admin
5.1 做一个服务端
5.1.1 插入对应的maven
<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>
</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.7.4</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
5.1.2 写一个配置类
书写对应的config代码
package com.example.demo.config;
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
@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 {
// @formatter:off
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(adminContextPath + "/");
http.authorizeRequests()
.antMatchers(adminContextPath + "/assets/**").permitAll()//Grants public access to all static assets and the login page.
.antMatchers(adminContextPath + "/login").permitAll()
.anyRequest().authenticated()// Every other request must be authenticated.
.and()
.formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()//Configures login and logout.
.logout().logoutUrl(adminContextPath + "/logout").and()
.httpBasic().and()//Enables HTTP-Basic support. This is needed for the Spring Boot Admin Client to register.
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())// Enables CSRF-Protection using Cookies
.ignoringAntMatchers(
adminContextPath + "/instances",// Disables CRSF-Protection the endpoint the Spring Boot Admin Client uses to register.
adminContextPath + "/actuator/**"//Disables CRSF-Protection for the actuator endpoints.
);
}
}
5.1.3 书写application.yml文件
server:
port: 8081
spring:
security:
user:
password: 123456
name: root
其中可以根据spring.security.user.name和spring.security.user.password进行登录
5.2 做一个客户端
5.2.1 导入对应的maven
<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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.7.4</version>
</dependency>
5.2.2 在对应的配置文件下填入对应的配置信息
下面图片都是在spring下的,其中spring.boot.admin.client.username和spring.boot.admin.client.username对应服务端的账户和密码
spring.boot.admin.client.url是对应服务端的地址链接