在平常開發與測試中有一些Spring Cloud 的相關的組件如 eureka、configserver、zipkin、hystrixdashboard等相對來講不容易發生變更,這裏就介紹一種Spring 官方爲咱們提供的開箱即用的 Spring Boot Cloud CLI 只須要一條命令就能夠啓動這些相關的組件服務。html
Spring Boot Cloud CLI 官方是這樣描述的:git
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).
翻譯以後:spring
Springbootcli爲SpringCloud提供了Springboot命令行功能。您能夠編寫groovy腳原本運行Spring Cloud組件應用程序(例如@enableurekaserver)。您還能夠輕鬆地執行加密和解密等操做,以支持具備機密配置值的SpringCloud配置客戶機。使用啓動器cli,您能夠從命令行方便地同時啓動諸如eureka、zipkin、config server等服務(在開發時很是有用)。
官方提供的最新版本是2.2.0.BUILD-SNAPSHOT,因爲版本依賴的問題在運行時出了一些問題,而後將版本改成了:數據庫
1.1 須要先安裝Spring CLI
已liunx 爲例:windows
首先將剛纔下載的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%
更多安裝方式參考官方文檔app
檢查是否安裝成功:ssh
spring --version
1.2安裝Spring Cloud CLImaven
命令以下:
spring install org.springframework.cloud:spring-cloud-cli:1.3.2.RELEASE
檢查是否安裝成功:
spring cloud --version
在開發中運行Spring Cloud Services。
Launcher CLI可用於從命令行運行Eureka,Config Server等經常使用服務。列出您能夠執行的可用服務spring cloud --list,並僅啓動一組默認服務spring cloud。要選擇要部署的服務,只需在命令行中列出它們,例如:
spring cloud eureka configserver h2 zipkin
支持的可部署的服務摘要:
Service | Name | Address | Description |
---|---|---|---|
eureka | Eureka Server | http://localhost:8761 | Eureka服務器用於服務註冊和發現 |
configserver | Config Server | http://localhost:8888 | 配置服務並從本地目錄./launcher提供配置 |
h2 | H2 Database | http://localhost:9095 (console), jdbc:h2:tcp://localhost:9096/{data} | h2數據庫 |
kafka | Kafka Broker | http://localhost:9091 (actuator endpoints), localhost:9092 | |
hystrixdashboard | Hystrix Dashboard | http://localhost:7979 | 斷路器 |
dataflow | Dataflow Server | http://localhost:9393 | |
zipkin | Zipkin Server | http://localhost:9411 | 用於可視化跟蹤 |
stubrunner | Stub 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
能夠在./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
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'] } }
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 + ...