spring-cloud-cli.md 7.8 KB
Newer Older
茶陵後's avatar
茶陵後 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
# Spring Boot Cloud CLI

## [Installation](#_installation)

To install, make
sure you have[Spring Boot CLI](https://github.com/spring-projects/spring-boot)(2.0.0 or better):

```
$ spring version
Spring CLI v2.2.3.RELEASE
```

E.g. for SDKMan users

```
$ sdk install springboot 2.2.3.RELEASE
$ sdk use springboot 2.2.3.RELEASE
```

and install the Spring Cloud plugin

```
$ mvn install
$ spring install org.springframework.cloud:spring-cloud-cli:2.2.0.RELEASE
```

|   |**Prerequisites:** to use the encryption and decryption features<br/>you need the full-strength JCE installed in your JVM (it’s not there by default).<br/>You can download the "Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files"<br/>from Oracle, and follow instructions for installation (essentially replace the 2 policy files<br/>in the JRE lib/security directory with the ones that you downloaded).|
|---|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

## [Running Spring Cloud Services in Development](#_running_spring_cloud_services_in_development)

The Launcher CLI can be used to run common services like Eureka,
Config Server etc. from the command line. To list the available
services you can do `spring cloud --list`, and to launch a default set
of services just `spring cloud`. To choose the services to deploy,
just list them on the command line, e.g.

```
$ spring cloud eureka configserver h2 kafka stubrunner zipkin
```

Summary of supported deployables:

|  Service   |      Name      |                                           Address                                           |                                                                                                     Description                                                                                                      |
|------------|----------------|---------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|   eureka   | Eureka Server  |                       [http://localhost:8761](http://localhost:8761)                        |                                                   Eureka server for service registration and discovery. All the other services show up in its catalog by default.                                                    |
|configserver| Config Server  |                       [http://localhost:8888](http://localhost:8888)                        |                                               Spring Cloud Config Server running in the "native" profile and serving configuration from the local directory ./launcher                                               |
|     h2     |  H2 Database   |[http://localhost:9095](http://localhost:9095) (console), jdbc:h2:tcp://localhost:9096/{data}| Relation database service. Use a file path for `{data}` (e.g. `./target/test`) when you connect. Remember that you can add `;MODE=MYSQL` or `;MODE=POSTGRESQL` to connect with compatibility to other server types.  |
|   kafka    |  Kafka Broker  |     [http://localhost:9091](http://localhost:9091) (actuator endpoints), localhost:9092     |                                                                                                                                                                                                                      |
|  dataflow  |Dataflow Server |                       [http://localhost:9393](http://localhost:9393)                        |                                                        Spring Cloud Dataflow server with UI at /admin-ui. Connect the Dataflow shell to target at root path.                                                         |
|   zipkin   | Zipkin Server  |                       [http://localhost:9411](http://localhost:9411)                        |                                                Zipkin Server with UI for visualizing traces. Stores span data in memory and accepts them via HTTP POST of JSON data.                                                 |
| stubrunner |Stub Runner Boot|                       [http://localhost:8750](http://localhost:8750)                        |Downloads WireMock stubs, starts WireMock and feeds the started servers with stored stubs. Pass `stubrunner.ids` to pass stub coordinates and then go to `[http://localhost:8750/stubs](http://localhost:8750/stubs)`.|

Each of these apps can be configured using a local YAML file with the same name (in the current
working directory or a subdirectory called "config" or in `~/.spring-cloud`). E.g. in `configserver.yml` you might want to
do something like this to locate a local git repository for the backend:

configserver.yml

```
spring:
  profiles:
    active: git
  cloud:
    config:
      server:
        git:
          uri: file://${user.home}/dev/demo/config-repo
```

E.g. in Stub Runner app you could fetch stubs from your local `.m2` in the following way.

stubrunner.yml

```
stubrunner:
  workOffline: true
  ids:
    - com.example:beer-api-producer:+:9876
```

### [Adding Additional Applications](#_adding_additional_applications)

Additional applications can be added to `./config/cloud.yml` (not`./config.yml` because that would replace the defaults), e.g. with

config/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
```

when you list the apps:

```
$ spring cloud --list
source sink configserver dataflow eureka h2 kafka stubrunner zipkin
```

(notice the additional apps at the start of the list).

## [Writing Groovy Scripts and Running Applications](#_writing_groovy_scripts_and_running_applications)

Spring Cloud CLI has support for most of the Spring Cloud declarative
features, such as the `@Enable*` class of annotations. For example,
here is a fully functional Eureka server

app.groovy

```
@EnableEurekaServer
class Eureka {}
```

which you can run from the command line like this

```
$ spring run app.groovy
```

To include additional dependencies, often it suffices just to add the
appropriate feature-enabling annotation, e.g. `@EnableConfigServer`,`@EnableOAuth2Sso` or `@EnableEurekaClient`. To manually include a
dependency you can use a `@Grab` with the special "Spring Boot" short
style artifact co-ordinates, i.e. with just the artifact ID (no need
for group or version information), e.g. to set up a client app to
listen on AMQP for management events from the Spring CLoud Bus:

app.groovy

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

## [Encryption and Decryption](#_encryption_and_decryption)

The Spring Cloud CLI comes with an "encrypt" and a "decrypt"
command. Both accept arguments in the same form with a key specified
as a mandatory "--key", e.g.

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

To use a key in a file (e.g. an RSA public key for encyption) prepend
the key value with "@" and provide the file path, e.g.

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