网站建设集群化的必要,自己的网站怎么做的,wordpress 点评网,织梦网站开发转载请标明出处#xff1a; https://blog.csdn.net/forezp/...本文出自方志朋的博客 Spring Cloud Config Server最常见是将配置文件放在本地或者远程Git仓库#xff0c;放在本地是将将所有的配置文件统一写在Config Server工程目录下#xff0c;如果需要修改配置#xff0… 转载请标明出处 https://blog.csdn.net/forezp/...本文出自方志朋的博客 Spring Cloud Config Server最常见是将配置文件放在本地或者远程Git仓库放在本地是将将所有的配置文件统一写在Config Server工程目录下如果需要修改配置需要重启config server放在Git仓库是将配置统一放在Git仓库可以利用Git仓库的版本控制。本文将介绍使用另外一种方式存放配置信息即将配置存放在Mysql中。 整个流程Config Sever暴露Http API接口Config Client 通过调用Config Sever的Http API接口来读取配置Config Server的配置信息Config Server从数据中读取具体的应用的配置。流程图如下 案例实战 在本案例中需要由2个工程分为config-server和config-client其中config-server工程需要连接Mysql数据库读取配置config-client则在启动的时候从config-server工程读取。本案例Spring Cloud版本为Greenwich.RELEASESpring Boot版本为2.1.0.RELEASE。 工程描述config-server端口8769从数据库中读取配置config-client端口8083从config-server读取配置搭建config-server工程 创建工程config-server在工程的pom文件引入config-server的起步依赖mysql的连接器jdbc的起步依赖代码如下: dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-config-server/artifactId
/dependency
dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactId
/dependency
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-jdbc/artifactId
/dependency在工程的配置文件application.yml下做以下的配置 spring:profiles:active: jdbcapplication:name: config-jdbc-serverdatasource:url: jdbc:mysql://127.0.0.1:3306/config-jdbc?useUnicodetruecharacterEncodingutf8characterSetResultsutf8serverTimezoneGMT%2B8username: rootpassword: 123456driver-class-name: com.mysql.jdbc.Drivercloud:config:label: masterserver:jdbc: true
server:port: 8769
spring.cloud.config.server.jdbc.sql: SELECT key1, value1 from config_properties where APPLICATION? and PROFILE? and LABEL?其中spring.profiles.active为spring读取的配置文件名从数据库中读取必须为jdbc。spring.datasource配置了数据库相关的信息spring.cloud.config.label读取的配置的分支这个需要在数据库中数据对应。spring.cloud.config.server.jdbc.sql为查询数据库的sql语句该语句的字段必须与数据库的表字段一致。 在程序的启动文件ConfigServerApplication加上EnableConfigServer注解开启ConfigServer的功能代码如下 SpringBootApplication
EnableConfigServer
public class ConfigServerApplication {public static void main(String[] args) {SpringApplication.run(ConfigServerApplication.class, args);}
} 初始化数据库 由于Config-server需要从数据库中读取所以读者需要先安装MySQL数据库安装成功后创建config-jdbc数据库数据库编码为utf-8然后在config-jdbc数据库下执行以下的数据库脚本 CREATE TABLE config_properties (id bigint(20) NOT NULL AUTO_INCREMENT,key1 varchar(50) COLLATE utf8_bin NOT NULL,value1 varchar(500) COLLATE utf8_bin DEFAULT NULL,application varchar(50) COLLATE utf8_bin NOT NULL,profile varchar(50) COLLATE utf8_bin NOT NULL,label varchar(50) COLLATE utf8_bin DEFAULT NULL,PRIMARY KEY (id)
) ENGINEInnoDB AUTO_INCREMENT3 DEFAULT CHARSETutf8 COLLATEutf8_bin其中key1字段为配置的key,value1字段为配置的值application字段对应于应用名profile对应于环境label对应于读取的分支一般为master。 插入数据config-client 的2条数据包括server.port和foo两个配置具体数据库脚本如下:
insert into config_properties (id, key1, value1, application, profile, label) values(1,server.port,8083,config-client,dev,master);
insert into config_properties (id, key1, value1, application, profile, label) values(2,foo,bar-jdbc,config-client,dev,master); 搭建config-client 在 config-client工程的pom文件引入web和config的起步依赖代码如下 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId
/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-config/artifactId
/dependency在程序的启动配置文件 bootstrap.yml做程序的相关配置一定要是bootstrap.yml不可以是application.ymlbootstrap.yml的读取优先级更高配置如下 spring:application:name: config-clientcloud:config:uri: http://localhost:8769fail-fast: trueprofiles:active: dev其中spring.cloud.config.uri配置的config-server的地址spring.cloud.config.fail-fast配置的是读取配置失败后执行快速失败。spring.profiles.active配置的是spring读取配置文件的环境。 在程序的启动文件ConfigClientApplication写一个RestAPI读取配置文件的foo配置返回给浏览器代码如下 SpringBootApplication
RestController
public class ConfigClientApplication {public static void main(String[] args) {SpringApplication.run(ConfigClientApplication.class, args);}Value(${foo})String foo;RequestMapping(value /foo)public String hi(){return foo;}
}依次启动2个工程其中config-client的启动端口为8083这个是在数据库中的可见config-client从 config-server中读取了配置。在浏览器上访问http://localhost:8083/foo浏览器显示bar-jdbc,这个是在数据库中的可见config-client从 config-server中读取了配置。 参考资料 https://cloud.spring.io/sprin... 源码下载 https://github.com/forezp/Spr... div p aligncenterimg srchttps://www.fangzhipeng.com/img/avatar.jpg width258 height258/br扫一扫支持下作者吧
/p
p aligncenter stylemargin-top: 15px; font-size: 11px;color: #cc0000;strong转载本站文章请注明作者和出处 a hrefhttps://www.fangzhipeng.com方志朋的博客/a/strong
/p /div