簡介
應用監控 和 管理的功能
通過 http JMX SSH 協議操作
actuator EndPoint的列表
autoconfig 所有自動配置
beans bean的信息
configprops 所有的配置屬性
dump 當前應用線程的狀態信息
env 當前環境信息
health 當前應用的健康狀態
info 當前應用的信息
metrics 各項指標信息
mappings 所有的映射的路徑
shutdown 關閉當前應用
trace 顯示追蹤信息
pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
http://localhost:8080/actuator
{
"links": [
{
"rel": "self",
"href": "http://localhost:8080/actuator"
},
{
"rel": "mappings",
"href": "http://localhost:8080/mappings" 映射信息
},
{
"rel": "shutdown",
"href": "http://localhost:8080/shutdown" 關閉(POST提交)
},
{
"rel": "autoconfig",
"href": "http://localhost:8080/autoconfig" 自動配置
},
{
"rel": "info",
"href": "http://localhost:8080/info" 當前應用信息
},
{
"rel": "env",
"href": "http://localhost:8080/env"
},
{
"rel": "trace",
"href": "http://localhost:8080/trace" 顯示追蹤信息
},
{
"rel": "beans",
"href": "http://localhost:8080/beans" 所有的bean
},
{
"rel": "metrics",
"href": "http://localhost:8080/metrics" 各項指標信息
},
{
"rel": "dump",
"href": "http://localhost:8080/dump" 線程的狀態
},
{
"rel": "status",
"href": "http://localhost:8080/status"
},
{
"rel": "health",
"href": "http://localhost:8080/health" 健康狀態
},
{
"rel": "configprops",
"href": "http://localhost:8080/configprops" 所有的配置屬性
}
]
}
endpoints.shutdown.enabled=true 開啓訪問關閉,post提交
定製端點
endpoints.beans.id=mybeans #變成了 http://localhost:8080/mybeans
endpoints.beans.enabled=false #關閉上面的bean端點
endpoints.enabled=false #關閉所有的端點
endpoints.beans.enabled=true #開戶所需的端點
management.context-path=/manage http://localhost:8080/manage/beans
management.port=8081 #改變端口的訪問地址
management.port=-1 #關閉http端點
自定義端點
@Service
public class StatusService {
private String status;
}
//可以通過配置文件進行配置
@ConfigurationProperties(prefix = "endpoints.status", ignoreUnknownFields = false) //1
public class StatusEndPoint extends AbstractEndpoint<String> implements ApplicationContextAware{//2 繼承Endpoint接口的抽象實現。實現 意識
ApplicationContext context;
public StatusEndPoint() {
super("status");
}
@Override
public String invoke() { //3 重寫
StatusService statusService = context.getBean(StatusService.class);
return "The Current Status is :"+statusService.getStatus();
}
@Override
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
this.context = arg0;
}
}
@SpringBootApplication
@RestController
public class DemoApplication {
@Autowired
StatusService statusService;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
public Endpoint<String> status() {
Endpoint<String> status = new StatusEndPoint();
return status;
}
@RequestMapping("/change")
public String changeStatus(String status){
statusService.setStatus(status);
return "OK";
}
}
http://localhost:8080/change?status=running
http://localhost:8080/status 返回:The Current Status is :running
自定義HealthIndicator
Health信息都是從 ApplicationContext中所有的 HealthIndicator的Bean中收集的。
Spring中內置了一些 HealthIndicator
檢測 ElasticSearch集羣是否運行
檢測JMS消息代理是否運行
郵件服務器
MongoDB
RabbitMQ
Redis
@Component
public class StatusHealth implements HealthIndicator {//1
@Autowired
StatusService statusService;
@Override
public Health health() {
String status = statusService.getStatus();
if(status == null||!status.equals("running")){
return Health.down().withDetail("Error", "Not Running").build(); //2
} //status爲null 或 status 不爲 running 時構造失敗。其他爲運行成功
return Health.up().build(); //3
}
}
http://localhost:8080/health
{
"status": "UP",
"statusHealth": {
"status": "UP"
},
"diskSpace": {
"status": "UP",
"total": 302643146752,
"free": 217826213888,
"threshold": 10485760
},
"links": [
{
"rel": "self",
"href": "http://localhost:8080/health"
}
]
}
當改成其他的時候,變成down
{
"status": "DOWN",
"statusHealth": {
"status": "DOWN",
"Error": "Not Running"
}
}
JMX
是對應用進行監控和管理,裝了java即可用。
控制檯輸入。 jconsole
在頁面選擇 MBean ,找到 org.springframework.boot ,Endpoint
healthEndpoint 和 status (上面自定義的)
ssh
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-remote-shell</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
shell.auth.simple.user.name=wyf #默認用戶名爲user,密碼會默認打印
shell.auth.simple.user.password=wyf
端口默認爲2000
help 獲得命令的列表
metrics
NAME VALUE
-----------------------------------------------------------------------------------------
mem 565248
mem.free 322311
processors 12
endpoint list
endpoint invoke health #執行某一端點
endpoint invoke health
{status=UP, diskSpace={status=UP, total=302643146752, free=217814908928, threshold=10485760}}
使用 Groovy語言來編制命令,運行於 JVM的動態語言。Spring Boot也可用 Groovy語言開發。
自定義指令
commands/hello.groovy
package commands
import org.crsh.cli.Command
import org.crsh.cli.Usage
import org.crsh.command.InvocationContext
class hello {
@Usage("Say Hello") #命令的用途
@Command #當前是一個CRaSH命令
def main(InvocationContext context) {
def bootVersion = context.attributes['spring.boot.version']; 1.3.0.M4
def springVersion = context.attributes['spring.version'] 4.2.0.RELEASE
return "Hello,your Spring Boot version is "+bootVersion +",your Spring Framework version is "+springVersion
}
}
//2020 04 19 看
2.2.6 spring boot 版本
5.2.5 spring framework版本
Hoxton SR3 cloud 版本