Add local-datacenter property for Cassandra
The Cassandra v4 driver does not longer have automatic local DC inference from contact points. As a result, the "local-datacenter" property must be set with the default load balancing policy and the contact points must be of that data center. This commit adds a new property for the local datacenter so that it can be specified without the use of a customizer. Closes gh-19779
This commit is contained in:
parent
eb08ad02b9
commit
df7b3839a1
|
|
@ -114,6 +114,8 @@ public class CassandraAutoConfiguration {
|
|||
mapPoolingOptions(properties, options);
|
||||
map.from(properties::getContactPoints)
|
||||
.to((contactPoints) -> options.add(DefaultDriverOption.CONTACT_POINTS, contactPoints));
|
||||
map.from(properties.getLocalDatacenter()).to(
|
||||
(localDatacenter) -> options.add(DefaultDriverOption.LOAD_BALANCING_LOCAL_DATACENTER, localDatacenter));
|
||||
ConfigFactory.invalidateCaches();
|
||||
return ConfigFactory.defaultOverrides().withFallback(options.build())
|
||||
.withFallback(ConfigFactory.defaultReference()).resolve();
|
||||
|
|
|
|||
|
|
@ -55,6 +55,12 @@ public class CassandraProperties {
|
|||
*/
|
||||
private final List<String> contactPoints = new ArrayList<>(Collections.singleton("127.0.0.1:9042"));
|
||||
|
||||
/**
|
||||
* Datacenter that is considered "local". Contact points should be from this
|
||||
* datacenter.
|
||||
*/
|
||||
private String localDatacenter;
|
||||
|
||||
/**
|
||||
* Login user of the server.
|
||||
*/
|
||||
|
|
@ -141,6 +147,14 @@ public class CassandraProperties {
|
|||
return this.contactPoints;
|
||||
}
|
||||
|
||||
public String getLocalDatacenter() {
|
||||
return this.localDatacenter;
|
||||
}
|
||||
|
||||
public void setLocalDatacenter(String localDatacenter) {
|
||||
this.localDatacenter = localDatacenter;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return this.username;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -51,6 +51,20 @@ class CassandraAutoConfigurationTests {
|
|||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void driverConfigLoaderWithContactPoints() {
|
||||
this.contextRunner.withPropertyValues("spring.data.cassandra.contact-points=cluster.example.com:9042",
|
||||
"spring.data.cassandra.local-datacenter=cassandra-eu1").run((context) -> {
|
||||
assertThat(context).hasSingleBean(DriverConfigLoader.class);
|
||||
DriverExecutionProfile configuration = context.getBean(DriverConfigLoader.class).getInitialConfig()
|
||||
.getDefaultProfile();
|
||||
assertThat(configuration.getStringList(DefaultDriverOption.CONTACT_POINTS))
|
||||
.containsOnly("cluster.example.com:9042");
|
||||
assertThat(configuration.getString(DefaultDriverOption.LOAD_BALANCING_LOCAL_DATACENTER))
|
||||
.isEqualTo("cassandra-eu1");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void driverConfigLoaderWithCustomSessionName() {
|
||||
this.contextRunner.withPropertyValues("spring.data.cassandra.session-name=testcluster").run((context) -> {
|
||||
|
|
|
|||
|
|
@ -29,12 +29,9 @@ import org.testcontainers.junit.jupiter.Testcontainers;
|
|||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
|
||||
import org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.cassandra.CqlSessionBuilderCustomizer;
|
||||
import org.springframework.boot.autoconfigure.data.cassandra.city.City;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.cassandra.config.SchemaAction;
|
||||
import org.springframework.data.cassandra.config.SessionFactoryFactoryBean;
|
||||
|
||||
|
|
@ -58,9 +55,9 @@ class CassandraDataAutoConfigurationIntegrationTests {
|
|||
@BeforeEach
|
||||
void setUp() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(TestConfiguration.class);
|
||||
TestPropertyValues
|
||||
.of("spring.data.cassandra.contact-points:localhost:" + cassandra.getFirstMappedPort(),
|
||||
"spring.data.cassandra.local-datacenter=datacenter1",
|
||||
"spring.data.cassandra.read-timeout=24000", "spring.data.cassandra.connect-timeout=10000")
|
||||
.applyTo(this.context.getEnvironment());
|
||||
}
|
||||
|
|
@ -105,14 +102,4 @@ class CassandraDataAutoConfigurationIntegrationTests {
|
|||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class TestConfiguration {
|
||||
|
||||
@Bean
|
||||
CqlSessionBuilderCustomizer sessionCustomizer() {
|
||||
return (builder) -> builder.withLocalDatacenter("datacenter1");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue