SpringCloud之配置中心Config

客官°小女子只卖身不卖艺 2022-05-23 23:13 646阅读 0赞

Spring Cloud Config可以把一些配置信息配置在远程服务器上,集中化管理集群配置。可以使用Spring Cloud Config来获取远程服务器上的配置信息。
可以分为两个部分:

服务端: 配置服务端,服务管理配置信息
客户端:客户端调用server端暴露接口获取配置信息

先来看看我github的目录和要读取的配置:里面定义四个配置文件读取。
这里写图片描述

现在读取第一个配置文件中的值:读取的方式和配置名称可以有关
这里写图片描述

读取项目可以分为两个部分:

服务端: 配置服务端,服务管理配置信息
客户端:客户端调用server端暴露接口获取配置信息

先来看服务器:

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-config-server</artifactId>
  4. </dependency>
  5. @SpringBootApplication
  6. @EnableConfigServer
  7. public class Applicationn {
  8. public static void main(String[] args){
  9. SpringApplication.run(Applicationn.class,args);
  10. }
  11. }
  12. server.port=8081
  13. #spring.application.name=microservice-foo
  14. spring.application.name=ok
  15. # 远程仓库地址,具体到项目
  16. spring.cloud.config.server.git.uri=https://github.com/meihuiziaaaaaa/firstTest
  17. # 项目下的具体路径可以配置多个
  18. spring.cloud.config.server.git.searchPaths=config/config
  19. #spring.cloud.config.label=master
  20. # 用户名
  21. #spring.cloud.config.server.git.username=
  22. # 密码
  23. #spring.cloud.config.server.git.password=

服务器代码就此结束:读取方式如下:
启动服务端之后,我们可以通过url的方式对配置文件进行访问,访问关系如下:

url:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
上面的url会映射{application}-{profile}.properties对应的配置文件,{label}对应git上不同的分支,默认为master。

例如:访问microservice-foo-dev.properties文件的方式
这里写图片描述

在例如访问microservice-foo-dev.properties和microservice-foo.properties
这里写图片描述
这里写图片描述

先开启服务端,在启动客户端
现在客户端的读取方式:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.cloud</groupId>
  7. <artifactId>spring-cloud-starter-config</artifactId>
  8. </dependency>
  9. @SpringBootApplication
  10. public class Application {
  11. public static void main(String[] args){
  12. SpringApplication.run(Application.class,args);
  13. }
  14. }

bootstrap.properties

需要配置在bootstrap中才能生效,而不是在application配置文件中,bootstrap配置文件会比application更加早的加载。

  1. spring.application.name=microservice
  2. spring.cloud.config.profile=foo-dev
  3. spring.cloud.config.uri=http://localhost:8081/
  4. #spring.cloud.config.label=master
  5. @PropertySource(value = "classpath:bootstrap.properties")
  6. @RestController
  7. public class Test1 {
  8. @Value("${profile}")
  9. private String profile;
  10. @RequestMapping("/profile")
  11. public String profile(){
  12. System.out.println(this.profile+"....");
  13. return this.profile;
  14. }
  15. }

这里写图片描述

配置刷新

在上面的配置中,客户端是如何获得配置中心的配置信息的呢?是在bootstrap配置文件中配置,在程序启动时加载获取的。显然,在加载完成之后,如果配置中心中的信息发生变化的话,客户端的信息是不会跟着变化的,这是实际开发过程中所不允许的。我们应该怎么做呢?

我们需要增加一个监控模块:spring-boot-starter-actuator

在客户端中增加如下依赖:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-actuator</artifactId>
  4. </dependency>

并且在Controller中增加@RefreshScope注解。

最后,actuator给我们提供了一个/refresh接口,修改完git仓库信息之后,向这个接口发送一个POST信息,就会更新配置信息了。不需要自己写/refresh接口
修改github配置文件后 (bootstrap.properties: management.security.enabled=false)访问http://localhost:8080/refresh,再一次http://localhost:8080/profile取得的是最新的数据

但是服务端和客户端最终是要成为微服务的,所以还得向eureka服务注册

在服务端和客户端都增加

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-eureka</artifactId>
  4. </dependency>
  5. 启动类上加
  6. @EnableDiscoveryClient

客户端

  1. eureka.client.serviceUrl.defaultZone=http://xxx.xxx.15.188:8888/eureka/,http://xxx.xxx.15.189:8888/eureka/,http://xxx.168.15.190:8888/eureka/

服务端

  1. #服务注册
  2. spring.cloud.config.discovery.enabled=true
  3. spring.cloud.config.discovery.service-id=configServier
  4. eureka.client.serviceUrl.defaultZone=http://xxx.xxx.15.188:8888/eureka/,http://xxx.xxx.15.189:8888/eureka/,http://xxx.xxx.15.190:8888/eureka/

注意:

在客户端上面实现了两种方式读取配置一种是url,另一种是通过注册的服务名称读取

  1. 第一种: spring.cloud.config.uri=http://localhost:8081/
  2. 第二种: spring.cloud.config.discovery.enabled=true
  3. spring.cloud.config.discovery.service-id=configServier

最终的代码:

服务端:

pom.xml

  1. ------- <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <!-- <scope>test</scope>--> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!-- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!-- Eureka 客户端依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>

启动类

  1. @SpringBootApplication
  2. @EnableDiscoveryClient
  3. @EnableConfigServer
  4. public class Applicationn {
  5. public static void main(String[] args){
  6. SpringApplication.run(Applicationn.class,args);
  7. }
  8. }

application.properties

  1. server.port=8081
  2. #spring.application.name=microservice-foo
  3. spring.application.name=configServier
  4. # 远程仓库地址,具体到项目
  5. spring.cloud.config.server.git.uri=https://github.com/meihuiziaaaaaa/firstTest
  6. # 项目下的具体路径可以配置多个
  7. spring.cloud.config.server.git.searchPaths=config/config
  8. #spring.cloud.config.label=master
  9. # 用户名
  10. #spring.cloud.config.server.git.username=
  11. # 密码
  12. #spring.cloud.config.server.git.password=
  13. eureka.client.serviceUrl.defaultZone=http://192.168.15.188:8888/eureka/,http://192.168.15.189:8888/eureka/,http://192.168.15.190:8888/eureka/

客户端:

pom.xml

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>1.5.6.RELEASE</version>
  5. </parent>
  6. <properties>
  7. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  8. <maven.compiler.source>1.7</maven.compiler.source>
  9. <maven.compiler.target>1.7</maven.compiler.target>
  10. </properties>
  11. <dependencies>
  12. <dependency>
  13. <groupId>junit</groupId>
  14. <artifactId>junit</artifactId>
  15. <version>4.11</version>
  16. <scope>test</scope>
  17. </dependency>
  18. <!-- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> -->
  19. <dependency>
  20. <groupId>org.springframework.boot</groupId>
  21. <artifactId>spring-boot-starter-test</artifactId>
  22. <!-- <scope>test</scope>-->
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-actuator</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.cloud</groupId>
  30. <artifactId>spring-cloud-starter-config</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.cloud</groupId>
  34. <artifactId>spring-cloud-starter-eureka</artifactId>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.cloud</groupId>
  38. <artifactId>spring-cloud-dependencies</artifactId>
  39. <version>Dalston.SR1</version>
  40. <type>pom</type>
  41. <scope>import</scope>
  42. </dependency>
  43. <!-- Eureka 客户端依赖-->
  44. <dependency>
  45. <groupId>org.springframework.cloud</groupId>
  46. <artifactId>spring-cloud-starter-eureka</artifactId>
  47. </dependency>
  48. </dependencies>
  49. <repositories>
  50. <repository>
  51. <id>spring-milestones</id>
  52. <name>Spring Milestones</name>
  53. <url>https://repo.spring.io/milestone</url>
  54. <snapshots>
  55. <enabled>false</enabled>
  56. </snapshots>
  57. </repository>
  58. </repositories>
  59. <dependencyManagement>
  60. <dependencies>
  61. <dependency>
  62. <groupId>org.springframework.cloud</groupId>
  63. <artifactId>spring-cloud-dependencies</artifactId>
  64. <version>Dalston.SR1</version>
  65. <type>pom</type>
  66. <scope>import</scope>
  67. </dependency>
  68. </dependencies>
  69. </dependencyManagement>

启动类:

  1. @SpringBootApplication
  2. @EnableDiscoveryClient
  3. public class Application {
  4. public static void main(String[] args){
  5. SpringApplication.run(Application.class,args);
  6. }
  7. }

bootstrap.properties

  1. server.port=8080
  2. spring.application.name=microservice
  3. spring.cloud.config.profile=foo-dev
  4. #spring.cloud.config.uri=http://localhost:8081/
  5. #spring.cloud.config.label=master
  6. management.security.enabled=false
  7. #服务注册
  8. spring.cloud.config.discovery.enabled=true
  9. spring.cloud.config.discovery.service-id=configServier
  10. eureka.client.serviceUrl.defaultZone=http://xxx.xxx.15.188:8888/eureka/,http://xxx.xxx.15.189:8888/eureka/,http://xxx.xxx.15.190:8888/eureka/

测试

  1. @PropertySource(value = "classpath:bootstrap.properties")
  2. @RestController
  3. @RefreshScope
  4. public class Test1 {
  5. @Value("${profile}")
  6. private String profile;
  7. @RequestMapping("/profile")
  8. public String profile(){
  9. System.out.println(this.profile+"....");
  10. return this.profile;
  11. }
  12. }

发表评论

表情:
评论列表 (有 0 条评论,646人围观)

还没有评论,来说两句吧...

相关阅读