我已经尝试了cassandra的各种自定义配置,但它不工作(我的应用程序没有启动),它与默认的自动配置一起工作。但是,因为我必须从keyvault检索用户名和密码(所以我需要自定义配置)。
@ configuration@ enablereactivecassandrarepository (basePackages = {"com. eye .nexus.map.viewer.repository"})@EntityScan({"com.ey.nexus.map.viewer.model")@RequiredArgsConstructorcassandracconfiguration扩展abstractreactivecassandracconfiguration {
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;
}
}
你应该在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-petclini -reactive项目有一个示例application.conf
,您可以使用作为参考。
有关更多信息,请参阅在Spring引导应用程序中将密钥从Azure密钥库加载到属性源。干杯!