Spring Cloud Config Server问题 - 配置多个源native和jdbc

时间:2018-05-30 10:44:41

标签: spring configuration cloud

我想连接到多个存储库,即spring cloud config中的native(文件系统)和jdbc。我创建了一个带有以下细节的spring cloud配置服务器

application.properties

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仍然具有相同的行为。 我什么都缺了?我的配置是否正确?请建议

1 个答案:

答案 0 :(得分:1)

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

并且在应用程序中 - " 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或属性中的同一层中创建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);
    }  
}