Skip to content

spring boot integration

王宇轩 edited this page Sep 26, 2018 · 2 revisions

application.yml

config-toolkit:
  connect-str: 192.168.5.99:2181
  root-node: /projectx/modulex
  version: 1.0.0

添加config bean(EL方式使用)

@Configuration
public class ConfigToolkitConfig {

    @Bean
    public ZookeeperConfigProfile getConfigProfile(@Value("${config-toolkit.connect-str}") String connectStr,
                                                   @Value("${config-toolkit.root-node}") String rootNode,
                                                   @Value("${config-toolkit.version}") String version) {
        return new ZookeeperConfigProfile(connectStr, rootNode, version);
    }

    @Bean
    public ConfigGroup getConfigGroup(ZookeeperConfigProfile configProfile) {
        return new ZookeeperConfigGroup(configProfile, "property-group1");
    }

}

Placehodler方式

@Configuration
public class Config {

    @Bean
    public PropertySourcesPlaceholderConfigurer properties() {
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        configurer.setOrder(1);
        configurer.setIgnoreUnresolvablePlaceholders(true);

        ZookeeperConfigProfile profile = new ZookeeperConfigProfile("localhost:2181", "/projectx/modulex", "1.0.0");
        ConfigGroup configGroup1 = new ZookeeperConfigGroup(profile, "property-group1");
        ConfigGroup configGroup2 = new ZookeeperConfigGroup(profile, "property-group2");

        MutablePropertySources sources = new MutablePropertySources();
        sources.addLast(new ConfigGroupResource(configGroup1));
        sources.addLast(new ConfigGroupResource(configGroup2));
        configurer.setPropertySources(sources);

        return configurer;
    }

}

确保config bean在spring boot的扫描路径中

demo地址

https://github.com/crnlmchina/config-toolkit-with-spring-boot