SpringCloud教程 | 九.分布式配置中心(Spring Cloud Config)

冷不防 2021-09-26 11:48 450阅读 0赞

在分布式环境中,为了方便配置文件的管理,引入了spring cloud config组件,在该组件中,分为两个角色,一个是server,一个是client,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。

实战

一.config server

1.创建一个configserver项目,pom如下:

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>1.5.9.RELEASE</version>
  5. <relativePath/> <!-- lookup parent from repository -->
  6. </parent>
  7. <properties>
  8. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  9. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  10. <java.version>1.8</java.version>
  11. <spring-cloud.version>Edgware.SR1</spring-cloud.version>
  12. </properties>
  13. <dependencies>
  14. <dependency>
  15. <groupId>org.springframework.cloud</groupId>
  16. <artifactId>spring-cloud-config-server</artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-test</artifactId>
  21. <scope>test</scope>
  22. </dependency>
  23. <!-- 此包是为了解决异常 -->
  24. <dependency>
  25. <groupId>org.eclipse.jgit</groupId>
  26. <artifactId>org.eclipse.jgit</artifactId>
  27. <version>4.8.0.201706111038-r</version>
  28. </dependency>
  29. </dependencies>
  30. <dependencyManagement>
  31. <dependencies>
  32. <dependency>
  33. <groupId>org.springframework.cloud</groupId>
  34. <artifactId>spring-cloud-dependencies</artifactId>
  35. <version>${spring-cloud.version}</version>
  36. <type>pom</type>
  37. <scope>import</scope>
  38. </dependency>
  39. </dependencies>
  40. </dependencyManagement>
  41. <build>
  42. <plugins>
  43. <plugin>
  44. <groupId>org.springframework.boot</groupId>
  45. <artifactId>spring-boot-maven-plugin</artifactId>
  46. </plugin>
  47. </plugins>
  48. </build>

2.在运行类上加入@EnableConfigServer注解开启配置服务器的功能

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.cloud.config.server.EnableConfigServer;
  4. @SpringBootApplication
  5. @EnableConfigServer
  6. public class SpringCloudEurekaConfigServerApplication {
  7. public static void main(String[] args) {
  8. SpringApplication.run(SpringCloudEurekaConfigServerApplication.class, args);
  9. }
  10. }

3.application.properties文件配置以下:

  1. spring.application.name=config-server
  2. server.port=8888
  3. spring.cloud.config.server.git.uri=https://github.com/1154978352/config.git
  4. spring.cloud.config.server.git.searchPaths=/
  5. spring.cloud.config.server.git.username=username
  6. spring.cloud.config.server.git.password=password

spring.cloud.config.server.git.uri:配置git仓库地址
spring.cloud.config.server.git.searchPaths:配置仓库路径下的相对搜索位置,可以配置多个
spring.cloud.config.label:配置仓库的分支
spring.cloud.config.server.git.username:访问git仓库的用户名
spring.cloud.config.server.git.password:访问git仓库的用户密码
如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写,本例子是公开仓库,放心使用。

Spring Cloud Config也提供本地存储配置的方式。我们只需要设置属性spring.profiles.active=native,Config Server会默认从应用的src/main/resource目录下检索配置文件。也可以通过spring.cloud.config.server.native.searchLocations=file:F:/properties/属性来指定配置文件的位置。虽然Spring Cloud Config提供了这样的功能,但是为了支持更好的管理内容和版本控制的功能,还是推荐使用git的方式。

20180130143350467

如上图所示,在远程仓库中有git-dev.properties文件,该文件内的内容为from=git-dev,如下图所示

SouthEast

我们在地址栏上输入http://localhost:8888/from/dev,得到如下结果则证明连接仓库成功

  1. {"name":"from","profiles":["dev"],"label":null,"version":"04272bd9598899ac1174c07deb467e78ed157630","state":null,"propertySources":[]}

URL与配置文件的映射关系如下:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties

/{label}/{application}-{profile}.properties

二.构建一个config client

1.项目名称为spring-cloud-eureka-configClient,pom.xml如下

  1. <name>spring-cloud-eureka-configClient</name>
  2. <description>Demo project for Spring Boot</description>
  3. <parent>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-starter-parent</artifactId>
  6. <version>1.5.9.RELEASE</version>
  7. <relativePath/> <!-- lookup parent from repository -->
  8. </parent>
  9. <properties>
  10. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  11. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  12. <java.version>1.8</java.version>
  13. <spring-cloud.version>Edgware.SR1</spring-cloud.version>
  14. </properties>
  15. <dependencies>
  16. <dependency>
  17. <groupId>org.springframework.cloud</groupId>
  18. <artifactId>spring-cloud-starter-config</artifactId>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-web</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-tomcat</artifactId>
  27. <scope>provided</scope>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-test</artifactId>
  32. <scope>test</scope>
  33. </dependency>
  34. </dependencies>
  35. <dependencyManagement>
  36. <dependencies>
  37. <dependency>
  38. <groupId>org.springframework.cloud</groupId>
  39. <artifactId>spring-cloud-dependencies</artifactId>
  40. <version>${spring-cloud.version}</version>
  41. <type>pom</type>
  42. <scope>import</scope>
  43. </dependency>
  44. </dependencies>
  45. </dependencyManagement>
  46. <build>
  47. <plugins>
  48. <plugin>
  49. <groupId>org.springframework.boot</groupId>
  50. <artifactId>spring-boot-maven-plugin</artifactId>
  51. </plugin>
  52. </plugins>
  53. </build>

2.bootstarp.properties

【注意】:这边的spring.application.name以及spring.cloud.config.profile要与对应的配置文件名称一致

即spring.application.name:对应前配置文件中的{application}部分,spring.cloud.config.profile:对应前配置文件中的{profile}部分。

这里需要格外注意:下面这些属性必须配置在bootstrap.properties中,config部分内容才能被正确加载。因为config的相关配置会先于application.properties,而bootstrap.properties的加载也是先于application.properties。

  1. server.port=8001
  2. spring.application.name=gwd
  3. spring.cloud.config.profile=dev
  4. spring.cloud.config.label=master
  5. spring.cloud.config.uri= http://localhost:8888/

spring.cloud.config.label 指明远程仓库的分支
spring.cloud.config.uri= http://localhost:8888/ 指明配置服务中心的地址

SouthEast 1

3.controller

  1. import org.springframework.beans.factory.annotation.Value;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. /**
  5. * @FileName ConfigClientController.java
  6. * @Description:TODO
  7. * @author JackHisen(gu.weidong)
  8. * @version V1.0
  9. * @createtime 2018年1月29日 下午3:46:54
  10. * 修改历史:
  11. * 时间 作者 版本 描述
  12. *====================================================
  13. */
  14. @RestController
  15. public class ConfigClientController {
  16. @Value("${from}")
  17. String from;
  18. @RequestMapping("/hi")
  19. public String testMsg() {
  20. return from;
  21. }
  22. }

4.输入网址http://localhost:8001/hi,显示git-dev,则表示成功

发表评论

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

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

相关阅读