背景
在微服务架构中,在没有配置中心出现时,我们每个应用的配置信息都在其配置文件application.properties中维护。加入整个系统中有很多应用,那么我们在调整起配置信息来将会非常繁琐。另外不同的环境、如开发环境、测试环境、生产环境等不同环境还需要调整不同的配置参数。在运行中的系统中当有需要调整起配置文件信息时,不能立即生效,仍需要我们进行重启生效。
等等问题迫切需要一个能够集中维护整个系统中不同应用的一个配置服务功能。来为我们解决如下问题:
- 集中配置管理。 一个大型微服务架构可能有成百上千服务,所以集中配置管理非常重要。
- 不同环境不同配置。 比如不同环境下的服务器端口,数据源信息、注册中心地址、redis、es等等配置信息都是不同的。
- 运行期间可以动态调整。 运行中可以根据微服务负载情况,动态的调整数据源资源池大小等。
- 配置修改后可以自动更新。 当配置发生变化后,在不重启应用的情况下微服务可以自动更新配置。
为了解决如上问题,SpringCloud为我们提供了分布式配置中心组件 SpringCloud Config。SpringCloud Config 是一个解决分布式系统配置管理方案。
它包含了Client 和 Server 两部分:
- Server端提供所有配置文件的存储(git或其他工具)、以及将配置文件以接口的形式提供出去。
- Client 端及我们的微服务端通过配置服务端信息,从接口获取其配置文件信息初始化自身应用。
服务端

示例代码

根pom.xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.9.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
pom.xml
<!-- springCloud Config 服务端 begin-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- springCloud Config 服务端 end-->
application.yml
server:
port: 8800
spring:
application:
name: basis-config
profiles:
active: native
cloud:
config:
server:
# 本地方式
native:
search-locations: classpath:/config
#spring:
# profiles:
# active: git, svn
# cloud:
# config:
# server:
# svn:
# uri: file:///path/to/svn/repo
# order: 2
# git:
# uri: https://github.com/xxxxx/config-demo #配置文件所在仓库
# username: github #登录账号
# password: github #登录密码
# default-label: master #配置文件分支
# search-paths: config #配置文件所在根目录
# order: 1 #order属性允许您指定所有存储库的优先级顺序
demo-dev.yml
server:
port: 9901
spring:
application:
name: demo
eureka:
instance:
instance-id: demo
test: aa
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@SpringBootApplication
public class BasisConfigApplication {
public static void main(String[] args) {
SpringApplication.run(BasisConfigApplication.class, args);
}
}
客户端

pom.xml
<!-- springCloud Config 客户端 begin-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- springCloud Config 客户端 end-->
bootstrap.yml
spring:
application:
name: demo
cloud:
config:
name: demo, common-eureka
uri: http://config-server:8800
fail-fast: true
profile: dev
DemoApplication
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@EnableDiscoveryClient
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
TestController
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("test")
public class TestController {
@Value("test")
private String test;
@GetMapping("hello")
public String hello(){
return test;
}
}
本地host配置
127.0.0.1 config-server
运行服务

浏览器访问:http://localhost:9901/test/hello

拓展知识:配置文件为什么用bootstrap.yml而不用application.yml
SpringBoot默认支持properties和YAML两种格式的配置文件。
前者格式简单,但是只支持键值对。如果需要表达列表,最好使用YAML格式。
一、执行顺序
-
bootstrap.yml(bootstrap.properties)用来在程序引导时执行,应用于更加早期配置信息读取,如可以使用来配置application.yml中使用到参数等
-
application.yml(application.properties) 应用程序特有配置信息,可以用来配置后续各个模块中需使用的公共参数等。
-
bootstrap.yml 先于 application.yml 加载
二、典型的应用场景如下:
- 当使用 Spring Cloud Config Server 的时候,你应该在 bootstrap.yml 里面指定 spring.application.name 和 spring.cloud.config.server.git.uri
- 和一些加密/解密的信息
技术上,bootstrap.yml 是被一个父级的 Spring ApplicationContext 加载的。这个父级的 Spring ApplicationContext是先加载的,在加载application.yml 的 ApplicationContext之前。
为何需要把 config server 的信息放在 bootstrap.yml 里?
当使用 Spring Cloud 的时候,配置信息一般是从 config server 加载的,为了取得配置信息(比如密码等),你需要一些提早的引导配置。因此,把 config server 信息放在 bootstrap.yml,用来加载在这个时期真正需要的配置信息。
三、 启动上下文
Spring Cloud会创建一个Bootstrap Context
,作为Spring应用的Application Context
的父上下文。初始化的时候,Bootstrap Context
负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment
。Bootstrap
属性有高优先级,默认情况下,它们不会被本地配置覆盖。
网友评论