📄 监控服务常见功能使用
内部资料,请刷新扫码登录
pigcloud
# 服务环境状态查看
当前面板将展示微服务所在宿主机的磁盘、Java 运行环境、堆栈内存以及其他中间件的资源占用等信息。
# 在线日志查看
服务部署在服务器上,大部分公司都有对服务器的安全管理要求。一般开发人员无法登录服务器,因此无法查看后台日志。这经常导致应用系统报错,但程序员不能快速定位错误,因为无法查看详细的服务日志。为了解决这个问题,可以使用 Spring Boot Admin (pigx-monitor)组件实现在线查看后端服务日志。这样,就不需要去服务器本地查看了。
# 1. 配置 logback 输出路径
- ① 修改目标微服务的 logback-spring.xml 文件
- ② 修改 log.path 为服务器实际的日志文件存储目录
# 2. 配置 monitor 日志加载
LOGGING_PATH: 对应如上图②处实际的日志文件存储目录
# 动态日志级别
应用默认的输出的日志级别是 INFO,生产中建议使用 ERROR 级别。
当我们遇到某些 bug 时,动态调整 log 输出日志非常有效,spring boot admin 提供了这样的功能。
目标服务重启后此处配置将失效
# 监控安全认证
默认情况下,Spring Boot Admin 不提供安全认证,任何用户都可以访问。但可以通过以下配置 bean 来限制登录。
# 1. 监控服务添加安全依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
# 2. 创建 CustomCsrfFilter
package com.pig4cloud.pigx.monitor.config;
public class CustomCsrfFilter extends OncePerRequestFilter {
public static final String CSRF_COOKIE_NAME = "XSRF-TOKEN";
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
if (csrf != null) {
Cookie cookie = WebUtils.getCookie(request, CSRF_COOKIE_NAME);
String token = csrf.getToken();
if (cookie == null || token != null && !token.equals(cookie.getValue())) {
cookie = new Cookie(CSRF_COOKIE_NAME, token);
cookie.setPath("/");
response.addCookie(cookie);
}
}
filterChain.doFilter(request, response);
}
}
# 3. 创建 SecurityConfig
package com.pig4cloud.pigx.monitor.config;
@Configuration(proxyBeanMethods = false)
public class SecuritySecureConfig {
private final AdminServerProperties adminServer;
private final SecurityProperties security;
public SecuritySecureConfig(AdminServerProperties adminServer, SecurityProperties security) {
this.adminServer = adminServer;
this.security = security;
}
@Bean
protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
successHandler.setTargetUrlParameter("redirectTo");
successHandler.setDefaultTargetUrl(this.adminServer.path("/"));
http.authorizeHttpRequests((authorizeRequests) -> authorizeRequests //
.requestMatchers(new AntPathRequestMatcher(this.adminServer.path("/assets/**")))
.permitAll()
.requestMatchers(new AntPathRequestMatcher(this.adminServer.path("/actuator/info")))
.permitAll()
.requestMatchers(new AntPathRequestMatcher(adminServer.path("/actuator/health")))
.permitAll()
.requestMatchers(new AntPathRequestMatcher(this.adminServer.path("/login")))
.permitAll()
.dispatcherTypeMatchers(DispatcherType.ASYNC)
.permitAll() // https://github.com/spring-projects/spring-security/issues/11027
.anyRequest()
.authenticated())
.formLogin(
(formLogin) -> formLogin.loginPage(this.adminServer.path("/login")).successHandler(successHandler))
.logout((logout) -> logout.logoutUrl(this.adminServer.path("/logout")))
.httpBasic(Customizer.withDefaults());
http.addFilterAfter(new CustomCsrfFilter(), BasicAuthenticationFilter.class) // <5>
.csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.csrfTokenRequestHandler(new CsrfTokenRequestAttributeHandler())
.ignoringRequestMatchers(
new AntPathRequestMatcher(this.adminServer.path("/instances"), POST.toString()), // <6>
new AntPathRequestMatcher(this.adminServer.path("/instances/*"), DELETE.toString()), // <6>
new AntPathRequestMatcher(this.adminServer.path("/actuator/**")) // <7>
));
http.rememberMe((rememberMe) -> rememberMe.key(UUID.randomUUID().toString()).tokenValiditySeconds(1209600));
return http.build();
}
// Required to provide UserDetailsService for "remember functionality"
@Bean
public InMemoryUserDetailsManager userDetailsService(PasswordEncoder passwordEncoder) {
UserDetails user = User.withUsername(security.getUser().getName())
.password(passwordEncoder.encode(security.getUser().getPassword()))
.roles("USER")
.build();
return new InMemoryUserDetailsManager(user);
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
# 4. 配置登录用户
spring:
security:
user:
name: pigx
password: pigx