文章目录
Spring Cloud Config简介
Spring Cloud Config为分布式系统外部化配置提供了服务器端和客户端的支持,它包括Config Service和Config Client两部分。由于Config Service和Config Client都实现了对Spring Environment和PropertySource抽象的映射,因此,Spring Cloud Config非常适合Spring应用程序,当然也可以与其它任何语言编写的应用程序配合使用。
Config Server是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环境下的配置,默认使用Git存储配置文件内容,也可以使用SVN存储,或者是本地文件存储。
Config Client是Config Server的客户端,用于操作存储在ConfigServer中的配置内容。微服务在启动时会请求Config Server获取配置文件的内容,请求到后再启动容器。
Git上的配置文件springcloud-config-resources
springcloud-config-resources项目结构
由于Spring Cloud Config Server默认采用的是Git,所以说我们就创建一个存放配置文件的目录,在这里,我将配置文件创建到了springcloud-config-resources中,其项目结构如下;
springcloud-config-resources源码
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-parent</artifactId>
<groupId>com.lyc</groupId>
<version>1.0-RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-config-resources</artifactId>
<name>SpringCloud微服务::配置文件</name>
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<finalName>${project.artifactId}</finalName>
</build>
</project>
microservice-dev.properties源码:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.1.1:3306/taobao?useUnicode=true
jdbc.username=root
jdbc.password=root
由于这个项目是用来演示的,所以说这里的生产环境microservice-production.properties
与测试环境microservice-test.properties
都与开发环境microservice-dev.properties
中的内容是一样的,因而我就不重复贴代码了。
最后要做的就是将springcloud-config-resources发布到Git中,供配置文件中心Spring Cloud Config Server来访问。