当前位置: 移动技术网 > IT编程>开发语言>Java > Spring Cloud CLI简单介绍

Spring Cloud CLI简单介绍

2019年07月19日  | 移动技术网IT编程  | 我要评论

怀孕34周,淘米儿童浏览器,正青春演员表

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

2、然后安装最新的稳定版cloud cli:

$ spring installorg.springframework.cloud:spring-cloud-cli:1.3.2.release

3、最后验证cloud cli:

$ spring cloud --version

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

3.默认服务和配置

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

在http://localhost:8888上启动cloud config服务器:

$ spring cloud configserver

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

$ spring cloud eureka

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

$ spring cloud h2

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

$ spring cloud kafka

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

$ spring cloud zipkin

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

$ spring cloud dataflow

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

$ spring cloud hystrixdashboard

列出可部署的应用服务:

$ spring cloud --list

帮助命令:

$ spring help cloud

有关这些命令的更多详细信息,请查看。

4.使用yml自定义云服务

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

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

这是一个简单的配置文件,我们可以使用它来启动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

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

5.运行自定义groovy脚本

自定义组件可以用groovy编写并高效部署,因为cloud cli可以编译和部署groovy代码。

这是一个非常简单的rest api实现示例:

@restcontroller
@requestmapping('/api')
class api {
 @getmapping('/get')
 def get() { [message: 'hello'] }
}

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

$ spring run restapi.groovy

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

{"message":"hello"}

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

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

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

'my_value'现在将被加密为:

c93cb36ce1d09d7d62dffd156ef742faaa56f97f135ebd05e90355f80290ce6b

可通过命令行进行解密:

$ spring decrypt --key my_key c93cb36ce1d09d7d62dffd156ef742faaa56f97f135ebd05e90355f80290ce6b

我们现在还可以在配置yaml或属性文件中使用加密值,在加载时它将由cloud config server自动解密:

encrypted_credential: "{cipher}c93cb36ce1d09d7d62dffd156ef742faaa56f97f135ebd05e90355f80290ce6b"

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

7.结论

我们在这里专注于spring boot cloud cli的介绍。有关更多的信息,请查看。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网