# Spring 引导云 CLI
Spring Boot CLI 为[Spring Boot](https://projects.spring.io/spring-boot)提供了[Spring Cloud](https://github.com/spring-cloud)命令行功能。你可以编写 Groovy 脚本来运行 Spring Cloud组件应用程序(例如`@EnableEurekaServer`)。你还可以轻松地进行加密和解密等操作,以支持具有秘密配置值的云配置客户机。通过 Launcher CLI,你可以方便地从命令行同时启动像 Eureka、Zipkin、Config Server 这样的服务(在开发时非常有用)。
| |Spring Cloud是在非限制性的 Apache2.0 许可下发布的。如果你想对文档的这一部分做出贡献,或者你发现了一个错误,请在[github](https://github.com/spring-cloud/spring-cloud-cli)上找到项目中的源代码和问题追踪器。|
|---|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
## [安装](#_installation)
要安装,请确保你有[Spring Boot CLI](https://github.com/spring-projects/spring-boot)(2.0.0 或更好):
```
$ spring version
Spring CLI v2.2.3.RELEASE
```
例如,对于 SDKMAN 个用户
```
$ sdk install springboot 2.2.3.RELEASE
$ sdk use springboot 2.2.3.RELEASE
```
并安装 Spring Cloud插件
```
$ mvn install
$ spring install org.springframework.cloud:spring-cloud-cli:2.2.0.RELEASE
```
| |**先决条件:**要使用加密和解密功能
,你需要在你的 JVM 中安装全强度 JCE(默认情况下不存在)。
你可以从 Oracle 下载“Java Cryptography Extension(JCE)Unlimited Strength Juridictory Policy Files”
,并遵循安装说明(基本上将 JRElib/security 目录中的 2 个策略文件
替换为你下载的文件)。|
|---|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
## [Running Spring Cloud Services in Development](#_running_spring_cloud_services_in_development)
启动器 CLI 可用于从命令行运行公共服务,如 Eureka、Config Server 等。要列出你可以执行`spring cloud --list`的可用服务,并仅启动`spring cloud`的默认服务集。要选择要部署的服务,只需在命令行中列出它们,例如。
```
$ spring cloud eureka configserver h2 kafka stubrunner zipkin
```
支持的可部署程序摘要:
| Service | Name | Address |说明|
|------------|----------------|---------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| eureka | Eureka Server | [http://localhost:8761](http://localhost:8761) |服务注册和发现的 Eureka 服务器。默认情况下,所有其他服务都会显示在其目录中。|
|configserver| Config Server | [http://localhost:8888](http://localhost:8888) |Spring 运行在“本机”配置文件中的云配置服务器和来自本地目录的服务配置。/Launcher|
| h2 | H2 Database |[http://localhost:9095](http://localhost:9095) (console), jdbc:h2:tcp://localhost:9096/{data}|关系数据库服务。在连接时使用`{data}`的文件路径(例如`./target/test`)。请记住,你可以添加`;MODE=MYSQL`或`;MODE=POSTGRESQL`以与其他服务器类型的兼容性连接。|
| kafka | Kafka Broker | [http://localhost:9091](http://localhost:9091) (actuator endpoints), localhost:9092 | |
| dataflow |Dataflow Server | [http://localhost:9393](http://localhost:9393) |Spring 带有 ui at/admin-ui 的云数据流服务器。将 DataFlow Shell 连接到根路径上的目标。|
| zipkin | Zipkin Server | [http://localhost:9411](http://localhost:9411) |Zipkin 服务器与 UI 可视化的痕迹。存储在内存中的跨数据,并通过 JSON 数据的 HTTP POST 接受它们。|
| stubrunner |Stub Runner Boot| [http://localhost:8750](http://localhost:8750) |下载 WiRemock 存根,启动 WiRemock,并用存储的存根向启动的服务器提供信息。传递`stubrunner.ids`以传递存根坐标,然后转到`[http://localhost:8750/stubs](http://localhost:8750/stubs)`。|
这些应用程序中的每一个都可以使用同名的本地 YAML 文件进行配置(在当前工作目录或名为“config”的子目录中或在`~/.spring-cloud`中)。例如,在`configserver.yml`中,你可能想做这样的事情来为后端定位一个本地 Git 存储库:
configserver.yml
```
spring:
profiles:
active: git
cloud:
config:
server:
git:
uri: file://${user.home}/dev/demo/config-repo
```
例如,在 Stub Runner 应用程序中,你可以通过以下方式从本地`.m2`获取 stub。
Stubrunner.yml
```
stubrunner:
workOffline: true
ids:
- com.example:beer-api-producer:+:9876
```
### [添加其他应用程序](#_adding_additional_applications)
可以将其他应用程序添加到`./config/cloud.yml`(而不是`./config.yml`,因为这将替换默认值),例如使用
config/cloud.yml
```
spring:
cloud:
launcher:
deployables:
source:
coordinates: maven://com.example:source:0.0.1-SNAPSHOT
port: 7000
sink:
coordinates: maven://com.example:sink:0.0.1-SNAPSHOT
port: 7001
```
当你列出应用程序时:
```
$ spring cloud --list
source sink configserver dataflow eureka h2 kafka stubrunner zipkin
```
(请注意列表开头的附加应用)。
## [编写 Groovy 脚本并运行应用程序](#_writing_groovy_scripts_and_running_applications)
Spring Cloud CLI 具有对 Spring Cloud的大多数声明性功能的支持,例如`@Enable*`类的注释。例如,这里有一个功能齐全的 Eureka 服务器
App.Groovy
```
@EnableEurekaServer
class Eureka {}
```
你可以像这样从命令行运行它
```
$ spring run app.groovy
```
要包含额外的依赖关系,通常只需添加适当的支持特性的注释就足够了,例如`@EnableConfigServer`,`@EnableOAuth2Sso`或`@EnableEurekaClient`。要手动包含依赖项,你可以使用`@Grab`和特殊的“ Spring 引导”短样式工件坐标,即只使用工件 ID(不需要组或版本信息),例如,设置一个客户端应用程序,以便在 AMQP 上监听来自 Spring Cloud总线的管理事件:
App.Groovy
```
@Grab('spring-cloud-starter-bus-amqp')
@RestController
class Service {
@RequestMapping('/')
def home() { [message: 'Hello'] }
}
```
## [加密和解密](#_encryption_and_decryption)
Spring cloud cli 带有一个“加密”和一个“解密”命令。两者都接受相同形式的参数,其中一个键被指定为强制性的“--key”,例如。
```
$ spring encrypt mysecret --key foo
682bc583f4641835fa2db009355293665d2647dade3375c0ee201de2a49f7bda
$ spring decrypt --key foo 682bc583f4641835fa2db009355293665d2647dade3375c0ee201de2a49f7bda
mysecret
```
要在文件中使用密钥(例如,用于 encyption 的 RSA 公钥),请在键值前加上“@”并提供文件路径。
```
$ spring encrypt mysecret --key @${HOME}/.ssh/id_rsa.pub
AQAjPgt3eFZQXwt8tsHAVv/QHiY5sI2dRcR+...
```