1.Config简介
一句话,对配置进行统一管理
2.Config工作流程
首先我们需要开发一个ConfigServer服务,所有微服务的配置文件写在Github或者Gitee仓库上。运行服务时,Config Server会从远程仓库拉取配置到本地,然后其他微服务从Config Server中拿去自己的配置。
A.ConfigServer的搭建
- 引入依赖(因为需要注册到服务中心,所以这里用了consul)
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--config server依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
- 配置文件
server.port=12345
//consul配置
spring.cloud.consul.port=8500
spring.cloud.consul.host=localhost
//服务名
spring.application.name=CONFIGSERVER
//config配置
spring.cloud.config.server.git.uri=https://gitee.com/xiang_pei/config.git //gitee仓库地址
spring.cloud.config.server.default-label=master //使用哪个分支
- 入口类加上@EnableConfigServer
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class,args);
}
}
B.ConfigClient的搭建(即微服务搭建)
- 引入依赖,SpringCloud 2020.0.*版本将bootstrap配置删除了,需要把它引进来,不然客户端跑不起来
参考:No spring.config.import property has been defined
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--config client的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--重新导入bootstrap-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
</dependencies>
- 配置文件(虽然说他们的配置文件都在gitee上管理着,但是必须要有一些配置才能实现config的效果)配置文件名必须是bootstrap.peoperties或者bootstrap.yml,首先我们应当知道SpringBoot加载配置文件的优先级bootstrap是大于application的。那为什么Config client需要用优先级高的配置文件呢?因为Config Client的配置文件中需要配置Config Server,这样才能保证Client能够拿到Server拉取下来的配置。至于bootstrap和application的区别,网上各有说法,有的说优先级区别,我学的spring cloud视频老师说bootstrap会等拉取完配置之后才启动。我思来想去,这两个说法都很勉强,不能说明为什么要使用。经过我一番思考,应该是这样的(仅仅是推测),springboot读取配置会从bootstrap开始,然后application,没有bootstrap就直接application,也就是优先级的说法。所以这里使用bootstrap是这样的:springboot读取到bootstrap会完成向ConfigServer获取自己的配置过程(这里也就是拿到application配置文件),然后执行application配置文件。所以需要使用bootstrap先拿取到配置!直接使用application就直接执行报错
//配置consul
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.application.name=CONFIGCLIENT
//配置configserve,为什么是以服务名的方式而不是写死uri,因为集群!
spring.cloud.config.discovery.service-id=CONFIGSERVER //从名为某某的服务名的configserver获取配置
spring.cloud.config.discovery.enabled=true //开启client去server拉取配置功能
//这里我远程的配置文件叫 config-dev.properties,大家比对阅读就好
spring.cloud.config.name=config //要拉取配置的名称
spring.cloud.config.label=master //分支
spring.cloud.config.profile=dev //哪个环境