微服务意味着要将单体应用中的业务拆分红一个个子服务,每一个服务的粒度相对较小,所以系统中标会出现大量的服务。因为每一个服务都须要必要的配置信息才能运行,因此一套集中式的、动态的配置管理设施是必不可少的。咱们每个微服务本身有一个 application.yml 文件,若是有上百个这样的文件维护起来确定容易让人崩溃,因此 SpringCloud 提供了 ConfigServer 来解决这个问题。git
SpringCloud Config 为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不一样微服务应用的全部环境提供了一个中心化的外部配置。github
SpringCloud Config 分为服务端和客户端两部分:web
一、在 GitHub 新建一个名为 "microservicecloud-config" 的 Repository,提交以下编码为 UTF-8 的文件:spring
spring: profiles: active: - dev --- spring: profiles: dev application: name: microservicecloud-config-dev --- spring: profiles: test application: name: microservicecloud-config-test
二、新建名为 "microservicecloud-config-3344" 的子工程做为配置中心服务,依赖以下:apache
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>microservicecloud</artifactId> <groupId>zze.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>microservicecloud-config-3344</artifactId> <dependencies> <!-- SpringCloud Config --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!-- 图形化监控 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- 熔断 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!-- 热部署插件 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
三、配置仓库地址:bootstrap
server: port: 3344 spring: application: name: microservicecloud-config cloud: config: server: git: uri: https://github.com/zze326/microservicecloud-config.git # 对应配置文件的 git 仓库连接
四、编写主启动类,使用注解启用配置中心功能:服务器
package zze.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer // 标识当前服务为配置中心 public class Application_3344 { public static void main(String[] args) { SpringApplication.run(Application_3344.class, args); } }
五、测试:架构
启动项目,访问 http://localhost:3344/application-dev.yml,能够看到,返回内容为 gibhub 中存放的配置:
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties
一、新建以下配置文件,提交到 GitHub 的配置仓库中:app
spring: profiles: active: - dev --- server: port: 8201 spring: profiles: dev application: name: microservicecloud-config-client eureka: client: service-url: defaultZone: http://www.eurekaserver1.com:7001/eureka --- server: port: 8202 spring: profiles: test application: name: microservicecloud-config-client eureka: client: service-url: defaultZone: http://www.eurekaserver1.com:7001/eureka
二、新建名为 "microservicecloud-config-client-3355" 的子工程做为使用配置中心的客户端,依赖以下:maven
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>microservicecloud</artifactId> <groupId>zze.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>microservicecloud-config-client-3355</artifactId> <dependencies> <!-- SpringCloud Config客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
三、新建系统级配置文件,指定配置中心地址和要读取环境的配置:
spring: cloud: config: name: microservicecloud-config-client # 须要从 github 上读取的资源名称 profile: dev # 环境,决定读取哪一个环境的配置内容 label: master uri: http://localhost:3344 # SpringCloud Config Server 地址
四、新建主启动类:
package zze.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class Application_3355 { public static void main(String[] args) { SpringApplication.run(Application_3355.class, args); } }
五、新建获取配置信息的 Controller:
package zze.springcloud.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * 经过 config 客户端从 config server 获取配置信息 */ @RestController public class ClientConfigController { @Value("${spring.application.name}") private String applicationName; @Value("${eureka.client.service-url.defaultZone}") private String eurekaServers; @Value("${server.port}") private String port; @GetMapping("/config") public String getConfig(){ String str="applicationName = %s <br> eurekaServer = %s <br> port = %s"; return String.format(str, applicationName, eurekaServers, port); } }
六、测试:
一、启动 7001 Eureka Server 二、启动 3344 配置中心服务 三、启动 "microservicecloud-config-client-3355" 服务,能够看到占用端口为 github 配置文件中 dev 环境配置下的 8201,访问 localhost:8201/config:
“application.yml”是用户级的资源配置项,而“bootstrap.yml”是系统级的,优先级更高。
SpringCloud 会建立一个“Bootstrap Context”,做为 Spring 应用的“Application Context”的父级上下文。初始化时,“Bootstrap Context”负责从外部源加载配置属性并解析配置。这两个上下文共享一个外部获取的“Environment”。
“Bootstrap”属性有高优先级,默认状况下,它们不会被本地配置覆盖。“Bootstrap Context”和“Application Context”有着不一样的约定,因此新增了一个“bootstrap.yml”文件,保证“Bootstrap Context”和“Application Context”配置的分离。
完整示例下载 | 提取码:3lb2