I will use Consul-KV as the property source, you can refer to below article to setup the consul environment
https://blog.csdn.net/yaominhua/article/details/82316287
Also you need integrate with consul client first, please refer to below article
https://blog.csdn.net/yaominhua/article/details/82317368
To retrieve the configuration dynamically
<dependency>
<groupId>com.netflix.archaiusgroupId>
<artifactId>archaius-coreartifactId>
<version>0.7.6version>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-configuration2artifactId>
<version>2.3version>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-lang3artifactId>
dependency>
public class ConsulConfigurationSource implements PolledConfigurationSource {
private String keyName;
public ConsulConfigurationSource(String keyName) {
this.keyName = keyName;
}
@Override
public PollResult poll(boolean initial, Object checkPoint) throws Exception {
KeyValueClient kvClient = Consul.newClient().keyValueClient();
Optional kvOpt = kvClient.getValueAsString(keyName);
String kvStr = StringUtils.EMPTY;
if (kvOpt.isPresent()) {
kvStr = kvOpt.get();
}
Properties props = new Properties();
props.load(new StringReader(kvStr));
Map propMap = new HashMap<>();
props.keySet().forEach(x -> {
propMap.put((String)x, props.get(x));
});
return PollResult.createFull(propMap);
}
}
public class ConsulPropertySourceInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
// default value is 60000 (60s)
private static final String ARCHAIUS_DELAY_KEY_MILLS = "archaius.fixedDelayPollingScheduler.delayMills";
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
System.setProperty(ARCHAIUS_DELAY_KEY_MILLS, "10000");
// This key/value has been added from Consul-UI
String keyName = "service/config/dev";
PolledConfigurationSource configSource = new ConsulConfigurationSource(keyName);
AbstractPollingScheduler scheduler = new FixedDelayPollingScheduler();
DynamicConfiguration configuration = new DynamicConfiguration(configSource, scheduler);
ConfigurationManager.install(configuration);
}
}
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication sa = new SpringApplication(Application.class);
// add custom initializer
sa.addInitializers(new ConsulPropertySourceInitializer());
sa.run(args);
}
}
@RestController
@RequestMapping("helloWorld")
public class HelloWorldController {
@RequestMapping(value="property/{propName}")
public String getPropertyValue(@PathVariable("propName") String propName) {
DynamicStringProperty dsp = DynamicPropertyFactory.getInstance().getStringProperty(propName, "");
return dsp.get();
}
}
We can use below URL to have a test:
http://localhost:8080/helloWorld/property/person.first.name
public class ConsulPropertySource extends MapPropertySource {
public ConsulPropertySource(String name, Map source) {
super(name, source);
}
}
public class LoadConsulEnv implements EnvironmentPostProcessor, Ordered {
@Override
public int getOrder() {
return ConfigFileApplicationListener.DEFAULT_ORDER + 1;
}
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
Map propMap = new HashMap<>();
KeyValueClient kvClient = Consul.newClient().keyValueClient();
Optional kvOpt = kvClient.getValueAsString("service/config/dev");
String kvStr = StringUtils.EMPTY;
if (kvOpt.isPresent()) {
kvStr = kvOpt.get();
}
Properties props = new Properties();
try {
props.load(new StringReader(kvStr));
} catch (IOException e) {
e.printStackTrace();
}
props.keySet().forEach(x -> {
propMap.put((String)x, props.get(x));
});
ConsulPropertySource propertySource = new ConsulPropertySource("thirdEnv", propMap);
MutablePropertySources propertySources = environment.getPropertySources();
propertySources.addLast(propertySource);
}
}
org.springframework.boot.env.EnvironmentPostProcessor=\
com.xuya.app.demo.config.archaius.LoadConsulEnv
@RestController
@RequestMapping("helloWorld")
public class HelloWorldController {
@Autowired
private Environment env;
@RequestMapping(value="envprop/{propName}")
public String getEnvPropertyValue(@PathVariable("propName") String propName) {
return env.getProperty(propName);
}
}
We can use below URL to have a test:
http://localhost:8080/helloWorld/envprop/person.first.name