Spring Boot Cloud CLI - 快速上手

导读

在日常开发与测试中有一些Spring Cloud 的相关的组件如 eureka、configserver、zipkin、hystrixdashboard等相对来说不容易发生变动,这里就介绍一种Spring 官方为我们提供的开箱即用的 Spring Boot Cloud CLI 只需要一条命令就可以启动这些相关的组件服务。

Spring Boot Cloud CLI 是什么?

Spring Boot Cloud CLI 官方是这样描述的:

Spring Boot CLI provides Spring Boot command line features for Spring Cloud. You can write Groovy scripts to run Spring Cloud component applications (e.g. @EnableEurekaServer). You can also easily do things like encryption and decryption to support Spring Cloud Config clients with secret configuration values. With the Launcher CLI you can launch services like Eureka, Zipkin, Config Server conveniently all at once from the command line (very useful at development time).

翻译之后:

Springbootcli为SpringCloud提供了Springboot命令行功能。您可以编写groovy脚本来运行Spring Cloud组件应用程序(例如@enableurekaserver)。您还可以轻松地执行加密和解密等操作,以支持具有机密配置值的SpringCloud配置客户机。使用启动器cli,您可以从命令行方便地同时启动诸如eureka、zipkin、config server等服务(在开发时非常有用)。

Spring Boot Cloud CLI 如何使用?

官方提供的最新版本是2.2.0.BUILD-SNAPSHOT,由于版本依赖的问题在运行时出了一些问题,然后将版本改为了:

1、安装:

1.1 需要先安装Spring CLI
已liunx 为例:

首先将刚才下载的Spring CLI v1.5.18.RELEASE 解压 ,然后命令设置如下:

export PATH=${PATH}:/spring-boot-cli-1.5.18.RELEASE/bin

windows:

set PATH=D:\spring-boot-cli-1.5.18.RELEASE\bin;%PATH%

更多安装方式参考官方文档

检查是否安装成功:

spring --version

1.2安装Spring Cloud CLI

命令如下:

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

检查是否安装成功:

spring cloud --version

2、运行服务

在开发中运行Spring Cloud Services。
Launcher CLI可用于从命令行运行Eureka,Config Server等常用服务。列出您可以执行的可用服务spring cloud –list,并仅启动一组默认服务spring cloud。要选择要部署的服务,只需在命令行中列出它们,例如:

 spring cloud eureka configserver h2 zipkin

支持的可部署的服务摘要:

ServiceNameAddressDescription
eurekaEureka Server http://localhost:8761Eureka服务器用于服务注册和发现
configserverConfig Server http://localhost:8888配置服务并从本地目录./launcher提供配置
h2H2 Database http://localhost:9095 (console), jdbc:h2:tcp://localhost:9096/{data}h2数据库
kafkaKafka Broker http://localhost:9091 (actuator endpoints), localhost:9092
hystrixdashboardHystrix Dashboard http://localhost:7979断路器
dataflowDataflow Server http://localhost:9393
zipkinZipkin Server http://localhost:9411用于可视化跟踪
stubrunnerStub Runner Boot http://localhost:8750

获取帮助

spring help cloud

可以使用具有相同名称的本地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

3、 添加其他应用

可以在./config目录下添加自己定义的程序,例如:
./config/my-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 hystrixdashboard kafka stubrunner zipkin

4、编写Groovy脚本和运行应用程序

Spring Cloud CLI支持大多数Spring Cloud声明性功能,例如@Enable*注释类。例如,这是一个功能齐全的Eureka服务器

app.groovy

@EnableEurekaServer
class Eureka {}

您可以从命令行运行,如下所示

spring run app.groovy

要包含其他依赖项,通常只需添加适当的启用特征的注释即可,例如@EnableConfigServer, @EnableOAuth2Sso或@EnableEurekaClient。要手动包含依赖项,您可以使用@Grab特殊的“Spring Boot”短样式工件坐标,即只使用工件ID(不需要组或版本信息),例如设置客户端应用程序以侦听AMQP来自Spring CLoud Bus的管理活动:
app.groovy

@Grab('spring-cloud-starter-bus-amqp')
@RestController
class Service {
  @RequestMapping('/')
  def home() { [message: 'Hello'] }
}

5、加密和解密

Spring Cloud CLI附带“加密”和“解密”命令。两者都接受相同形式的参数,并将键指定为必需的“–key”,例如

$ spring encrypt mysecret --key foo
682bc583f4641835fa2db009355293665d2647dade3375c0ee201de2a49f7bda
$ spring decrypt --key foo 682bc583f4641835fa2db009355293665d2647dade3375c0ee201de2a49f7bda
mysecret

要在文件中使用密钥(例如,用于加密的RSA公钥),请在密钥值前加上“@”并提供文件路径,例如

$ spring encrypt mysecret --key @ $ {HOME} 
/.ssh / id_rsa.pub AQAjPgt3eFZQXwt8tsHAVv / QHiY5sI2dRcR + ...

参考资料

getting-started-installing-the-cli
Spring Boot Cloud CLI

    原文作者:yangrd
    原文地址: https://segmentfault.com/a/1190000020103076
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞