Spring Cloud Config配置中心可以使用git,svn以及jdbc方式实现配置存储。
由于公司生产环境问题,需要实现jdbc方式,数据库为mysql
依赖包
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- MYSQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>
application.yml
spring.profiles.active=jdbc,自动实现JdbcEnvironmentRepository,sql语句自定义,否则会默认为“SELECT KEY, VALUE from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?”,具体可以参考JdbcEnvironmentRepository实现。本人数据库建表为config_info,由于key,value和profile是mysql关键字,所以我都加了e。当然表名字段名可以自定义,key加单引号就好,只要select出来是两个字段,框架会自动包装到environment的map<key,value>。
server:
port: 8041
spring:
application:
name: config-server-jdbc
profiles:
active: jdbc
cloud:
config:
server:
default-label: master
jdbc:
sql: SELECT KEYE , VALUEE FROM config_info where APPLICATION=? and PROFILEE=? and LABEL=?
#####################################################################################################
# mysql 属性配置
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://ip:3306/db
username: username
password: password
#####################################################################################################
springboot主程序
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
启动程序后,访问http://localhost:8041/label/application-profile.properties得到配置信息。
问题记录:
1、在阅读官网http://cloud.spring.io/spring-cloud-config/single/spring-cloud-config.html中关于jdbc配置中心实现内容中:
2.1.6 JDBC Backend
Spring Cloud Config Server supports JDBC (relation database) as a backend for configuration properties. You can enable this feature by adding spring-jdbc to the classpath, and using the "jdbc" profile, or by adding a bean of type JdbcEnvironmentRepository. Spring Boot will configure a data source if you include the right dependencies on the classpath (see the user guide for more details on that).
The database needs to have a table called "PROPERTIES" with columns "APPLICATION", "PROFILE", "LABEL" (with the usual Environment meaning), plus "KEY" and "VALUE" for the key and value pairs in Properties style. All fields are of type String in Java, so you can make them VARCHAR of whatever length you need. Property values behave in the same way as they would if they came from Spring Boot properties files named {application}-{profile}.properties, including all the encryption and decryption, which will be applied as post-processing steps (i.e. not in the repository implementation directly).
我加入了spring-jdbc
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
启动程序报错
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method jdbcEnvironmentRepository in org.springframework.cloud.config.server.config.JdbcRepositoryConfiguration required a bean of type 'org.springframework.jdbc.core.JdbcTemplate' that could not be found.
- Bean method 'jdbcTemplate' not loaded because @ConditionalOnSingleCandidate (types: javax.sql.DataSource; SearchStrategy: all) did not find any beans
Action:
Consider revisiting the conditions above or defining a bean of type 'org.springframework.jdbc.core.JdbcTemplate' in your configuration.
后来发现原来是需要spring boot的jdbc
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>