• Spring Cloud CLI简介


    1.简介

    在本文中,我们将介绍Spring Boot Cloud CLI(或简称Cloud CLI)。该工具为Spring Boot CLI提供了一组命令行增强功能,有助于进一步抽象和简化Spring Cloud部署。

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

    2.安装

    Spring Boot Cloud CLI 1.3.x需要Spring Boot CLI 1.5.x,因此请务必从Maven Central获取最新版本的Spring Boot CLI (安装说明)以及Maven资源库中的最新版本的Cloud CLI (官方Spring存储库)!

    本文中使用的是spring-boot-cli 1.5.18.RELEASE,spring-cloud-cli 1.3.2.RELEASE。

    1、首先安装Spring Boot CLI并可以使用,验证只需运行:

    $ spring --version
    
    • 1

    2、然后安装最新的稳定版Cloud CLI:

    $ spring installorg.springframework.cloud:spring-cloud-cli:1.3.2.RELEASE
    
    • 1

    3、最后验证Cloud CLI:

    $ spring cloud --version
    
    • 1

    可以在官方Cloud CLI 页面上找到更多安装说明!

    3.默认服务和配置

    CLI提供七种核心服务,可以使用单行命令运行和部署。

    在http://localhost:8888上启动Cloud Config服务器:

    $ spring cloud configserver
    
    • 1

    http://localhost:8761上启动Eureka服务器:

    $ spring cloud eureka
    
    • 1

    在http://localhost:9095上启动H2服务器:

    1 $ spring cloud h2
    
    • 1

    在http://localhost:9091上启动Kafka服务器:

    $ spring cloud kafka
    
    • 1

    在http://localhost:9411上启动Zipkin服务器:

    $ spring cloud zipkin
    
    • 1

    在http://localhost:9393上启动Dataflow服务器:

    $ spring cloud dataflow
    
    • 1

    在http://localhost:7979上启动Hystrix仪表板:

    $ spring cloud hystrixdashboard
    
    • 1

    列出可部署的应用服务:

    $ spring cloud --list
    
    • 1

    帮助命令:

    $ spring help cloud
    
    • 1

    有关这些命令的更多详细信息,请查看官方博客

    4.使用YML自定义云服务

    还可以使用相应命名的.yml文件配置可通过Cloud CLI部署的云服务:

    spring:
      profiles:
        active: git
      cloud:
        config:
          server:
            git:
              uri: https://github.com/spring-cloud-samples/config-repo
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    这是一个简单的配置文件,我们可以使用它来启动Cloud Config Server。

    上面的例子中,我们指定一个Git存储库作为URI源,当我们发出’spring cloud configserver’命令时,它将被自动克隆和部署。

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

    Spring Cloud配置符合’spring.cloud … '约定。可以在此链接中找到Spring Cloud和Spring Config Server的设置。

    我们还可以直接在下面的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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    该yml允许通过使用Maven或Git库,添加自定义的服务或模块。

    5.运行自定义Groovy脚本

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

    这是一个非常简单的REST API实现示例:

    @RestController
    @RequestMapping('/api')
    class api {
        @GetMapping('/get')
        def get() { [message: 'Hello'] }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    假设脚本保存为restapi.groovy,我们可以像这样启动我们的服务:

    $ spring run restapi.groovy
    
    • 1

    访问http://localhost:8080/api/get应该显示:

    {"message":"Hello"}
    
    • 1

    6.加密/解密

    Cloud CLI还提供了加密和解密工具(可在org.springframework.cloud.cli.command.*中找到),可以直接通过命令行使用,也可以通过将值传递给Cloud Config Server端点来间接使用。

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

    6.1 安装

    Cloud CLI和Spring Cloud Config Server都使用org.springframework.security.crypto.encrypt.* 来处理加密和解密的命令。

    因此,都需要通过Oracle提供的JCE Unlimited Strength Extension,可在这里找到。

    6.2 命令行加密和解密

    要通过终端加密’ my_value ',请调用:

    $ spring encrypt my_value --key my_key
    
    • 1

    可以使用“@”后跟路径(通常用于RSA公钥)代表文件路径(替换上面的“ my_key ”):

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

    'my_value’现在将被加密为:

    c93cb36ce1d09d7d62dffd156ef742faaa56f97f135ebd05e90355f80290ce6b
    
    • 1

    可通过命令行进行解密:

    $ spring decrypt --key my_key c93cb36ce1d09d7d62dffd156ef742faaa56f97f135ebd05e90355f80290ce6b
    
    • 1

    我们现在还可以在配置YAML或属性文件中使用加密值,在加载时它将由Cloud Config Server自动解密:

    encrypted_credential: "{cipher}c93cb36ce1d09d7d62dffd156ef742faaa56f97f135ebd05e90355f80290ce6b"
    
    • 1

    6.3 使用Config Server加密和解密

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

    有关如何正确设置和配置Cloud Config Server以接受对称或非对称加密的详细信息,请查看官方文档

    使用“ spring cloud configserver ”命令配置并运行Spring Cloud Config Server后,您就可以调用其API:

    $ curl localhost:8888/encrypt-d mysecret
    //682bc583f4641835fa2db009355293665d2647dade3375c0ee201de2a49f7bda$ 
    curl localhost:8888/decrypt-d 682bc583f4641835fa2db009355293665d2647dade3375c0ee201de2a49f7bda
    //mysecret
    
    • 1
    • 2
    • 3
    • 4

    7.结论

    我们在这里专注于Spring Boot Cloud CLI的介绍。有关更多的信息,请查看官方文档

  • 相关阅读:
    详解跨域处理
    vim 从嫌弃到依赖(19)——替换
    洛谷P1331 海战 题解
    数据挖掘实验(Apriori,fpgrowth)
    无需手动部署的正式版comfyUI是否就此收费?开源等同免费?
    windows文件和目录相关命令
    【docker】学习笔记
    COMO-ViT论文阅读笔记
    Vivado 2017.04版本安装教程
    Compose原理-视图和数据双向绑定的原理
  • 原文地址:https://blog.csdn.net/segegefe/article/details/126496799