卡桑德拉用于响应式 API 的自定义配置

huangapple go评论75阅读模式
英文:

Cassandra Custom Configuration for Reactive APIs

问题

以下是代码部分的翻译:

@Configuration
@EnableReactiveCassandraRepositories(basePackages = {"com.ey.nexus.map.viewer.repository"})
@EntityScan({"com.ey.nexus.map.viewer.model"})
@RequiredArgsConstructor
public class CassandraConfiguration extends AbstractReactiveCassandraConfiguration {

    private final KeyVaultConfigManager configManager;
    @Value("${spring.data.cassandra.keyspace-name}")
    private String keyspace;
    @Value("${spring.data.cassandra.contact-points}")
    private String contactPoints;
    @Value("${spring.data.cassandra.port}")
    private Integer port;
    @Value("${spring.data.cassandra.ssl}")
    private boolean sslEnabled;
    @Value("${spring.data.cassandra.username}")
    private String usernameKey;
    @Value("${spring.data.cassandra.password}")
    private String passKey;
    @Value("${spring.data.cassandra.local-datacenter}")
    private String localDatacenter;
    @Value("${spring.data.cassandra.socket-connection-timeout-millis}")
    private int socketConnectionTimeoutMillis;
    @Value("${spring.data.cassandra.socket-read-timeout-mills}")
    private int socketReadTimeoutMillis;
    @Value("${spring.data.cassandra.pooling-heartbeat-interval-seconds}")
    private int poolingHeartbeatIntervalSeconds;
    @Value("${spring.data.cassandra.pooling-idle-timeout-seconds}")
    private int poolingIdleTimeoutSeconds;
    @Value("${spring.data.cassandra.pooling-timeout-millis}")
    private int poolingTimeoutMillis;
    @Value("${spring.data.cassandra.pooling-options.local.core-connections-per-host}")
    private int poolingLocalCoreConnectionsPerHost;
    @Value("${spring.data.cassandra.pooling-options.local.max-connections-per-host}")
    private int poolingLocalMaxConnectionsPerHost;
    @Value("${spring.data.cassandra.pooling-options.remote.core-connections-per-host}")
    private int poolingRemoteCoreConnectionsPerHost;
    @Value("${spring.data.cassandra.pooling-options.remote.max-connections-per-host}")
    private int poolingRemoteMaxConnectionsPerHost;
    @Value("${spring.data.cassandra.pooling-options.local.max-requests-per-connection}")
    private int poolingLocalMaxRequestsPerConnection;
    @Value("${spring.data.cassandra.pooling-options.remote.max-requests-per-connection}")
    private int poolingRemoteMaxRequestsPerConnection;
    @Value("${spring.data.cassandra.reconnect-delay-millis}")
    private long reconnectDelayMillis;
    @Value("${spring.data.cassandra.reconnect-base-delay-millis}")
    private long reconnectBaseDelayMillis;
    @Value("${spring.data.cassandra.reconnect-max-delay-millis}")
    private long reconnectMaxDelayMillis;
    @Value("${spring.data.cassandra.retry-policy.class}")
    private String retryPolicyClass;
    @Value("${spring.data.cassandra.retry-policy.max-retry-count}")
    private int retryPolicyMaxRetryCount;
    @Value("${spring.data.cassandra.retry-policy.growing-back-off-time-millis}")
    private int retryPolicyGrowingBackOffTimeMillis;
    @Value("${spring.data.cassandra.retry-policy.fixed-back-off-time-millis}")
    private int retryPolicyFixedBackOffTimeMillis;
    @Value("${spring.data.cassandra.load-balancing-policy.read-datacenter}")
    private String readDatacenter;
    @Value("${spring.data.cassandra.load-balancing-policy.write-datacenter}")
    private String writeDatacenter;
    @Value("${spring.data.cassandra.load-balancing-policy.global-endpoint}")
    private String globalEndpoint;
    @Value("${spring.data.cassandra.load-balancing-policy.dnsExpirationInSeconds}")
    private int dnsExpirationInSeconds;
    private String username;
    private String password;

    // 省略了其他方法和注解的翻译
}

请注意,我仅提供了代码的翻译部分,不包括注释或其他文本内容。如果需要更多的翻译,请提出具体的要求。

英文:

I have tried various custom configuration for cassandra, but it is not working (my application is not starting up), It works with default autoconfiguration. But as I have to retrieve username and password from keyvault ( so I need to have custom Configuration).

@Configuration
@EnableReactiveCassandraRepositories(basePackages = {"com.ey.nexus.map.viewer.repository"})
@EntityScan({"com.ey.nexus.map.viewer.model"})
@RequiredArgsConstructor
public class CassandraConfiguration extends AbstractReactiveCassandraConfiguration {

private final KeyVaultConfigManager configManager;
@Value("${spring.data.cassandra.keyspace-name}")
private String keyspace;
@Value("${spring.data.cassandra.contact-points}")
private String contactPoints;
@Value("${spring.data.cassandra.port}")
private Integer port;
@Value("${spring.data.cassandra.ssl}")
private boolean sslEnabled;
@Value("${spring.data.cassandra.username}")
private String usernameKey;
@Value("${spring.data.cassandra.password}")
private String passKey;
@Value("${spring.data.cassandra.local-datacenter}")
private String localDatacenter;
@Value("${spring.data.cassandra.socket-connection-timeout-millis}")
private int socketConnectionTimeoutMillis;
@Value("${spring.data.cassandra.socket-read-timeout-mills}")
private int socketReadTimeoutMillis;
@Value("${spring.data.cassandra.pooling-heartbeat-interval-seconds}")
private int poolingHeartbeatIntervalSeconds;
@Value("${spring.data.cassandra.pooling-idle-timeout-seconds}")
private int poolingIdleTimeoutSeconds;
@Value("${spring.data.cassandra.pooling-timeout-millis}")
private int poolingTimeoutMillis;
@Value("${spring.data.cassandra.pooling-options.local.core-connections-per-host}")
private int poolingLocalCoreConnectionsPerHost;
@Value("${spring.data.cassandra.pooling-options.local.max-connections-per-host}")
private int poolingLocalMaxConnectionsPerHost;
@Value("${spring.data.cassandra.pooling-options.remote.core-connections-per-host}")
private int poolingRemoteCoreConnectionsPerHost;
@Value("${spring.data.cassandra.pooling-options.remote.max-connections-per-host}")
private int poolingRemoteMaxConnectionsPerHost;
@Value("${spring.data.cassandra.pooling-options.local.max-requests-per-connection}")
private int poolingLocalMaxRequestsPerConnection;
@Value("${spring.data.cassandra.pooling-options.remote.max-requests-per-connection}")
private int poolingRemoteMaxRequestsPerConnection;
@Value("${spring.data.cassandra.reconnect-delay-millis}")
private long reconnectDelayMillis;
@Value("${spring.data.cassandra.reconnect-base-delay-millis}")
private long reconnectBaseDelayMillis;
@Value("${spring.data.cassandra.reconnect-max-delay-millis}")
private long reconnectMaxDelayMillis;
@Value("${spring.data.cassandra.retry-policy.class}")
private String retryPolicyClass;
@Value("${spring.data.cassandra.retry-policy.max-retry-count}")
private int retryPolicyMaxRetryCount;
@Value("${spring.data.cassandra.retry-policy.growing-back-off-time-millis}")
private int retryPolicyGrowingBackOffTimeMillis;
@Value("${spring.data.cassandra.retry-policy.fixed-back-off-time-millis}")
private int retryPolicyFixedBackOffTimeMillis;
@Value("${spring.data.cassandra.load-balancing-policy.read-datacenter}")
private String readDatacenter;
@Value("${spring.data.cassandra.load-balancing-policy.write-datacenter}")
private String writeDatacenter;
@Value("${spring.data.cassandra.load-balancing-policy.global-endpoint}")
private String globalEndpoint;
@Value("${spring.data.cassandra.load-balancing-policy.dnsExpirationInSeconds}")
private int dnsExpirationInSeconds;
private String username;
private String password;
public int getSocketConnectionTimeoutMillis() {
return socketConnectionTimeoutMillis;
}
public int getSocketReadTimeoutMillis() {
return socketReadTimeoutMillis;
}
public int getPoolingHeartbeatIntervalSeconds() {
return poolingHeartbeatIntervalSeconds;
}
public int getPoolingIdleTimeoutSeconds() {
return poolingIdleTimeoutSeconds;
}
public int getPoolingTimeoutMillis() {
return poolingTimeoutMillis;
}
public int getPoolingLocalCoreConnectionsPerHost() {
return poolingLocalCoreConnectionsPerHost;
}
public int getPoolingLocalMaxConnectionsPerHost() {
return poolingLocalMaxConnectionsPerHost;
}
public int getPoolingRemoteCoreConnectionsPerHost() {
return poolingRemoteCoreConnectionsPerHost;
}
public int getPoolingRemoteMaxConnectionsPerHost() {
return poolingRemoteMaxConnectionsPerHost;
}
public int getPoolingLocalMaxRequestsPerConnection() {
return poolingLocalMaxRequestsPerConnection;
}
public int getPoolingRemoteMaxRequestsPerConnection() {
return poolingRemoteMaxRequestsPerConnection;
}
public long getReconnectDelayMillis() {
return reconnectDelayMillis;
}
public long getReconnectBaseDelayMillis() {
return reconnectBaseDelayMillis;
}
public long getReconnectMaxDelayMillis() {
return reconnectMaxDelayMillis;
}
public String getRetryPolicyClass() {
return retryPolicyClass;
}
public int getRetryPolicyMaxRetryCount() {
return retryPolicyMaxRetryCount;
}
public int getRetryPolicyGrowingBackOffTimeMillis() {
return retryPolicyGrowingBackOffTimeMillis;
}
public int getRetryPolicyFixedBackOffTimeMillis() {
return retryPolicyFixedBackOffTimeMillis;
}
public String getReadDatacenter() {
return readDatacenter;
}
public String getWriteDatacenter() {
return writeDatacenter;
}
public String getGlobalEndpoint() {
return globalEndpoint;
}
public int getDnsExpirationInSeconds() {
return dnsExpirationInSeconds;
}
protected String getLocalDataCenter() {
return localDatacenter;
}
protected String getKeyspaceName() {
return keyspace;
}
protected String getContactPoints() {
return contactPoints;
}
protected int getPort() {
return port;
}
public SchemaAction getSchemaAction() {
return SchemaAction.NONE;
}
public boolean isSslEnabled() {
return sslEnabled;
}
public String getUsername(){
return username;
}
public String getPassword(){
return password;
}
protected boolean getMetricsEnabled() { return false; }
@Bean
@NonNull
public CqlSessionFactoryBean cassandraSession() {
final CqlSessionFactoryBean cqlSessionFactoryBean = new CqlSessionFactoryBean();
cqlSessionFactoryBean.setContactPoints(contactPoints);
cqlSessionFactoryBean.setKeyspaceName(keyspace);
cqlSessionFactoryBean.setLocalDatacenter(localDatacenter);
cqlSessionFactoryBean.setPort(port);
cqlSessionFactoryBean.setUsername(configManager.getProperty(usernameKey));
cqlSessionFactoryBean.setPassword(configManager.getProperty(passKey));
return cqlSessionFactoryBean;
}

}

答案1

得分: 1

你应该继续在Spring中使用自动配置,但需要修改你的应用程序以使用spring-cloud-azure-starter-keyvault-secrets从你的密钥保险库中获取用户名和密码。然后,你需要将它们导出为环境变量。

然后,你应该能够使用环境变量配置application.conf。例如:

datastax-java-driver {
advanced {
auth-provider {
class = PlainTextAuthProvider
username = ${CASSANDRA_USER}
password = ${CASSANDRA_PASSWORD}
}
}
}

如果你感兴趣,DataStax-Examples/spring-petclinic-reactive 项目有一个 示例的 application.conf,你可以用作参考。

更多信息,请参阅 从Azure Key Vault加载秘密到Spring Boot应用程序的属性源。祝好!

英文:

You should continue using the auto-configuration in Spring but you will need to modify your app to use spring-cloud-azure-starter-keyvault-secrets to get the username and password from your key vault. You then need to export them as environment variables.

You should then be able to configure application.conf using the environment variables. For example:

datastax-java-driver {
advanced {
auth-provider {
class = PlainTextAuthProvider
username = ${CASSANDRA_USER}
password = ${CASSANDRA_PASSWORD}
}
}
}

If you're interested, the DataStax-Examples/spring-petclinic-reactive project has a sample application.conf you could use as reference.

For more information, see Loading Secrets From Azure Key Vault to Property Source in Spring Boot Application. Cheers!

huangapple
  • 本文由 发表于 2023年4月4日 18:04:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75928083.html
  • cassandra
  • reactive-programming
  • spring-boot
  • spring-data-cassandra
  • spring-webflux
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定