【开发技巧/经验分享】使用SpringCloudCofig实现远程配置管理(服务端、客户端实现)

野性酷女 2023-07-05 11:43 13阅读 0赞

1. 为什么需要需要远程配置管理?

首先我们分析传统的配置管理,传统项目中我们的配置文件都是写死在项目中,如果更改一些配置可能导致整个应用需要重新编译,并且我们在开发中也是整个开发团队一起开发的,如果其中一个人修改了配置那么所有的配置都将受到影响,所以在这一方面会有配置冲突的问题,而下面要提到的SpringCloudConfig就是为了解决以上问题而衍生出来的。

2. 如何使用SpringCloudConfig?

2.1 配置中心服务端实现
2.1.1 构建项目,并导入相关依赖,所需配置项如下所示
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <dependencies>
  4. <dependency>
  5. <groupId>org.springframework.boot</groupId>
  6. <artifactId>spring-boot-starter-web</artifactId>
  7. </dependency>
  8. <dependency>
  9. <groupId>org.springframework.cloud</groupId>
  10. <artifactId>spring-cloud-config-server</artifactId>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.springframework.cloud</groupId>
  14. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-test</artifactId>
  19. <scope>test</scope>
  20. </dependency>
  21. </dependencies>
  22. <dependencyManagement>
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-dependencies</artifactId>
  27. <version>${spring-cloud.version}</version>
  28. <type>pom</type>
  29. <scope>import</scope>
  30. </dependency>
  31. </dependencies>
  32. </dependencyManagement>
  33. </project>
2.1.2 编写相关配置文件
  1. spring:
  2. application:
  3. name: order-config
  4. cloud:
  5. config:
  6. server:
  7. git:
  8. uri: https://gitee.com/coder1024/order-config #指定远程git仓库目录
  9. basedir: /Users/qingyun/IdeaProjects/springcloud-wechatordersys/basedir #指定本地git仓库目录 建议不要放在项目中 防止git把项目覆盖
  10. username: 你的git服务用户名
  11. password: 你的git服务密码
  12. eureka: #因为配置需要被其他服务使用所以这里需要将服务注册到eureka中
  13. client:
  14. service-url:
  15. defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka
2.1.3 在主启动类上添加@EnableConfigServer注解,开启配置中心服务端
  1. package com.qingyun.orderconfig;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.config.server.EnableConfigServer;
  5. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  6. @SpringBootApplication
  7. @EnableEurekaClient
  8. @EnableConfigServer
  9. public class OrderConfigApplication {
  10. public static void main(String[] args) {
  11. SpringApplication.run(OrderConfigApplication.class, args);
  12. }
  13. }
2.1.4 开启服务进行测试

事先在远程库中创建好配置文件
在这里插入图片描述
通过浏览器访问
在这里插入图片描述
修改后缀为.properties
在这里插入图片描述
由此可见配置中心能够灵活的根据配置文件的给定后缀名灵活的转换格式,实质上这个规则的表达式为/{name}-{profiles}.后缀名,name表示的是服务名(文件名),profiles表示是环境(dev开发环境、prod生产环境、test测试环境)。

当然配置中心的规则可不止上面一种,比如还有一种规则为/{lable}/{name}-{profiles}.后缀名,{lable}表示的仓库中的一个分支。

2.2 配置中心服务端实现
2.2.1 在需要使用到配置服务的工程中导入相关依赖,所需依赖如下所示
  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-config-client</artifactId>
  4. </dependency>
2.2.2 将application.yml改成bootstrap.yml,并去除一些本地上不必要的配置(bootstrap要比application配置文件优先加载)
  1. spring:
  2. application:
  3. name: productService
  4. cloud:
  5. config:
  6. discovery:
  7. enabled: true #开启配置中心发现
  8. service-id: config-server #配置中心的名称
  9. profiles:
  10. active: test
  11. eureka:
  12. client:
  13. service-url:
  14. defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka

这里需要注意的是,像一些比如端口号指定、数据库配置等信息使用了配置中心后在本地都可以不再配置,直接拉取远程配置就好。但是这也也要特别注意像,eureka、服务名必须要配置,要不然无法找到配置中心拉取相关配置。

2.2.3 在远程创建于服务名称相同的配置文件。

productServer.yml

  1. spring:
  2. datasource:
  3. driver-class-name: com.mysql.jdbc.Driver
  4. url: jdbc:mysql://localhost:3306/wechat_ordersys?useUnicode=true&characterEncoding=utf-8&useSSL=false
  5. username: root
  6. password: 123456

productServer-test.yml

  1. server:
  2. port: 8082
  3. spring:
  4. datasource:
  5. driver-class-name: com.mysql.jdbc.Driver
  6. url: jdbc:mysql://localhost:3306/wechat_ordersys?useUnicode=true&characterEncoding=utf-8&useSSL=false
  7. username: root
  8. password: 123456

productServer-dev.yml

  1. server:
  2. port: 8081
  3. spring:
  4. datasource:
  5. driver-class-name: com.mysql.jdbc.Driver
  6. url: jdbc:mysql://localhost:3306/wechat_ordersys?useUnicode=true&characterEncoding=utf-8&useSSL=false
  7. username: root
  8. password: 123456

我们这里创建多个配置文件是为了方便配置文件的切换,因为实际开发中肯定会有开发环境(dev后缀)、测试环境(test后缀)等,而没有加后缀的在加载的时候也会一起跟对应的配置组合,没有加后缀的配置文件一般用与配置一些服务中的公共配置。

2.2.4 启动项目

test环境
在这里插入图片描述
dev环境
在这里插入图片描述
从上图可以分析出,在项目中可以通过spring.profiles.active动态选择运行环境。

3. 这种方式存什么不足?

这种方式虽然解决了组织开发中的配置冲突,但是它无法做到配置信息的实时更新,也就是没有做到更改了配置后无需重新启动、编译项目。

发表评论

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

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

相关阅读

    相关 Netty实现客户服务通信

    实现一个客户端与服务端通信的程序,可以使用socket网络编程来实现,而Netty作为一个封装了JDK的NIO通讯的异步事件驱动的网络应用框架,也同样可以实现。 1.创建M