Introduction to Spring Cloud CLI – Spring Cloud CLI简介

最后修改: 2017年 11月 22日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.介绍

In this article, we take a look at Spring Boot Cloud CLI (or Cloud CLI for short). The tool provides a set of command line enhancements to the Spring Boot CLI that helps in further abstracting and simplifying Spring Cloud deployments.

在这篇文章中,我们看一下Spring Boot Cloud CLI(简称Cloud CLI)。该工具为Spring Boot CLI提供了一套命令行增强功能,有助于进一步抽象和简化Spring Cloud部署。

The CLI was introduced in late 2016 and allows quick auto-configuration and deployment of standard Spring Cloud services using a command line, .yml configuration files, and Groovy scripts.

CLI于2016年底推出,允许使用命令行、.yml配置文件和Groovy脚本快速自动配置和部署标准Spring Cloud服务。

2. Set Up

2.设置

Spring Boot Cloud CLI 1.3.x requires Spring Boot CLI 1.5.x, so make sure to grab the latest version of Spring Boot CLI from Maven Central (installation instructions) and the most recent version of the Cloud CLI from Maven Repository (the official Spring repository)!

Spring Boot Cloud CLI 1.3.x需要Spring Boot CLI 1.5.x,因此请确保从Maven Central获取最新版本的Spring Boot CLI(安装说明),并从Maven资源库Spring官方资源库)获取最新版本的Cloud CLI!

To make sure the CLI is installed and ready to use, simply run:

要确保CLI已经安装并可以使用,只需运行。

$ spring --version

After verifying your Spring Boot CLI installation, install the latest stable version of Cloud CLI:

在验证了你的Spring Boot CLI安装后,安装最新的稳定版本的Cloud CLI。

$ spring install org.springframework.cloud:spring-cloud-cli:1.3.2.RELEASE

Then verify the Cloud CLI:

然后验证云端CLI。

$ spring cloud --version

Advanced installation features can be found on the official Cloud CLI page!

高级安装功能可以在官方的云计算CLI 网页上找到!

3. Default Services and Configuration

3.默认服务和配置

The CLI provides seven core services that can be run and deployed with single line commands.

CLI提供了七个核心服务,可以通过单行命令来运行和部署。

To launch a Cloud Config server on http://localhost:8888:

要在http://localhost:8888上启动一个云配置服务器。

$ spring cloud configserver

To start a Eureka server on http://localhost:8761:

要在http://localhost:8761上启动一个Eureka服务器。

$ spring cloud eureka

To initiate an H2 server on http://localhost:9095:

要在http://localhost:9095上启动一个H2服务器。

$ spring cloud h2

To launch a Kafka server on http://localhost:9091:

要在http://localhost:9091上启动一个Kafka服务器。

$ spring cloud kafka

To start a Zipkin server on http://localhost:9411:

要在http://localhost:9411上启动一个Zipkin服务器。

$ spring cloud zipkin

To launch a Dataflow server on http://localhost:9393:

要在 http://localhost:9393 上启动一个 Dataflow 服务器。

$ spring cloud dataflow

To start a Hystrix dashboard on http://localhost:7979:

要在http://localhost:7979上启动一个Hystrix仪表板。

$ spring cloud hystrixdashboard

List currently running cloud services:

列出当前运行的云服务。

$ spring cloud --list

The handy help command:

方便的帮助命令。

$ spring help cloud

For more details about these commands, please check out the official blog.

关于这些命令的更多细节,请查看官方博客

4. Customizing Cloud Services With YML

4.用YML定制云服务

Each of the services that are deployable through the Cloud CLI can also be configured using correspondingly-named .yml files:

可通过云计算CLI部署的每个服务也可以使用相应的命名.yml文件进行配置。

spring:
  profiles:
    active: git
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo

This constitutes a simple configuration file that we can use for launching the Cloud Config Server.

这构成了一个简单的配置文件,我们可以用它来启动云配置服务器。

We can, for example, specify a Git repository as the URI source that will be automatically cloned and deployed when we issue the ‘spring cloud configserver’ command.

例如,我们可以指定一个Git仓库作为URI源,当我们发出‘spring cloud configserver’命令时,该仓库将被自动克隆和部署。

Cloud CLI uses the Spring Cloud Launcher under the hood. That means that Cloud CLI supports most of the Spring Boot configuration mechanisms. Here’s the official list of Spring Boot properties.

Cloud CLI 在引擎盖下使用 Spring Cloud Launcher。这意味着Cloud CLI 支持大多数 Spring Boot 配置机制。 以下是Spring Boot 属性的官方列表。

Spring Cloud configuration conforms to the ‘spring.cloud…‘ convention. Settings for Spring Cloud and Spring Config Server can be found at this link.

Spring Cloud的配置符合‘spring.cloud…‘惯例。有关Spring Cloud和Spring配置服务器的设置可以在这个链接中找到。

We can also specify several different modules and services directly into the cloud.yml:

我们还可以直接在cloud.yml中指定几个不同的模块和服务。

spring:
  cloud:
    launcher:
      deployables:
        - name: configserver
          coordinates: maven://...:spring-cloud-launcher-configserver:1.3.2.RELEASE
          port: 8888
          waitUntilStarted: true
          order: -10
        - name: eureka
          coordinates: maven:/...:spring-cloud-launcher-eureka:1.3.2.RELEASE
          port: 8761

The cloud.yml allows custom services or modules to be added and the use of Maven and Git repositories to be used.

cloud.yml允许添加自定义服务或模块,并使用Maven和Git存储库。

5. Running Custom Groovy Scripts

5.运行自定义Groovy脚本

Custom components can be written in Groovy and deployed efficiently since Cloud CLI can compile and deploy Groovy code.

由于Cloud CLI可以编译和部署Groovy代码,因此可以用Groovy编写自定义组件并进行高效部署。

Here’s an example minimal REST API implementation:

这里有一个最小的REST API实现的例子。

@RestController
@RequestMapping('/api')
class api {
 
    @GetMapping('/get')
    def get() { [message: 'Hello'] }
}

Assuming that the script is saved as rest.groovy, we can launch our minimal server like this:

假设该脚本被保存为rest.groovy,我们可以像这样启动我们的最小服务器。

$ spring run rest.groovy

Pinging http://localhost:8080/api/get should reveal:

Pinging http://localhost:8080/api/get应该会发现。

{"message":"Hello"}

6. Encrypt/Decrypt

6.加密/解密

Cloud CLI also provides a tool for encryption and decryption (found in the package org.springframework.cloud.cli.command.*) that can be used directly through the command line or indirectly by passing a value to a Cloud Config Server endpoint.

云CLI还提供了一个用于加密和解密的工具(在org.springframework.cloud.cli.command.*包中找到),可以直接通过命令行使用,也可以通过向云配置服务器端点传递一个值间接使用。

Let’s set it up and see how to use it.

让我们把它设置好,看看如何使用它。

6.1. Setup

6.1.设置

Both Cloud CLI as well as Spring Cloud Config Server use org.springframework.security.crypto.encrypt.* for handling encrypt and decrypt commands.

Cloud CLI以及Spring Cloud Config Server都使用org.springframework.security.crypto.encrypt。* for handling加密和解密指令。

As such, both require the JCE Unlimited Strength Extension provided by Oracle here.

因此,两者都需要Oracle提供的JCE无限强度扩展这里

6.2. Encrypt and Decrypt by Command

6.2.通过命令加密和解密

To encrypt ‘my_value‘ via the terminal, invoke:

要通过终端对’my_value‘进行加密,调用。

$ spring encrypt my_value --key my_key

File paths can be substituted for the key name (e.g. ‘my_key‘ above) by using ‘@’ followed by the path (commonly used for RSA public keys):

文件路径可以通过使用路径后的’@’来代替钥匙名称(例如上面的’my_key‘)(通常用于RSA公钥)。

$ spring encrypt my_value --key @${WORKSPACE}/foos/foo.pub

my_value‘ will now be encrypted to something like:

my_value‘现在将被加密为类似的东西。

c93cb36ce1d09d7d62dffd156ef742faaa56f97f135ebd05e90355f80290ce6b

Furthermore, it will be stored in memory under key ‘my_key‘. This allows us to decrypt ‘my_key‘ back into’my_value‘ via command line:

此外,它将被存储在内存中的密钥’my_key‘下。这允许我们通过命令行将’my_key‘解密为’my_value‘。

$ spring decrypt --key my_key

We can also now use the encrypted value in a configuration YAML or properties file, where it will be automatically decrypted by the Cloud Config Server when loaded:

我们现在还可以在配置YAML或属性文件中使用加密的值,在加载时云配置服务器会自动解密。

encrypted_credential: "{cipher}c93cb36ce1d09d7d62dffd156ef742faaa56f97f135ebd05e90355f80290ce6b"

6.3. Encrypt and Decrypt With Config Server

6.3.使用配置服务器进行加密和解密

Spring Cloud Config Server exposes RESTful endpoints where keys and encrypted value pairs can be stored in the Java Security Store or memory.

Spring Cloud Config Server公开了RESTful端点,密钥和加密值对可以存储在Java Security Store或内存中。

For more information on how to correctly set up and configure your Cloud Config Server to accept symmetric or asymmetric encryption, please check out our article or the official docs.

有关如何正确设置和配置云配置服务器以接受对称非对称加密的更多信息,请查看我们的文章或官方文档

Once Spring Cloud Config Server is configured and up running using the ‘spring cloud configserver‘ command, you’ll be able to call its API:

一旦Spring Cloud Config Server被配置好并使用’spring cloud configserver‘命令运行,你就可以调用其API。

$ curl localhost:8888/encrypt -d mysecret
//682bc583f4641835fa2db009355293665d2647dade3375c0ee201de2a49f7bda
$ curl localhost:8888/decrypt -d 682bc583f4641835fa2db009355293665d2647dade3375c0ee201de2a49f7bda
//mysecret

7. Conclusion

7.结论

We’ve focused here on an introduction to Spring Boot Cloud CLI. For more information, please check out the official docs.

我们在这里重点介绍一下Spring Boot Cloud CLI。欲了解更多信息,请查看官方的docs

The configuration and bash examples used in this article are available over on GitHub.

本文中使用的配置和bash示例可在GitHub上获得over