分布式配置中心(spring cloud config)使用数据库的方式配置

spring cloud config搭建 借助sts(spring官方版eclipse)

分布式配置中心(spring cloud config)使用数据库的方式配置_第1张图片

第一步:创建项目

1.

分布式配置中心(spring cloud config)使用数据库的方式配置_第2张图片

2.

分布式配置中心(spring cloud config)使用数据库的方式配置_第3张图片

3.

分布式配置中心(spring cloud config)使用数据库的方式配置_第4张图片

经过上述操作生成了项目

第二步:引入必要的pom文件

 附上pom.xml



	4.0.0

	com.whty
	spring_config
	0.0.1-SNAPSHOT
	jar

	demo
	

	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.0.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
		Finchley.RC1
	

	
		
			org.springframework.cloud
			spring-cloud-config-server
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
		
			org.springframework.boot
			spring-boot-starter-jdbc
		
		
			mysql
			mysql-connector-java
			runtime
		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			1.3.2
		
		
			org.apache.commons
			commons-lang3
		
		
			commons-io
			commons-io
			2.5
		
		
			com.alibaba
			druid
			1.1.0
		

		
			com.github.pagehelper
			pagehelper
			5.1.3
		
	

	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				${spring-cloud.version}
				pom
				import
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

	
		
			spring-milestones
			Spring Milestones
			https://repo.spring.io/milestone
			
				false
			
		
	



 

第三步:编写配置文件

需要做的

(1)设置端口

(2)设置服务名

(3)读取配置信息方式:native使用本地属性文件,jdbc从数据库中读取

(4)配置数据源

(5)配置查询配置信息的sql

附上配置文件

#配置工程启动端口
server.port=8000

spring.application.name=config-center


#读取配置信息方式:native使用本地属性文件,jdbc从数据库中读取
spring.profiles.active = jdbc
#属性文件应用场景标识,配置文件命名是有规则的,规则为应用场景的${spring.application.name}-${spring.cloud.config.profile}.properties
#spring.cloud.config.profile=configInfo
#属性文件地址,只要指定文件夹的路径,在工程中格式举例为classpath:/properties,配置文件在本地但不在工程中格式举例为:E:/properties
#spring.cloud.config.server.native.searchLocations=classpath:/properties
spring.cloud.config.server.jdbc=true
spring.cloud.config.server.jdbc.sql=SELECT key_info, value_info from cc_config_info where (application_name=? or application_name='GLOBAL') and profile=? and label=?

#####数据库配置信息#####
spring.datasource.name: hx_config
spring.datasource.url: jdbc:mysql://10.5.200.151:3306/hx_config?useUnicode=true&characterEncoding=utf8&autoReconnect=true
spring.datasource.username: examkw
spring.datasource.password: exama1r1
spring.datasource.driver-class-name: com.mysql.jdbc.Driver

# 使用druid数据源
spring.datasource.type: com.alibaba.druid.pool.DruidDataSource
spring.datasource.filters: stat,slf4j
spring.datasource.maxActive: 20
spring.datasource.initialSize: 1
spring.datasource.maxWait: 60000
spring.datasource.minIdle: 1
spring.datasource.timeBetweenEvictionRunsMillis: 60000
spring.datasource.minEvictableIdleTimeMillis: 300000
spring.datasource.validationQuery: select 'x'
spring.datasource.testWhileIdle: true
spring.datasource.testOnBorrow: false
spring.datasource.testOnReturn: false
spring.datasource.poolPreparedStatements: true
spring.datasource.maxPoolPreparedStatementPerConnectionSize: 20
spring.datasource.maxOpenPreparedStatements: 20

#==================mybaties Config Start==================
#ORM Bean Package
mybatis.mapper-locations=classpath:mybatis/*.xml
mybatis.type-aliases-package: com.whty.hxx.config.web.model

mapper.mappers:  com.whty.hxx.config.web.mapper
mapper.not-empty: false
mapper.identity: MYSQL

#pagehelper
pagehelper.helperDialect: mysql
pagehelper.reasonable: true
pagehelper.supportMethodsArguments: true
pagehelper.params: count=countSql
pagehelper.returnPageInfo: check
#打印mybatiesSql语句
logging.level.com.whty.hxx.config.web.mapper=DEBUG

 数据库表结构

CREATE TABLE `cc_config_info` (
  `config_info_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `key_info` varchar(100) DEFAULT NULL COMMENT '属性名',
  `value_info` varchar(255) DEFAULT NULL COMMENT '属性值',
  `application_name` varchar(100) DEFAULT NULL COMMENT '应用名,GLOBAL为全局',
  `label` varchar(20) DEFAULT 'master' COMMENT '配置标识',
  `profile` varchar(20) DEFAULT 'configInfo',
  `remark` varchar(255) DEFAULT NULL COMMENT '备注',
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`config_info_id`)
) ENGINE=MyISAM AUTO_INCREMENT=1055 DEFAULT CHARSET=utf8 COMMENT='配置信息';

启动

分布式配置中心(spring cloud config)使用数据库的方式配置_第5张图片

启动测试项目

分布式配置中心(spring cloud config)使用数据库的方式配置_第6张图片

 

启动成功

分布式配置中心(spring cloud config)使用数据库的方式配置_第7张图片

读取到了

分布式配置中心(spring cloud config)使用数据库的方式配置_第8张图片

你可能感兴趣的:(springcloud)