今天用spring boot集成netty时候,刚开始挺顺利的,但是在引入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
之后,一切都变的不美好了,启动的时候会出现如下错误
An attempt was made to call a method that does not exist. The attempt was made from the following location:
io.lettuce.core.resource.DefaultEventLoopGroupProvider.createEventLoopGroup(DefaultEventLoopGroupProvider.java:141)
The following method did not exist:
io.netty.util.concurrent.DefaultEventExecutorGroup.<init>(ILjava/util/concurrent/ThreadFactory;)V
The method's class, io.netty.util.concurrent.DefaultEventExecutorGroup, is available from the following locations:
jar:file:/C:/work/maven/mymaven/repo/io/netty/netty-all/5.0.0.Alpha2/netty-all-5.0.0.Alpha2.jar!/io/netty/util/concurrent/DefaultEventExecutorGroup.class
jar:file:/C:/work/maven/mymaven/repo/io/netty/netty-common/4.1.45.Final/netty-common-4.1.45.Final.jar!/io/netty/util/concurrent/DefaultEventExecutorGroup.class
It was loaded from the following location:
file:/C:/work/maven/mymaven/repo/io/netty/netty-all/5.0.0.Alpha2/netty-all-5.0.0.Alpha2.jar
刚开始看的时候一脸懵逼的,我这里也没有隐去netty4.1.45的版本,因为netty4和netty5的差别还是蛮的,往上一看,是lettuce的问题,lettuce不用说肯定是redis引入的,然后发现redis果然引入了
<!-- https://mvnrepository.com/artifact/io.lettuce/lettuce-core -->
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>5.1.8.RELEASE</version>
</dependency>
而lettuce又关联了
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-common</artifactId>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-handler</artifactId>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-transport</artifactId>
</dependency>
出现这种情况我第一反应是屏蔽,然后强制的指定版本。
第一次的做法是直接屏蔽lettuce
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
这样是解决了上面的问题,但是导致了redis无法完成bean注入,出现RedisConnectFactory找不到的情况,所以直接放弃,改用下面的屏蔽方法。
<!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-common</artifactId>
<version>5.0.0.Alpha2</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-handler</artifactId>
<version>5.0.0.Alpha2</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-transport</artifactId>
<version>5.0.0.Alpha2</version>
</dependency>
<!-- 提高redis的版本-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty-common</artifactId>
</exclusion>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty-handler</artifactId>
</exclusion>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty-transport</artifactId>
</exclusion>
</exclusions>
</dependency>
然并卵,问题依旧没解决,最后没办法了,我就只能降低netty的版本了。
<!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.45.Final</version>
</dependency>
和lettuce-core的版本中的netty版本保持一致,问题解决。其实还是蛮坑的,毕竟netty4和netty5的代码实现还是有很大的差别的,只能去改动一下实现代码了