1.概念简介
(1)SpringCloud Config出现的原因
<1>没有springCloud前,传统的服务配置文件都是配置在项目里面的
1.1.resource目录下面的application.yml或者application.properties文件
1.2.这种配置文件的局限性是修改起来比较麻烦
1.3.要重新打包发布并且重启服务,这些缺点就是springCloud的优点
<2>SpringCloud Config解决的问题
2.1.配置文件和项目分离,配置在git上或者svn上
2.2.配置文件修改后不需要重启服务,请求刷新接口后直接生效
2.3.主要是为分布式系统中的基础设置和微服务应用提供集中化的外部配置支持
(2)使用配置服务SpringCloud Config来保存各个服务的配置文件
<1>在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件
<2>在SpringCloud中,有分布式配置中心组件Config
<3>它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中
(3)在Config组件中,分两个角色,一是ConfigServer,二是ConfigClient
<1>服务器存储后端的默认实现使用git
2.构建ConfigServer
(1)创建一个SpringBoot项目,取名为ConfigServer,其pom.xml如下
<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">
<modelVersion>4.0.0</modelVersion>
<artifactId>ConfigServer</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>com.bisien</groupId>
<artifactId>SpringCloud-Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
(2)编写ConfigServer程序的入口Application类,示例代码如下
@SpringBootApplication
//通过@EnableConfigServer注解开启配置服务器的功能
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);}}
(3)编写程序的配置文件application.properties,配置如下
spring.application.name=ConfigServer
server.port=8888
#配置git仓库地址
spring.cloud.config.server.git.uri=https://github.com/forezp/SpringcloudConfig/
#配置仓库路径
spring.cloud.config.server.git.searchPaths=respo
#配置仓库的分支
spring.cloud.config.label=master
#访问git仓库的用户名
spring.cloud.config.server.git.username=
#访问git仓库的用户密码
spring.cloud.config.server.git.password=
<1>如果Git仓库为公开仓库,可以不填写用户名和密码
<2>如果是私有仓库需要填写,本例子是公开仓库,放心使用
<3>远程仓库中有个文件config-client-dev.properties文件中有一个属性:
3.1.foo = foo version 3
<4>启动程序:访问http://localhost:8006/foo/dev
4.1.有内容证明配置服务中心可以从远程程序获取配置信息
3.构建ConfigClient
(1)重新创建一个SpringBoot项目,取名为ConfigClient,其pom文件如下:
<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">
<modelVersion>4.0.0</modelVersion>
<artifactId>ConfigClient</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>com.bisien</groupId>
<artifactId>SpringCloud-Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
(2)编写配置文件bootstrap.properties:代码示例如下:
spring.application.name=ConfigClient
#指明远程仓库的分支
spring.cloud.config.label=master
#dev 开发环境配置文件 | test 测试环境 | pro 正式环境
spring.cloud.config.profile=dev
#指明配置服务中心的网址
spring.cloud.config.uri= http://localhost:8006/
server.port=8007
(3)bootstrap.properties和application.properties区别
<1>bootstrap.properties优先级比application.properties高
(4)编写程序的入口类,返回从配置中心读取的foo变量的值,代码示例如下:
@SpringBootApplication
@RestController
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);}
@Value("${foo}")//git配置文件里的key
String foo;
@RequestMapping(value = "/hi")
public String hi(){return foo;}}
(5)打开网址访问:http://localhost:8007/hi,网页显示:foo version 3
<1>说明ConfigClient从ConfigServer获取了foo的属性
<2>config-server是从git仓库读取的,过程如下:
2.1.ConfigClient-->ConfigServer-->git
4.参考文档:
(1)https://www.fangzhipeng.com/springcloud/2018/08/06/sc-f6-config.html
(2)https://blog.csdn.net/chengkui1990/article/details/80894076
(3)源码下载:https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-chapter6