我希望能够在我的应用程序使用的第 3 方组件之间灵活选择。ATM 我有硬编码的值
spring.cloud.vault.token=ffffffff-ffff-ffff-ffff-ffffffffffff
spring.cloud.vault.scheme=http
spring.cloud.vault.application-name=test
spring.cloud.vault.host=127.0.0.1
spring.cloud.vault.port=8200
目标是:
- 如果
spring.cloud.vault.host
设置尝试使用它。如果spring.cloud.vault.host
为空,请改用本地数据库(配置正确)
我的第一个想法是使用environment
如下变量:
spring.cloud.vault.token=${VAULT_TOKEN}
spring.cloud.vault.scheme=${VAULT_SCHEME}
spring.cloud.vault.application-name=${VAULT_APP_NAME}
spring.cloud.vault.host=${VAULT_HOST}
spring.cloud.vault.port=${VAULT_PORT}
然后使用组件类,如:
@Component
public class VaultHelper {
private final VaultOperations vaultOperations;
@Value("${spring.cloud.vault.host}")
String vaultHostname;
@Autowired
public VaultHelper(VaultOperations vaultOperations){
this.vaultOperations = vaultOperations;
}
public void saveToVault(String password){
if (!vaultHostname.equals("")) {
// Use vaultOperations to store password
} else {
// Use plaintext db storage
}
}
public byte[] readFromVault(String passwordLoc){
if (!vaultHostname.equals("")) {
// Use vaultOperations to access password
} else {
// return plaintext password
}
return new byte[0];
}
}
这样我${VAULT_HOST}
既可以从环境变量(从 docker-compose 运行应用程序)设置,也可以--spring.cloud.vault.host=URL
在本地机器上设置为java
参数。
${VAULT_PORT}
不幸的是,我在解析字段时遇到异常
Caused by: org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [int] for value '${VAULT_PORT}'; nested exception is java.lang.NumberFormatException: For input string: "${VAULT_PORT}"
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:47) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:191) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.boot.context.properties.bind.BindConverter$CompositeConversionService.convert(BindConverter.java:170) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.boot.context.properties.bind.BindConverter.convert(BindConverter.java:96) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.boot.context.properties.bind.BindConverter.convert(BindConverter.java:88) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.boot.context.properties.bind.Binder.bindProperty(Binder.java:435) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.boot.context.properties.bind.Binder.bindObject(Binder.java:380) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:320) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
... 83 common frames omitted
Caused by: java.lang.NumberFormatException: For input string: "${VAULT_PORT}"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[na:1.8.0_231]
at java.lang.Integer.parseInt(Integer.java:569) ~[na:1.8.0_231]
at java.lang.Integer.valueOf(Integer.java:766) ~[na:1.8.0_231]
at org.springframework.util.NumberUtils.parseNumber(NumberUtils.java:211) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.core.convert.support.StringToNumberConverterFactory$StringToNumber.convert(StringToNumberConverterFactory.java:62) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.core.convert.support.StringToNumberConverterFactory$StringToNumber.convert(StringToNumberConverterFactory.java:49) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.core.convert.support.GenericConversionService$ConverterFactoryAdapter.convert(GenericConversionService.java:436) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
... 90 common frames omitted
Process finished with exit code 1
谁能给我一个提示如何使它工作?