Spring Cloud 应用篇 之 Spring Cloud Config(配置中心) 客官°小女子只卖身不卖艺 2022-05-22 03:54 108阅读 0赞 从前几篇文章中我们就可以看出,在分布式系统中,服务数量会很多,如果要修改服务的配置文件,会很麻烦,这个时候,我们想把配置文件放在一个地方统一管理,实时更新,Spring Cloud 就给我们提供了这样一个组件——Spring Cloud Config。 ## (一)简介 ## Spring Cloud Config 支持配置文件放在远程仓库中,例如 Git、SVN,也可以挂载到本地。Spring Cloud Config 和 Spring Boot Admin 有一点类似,它也由服务端和客户端组成,即 server、client。 ## (二)基于 server-client 构建项目 ## ### 2.1 创建 server ### #### 2.1.1 新建一个 module(spring-cloud-config) #### #### 2.1.2 pom 文件添加相关依赖 #### <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> #### 2.1.3 启动类添加注解 @EnableConfigServer 开启配置中心的功能 #### @SpringBootApplication @EnableConfigServer public class SpringCloudConfigApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudConfigApplication.class,args); } } #### 2.1.4 添加配置文件 #### server: port: 8888 spring: cloud: config: server: git: uri: https://github.com/shmilyah/cloud-config-samples.git search-paths: '{application}' application: name: spring-cloud-config spring.cloud.config.server.git.uri:配置git仓库地址 spring.cloud.config.server.git.searchPaths:配置仓库路径,这里的配置会根据传入的应用名动态查找 由于我这里配置的 git 仓库为公开的,所以不需要配置用户名和密码,如果你配置的是私有仓库,还需要配置用户名和密码 (spring.cloud.config.server.git.username 和 spring.cloud.config.server.git.password) #### 2.1.5 创建仓库里的配置文件 #### 本例为了测试,我在 [https://github.com/shmilyah/cloud-config-samples.git][https_github.com_shmilyah_cloud-config-samples.git] 仓库里创建了一个文件夹 config-client,文件夹下创建了一个配置文件 config-client-dev.yaml,内容如下 hello: world #### 2.1.6 启动 spring-cloud-config 服务,访问 http://localhost:8888/config-client/dev/master #### {"name":"config-client","profiles":["dev"],"label":"master", "version":"15a6b2770f282a7d12790ea4d03a548fe1fea664","state":null, "propertySources":[{"name":"https://github.com/shmilyah/cloud-config-samples.git/config-client/config-client-dev.yaml","source":{"hello":"world"}}]} 访问配置中心获取配置信息成功,这里的请求路径映射为:/\{application\}/\{profile\}\[/\{label\}\],还有一些其他路径的映射,详细信息可以网上查看相关文档。 ### 2.2 创建客户端 client ### #### 2.2.1 创建新的 module(spring-cloud-client) #### #### 2.2.2 添加 pom 依赖 #### <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> #### 2.2.3 添加配置文件 application.yaml #### server: port: 8989 spring: application: name: config-client cloud: config: uri: http://localhost:8888/ profile: dev label: master spring.cloud.config.uri 配置要连接的配置中心的地址 spring.cloud.config.profile 配置获取哪个环境的配置文件 一个项目一般会有开发(dev)、测试(test)、生产(pro)环境,还有一些公司的定义不同,还有 dat、uat 环境等,这里我们配置的是开发环境 dev spring.cloud.config.lable 配置连接远程仓库的分支,这里我们是 master 这里的 spring.application.name 配置要和 git 仓库的文件夹名称一致,因为配置中心配置了 search-paths 是通过应用名查找对应的路径 #### 2.2.4 写一个测试接口,代码如下: #### @SpringBootApplication @RestController public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } @Value("${hello}") private String hello; @RequestMapping("hello") public String hello() { return hello; } } 访问 http://localhost:8989/hello ![70][] 这说明我们的 config-client 已经成功获取了配置中心里的配置文件,下面我们介绍基于 eureka 来使用配置中心。 ## (三)基于 Eureka 构建项目 ## 如(二)的配置,我们需要为每个服务配置指定 git 的 uri,但我们基于 Eureka 来使用,就可以省略这一步,把配置中心和其他服务都注册到 eureka server,这个时候,服务会自动获取 eureka 里的 config,去获取自己配置的需要的配置文件。 ### 3.1 修改 spring-cloud-config 服务 ### #### 3.1.1 pom 文件添加 Eureka 的依赖 #### <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> #### 3.1.2 启动类添加注解 #### @SpringBootApplication @EnableEurekaClient @EnableConfigServer public class SpringCloudConfigApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudConfigApplication.class,args); } } #### 3.1.3 配置文件添加 eureka 的配置 #### server: port: 8888 spring: cloud: config: server: git: uri: https://github.com/shmilyah/cloud-config-samples.git search-paths: '{application}' application: name: spring-cloud-config eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ #### 3.1.4 依次启动 eureka server、spring-cloud-config,访问 http://localhost:8888/config-client/dev/master #### ![70 1][] ### 3.2 重构 spring-cloud-config-client 服务 ### #### 3.2.1 添加 pom 依赖 #### <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <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> #### 3.2.2 启动类添加注解 #### @SpringBootApplication @RestController @EnableEurekaClient public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } @Value("${hello}") private String hello; @RequestMapping("hello") public String hello() { return hello; } } #### 3.2.3 修改配置文件 #### server: port: 8989 spring: application: name: config-client cloud: config: # uri: http://localhost:8888/ profile: dev label: master discovery: enabled: true service-id: spring-cloud-config eureka: client: service-url: defaultZone: http://localhost:8761/eureka 这种方式,我们通过 spring.cloud.config.discovery 的配置,通过 service-id 向注册中心查找配置中心,这个 service-id 和配置中心的 application.name 一致 #### 3.2.4 启动 spring-cloud-config-client 服务 #### 访问 http://localhost:8989/hello,虽然我们没有配置 git 的 uri,这里依然获取到了配置信息,因为我们从注册中心里拿到了配置中心 ![70 2][] 至此,我们的配置中心就已经搭建成功了,后续就可以把其他服务的配置文件都放到配置中心指定的远程仓库中统一管理了 不过这里我们没有还没有实现配置中心文件实时刷新,这个后续会讲 其实还有一个问题,**这里面有一个坑**,可能有人已经发现了,当你配置的配置中心的 server.port 不是 8888 的时候,其他服务就起不来了,从日志中可以发现,服务启动的时候,**Fetching config from server at: http://localhost:8888**,时间有限,这个问题下一篇来说吧! #### 源码下载:[https://github.com/shmilyah/spring-cloud-componets][https_github.com_shmilyah_spring-cloud-componets] #### [https_github.com_shmilyah_cloud-config-samples.git]: https://github.com/shmilyah/cloud-config-samples.git [70]: /images/20220522/bfce9f43ab8d4d70819b684df9d18e04.png [70 1]: /images/20220522/338134f188b24be99efbfbae0c04270c.png [70 2]: /images/20220522/5f30ff6dbe664f6cbfb4a10a15fbacb2.png [https_github.com_shmilyah_spring-cloud-componets]: https://github.com/shmilyah/spring-cloud-componets
还没有评论,来说两句吧...