spring cloud congif 基于db的配置中心【(spring cloud congif + mysql)jpa调用】

Java程序员都很会遇到这样的烦恼,工作中在配置spring cloud congif 配置中心的时候,项目要求或者经理要求不使用git存储,而是使用db存储,下面是我自己写的一个基于db的配置中心,此配置中没有集成eureka,有需要的小伙伴自己添加一下就好。

1.配置

Idea+SpringCloud+Mysql

2.pom依赖



	4.0.0

	com.didispace
	config-server-db
	1.0.0
	jar

	config-server-db
	Spring Cloud In Action

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

	
		UTF-8
		1.8
	

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

		
			org.springframework.boot
			spring-boot-starter-jdbc
		
		
			mysql
			mysql-connector-java
			5.1.21
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				Finchley.RELEASE
				pom
				import
			
		
	
	
	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			

			
				org.apache.maven.plugins
				maven-surefire-plugin
				
					false
					false
				
			
		


	

pom中需要注意版本的问题,尽量使用一样的版本,否则会出现异常。

3.application.properties配置

spring.application.name=config-server-db
server.port=10020

spring.profiles.active=jdbc
#KEY和VALUE 都是mysql的保留字段,所以这里重写了这个sql
spring.cloud.config.server.jdbc.sql=SELECT `KEY`, `VALUE` from properties where APPLICATION=? and PROFILE=? and LABEL=?
#db的配置
spring.datasource.url=jdbc:mysql://192.162.31.181:3306/aobo_config?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=****#此处配置到自己的数据库连接就好了
spring.datasource.password=****
spring.datasource.driver-class-name=com.mysql.jdbc.Driver






# ============ JPA Configuration:  ============
#spring.jpa.database=MYSQL
## Show or not log for each sql query
#spring.jpa.show-sql=true
#spring.jpa.generate-ddl=true
## Hibernate ddl auto (create, create-drop, update)
#spring.jpa.hibernate.ddl-auto=update
##spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
#spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
##spring.jpa.database=org.hibernate.dialect.MySQL5InnoDBDialect
#spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

4.主启动类

package com.didispace.config.server.db;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

@EnableConfigServer
@SpringBootApplication
public class ConfigServerBootstrap {

    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(ConfigServerBootstrap.class);


}

5.mysql数据表

CREATE TABLE `properties` (
  `id` varchar(32) NOT NULL,
  `key` varchar(50) DEFAULT NULL,
  `value` varchar(200) DEFAULT NULL,
  `application` varchar(20) DEFAULT NULL,
  `profile` varchar(10) DEFAULT NULL,
  `label` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

创建完可以自行添加一些测试数据,以便后期访问使用。

6.调用

上述配置完成后,只需要在你自己的项目中创建一个bootstrap.properties文件,添加如下配置

#端口号
spring.cloud.config.uri=config-center:10020
#profile
spring.cloud.config.profile=pro
#label
spring.cloud.config.label=master

配置完成后,先启动配置中心,在启动自己的项目。

大功告成!

最后附上源码,希望大家不喜勿喷谢谢!

https://github.com/shuzhen123/config-server-db

 

你可能感兴趣的:(java)