外贸公司网站开发,手机网站建设全包,网页游戏排行榜2024,淄博市住房和城乡建设厅网站在之前的《Spring Cloud构建微服务架构#xff1a;分布式配置中心》一文中#xff0c;我们介绍的Spring Cloud Server配置中心采用了Git的方式进行配置信息存储。这一设计巧妙的利用Git自身机制以及其他具有丰富功能的Git服务端产品#xff0c;让Spring Cloud Server在配置存…在之前的《Spring Cloud构建微服务架构分布式配置中心》一文中我们介绍的Spring Cloud Server配置中心采用了Git的方式进行配置信息存储。这一设计巧妙的利用Git自身机制以及其他具有丰富功能的Git服务端产品让Spring Cloud Server在配置存储和管理的上避开了很多与管理相关的复杂实现使其具备了配置中心存储配置和读取配置的基本能力而更上层的管理机制由于不具备普遍适用性所以Spring Cloud Server并没有自己去实现这部分内容而是通过Git服务端产品来提供一部分实现如果还需要更复杂的功能也能自己实现与定义。即便如此对于Spring Cloud Server默认使用Git来存储配置的方案一直以来还是饱受争议。所以本文将介绍一下Spring Cloud Config从Edgware版本开始新增的一种配置方式采用数据库存储配置信息。
构建配置中心服务端
第一步创建一个基础的Spring Boot项目在pom.xml中引入几个主要依赖
spring-cloud-config-server配置中心的基础依赖spring-boot-starter-jdbc由于需要访问数据库所以需要加载jdbc的依赖mysql-connector-javaMySQL数据库的连接包flyway-core该内容非强制主要用来管理schema如果您不了解可以看一下这篇文章
parent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version1.5.11.RELEASE/version relativePath//parentdependencies dependency groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-config-server/artifactId /dependency dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-jdbc/artifactId /dependency dependency groupIdorg.flywaydb/groupId artifactIdflyway-core/artifactId version5.0.3/version /dependency dependency groupIdmysql/groupId artifactIdmysql-connector-java/artifactId version5.1.21/version /dependency/dependenciesdependencyManagement dependencies dependency groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-dependencies/artifactId versionEdgware.SR3/version typepom/type scopeimport/scope /dependency /dependencies/dependencyManagement第二步准备schema创建文件。在resources下创建schema目录并加入V1__Base_version.sql文件具体内容如下
CREATE TABLE properties ( id int(11) NOT NULL, key varchar(50) NOT NULL, value varchar(500) NOT NULL, application varchar(50) NOT NULL, profile varchar(50) NOT NULL, label varchar(50) NOT NULL, PRIMARY KEY (id)) ENGINEInnoDB DEFAULT CHARSETutf8;该脚本会在程序运行时由flyway自动执行 第三步创建应用主类具体如下
EnableConfigServerSpringBootApplicationpublic class ConfigServerBootstrap { public static void main(String[] args) { ApplicationContext context SpringApplication.run(ConfigServerBootstrap.class); // 测试用数据仅用于本文测试使用 JdbcTemplate jdbcTemplate context.getBean(JdbcTemplate.class); jdbcTemplate.execute(delete from properties); jdbcTemplate.execute(INSERT INTO properties VALUES(1, com.didispace.message, test-stage-master, config-client, stage, master)); jdbcTemplate.execute(INSERT INTO properties VALUES(2, com.didispace.message, test-online-master, config-client, online, master)); jdbcTemplate.execute(INSERT INTO properties VALUES(3, com.didispace.message, test-online-develop, config-client, online, develop)); jdbcTemplate.execute(INSERT INTO properties VALUES(4, com.didispace.message, hello-online-master, hello-service, online, master)); jdbcTemplate.execute(INSERT INTO properties VALUES(5, com.didispace.message, hello-online-develop, hello-service, online, develop)); }}这里增加了一些测试用数据以便于后续的配置读取验证。 第四步配置application.properties具体内容如下
spring.application.nameconfig-server-dbserver.port10020spring.profiles.activejdbcspring.cloud.config.server.jdbc.sqlSELECT KEY, VALUE from PROPERTIES where APPLICATION? and PROFILE? and LABEL?spring.datasource.urljdbc:mysql://localhost:3306/config-server-dbspring.datasource.usernamerootspring.datasource.passwordspring.datasource.driver-class-namecom.mysql.jdbc.Driverflyway.locations/schema这里主要涉及几个配置
spring.profiles.activejdbc必须设置将配置中心的存储实现切换到jdbc的方式spring.cloud.config.server.jdbc.sql非必须这里由于采用mysql数据源key、value是保留关键词原生的实现语句会报错所以需要重写一下这句查询语句如果存储的表结构设计不同于上面准备的内容也可以通过这个属性的配置来修改配置的获取逻辑spring.datasource.*存储配置信息的数据源配置这里采用mysql开发者根据自己实际情况修改flyway.locationsflyway加载schema创建sql的位置
服务端配置验证
完成了上一节内容之后我们就已经构建一个通过数据酷来存储配置内容的配置中心了下面我们可以通过配置中心暴露的端点来尝试读取配置。
第一步先将上面构建的配置中心启动起来。
第二步验证配置信息获取
curl http://localhost:10020/config-client/stage/获取信息config-client服务stage环境的配置内容根据上面的数据准备我们会获得如下返回内容
{ name: config-client, profiles: [ stage ], label: null, version: null, state: null, propertySources: [ { name: config-client-stage, source: { com.didispace.message: test-stage-master } } ]}curl http://localhost:10020/hello-service/stage/develop获取信息hello-service服务stage环境develop标签的配置内容根据上面的数据准备我们会获得如下返回内容
{ name: hello-service, profiles: [ online ], label: develop, version: null, state: null, propertySources: [ { name: hello-service-online, source: { com.didispace.message: hello-online-develop } } ]}关于如何访问Spring Cloud Config构建配置中心获取配置信息的详细内容可以查看前文《Spring Cloud构建微服务架构分布式配置中心》本文不做详细介绍。
总结
本文主要具体介绍了在Spring Cloud Config在Edgware版本开始新增的JDBC存储的使用思路具体使用实际上还有很多可以优化的空间比如索引的优化、查询语句的优化如果还需要进一步定制管理对于表结构的优化也是很有必要的。
最后安利一个基于Spring Cloud Config的配置管理项目https://github.com/dyc87112/spring-cloud-config-admin正在紧锣密鼓的开发中尽情期待
本文示例
读者可以根据喜好选择下面的两个仓库中查看config-server-db和config-client两个项目
Githubhttps://github.com/dyc87112/SpringCloud-Learning/Giteehttps://gitee.com/didispace/SpringCloud-Learning/
如果您对这些感兴趣欢迎star、follow、收藏、转发给予支持