Spring Cloud的微服务基础架构大多基于Spring Boot,因此本章为介绍的内容与Spring Boot有关
框架简介
对于很多Spring框架的初学者来说,经常会因为其繁杂的配置文件而困惑,所以出现了Maven等构建工具来针对不同的场景搭建脚手架。这样虽然减少了工作量,但是这些配置还是大量散布在我们的工程中,增加了工程的复杂度。于是Spring Boot的出现解决了这些问题,Spring Boot的宗旨并非要重写Spring或是替代,它是在原有的基础上进行了一些改造,通过大量的自动化配置等方式来简化Spring原有的样板化配置。
除了解决配置问题之外,Spring Boot还通过一些列的Starter POMS的定义,让我们整合各项功能的时候,不需要在Maven的pom.xml中维护那些错综复杂的依赖关系了,而是通过模板化Starter来引用,使依赖管理工作变得更加简单。
在如今容器化大行其道的时代,Spring Boot除了可以很好融入Docker之外,其自身就支持嵌入式的Tomcat,Jetty容器。所以通过Spring Boot构建的应用不再需要依赖外部的Tomcat了,只需要将Spring Boot应用打成jar包,通过java -jar命令直接运行就能启动一个标准化Web应用,这使得Spring Boot应用变得非常轻松。
说了这么多,其实只是Spring Boot的优点,不过Spring Boot的搭建与使用,已经有太多的博客在写了,作者在此不再过多赘述,不过有一个actuator模块,还是拿出来写一写。
actuator
actuator模块是用于监控spring Boot的扩展组件,使用前需要引入依赖,如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
之后在控制台中可以看到如下输出:

在这里我们可以看到,spring boot只暴露了2个端点。只需要访问http://127.0.0.1:8080/actuator,后面加上/health或/info端点即可查看对应接口参数。
但是问题是在这里只暴露了两个端点,那么actuator所提供的大部分端点其实被限制了,大概是因为安全问题,所以想要访问需要在配置文件中另外添加参数
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
添加该参数后即可查看所有的端点。
具体的配置可以参照该博客:spring boot2.0官方文档之actuator
根据端点的作用,我们可以将端点分为以下三大类:
- 应用配置类:获取应用程序中加载的应用配置,环境变量,自动化配置报告等与Spring Boot应用密切相关的配置类信息。
- 度量指标类:获取应用程序运行过程中用于监控的度量指标,比如内存信息,线程池信息,HTTP请求统计等。
- 操作控制类:提供了对应用的关闭等操作类功能。
/conditions:该端点用来获取应用中的自动化配置报告
- positiveMatches返回的是条件匹配成功的自动化配置
- negativeMatches中返回的是条件匹配不成功的自动化配置
-positiveMatches: {
AuditAutoConfiguration#auditListener: [
{
condition: "OnBeanCondition",
message: "@ConditionalOnMissingBean (types: org.springframework.boot.actuate.audit.listener.AbstractAuditListener; SearchStrategy: all) did not find any beans"
}
],
}
-negativeMatches: {
RabbitHealthIndicatorAutoConfiguration: {
notMatched: [
{
condition: "OnClassCondition",
message: "@ConditionalOnClass did not find required class 'org.springframework.amqp.rabbit.core.RabbitTemplate'"
}
],
matched: [ ]
}
}如上示例中我们可以看到,每个自动化配置候选项中都有一系列条件,比如上述的RabbitTemplate需要工程中包含RabbitTemplate类,由于我们没有引入相关的依赖,它就不会执行自动化配置内容。
-beans: {
endpointCachingOperationInvokerAdvisor: {
aliases: [ ],
scope: "singleton",
type: "org.springframework.boot.actuate.endpoint.invoker.cache.CachingOperationInvokerAdvisor",
resource: "class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/EndpointAutoConfiguration.class]",
dependencies: [
"environment"
]
},
defaultServletHandlerMapping: {
aliases: [ ],
scope: "singleton",
type: "org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport$EmptyHandlerMapping",
resource: "class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]",
dependencies: [ ]
},
如上示例中我们看到在每个bean中都包含了如下信息
aliases:
scope:作用域
type:java类型
resource:class文件的具体路径
dependencies:依赖的bean名称
/configprops: 该端点用来获取应用中配置的属性信息报告。
-server-org.springframework.boot.autoconfigure.web.ServerProperties: {
prefix: "server",
properties: {
undertow: {
maxHttpPostSize: 0,
eagerFilterInit: true,
accesslog: {
enabled: false,
pattern: "common",
prefix: "access_log.",
suffix: "log",
dir: "/home/rick/eclipse-workspace/cloud-demo/logs",
rotate: true
}
},
port: 8080,
maxHttpHeaderSize: 0
prefix属性代表属性的配置前缀,properties代表了各个属性的名称和值。所以,我们可以通过该报告来看到各个属性的配置路径,比如我们要关闭该端点,就可以通过使用management.endpoint.configprops.enabled=false来完成设置
/env:该端点与/configprops不同,它用来获取应用所有可用的环境属性报告。包括环境变量,jvm属性,应用的配置属性,命令行中的参数。
activeProfiles: [ ],
propertySources: [
{
name: "server.ports",
properties: {
local.management.port: {
value: 8081
},
local.server.port: {
value: 8080
}
}
},
{
name: "servletContextInitParams",
properties: { }
},
{
name: "systemProperties",
properties: {
com.sun.management.jmxremote.authenticate: {
value: "false"
},
java.runtime.name: {
value: "Java(TM) SE Runtime Environment"
},
spring.output.ansi.enabled: {
value: "always"
}
}
/mappings:该端点用来返回所有Spring MVC的控制器映射关系报告。报告中的信息与我们在启用Spring MVC的Web应用时输出的日志信息类似,其中bean属性标识了该映射关系的请求处理器,method属性标识了映射关系的具体处理类和处理函数。
/info:该端点用来返回一些应用自定义的信息。默认情况下,该端点只会返回一个空的json内容,我们可以在配置文件中通过info前缀来设置一些属性,比如
info.app.name=spring-boot-hello
info.app.version=v0.0.1
再访问/info端点,我们可以得到下面的返回报告,其中就包含了我们在应用中自定义的参数
{
app: {
name: "spring-boot-hello",
version: "v0.0.1"
}
}
度量指标类
上面我们锁介绍的应用配置类端点所提供的信息报告在应用启动的时候基本已经确定了其返回内容,是一个静态报告。而度量指标类端点提供的报告内容则是动态变化的,这些端点提供了应用程序在运行过程中的快照信息,比如内存使用情况,HTTP请求统计,外部资源指标等。
/metrics:该端点用来返回当前应用的各类重要度量指标,比如内存信息,线程信息,垃圾回收信息等。
{
names: [
"jvm.memory.max",
"process.files.max",
"jvm.gc.memory.promoted",
"tomcat.cache.hit",
"system.load.average.1m",
"tomcat.cache.access",
"jvm.memory.used",
"jvm.gc.max.data.size",
"jvm.gc.pause",
"jvm.memory.committed",
"system.cpu.count",
"logback.events",
"tomcat.global.sent",
"jvm.buffer.memory.used",
"tomcat.sessions.created",
"jvm.threads.daemon",
"system.cpu.usage",
"jvm.gc.memory.allocated",
"tomcat.global.request.max",
"tomcat.global.request",
"tomcat.sessions.expired",
"jvm.threads.live",
"jvm.threads.peak",
"tomcat.global.received",
"process.uptime",
"tomcat.sessions.rejected",
"process.cpu.usage",
"tomcat.threads.config.max",
"jvm.classes.loaded",
"jvm.classes.unloaded",
"tomcat.global.error",
"tomcat.sessions.active.current",
"tomcat.sessions.alive.max",
"jvm.gc.live.data.size",
"tomcat.servlet.request.max",
"tomcat.threads.current",
"tomcat.servlet.request",
"process.files.open",
"jvm.buffer.count",
"jvm.buffer.total.capacity",
"tomcat.sessions.active.max",
"tomcat.threads.busy",
"process.start.time",
"tomcat.servlet.error"
]
}
结果很多,我们只需要把我们需要的比如jvm.memory.max 放到地址后,即可查看
比如访问http://127.0.0.1:8081/actuator/metrics/jvm.memory.max
{
name: "jvm.memory.max",
measurements: [
{
statistic: "VALUE",
value: 4425515007
}
],
availableTags: [
{
tag: "area",
values: [
"heap",
"nonheap"
]
},
{
tag: "id",
values: [
"Compressed Class Space",
"PS Survivor Space",
"PS Old Gen",
"Metaspace",
"PS Eden Space",
"Code Cache"
]
}
]
}在本章我们介绍spring boot工程actuator模块的使用,更多关于使用spring boot开发微服务应用的内容,后续将不再介绍。想要学习的话可以取查阅官方文档。
本文深入探讨SpringBoot的Actuator模块,介绍其监控和管理功能,包括应用配置、度量指标和操作控制等方面,帮助开发者更好地理解和使用Actuator。
6842

被折叠的 条评论
为什么被折叠?



