7.2 连接 Actuator 的远程 shell
Actuator 通过 REST 端点提供了不少非常有用的信息。另一个深入运行中应用程序内部的方式是使用远程 shell。Spring Boot 集成了 CRaSH,一种能嵌入任意 Java 应用程序的 shell。Spring Boot 还扩展了 CRaSH,添加了不少 Spring Boot 特有的命令,提供了与 Actuator 端点类似的功能。
要使用远程 shell,只需加入远程 shell 的起步依赖即可。你需要这样的 Gradle 依赖:
compile("org.springframework.boot:spring-boot-starter-remote-shell")
如果用 Maven 构建项目,你需要在 pom.xml 文件里添加如下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-remote-shell</artifactId>
</dependency>
如果要用 Spring Boot CLI 来运行你所开发的应用程序,则需要如下 @Grab
注解:
@Grab("spring-boot-starter-remote-shell")
添加了远程 shell 依赖后,就可以构建并运行应用程序了。在启动的时候,可以看到要写进日志的一行密码。这行密码所在的行大概是这样的:
Using default security password: efe30c70-5bf0-43b1-9d50-c7a02dda7d79
与这个密码搭配使用的用户名是 user。密码本身是随机生成的,每次运行应用程序时都会有所变化。
现在你可以通过 SSH 工具连接 shell 了,它监听的端口号是 2000。如果你用的是 Unix 的 ssh
命令,那么它看起来大概是这样的:
~% ssh user@localhost -p 2000
Password authentication
Password:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.3.0.RELEASE) on habuma.local
>
太棒了!你已经连上 shell 了。现在应该做什么?
远程 shell 提供了 24 个可以在运行应用程序上下文中执行的命令,其中大部分都是 CRaSH 自带的。但 Spring Boot 也添加了一些。表 7-4 列出了这些 Spring Boot 特有的命令。
表 7-4 Spring Boot 提供的 CRaSH shell 命令
命令 | 描述 |
---|---|
| 生成自动配置说明报告,和/autoconfig 端点输出的内容类似,只是把 JSON 换成了纯文本 |
| 列出 Spring 应用程序上下文里的 Bean,与/beans 端点输出的内容类似 |
| 调用 Actuator 端点 |
| 显示 Spring Boot 的度量信息,与/metrics 端点类似,但显示的是实时更新的数据 |
让我们看看如何使用这些 Spring Boot 添加的 shell 命令。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论