1 分布式系统:配置文件
- 微服务意味着要将单体应用各种的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。
- SpringCloud提供了ConfigServer来解决这个问题。
2 SpringCloud Config分布式配置中心
- SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环节提供了一个中心化的外部配置。
- SpringCloud Config分为服务端和客户端两部分。
- 服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息、加密、解密信息等访问接口。
- 客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置,并在启动的时候从配置中心获取和加载配置信息。配置服务器默认采用Git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过Git客户端工具来方便地管理和访问配置内容。
3 SpringCloud Config能干什么?
- 集中式管理配置文件。
- 不同环境、不同配置,动态化的配置更新、分环境部署(如dev、test、prod、beta、release)。
- 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息。
- 当配置发生变动时,服务不需要重启,即可感知到配置的变化,并应用新的配置。
- 将配置信息以REST接口的形式暴露。
4 SpringCloud Config与GitHub整合
- 由于SpringCloud Config默认使用Git来存储配置文件(也有其他方式,如SVN、本地文件),使用http/https的访问形式。
5 起步
1、在Gitee上创建一个仓库
2、新建module:springcloud-config-server-3344
pom.xml(导入依赖)
<?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>springcloud</artifactId> <groupId>com.xianhuii</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springcloud-config-server-3344</artifactId> <dependencies> <!--config-server--> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-config-server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> <version>2.2.1.RELEASE</version> </dependency> <!-- Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
application.yaml(编写配置)
server: port: 3344 spring: application: name: springcloud-config-server cloud: config: server: git: uri: https://gitee.com/xianhuii/springcloud-config.git
Config_Server_3344(开启注解)
package com.xianhuii.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @EnableConfigServer // 开启ConfigServer服务 @SpringBootApplication public class Config_Server_3344 { public static void main(String[] args) { SpringApplication.run(Config_Server_3344.class, args); } }
3、新建module:springcloud-config-client-3355
pom.xml(导入依赖)
<?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>springcloud</artifactId> <groupId>com.xianhuii</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springcloud-config-client-3355</artifactId> <dependencies> <!--config-client--> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-config-server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> <version>2.2.1.RELEASE</version> </dependency> <!-- Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
bootstrap.yaml(系统级别配置)
spring: cloud: config: uri: http://localhost:3344 name: config-client profile: dev label: master
application.yaml(用户级别配置)
spring: application: name: springcloud-config-client
ConfigClient_3355
package com.xianhuii.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ConfigClient_3355 { public static void main(String[] args) { SpringApplication.run(ConfigClient_3355.class, args); } }
ConfigClientController
package com.xianhuii.springcloud.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ConfigClientController { @Value("$(spring.application.name") private String applicationName; @Value("${eureka.client.service-url.defaultZone}") private String eurekaServer; @Value("${server.port}") private String port; @RequestMapping("/config") public String getConfig() { return applicationName + ", " + eurekaServer + ", " + port; } }