Spring Boot 服务监控,健康检查,线程信息,JVM堆信息,指标收集,运行情况监控等!(二)

简介: Spring Boot 服务监控,健康检查,线程信息,JVM堆信息,指标收集,运行情况监控等!

5.2 /metrics端点


/metrics端点用来返回当前应用的各类重要度量指标,比如:内存信息、线程信息、垃圾回收信息、tomcat、数据库连接池等。

{
    "names": [
        "tomcat.threads.busy",
        "jvm.threads.states",
        "jdbc.connections.active",
        "jvm.gc.memory.promoted",
        "http.server.requests",
        "hikaricp.connections.max",
        "hikaricp.connections.min",
        "jvm.memory.used",
        "jvm.gc.max.data.size",
        "jdbc.connections.max",
         ....
    ]
}

不同于1.x,Actuator在这个界面看不到具体的指标信息,只是展示了一个指标列表。为了获取到某个指标的详细信息,我们可以请求具体的指标信息,像这样:

http://localhost:8080/actuator/metrics/{MetricName}

比如我访问 /actuator/metrics/jvm.memory.max,返回信息如下:

12ca79d954a0636d7c03663de1dc9d6d_640_wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1.jpg

你也可以用query param的方式查看单独的一块区域。比如你可以访问 /actuator/metrics/jvm.memory.max?tag=id:Metaspace。结果就是:

0dee8fd17751668c408e5dfed57d4d9a_640_wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1.jpg


5.3 /loggers端点


/loggers 端点暴露了我们程序内部配置的所有logger的信息。我们访问 /actuator/loggers可以看到,

4552aca2a248c496b248fef51030840d_640_wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1.jpg

你也可以通过下述方式访问单独一个logger,

http://localhost:8080/actuator/loggers/{name}

比如我现在访问 root logger, http://localhost:8080/actuator/loggers/root

{
    "configuredLevel": "INFO",
    "effectiveLevel": "INFO"
}


⭐改变运行时的日志等级


/loggers端点我最想提的就是这个功能,能够动态修改你的日志等级。

比如,我们可以通过下述方式来修改 root logger的日志等级。我们只需要发起一个URL 为 http://localhost:8080/actuator/loggers/rootPOST请求,POST报文如下:

1. {
2.    "configuredLevel": "DEBUG"
3. }

e0c922d75985f6262a030ae172ff5c5d_640_wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1.png

仔细想想,这个功能是不是非常有用。如果在生产环境中,你想要你的应用输出一些Debug信息以便于你诊断一些异常情况,你你只需要按照上述方式就可以修改,而不需要重启应用。

如果想重置成默认值,把value 改成 null


5.4 /info端点


/info端点可以用来展示你程序的信息。我理解过来就是一些程序的基础信息。并且你可以按照自己的需求在配置文件 application.properties中个性化配置(默认情况下,该端点只会返回一个空的json内容。):

info.app.name=actuator-test-demo
info.app.encoding=UTF-8
info.app.java.source=1.8
info.app.java.target=1.8
# 在 maven 项目中你可以直接用下列方式引用 maven properties的值
# info.app.encoding=@project.build.sourceEncoding@
# info.app.java.source=@java.version@
# info.app.java.target=@java.version@

启动项目,访问 http://localhost:8080/actuator/info

{
    "app": {
        "encoding": "UTF-8",
        "java": {
            "source": "1.8.0_131",
            "target": "1.8.0_131"
        },
        "name": "actuator-test-demo"
    }
}


5.5 /beans端点


/beans端点会返回Spring 容器中所有bean的别名、类型、是否单例、依赖等信息。

访问 http://localhost:8080/actuator/beans,返回如下:

ce929647674974d84e7d0a292234f35c_640_wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1.jpg


5.6 /heapdump 端点


访问:http://localhost:8080/actuator/heapdump会自动生成一个 Jvm 的堆文件 heapdump。我们可以使用 JDK 自带的 Jvm 监控工具 VisualVM 打开此文件查看内存快照。

37cc8e7c3a723085555c619f1b33b2b1_640_wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1.jpg


5.7 /threaddump 端点


这个端点我个人觉得特别有用,方便我们在日常定位问题的时候查看线程的情况。主要展示了线程名、线程ID、线程的状态、是否等待锁资源、线程堆栈等信息。就是可能查看起来不太直观。访问 http://localhost:8080/actuator/threaddump返回如下:

f413337fddc801c41ab60903a607c06d_640_wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1.jpg


5.8 /shutdown端点


这个端点属于操作控制类端点,可以优雅关闭 Spring Boot 应用。要使用这个功能首先需要在配置文件中开启:

management.endpoint.shutdown.enabled=true

由于 shutdown 接口默认只支持 POST 请求,我们启动Demo项目,向 http://localhost:8080/actuator/shutdown发起 POST请求。返回信息:

{
    "message": "Shutting down, bye..."
}

然后应用程序被关闭。

由于开放关闭应用的操作本身是一件非常危险的事,所以真正在线上使用的时候,我们需要对其加入一定的保护机制,比如:定制Actuator的端点路径、整合Spring Security进行安全校验等。(不是特别必要的话,这个端点不用开)


六、整合Spring Security 对端点进行安全校验


由于端点的信息和产生的交互都是非常敏感的,必须防止未经授权的外部访问。如果您的应用程序中存在Spring Security的依赖,则默认情况下使用基于表单的HTTP身份验证来保护端点。

如果没有,只需要增加对应的依赖即可:

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

添加之后,我们需要定义安全校验规则,来覆盖Spring Security 的默认配置。

这里我给出了两个版本的模板配置:

import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
import org.springframework.boot.actuate.context.ShutdownEndpoint;
import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
/**
 * @author Richard_yyf
 */
@Configuration
public class ActuatorSecurityConfig extends WebSecurityConfigurerAdapter {
    /*
     * version1:
     * 1. 限制 '/shutdown'端点的访问,只允许ACTUATOR_ADMIN访问
     * 2. 允许外部访问其他的端点
     * 3. 允许外部访问静态资源
     * 4. 允许外部访问 '/'
     * 5. 其他的访问需要被校验
     * version2:
     * 1. 限制所有端点的访问,只允许ACTUATOR_ADMIN访问
     * 2. 允许外部访问静态资源
     * 3. 允许外部访问 '/'
     * 4. 其他的访问需要被校验
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // version1
//        http
//                .authorizeRequests()
//                    .requestMatchers(EndpointRequest.to(ShutdownEndpoint.class))
//                        .hasRole("ACTUATOR_ADMIN")
//                .requestMatchers(EndpointRequest.toAnyEndpoint())
//                    .permitAll()
//                .requestMatchers(PathRequest.toStaticResources().atCommonLocations())
//                    .permitAll()
//                .antMatchers("/")
//                    .permitAll()
//                .antMatchers("/**")
//                    .authenticated()
//                .and()
//                .httpBasic();
        // version2
        http
                .authorizeRequests()
                .requestMatchers(EndpointRequest.toAnyEndpoint())
                    .hasRole("ACTUATOR_ADMIN")
                .requestMatchers(PathRequest.toStaticResources().atCommonLocations())
                    .permitAll()
                .antMatchers("/")
                    .permitAll()
                .antMatchers("/**")
                    .authenticated()
                .and()
                .httpBasic();
    }
}

application.properties的相关配置如下:

# Spring Security Default user name and password
spring.security.user.name=actuator
spring.security.user.password=actuator
spring.security.user.roles=ACTUATOR_ADMIN

相关文章
|
4月前
|
并行计算 Java 数据处理
SpringBoot高级并发实践:自定义线程池与@Async异步调用深度解析
SpringBoot高级并发实践:自定义线程池与@Async异步调用深度解析
351 0
|
7天前
|
网络协议 Java Shell
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
33 7
|
6月前
|
缓存 NoSQL Java
【Azure Redis 缓存】示例使用 redisson-spring-boot-starter 连接/使用 Azure Redis 服务
【Azure Redis 缓存】示例使用 redisson-spring-boot-starter 连接/使用 Azure Redis 服务
|
2月前
|
安全 Java 开发者
Spring容器中的bean是线程安全的吗?
Spring容器中的bean默认为单例模式,多线程环境下若操作共享成员变量,易引发线程安全问题。Spring未对单例bean做线程安全处理,需开发者自行解决。通常,Spring bean(如Controller、Service、Dao)无状态变化,故多为线程安全。若涉及线程安全问题,可通过编码或设置bean作用域为prototype解决。
50 1
|
4月前
|
算法 NoSQL Java
Springboot3新特性:GraalVM Native Image Support和虚拟线程(从入门到精通)
这篇文章介绍了Spring Boot 3中GraalVM Native Image Support的新特性,提供了将Spring Boot Web项目转换为可执行文件的步骤,并探讨了虚拟线程在Spring Boot中的使用,包括如何配置和启动虚拟线程支持。
251 9
Springboot3新特性:GraalVM Native Image Support和虚拟线程(从入门到精通)
|
5月前
|
Java Spring
spring多线程实现+合理设置最大线程数和核心线程数
本文介绍了手动设置线程池时的最大线程数和核心线程数配置方法,建议根据CPU核数及程序类型(CPU密集型或IO密集型)来合理设定。对于IO密集型,核心线程数设为CPU核数的两倍;CPU密集型则设为CPU核数加一。此外,还讨论了`maxPoolSize`、`keepAliveTime`、`allowCoreThreadTimeout`和`queueCapacity`等参数的设置策略,以确保线程池高效稳定运行。
555 10
spring多线程实现+合理设置最大线程数和核心线程数
|
5月前
|
Java API 对象存储
微服务魔法启动!Spring Cloud与Netflix OSS联手,零基础也能创造服务奇迹!
这段内容介绍了如何使用Spring Cloud和Netflix OSS构建微服务架构。首先,基于Spring Boot创建项目并添加Spring Cloud依赖项。接着配置Eureka服务器实现服务发现,然后创建REST控制器作为API入口。为提高服务稳定性,利用Hystrix实现断路器模式。最后,在启动类中启用Eureka客户端功能。此外,还可集成其他Netflix OSS组件以增强系统功能。通过这些步骤,开发者可以更高效地构建稳定且可扩展的微服务系统。
88 1
|
4月前
|
Java
SpringBoot线程问题
SpringBoot线程问题
34 0
|
7月前
|
Java Spring
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
174 3
|
6月前
|
Java Spring 容器
【Azure Spring Cloud】在Azure Spring Apps上看见 App Memory Usage 和 jvm.menory.use 的指标的疑问及OOM
【Azure Spring Cloud】在Azure Spring Apps上看见 App Memory Usage 和 jvm.menory.use 的指标的疑问及OOM

热门文章

最新文章