【问题标题】:Spring Cloud Config Server issue - Configuring multiple sources native and jdbcSpring Cloud Config Server 问题 - 配置多个源 native 和 jdbc
【发布时间】:2018-05-30 10:44:40
【问题描述】:

我想在 spring 云配置中连接到多个存储库,即本机(文件系统)和 jdbc。我创建了一个带有以下详细信息的 Spring Cloud 配置服务器

应用程序属性

server.port=8888
spring.profiles.include=native,jdbc

spring.cloud.config.server.native.search-locations=classpath:/config,classpath:/app1, classpath:/app2,classpath:/ep
encrypt.key=abcdef


spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/configuration?useSSL=false
spring.cloud.config.server.jdbc.sql=SELECT properties.key, properties.value from  PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?

spring.datasource.username=root
spring.datasource.password=root  

spring.cloud.config.server.native.order=1
spring.cloud.config.server.jdbc.order=2 

无论优先级如何,它总是从 jdbc 获取信息,而不是从本地获取信息。 我尝试将最后 2 个属性添加到 bootstrap.properties 仍然是相同的行为。 我错过了什么吗?我的配置正确吗?请推荐

【问题讨论】:

标签: spring configuration cloud


【解决方案1】:

在 application.yml 之前加载 spring boostrap.yml 所以你声明服务器端口,配置搜索位置和活动配置文件配置是这个堆栈的好方法,所以保持简单 boostrap.yml 并且 spring cloud 默认配置文件是本地的

并且在应用程序-"profile".yml 中具有环境和其他配置属性

还有你的 boostrap.yml 或类似的属性

server:
  port: 8888

spring:
  application:
    name: appName
  profiles:
    active: native,jdbc
  cloud:
     config:
       server:
         native:
           order: 1
           searchLocations: classpath:/config,classpath:/app1, classpath:/app2,classpath:/ep

并在 boostrap.yml 或 properties 的同一层中创建 applicaiton-jdbc.properties 或 yml 文件并声明 jdbc 属性

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: 'jdbc:mysql://localhost:3306/configuration?useSSL=false'
  cloud:
    config:
      server:
        jdbc:
          order: 2
          sql: 'SELECT properties.key, properties.value from  PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?'
  username: root
  password: root

你的配置服务器配置是这样的

@SpringBootApplication
@EnableConfigServer
@Import({JdbcEnvironmentRepository.class})
public class ConfigServer {

    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource dataSource() {
       return DataSourceBuilder.create().build();
    }

    @Bean
    public JdbcTemplate jdbcTemplate() {
        return new JdbcTemplate(dataSource());
    }

    public static void main(String[] arguments) {
        SpringApplication.run(ConfigServer.class, arguments);
    }  
}

【讨论】:

  • 感谢您的帮助,我尝试了您提到的方法,即分离出 application-jdbc.yml 并在 bootstrap.yml 中保持休息,但是如果表中的数据匹配,则始终从 jdbc 中获取数据,否则不会返回任何数据即使相同的标准应该从文件系统中获取数据。对于 jdbc,我正在添加一个依赖项 - spring-boot-starter-jdbc ,但是如果我们从文件系统中删除依赖项数据,则服务器会获取该数据。任何其他建议
  • 您的配置已为 JdbcEnvironmentRepository.class 添加此注解 @Import({JdbcEnvironmentRepository.class }) 到 @EnableConfigServer
  • 你能分享任何使用 @Import({JdbcEnvironmentRepository.class }) 的例子吗?在导入 JdbcEnvironmentRepository.class 时,我遇到了与设置 JdbcTemplate 相关的问题。当我做 Import({JdbcEnvironmentRepository.class ,JdbcTemplate.class}) 它给出数据源问题
  • @AmolDeopujari 我更新我的答案并添加示例代码
  • 感谢您的选择。同时,通过在引导程序中设置配置文件属性,我成功地支持本机和 jdbc,如下所示 - spring.profiles.active=jdbc 和导入的 Native Repository @Import({NativeEnvironmentRepository.class}) 。感谢您的帮助
猜你喜欢
  • 2020-04-22
  • 2017-06-23
  • 2020-09-05
  • 2016-09-29
  • 2022-01-14
  • 2017-10-12
  • 2016-07-06
  • 2021-01-09
  • 2017-01-06
相关资源
最近更新 更多