Spring Cloud引入Actuator执行器

Actuator是spring boot项目中非常强大的功能,用于对应用程序进行监视和管理。
执行器端点(endpoints)是actuator的核心部分,它用来监视应用程序及交互。
actuator内置了非常多的Endpoints(health、info、beans、httptrace、shutdown等),同时也允许我们自己扩展自己的端点。默认只开放了info、health两个端点。
通过restful api请求来监管、审计、收集应用的运行情况,针对微服务而言它是必不可少的一个环节。
远程访问端点必须通过JMX或HTTP进行暴露,一般选择HTTP。端点的ID映射到一个带/actuator前缀的URL,也可以自定义前缀的URL,比如:management.endpoints.web.base-path=/manage
3个端点的说明:

ID 说明
health 显示应用的健康信息(当使用一个未认证连接访问时显示一个简单的’status’,使用认证连接访问则显示全部信息详情)
info 显示任意的应用信息(在资源文件写info.xxx即可)
beans 显示一个应用中所有Spring Beans及其关系列表

服务提供者引入Actuator

pom.xml

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

application.properties

# 定义任意的应用信息
info.content=spring-cloud-provider
info.author=tom
info.version=@project.version@
# 通过JMX公开health和info端点
management.endpoints.jmx.exposure.include=health,info
# 通过HTTP公开health,beans和info端点
management.endpoints.web.exposure.include=health,info,beans
# 通过HTTP公开除env端点之外的所有内容
management.endpoints.web.exposure.exclude=env

测试
入口地址:点击图中红圈处的链接
测试入口
没有配置actuator,报错404
没有配置actuator
配置actuator后,info信息如下:
{“content”:“spring-cloud-provider”,“author”:“tom”,“version”:“0.0.1-SNAPSHOT”}

访问http://cos6743:8081/actuator/beans可以获取所有beans列表
beans
访问http://cos6743:8081/actuator获取所有公开端点列表
{
“_links”: {
“self”: {
“href”: “http://cos6743:8081/actuator”,
“templated”: false
},
“beans”: {
“href”: “http://cos6743:8081/actuator/beans”,
“templated”: false
},
“health”: {
“href”: “http://cos6743:8081/actuator/health”,
“templated”: false
},
“info”: {
“href”: “http://cos6743:8081/actuator/info”,
“templated”: false
}
}
}
待续:因没有引入Spring Security,故此health只返回{“status”:“UP”},认证后可以显示全部信息

猜你喜欢

转载自blog.csdn.net/weixin_44153121/article/details/86687341