gpt4 book ai didi

java - 如何将 Spring Retry 与带有 VaultPropertySource 的 Spring Vault 配置一起使用?

转载 作者:行者123 更新时间:2023-12-05 05:18:23 25 4
gpt4 key购买 nike

我希望标有 VaultPropertySource 的 spring-vault 配置能够在失败时重试对 vault 的请求。我应该将什么标记为可重试?我正在使用 Spring-Retry,我正在查看 http://www.baeldung.com/spring-retry .

没有可见的方法来标记为可重试。我是否应该更改 vaultTemplate 的实现并将 vaultOperations 标记为可重试?

ProvisioningSecrets.java

    @Configuration
@VaultPropertySource(
value="secret/provisioning",
propertyNamePrefix = "provisioning.",
renewal = Renewal.RENEW
)
@EnableRetry
@Lazy
@Profile("!test")
public class ProvisioningSecrets {
private static final Logger logger = LoggerFactory.getLogger(ProvisioningSecrets.class);

@Autowired
public void setPassword(@Value("${provisioning.password}") final String password) throws Exception {
logger.info("We successfully set the provisioning db password.");
EnvVars.changeSetting(Setting.PROVISIONING_PASS, password);
}

@Autowired
public void setHost(@Value("${provisioning.host}") final String host) throws Exception {
logger.info("We successfully set the provisioning db host.");
EnvVars.changeSetting(Setting.PROVISIONING_HOST, host);
}

@Autowired
public void setPort(@Value("${provisioning.port}") final int port) throws Exception {
logger.info("We successfully set the provisioning db port.");
EnvVars.changeSetting(Setting.PROVISIONING_PORT, Integer.toString(port));
}

@Autowired
public void setUsername(@Value("${provisioning.username}") final String username) throws Exception {
logger.info("We successfully set the provisioning db username.");
EnvVars.changeSetting(Setting.PROVISIONING_USER, username);
}

@Autowired
public void setDbName(@Value("${provisioning.name}") final String name) throws Exception {
logger.info("We successfully set the provisioning db name.");
EnvVars.changeSetting(Setting.PROVISIONING_DB_NAME, name);
}
}

保险库配置.java

@Configuration
@Profile("!test")
public class VaultConfiguration extends AbstractVaultConfiguration {

private static final Logger logger = LoggerFactory.getLogger(VaultConfiguration.class);

private URI vaultHost;

private String vaultToken;

/**
* Configure the Client Authentication.
*
* @return A configured ClientAuthentication Object.
* @see ClientAuthentication
*/
@Override
public ClientAuthentication clientAuthentication() {
// testing out environment variable value injection
logger.debug("Vault Token configuration done.");
return new TokenAuthentication(vaultToken);
}

@Override
@Bean
@DependsOn("vaultToken")
public SessionManager sessionManager() {
return super.sessionManager();
}

@Override
public SslConfiguration sslConfiguration() {
logger.info("Configuring Vault SSL with NONE.");
return SslConfiguration.NONE;
}

/**
* Specify an endpoint for connecting to Vault.
*
* @return A configured VaultEndpoint.
* @see VaultEndpoint
*/
@Override
public VaultEndpoint vaultEndpoint() {
logger.debug("Vault Host:" + vaultHost.toString());

if (vaultHost.toString().isEmpty()) {
logger.info("Creating default Vault Endpoint.");
return new VaultEndpoint();
}

logger.info("Creating Vault Endpoint based on address: " + vaultHost.toString());
final VaultEndpoint endpoint = VaultEndpoint.from(vaultHost);
logger.info("Created Vault Endpoint: " + endpoint.toString());

return endpoint;
}

@Bean("vaultHost")
public URI vaultHost(@Value("${spring.vault.host}") final URI vaultHost) {
this.vaultHost = vaultHost;
return vaultHost;
}

@Override
@Bean
@DependsOn("vaultHost")
public VaultTemplate vaultTemplate() {
return super.vaultTemplate();
}

@Bean("vaultToken")
public String vaultToken(@Value("${spring.vault.token}") final String vaultToken) {
this.vaultToken = vaultToken;
return vaultToken;
}
}

最佳答案

如何使用 RetryTemplate 创建自定义 VaultTemplate bean 类?

public class RetryableVaultTemplate extends VaultTemplate {

private final RetryTemplate retryTemplate;

public RetryableVaultTemplate(VaultEndpointProvider endpointProvider,
ClientHttpRequestFactory clientHttpRequestFactory,
SessionManager sessionManager, RetryTemplate retryTemplate) {
super(endpointProvider, clientHttpRequestFactory, sessionManager);
this.retryTemplate = retryTemplate;
}

@Override
public VaultResponse read(final String path) {

return retryTemplate
.execute(new RetryCallback<VaultResponse, RuntimeException>() {
@Override
public VaultResponse doWithRetry(RetryContext context) {
System.out.println("doWithRetry");
return RetryableVaultTemplate.super.read(path);
}
});
}

@Override
public <T> VaultResponseSupport<T> read(final String path, final Class<T> responseType) {

return retryTemplate
.execute(new RetryCallback<VaultResponseSupport<T>, RuntimeException>() {
@Override
public VaultResponseSupport<T> doWithRetry(RetryContext context) {
return RetryableVaultTemplate.super.read(path, responseType);
}
});
}
}

确保将此 bean 类注册为 vaultTemplate bean 而不是 VaultTemplate

关于java - 如何将 Spring Retry 与带有 VaultPropertySource 的 Spring Vault 配置一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48035036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com