Spring Cloud Config

12 篇文章 0 订阅
12 篇文章 0 订阅

Spring Cloud Config为分布式系统中的外部化配置提供服务器和客户端支持。使用Config Server,您可以集中管理所有环境中应用程序的外部属性。客户端和服务器上的概念与Spring Environment和PropertySource抽象,因此它们非常适合Spring应用程序,但可以与以任何语言运行的任何应用程序一起使用。当应用程序从开发人员到测试人员进入生产过程进入生产过程时,您可以管理这些环境之间的配置,并确保应用程序具有它们迁移时所需的一切。服务器存储后端的默认实现使用git,因此它轻松支持带标签的配置环境版本,并且可以通过各种工具来访问这些内容来管理内容。添加替代实现并将其插入Spring配置很容易。
快速开始
启动服务器:
$ cd spring-cloud-config-server
$ mvn spring-boot:运行
该服务器是Spring Boot应用程序,因此您可以根据需要从IDE中运行它(主要类是 ConfigServerApplication)。然后尝试一个客户端:
$ curl本地主机:8888 / foo / development
{“名称”:“开发”,“标签”:“主”,“ propertySources”:[
{“ name”:“ https://github.com/scratches/config-repo/foo-development.properties”,“ source”:{“ bar”:“ spam”}},
{“ name”:“ https://github.com/scratches/config-repo/foo.properties”,“ source”:{“ foo”:“ bar”}}
]}
定位属性源的默认策略是克隆git存储库(位于spring.cloud.config.server.git.uri)并使用它初始化mini SpringApplication。迷你应用程序 Environment用于枚举属性源,并通过JSON端点发布它们。
HTTP服务具有以下形式的资源:
/ {application} / {profile} [/ {label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
其中“应用”注入作为spring.config.name在 SpringApplication(即什么是正常地“应用”,在一个普通的Spring启动的应用程序),“个人资料”是一个活动的配置文件(或逗号分隔的属性列表),和“标签”是一种可选的git标签(默认为“ master”。)
即使值的来源(反映在“标准”形式的“ propertySources”中)具有多个来源,YAML和属性形式也会合并为一个映射。
Spring Cloud Config Server从git存储库(必须提供)中为远程客户端提取配置:
spring:
cloud:
config:
server:
git:
uri: https://github.com/spring-cloud-samples/config-repo
客户端使用
要在应用程序中使用这些功能,只需将其构建为依赖于spring-cloud-config-client的Spring Boot应用程序即可(例如,查看config-client的测试用例或示例应用程序)。添加依赖项最方便的方法是通过Spring Boot启动器org.springframework.cloud:spring-cloud-starter-config。spring-cloud-starter-parent对于Maven用户,还有一个父pom和BOM(),对于Gradle和Spring CLI用户,还有一个Spring IO版本管理属性文件。Maven配置示例:
pom.xml

org.springframework.boot
spring-boot-starter-parent
1.2.3.RELEASE

org.springframework.cloud spring-cloud-starter-parent 1.0.1.RELEASE pom import org.springframework.cloud spring-cloud-starter-config org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin

然后,您可以创建一个标准的Spring Boot应用程序,例如以下简单的HTTP服务器:
@SpringBootApplication
@RestController
public class Application {

@RequestMapping("/")
public String home() {
    return "Hello World!";
}

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}

}
运行时,它将从端口8888上的默认本地配置服务器中获取外部配置。要修改启动行为,您可以使用来更改配置服务器的位置bootstrap.properties(例如,application.properties但是对于应用程序上下文的引导阶段),例如
spring.cloud.config.uri:http://myconfigserver.com
引导程序属性将在/env端点中显示为高优先级属性源,例如
$ curl localhost:8080/env
{
“profiles”:[],
“configService:https://github.com/spring-cloud-samples/config-repo/bar.properties”:{“foo”:“bar”},
“servletContextInitParams”:{},
“systemProperties”:{…},

}
(名为“ configService:/”的属性源包含属性“ foo”,其值为“ bar”,并且具有最高优先级)。
注意 属性源名称中的URL是git存储库,而不是配置服务器URL。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值