springcloud实战之10 分布式配置中心(config)

傷城~ 2022-06-03 01:43 254阅读 0赞

基于为服务群,如果为每个服务读取自己的配置文件,有点重复造轮子。spingcloud提供了一个git远程仓库来创建分布式配置中心。

构建Config Server

创建一个springcloud-server-config项目

添加依赖

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>1.5.9.RELEASE</version>
  5. <relativePath />
  6. </parent>
  7. <dependencyManagement>
  8. <dependencies>
  9. <dependency>
  10. <groupId>org.springframework.cloud</groupId>
  11. <artifactId>spring-cloud-dependencies</artifactId>
  12. <version>Edgware.RELEASE</version>
  13. <type>pom</type>
  14. <scope>import</scope>
  15. </dependency>
  16. </dependencies>
  17. </dependencyManagement>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.cloud</groupId>
  21. <artifactId>spring-cloud-config-server</artifactId>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework.cloud</groupId>
  25. <artifactId>spring-cloud-starter-eureka</artifactId>
  26. </dependency>
  27. </dependencies>

添加配置文件

新建application.yml

  1. server:
  2. port: 8001
  3. spring:
  4. application:
  5. name: springcloud-server-config
  6. cloud:
  7. config:
  8. server:
  9. git:
  10. uri: https://github.com/shiyuan2he/springcloud
  11. search-paths: springcloud-repo-config ## 配置仓库路径下的相对搜索位置,可以配置多个
  12. username:
  13. password:
  14. label: master
  15. eureka:
  16. client:
  17. service-url:
  18. defaultZone: http://peer1:8080/eureka/

添加入口类

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

Server config已经配置完成

构建Config Client

添加依赖

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>1.5.9.RELEASE</version>
  5. <relativePath />
  6. </parent>
  7. <dependencyManagement>
  8. <dependencies>
  9. <dependency>
  10. <groupId>org.springframework.cloud</groupId>
  11. <artifactId>spring-cloud-dependencies</artifactId>
  12. <version>Edgware.RELEASE</version>
  13. <type>pom</type>
  14. <scope>import</scope>
  15. </dependency>
  16. </dependencies>
  17. </dependencyManagement>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.cloud</groupId>
  21. <artifactId>spring-cloud-starter-config</artifactId>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-web</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.cloud</groupId>
  29. <artifactId>spring-cloud-starter-eureka</artifactId>
  30. </dependency>
  31. </dependencies>

添加配置文件

新建bootstrap.properties

  1. spring.application.name=springcloud-service-config
  2. spring.cloud.config.label=master
  3. spring.cloud.config.profile=dev
  4. spring.cloud.config.uri= http://localhost:8001/
  5. server.port=8002
  6. eureka.client.serviceUrl.defaultZone=http://peer1:8080/eureka/
  7. ## 从配置中心读取文件
  8. spring.cloud.config.discovery.enabled=true
  9. ## 配置中心的servieId,即服务名。
  10. spring.cloud.config.discovery.serviceId=springcloud-config-server

创建入口类

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

创建web层接口

  1. @RestController
  2. @RequestMapping(value = "/api/rest")
  3. public class RestfulController {
  4. @Value("${user.name}")
  5. String name;
  6. @Value("${user.age}")
  7. String age ;
  8. @GetMapping("/name")
  9. public String getName(){
  10. return name +":"+ age;
  11. }
  12. }

构建仓库

在仓库下新建配置文件springcloud-service-config-dev.properties

内容:

  1. user.name=hehe
  2. user.age=18

启动项目

启动springcloud-service-config项目

测试项目

调用http://localhost:8002/api/rest/name

效果图:
这里写图片描述

发表评论

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

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

相关阅读