Inv*_*ble 6 java spring amazon-web-services spring-boot aws-parameter-store
我创建了 spring boot(gradle) 应用程序,并包含了依赖项:
org.springframework.cloud:spring-cloud-starter-aws-parameter-store-config.
我想用来AWSSimpleSystemsManagement从 AWS 参数存储中读取配置,但我被迫这样写(在 aws 中):
config/application_dev/server.port: 8080
Run Code Online (Sandbox Code Playgroud)
有什么办法可以从spring boot中读取这样的内容: dev.application.server.port:8080
目前我认为所有这些都是通过自动配置管理的,有没有办法覆盖它
在application.properties 中,您可以定义 property server.port=8081。
spring-cloud-starter-aws-parameter-store-config支持的参数格式有:
/config/application/server.port/config/application_dev/server.port/config/my-service/server.port/config/my-service_dev/server.port通过在bootstrap.properties 中定义以下属性,您可以以某种方式更改格式:
spring.application.name=my-service
aws.paramstore.prefix=/config
aws.paramstore.defaultContext=application
aws.paramstore.profileSeparator=_
Run Code Online (Sandbox Code Playgroud)
但仅支持简单的自定义,因为主要参数命名逻辑是AwsParamStorePropertySourceLocator.
要显着更改参数格式,您必须定义自定义PropertySourceLocator并将其注册为引导程序配置。
问题是dev.application.server.port参数名无效。
AWS Systems Manager Parameter Store/用作路径分隔符,Spring 使用操作get-parameters-by-path。一种解决方法是使用 name dev.application/server.port。
但是这个名字也是无效的。参数名称必须是完全限定名称,因此有效名称为/dev.application/server.port.
为了支持这样的参数格式定义一个自定义 PropertySourceLocator
@Configuration
public class CustomAwsParamStorePropertySourceLocator implements PropertySourceLocator {
private static final Logger LOGGER =
LoggerFactory.getLogger(CustomAwsParamStorePropertySourceLocator.class);
private AWSSimpleSystemsManagement ssmClient;
private List<String> contexts = new ArrayList<>();
public CustomAwsParamStorePropertySourceLocator(AWSSimpleSystemsManagement ssmClient) {
this.ssmClient = ssmClient;
}
public List<String> getContexts() {
return contexts;
}
@Override
public PropertySource<?> locate(Environment environment) {
if (!(environment instanceof ConfigurableEnvironment)) {
return null;
}
ConfigurableEnvironment env = (ConfigurableEnvironment) environment;
List<String> profiles = Arrays.asList(env.getActiveProfiles());
String defaultAppName = "application";
this.contexts.add("/" + defaultAppName + "/");
addProfiles(this.contexts, defaultAppName, profiles);
String appName = env.getProperty("spring.application.name");
this.contexts.add("/" + appName + "/");
addProfiles(this.contexts, appName, profiles);
Collections.reverse(this.contexts);
CompositePropertySource composite = new CompositePropertySource("custom-aws-param-store");
for (String propertySourceContext : this.contexts) {
try {
composite.addPropertySource(create(propertySourceContext));
} catch (Exception e) {
LOGGER.warn("Unable to load AWS config from " + propertySourceContext, e);
}
}
return composite;
}
private void addProfiles(List<String> contexts, String appName, List<String> profiles) {
for (String profile : profiles) {
contexts.add("/" + profile + "." + appName + "/");
}
}
private AwsParamStorePropertySource create(String context) {
AwsParamStorePropertySource propertySource =
new AwsParamStorePropertySource(context, this.ssmClient);
propertySource.init();
return propertySource;
}
}
Run Code Online (Sandbox Code Playgroud)
并通过添加文件在引导程序上下文中注册它 META-INF/spring.factories
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.example.CustomAwsParamStorePropertySourceLocator
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7636 次 |
| 最近记录: |