Datastax java 驱动程序 4.0 以编程方式配置

Datastax java driver 4.0 configuration programmatically

是否仍然可以使用新的 4.0 版本配置集群(如 Datastax java 驱动程序 3.8 驱动程序版本)。或者唯一的解决方案是使用文档中的配置文件? https://docs.datastax.com/en/developer/java-driver/4.0/manual/core/configuration/

是的,可以通过编程方式配置驱动程序。只需遵循 "" of driver documentation 部分。您只需要使用 DriverConfigLoader.programmaticBuilder 定义配置加载器,然后在构建 CqlSession:

时使用它
DriverConfigLoader loader =
    DriverConfigLoader.programmaticBuilder()
        .withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofSeconds(5))
        .startProfile("slow")
        .withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofSeconds(30))
        .endProfile()
        .build();
CqlSession session = CqlSession.builder().withConfigLoader(loader).build();

驱动程序有很多可用选项,但正如实践所示,可以在配置文件中定义许多默认值,并且仅将加载器用于非标准的东西。

P.S。最好使用驱动程序 4.5,因为它适用于 OSS 和 DSE 版本...还有许多改进,例如响应式支持等。