功能实现:使用JDBC(mysql)方式实现Spring Cloud Config配置中心

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>

 

转载于:https://my.oschina.net/gore/blog/1609635

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值