什么是git?
咳咳,开个玩笑,这个真的没必要介绍了8。。。
如果不是很熟悉,请看文档,很详细,很奈斯~
来个正经的问题。
为什么要使用远程配置中心?
随着线上项目变的日益庞大,每个项目都散落着各种配置文件,如果采用分布式的开发模式,需要的配置文件随着服务增加而不断增多。如果某一个基础服务信息变更,必须要进行一系列的更新和重启配置才能生效,运维苦不堪言也容易出错。配置中心便是解决此类问题的灵丹妙药。
我们想要配置中心拥有怎样的功能才能满足我们的期望呢?
- 提供服务端和客户端支持
- 集中管理各环境的配置文件
- 配置文件修改之后,可以快速的生效
- 可以进行版本管理
- 支持大的并发查询
- 支持各种语言
你没有看错!接下里出场的spring cloud config完美满足这些需求。
介绍:Spring Cloud Config项目是一个解决分布式系统的配置管理方案。它包含了Client和Server两个部分,server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client通过接口获取数据、并依据此数据初始化自己的应用。Spring cloud使用git或svn存放配置文件,默认情况下使用git。
?白话文解读:【一般config server就是老大,注册到eureka上之后,其他client子服务去发现使用server中的各种配置,只要把配置写进去server即可,client负责读取~加载配置,初始化自己的应用~】
?美滋滋。
Spring Cloud Config同样支持本地存储配置
Spring Cloud Config也提供本地存储配置的方式。
我们只需要设置属性
spring.profiles.active=native
,Config Server会默认从应用的src/main/resource
目录下检索配置文件。也可以通过
spring.cloud.config.server.native.searchLocations=file:E:/properties/(本地配置文件存储位置)
属性来指定配置文件的位置。
虽然Spring Cloud Config提供了这样的功能,但是为了支持更好的管理内容和版本控制的功能,还是推荐使用git远程配置的方式。
下面用gitee码云来模拟远程配置
➡准备工作(必须先在git仓库有配置撒~)
1.首先新建一个文件夹,然后在里面创建一个文件夹名为:config-repo
2.在config-repo文件夹内新建三个文件,分别模拟生产/测试/开发环境
// 开发环境
neo-config-dev.properties内容:neo.hello= my name is dev
// 测试环境
neo-config-test.properties内容:neo.hello= my name is test
// 生产环境
neo-config-pro.properties内容:neo.hello= my name is pro
➡配置老大server端【没啥特别功能,只为提供配置】
1.新建springboot项目:
使用官方快速创建➡:https://start.spring.io/
取名为:spring-cloud-config-server
2.pom加入依赖:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
ps: 此处还需加eureka依赖,否则,无法注册。
笔者springboot版本为1.4.3 ;cloud版本为1.3.1
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>
pps: springboot版本为2.1.10时;cloud版本为Greenwich.SR4,依赖如下:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
3.配置文件
server:
port: 8001 #项目端口号
spring:
application:
name: spring-cloud-config-server #项目名字
cloud:
config:
server:
git:
uri: https://gitee.com/hanson629/mscdemo-spring-cloud-config # 配置git仓库的地址
search-paths: config-repo # git仓库地址下的相对地址,可以配置多个,用,分割。
username: xxxx # git仓库的账号(自己的)
password: xxxx # git仓库的密码
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8000/eureka/ ## 注册中心eurka地址
4.启动类添加@EnableConfigServer注解,启用对配置中心的支持
//启用服务发现
@EnableDiscoveryClient
//启用配置中心支持
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
5.测试
依次启动eureka,spring-cloud-config-server项目
打开浏览器访问:localhost:8001/neo-config/dev:
可以发现已经访问到了仓库的配置~
如果直接查看配置文件中的配置信息可访问:http://localhost:8001/neo-config-dev.properties 返回结果:
·如果直接查看配置文件中的配置信息可访问:http://localhost:8001/neo-config-dev.properties,返回:neo.hello: hello im dev
·仓库中的配置文件会被转换成web接口,访问可以参照以下的规则:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
·以neo-config-dev.properties为例子,它的application是neo-config,profile是dev。➡/{application}-{profile}.properties
·client会根据填写的参数来选择读取对应的配置。
➡配置小弟client端
主要是业务服务去获取server端配置信息~
1.新建springboot项目:
使用官方快速创建➡:https://start.spring.io/
取名为:spring-cloud-config-client
2.pom加入依赖,引入spring-boot-starter-web包方便web测试:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3.配置文件
spring:
application:
name: spring-cloud-config-client
cloud:
config:
name: neo-config #对应{application}部分
profile: dev #对应{profile}部分
uri: http://localhost:8001/ #配置中心的具体地址
label: master #对应git的分支。如果配置中心使用的是本地存储,则该参数无用
discovery:
service-id: spring-cloud-config-server #指定server端的name,也就是server
server:
port: 8002
注意:
上面这些与spring-cloud相关的属性必须配置在bootstrap.yml中,
config部分内容才能被正确加载。
因为config的相关配置会先于application.properties,
而bootstrap.properties的加载也是先于application.properties。
4.启动类
本测试无需特别的注解
@SpringBootApplication
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
5.新建controller类,便于web测试
@RestController
class HelloController {
//使用@Value注解来获取server端参数的值
@Value("${neo.hello}")
private String hello;
@RequestMapping("/hello")
public String from() {
return this.hello;
}
}
6.测试
启动spring-cloud-config-client项目
打开浏览器访问web:localhost:8002/hello
说明客户端已经从server端读取到了配置文件参数~
7.问题
手动修改git仓库neo-config-dev.properties文件内容为:hello my name is dev~xixixi,刷新发现client访问结果neo.hello属性还是hello my name is dev~并没有改变,说明获取的信息还是旧的参数,这是为什么呢?因为springboot项目只有在启动的时候才会获取配置文件的值,修改git仓库配置文件信息后,client端并没有再次去获取配置,所以导致这个问题。如何去解决这个问题呢?后面我们在研究一波。