简体   繁体   English

Spring Boot 1.4.1和Cassandra 3.x

[英]Spring Boot 1.4.1 and Cassandra 3.x

I want to use Cassandra 3.x in a Spring Boot project. 我想在Spring Boot项目中使用Cassandra 3.x. I found out that the current release version of the Spring Data Cassandra project only supports Cassandra 2.x. 我发现当前发布的Spring Data Cassandra项目版本仅支持Cassandra 2.x. So I wanted to use the DataStax Driver instead of the Spring Data Cassandra project. 所以我想使用DataStax驱动程序而不是Spring Data Cassandra项目。 I added 我补充道

compile 'com.datastax.cassandra:cassandra-driver-core:3.1.1'

as a dependency. 作为依赖。 Now I can insert values into a keyspace on a Cassandra cluster. 现在我可以将值插入到Cassandra集群的键空间中。 But when running tests for a REST controller I get an error 但是当运行REST控制器的测试时,我得到一个错误

java.lang.NoClassDefFoundError: io/netty/handler/codec/http/FullHttpRequest

So I added 所以我补充道

compile 'io.netty:netty-all:4.1.6.Final'

as a dependency and the error went away. 作为依赖,错误消失了。 But now all tests using 但现在所有测试都使用

TestRestTemplate.postForObject(...)

or 要么

TestRestTemplate.put(...)

fail. 失败。 But using 但是使用

TestRestTemplate.getForObject(...)

works as expected. 按预期工作。 I assume there's some clash in the dependencies of Spring Boot and the Netty version I added as a dependency. 我假设在Spring Boot的依赖项和我作为依赖项添加的Netty版本中存在一些冲突。

I found out that the latest version of the DataStax Cassandra driver to work without the additional Netty dependency is 2.1.5 which is dated Mar 2015 and doesn't support Cassandra 3. Using this driver everything works but I don't want to use a driver that old. 我发现最新版本的DataStax Cassandra驱动程序在没有额外的Netty依赖性的情况下工作是2.1.5,其日期为2015年3月,并且不支持Cassandra 3.使用此驱动程序一切正常但我不想使用那个老司机。

UPDATE: I removed the DataStax driver dependency and tried to use the 1.5.0.M1 version of Spring Data Cassandra and overrode the Spring, Spring Data Cassandra and Cassandra driver versions in the buildscript. 更新:我删除了DataStax驱动程序依赖项,并尝试使用Spring Data Cassandra的1.5.0.M1版本,并在buildscript中覆盖Spring,Spring Data Cassandra和Cassandra驱动程序版本。

ext['spring.version'] = '5.0.0.M2'
ext['spring-data-releasetrain.version'] = 'Ingalls-M1'
ext['cassandra-driver.version'] = '3.1.1'

This resulted in the following error: 这导致以下错误:

java.lang.NoClassDefFoundError: io/netty/util/Timer

when using Cassandra functionality. 使用Cassandra功能时。 When I include Netty again, Cassandra functionality works but my tests using TestRestTemplate.put and .post aren't running anymore. 当我再次包含Netty时,Cassandra功能正常,但我使用TestRestTemplate.put.post测试不再运行了。 I gave it another try upgrading to Spring Boot Version 2.0.0.BUILD-SNAPSHOT which also includes Spring Data Cassandra 1.5.0.M1. 我再次尝试升级到Spring Boot Version 2.0.0.BUILD-SNAPSHOT,其中还包括Spring Data Cassandra 1.5.0.M1。 Now when I start the app and use DataStax Driver functionality I get the same NoClassDefFoundError as before. 现在,当我启动应用程序并使用DataStax驱动程序功能时,我得到与以前相同的NoClassDefFoundError。 Adding Netty as a dependency kills my TestRestTemplate based unit tests again... 将Netty添加为依赖项会再次导致基于TestRestTemplate的单元测试...

UPDATE: TestRestTemplate isn't working because Spring Boot configures it to use Netty4ClientHttpRequestFactory when it finds Netty on the classpath and the Netty4ClientHttpRequestFactory doesn't seem to work. 更新: TestRestTemplate无法正常工作,因为Spring Boot将它配置为使用Netty4ClientHttpRequestFactory当它在类路径上找到Netty并且Netty4ClientHttpRequestFactory似乎不起作用时。

See https://github.com/spring-projects/spring-boot/issues/7240 and https://jira.spring.io/browse/SPR-14860 请参阅https://github.com/spring-projects/spring-boot/issues/7240https://jira.spring.io/browse/SPR-14860

For a fix see my answer to this question. 有关修复,请参阅我对此问题的回答。

I stick to using Spring Data Cassandra 1.5.0.M1 and Cassandra driver 3.1.1 using the following version overrides: 我坚持使用Spring Data Cassandra 1.5.0.M1和Cassandra驱动程序3.1.1使用以下版本覆盖:

ext['spring.version'] = '5.0.0.M2'
ext['spring-data-releasetrain.version'] = 'Ingalls-M1'
ext['cassandra-driver.version'] = '3.1.1'

To make Cassandra driver functionality work I had to add Netty as a dependency. 为了使Cassandra驱动程序功能正常工作,我不得不将Netty添加为依赖项。

compile 'io.netty:netty-all:4.1.6.Final'

To make TestRestTemplate.postForObject(...) and TestRestTemplate.put(...) I had to provide a RestTemplateBuilder @Bean and configure it to use SimpleClientHttpRequestFactory . TestRestTemplate.postForObject(...)TestRestTemplate.put(...)我必须提供RestTemplateBuilder @Bean并将其配置为使用SimpleClientHttpRequestFactory

@TestConfiguration
static class TestConfig {
    @Bean
    public RestTemplateBuilder restTemplateBuilder() {
        return new RestTemplateBuilder().detectRequestFactory(false).requestFactory(SimpleClientHttpRequestFactory.class);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM