分布式配置中心spring cloud config

以你之姓@ 2022-05-20 09:51 317阅读 0赞

spring cloud config 是用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,分为服务端和客户端。

服务端:分布式配置中心,是一个独立的微服务,用来连接配置仓库并为客户端提供配置信息;

客户端:微服务框架中微服务应用或基础设施。

服务端:

pom.xml引入依赖:

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>1.5.4.RELEASE</version>
  5. <relativePath />
  6. </parent>
  7. <dependencies>
  8. <dependency>
  9. <groupId>org.springframework.cloud</groupId>
  10. <artifactId>spring-cloud-starter-config</artifactId>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.springframework.cloud</groupId>
  14. <artifactId>spring-cloud-config-server</artifactId>
  15. </dependency>
  16. </dependencies>
  17. <dependencyManagement>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.cloud</groupId>
  21. <artifactId>spring-cloud-dependencies</artifactId>
  22. <version>Dalston.SR1</version>
  23. <type>pom</type>
  24. <scope>import</scope>
  25. </dependency>
  26. </dependencies>
  27. </dependencyManagement>
  28. <build>
  29. <plugins>
  30. <plugin>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-maven-plugin</artifactId>
  33. </plugin>
  34. </plugins>
  35. </build>

2.在程序主类开启spring cloud config 的服务端功能:

  1. @EnableConfigServer
  2. @SpringBootApplication

3.在application.properties添加配置类信息:

  1. spring.cloud.config.server.git.uri=http://git.oschina.net/didispace/SpringCloud-Learning/
  2. spring.cloud.config.server.git.searchPaths=spring_cloud_in_action/config-repo
  3. spring.cloud.config.server.git.username=username
  4. spring.cloud.config.server.git.password=password

客户端:

客户端配置映射:

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

创建主应用类:

  1. @SpringBootApplication

创建bootstrap.properties文件:

  1. spring.application.name=didispace 对应文件规则{application}部分
  2. spring.cloud.config.profile=dev 对应文件规则{profile}部分
  3. spring.cloud.config.label=master 对应文件规则{label}部分
  4. spring.cloud.config.uri=http://localhost:7001/
  5. server.port=7002

追加:和eureka的整合与本地配置:

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-eureka</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.cloud</groupId>
  7. <artifactId>spring-cloud-config-server</artifactId>
  8. </dependency>
  9. @EnableDiscoveryClient
  10. @EnableConfigServer
  11. @SpringBootApplication
  12. public class SpringCloudConfigApplication {
  13. public static void main(String[] args) {
  14. SpringApplication.run(SpringCloudConfigApplication.class, args);
  15. }
  16. }
  17. server:
  18. port: 8005
  19. eureka:
  20. client:
  21. serviceUrl:
  22. defaultZone: http://localhost:8001/eureka/
  23. instance:
  24. prefer-ip-address: true
  25. instance-id: ${spring.cloud.client.ipAddress}:${server.port}
  26. spring:
  27. cloud:
  28. config:
  29. server:
  30. native:
  31. search-locations: classpath:自定义路径/ # 搜索src/main/resource 下自定义的路径
  32. application:
  33. name: service-config
  34. profiles:
  35. active: native # 配置使用本地储存

客户端:

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-eureka</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.cloud</groupId>
  7. <artifactId>spring-cloud-starter-config</artifactId>
  8. </dependency>
  9. @RestController
  10. @EnableDiscoveryClient // 在注册中心发现服务
  11. @SpringBootApplication
  12. public class SpringCloudConfigClientApplication {
  13. public static void main(String[] args) {
  14. SpringApplication.run(SpringCloudConfigClientApplication.class, args);
  15. }
  16. @Value("${from}") // 从对应的配置中心找到文件并把属性注入到value值中
  17. private String value;
  18. @RequestMapping("/hello")
  19. public String hello() {
  20. return "hello" + value;
  21. }
  22. }
  23. server:
  24. port: 8006
  25. spring:
  26. application:
  27. name: service-config-client
  28. cloud:
  29. config:
  30. discovery:
  31. enabled: true # 通过服务发现的方式去找配置中心
  32. service-id: service-config # 配置中心的名字,直接配置名称可以在配置中心集群的时候实现负载均衡
  33. profile: base # 对应配置中心文件的${profile}部分
  34. eureka:
  35. client:
  36. serviceUrl:
  37. defaultZone: http://localhost:8001/eureka/
  38. instance:
  39. prefer-ip-address: true # 使用ip地址注册到eureka server
  40. instance-id: ${spring.cloud.client.ipAddress}:${server.port} # 在eureka server中看到的status显示为具体的ip地址和port

发表评论

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

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

相关阅读