领先一步
VMware 提供培训和认证,助你加速前进。
了解更多Spring Cloud 的入门可能令人望而却步。如果你看过 Josh Long 精彩的云原生 Java 演示文稿,你会注意到在你的应用程序运行之前,你需要创建几个支持应用程序。
作为从 Spring XD 过渡到Spring Cloud Dataflow 的一部分,其中一个提取出的项目叫做Spring Cloud Deployer。Deployer 允许在各种平台上启动应用程序,包括在开发人员的机器上启动。
我们希望简化用户尝试使用 Spring Cloud 的能力,因此诞生了 spring cloud
命令。
spring cloud
命令会自动运行在 Spring Cloud 环境中运行应用程序所需的一些支持服务器,例如 Config Server 和 Eureka。
第一步是安装Spring Boot CLI(即 spring
命令)。所需的最低版本是 1.4.1.RELEASE
。我个人更喜欢SDKMAN 方法。
$ sdk install springboot
$ spring --version
Spring Boot v1.4.1.RELEASE
接下来,你需要通过 Spring Boot CLI 安装 Spring Cloud Launcher CLI(即 spring cloud
命令)。
$ spring install org.springframework.cloud:spring-cloud-cli:1.2.1.RELEASE
如果你运行 spring cloud
而不带任何参数,则会启动 Config Server 和 Eureka。Config Server 会在其他服务之前启动,以便这些服务可以拉取它们的配置。
以下服务器可供启动:
$ spring cloud --list
configserver dataflow eureka h2 hystrixdashboard kafka zipkin
要运行特定的一组服务器,只需将其列为参数即可
$ spring cloud configserver eureka hystrixdashboard
前面的命令将运行 Config Server、Eureka 和 Hystrix Dashboard。
$ spring help cloud
spring cloud - Start Spring Cloud services, like Eureka, Config Server, etc.
Option Description
------ -----------
-d, --debug Debug logging for the deployer
-l, --list List the deployables (don't launch
anything)
-v, --version Show the version (don't launch
examples:
Launch Eureka:
$ spring cloud eureka
Launch Config Server and Eureka:
$ spring cloud configserver eureka
List deployable apps:
$ spring cloud --list
Show version:
$ spring cloud --version
每个进程都可以使用常规的 Spring Boot 外部配置文件进行配置,这些文件具有特殊的名称和位置。文件名基于进程名称(有关完整列表,请再次参阅 spring cloud --list
)。例如,Config Server 可以在名为 configserver.yml
或 configserver.properties
的文件中配置。配置文件的搜索路径是当前目录 (.
)、./config
和 $HOME/.spring-cloud/
。
默认情况下,configserver
进程使用 native
配置 profile 及其 JAR 中的配置。例如,让我们全局配置 Config Server 从 GitHub 仓库加载属性。$HOME/.spring-cloud/configserver.yml
的内容可以如下:
spring:
profiles:
active: git
cloud:
config:
server:
git:
uri: https://github.com/spring-cloud-samples/config-repo
进程 | 端口 | URL |
---|---|---|
configserver |
8888 | /info |
eureka |
8761 | 不适用 |
hystrixdashboard |
7979 | /hystrix |
dataflow |
9393 | /dashboard |
kafka |
9092 | 不适用 |
zipkin |
9411 | 不适用 |
h2 |
9095 | / |
URL 列是在 Eureka 中注册的内容(如果正在运行)。这允许你点击 Eureka Dashboard 中的实例链接。
如果一个进程注册到 Eureka,它将使用 Process 列中的值作为 spring.application.name
。
kafka
进程也会在端口 2181 启动 ZooKeeper。Kafka 用于 Spring Cloud Bus。
h2
数据库在端口 9096 上可用,JDBC URL 为:jdbc:h2:tcp://localhost:9096/./target/test
。H2 URL 可以通过 spring.datasource.url
属性自定义。
请访问 GitHub 上的项目。团队非常欢迎社区提出问题和增强请求。