一 再docker启动镜像文件,异常:
老项目,启动异常:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.lambdaworks.redis.api.async.RedisAsyncCommands]: Factory method 'createAsyncRedisClient' threw exception; nested exception is java.lang.NoSuchMethodError: io.netty.util.internal.MathUtil.safeFindNextPositivePowerOfTwo(I)I
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
... 74 common frames omitted
Caused by: java.lang.NoSuchMethodError: io.netty.util.internal.MathUtil.safeFindNextPositivePowerOfTwo(I)I
at io.netty.buffer.PoolThreadCache$MemoryRegionCache.<init>(PoolThreadCache.java:387) ~[netty-buffer-4.1.15.Final.jar:4.1.15.Final]
at io.netty.buffer.PoolThreadCache$SubPageMemoryRegionCache.<init>(PoolThreadCache.java:355) ~[netty-buffer-4.1.15.Final.jar:4.1.15.Final]
at io.netty.buffer.PoolThreadCache.createSubPageCaches(PoolThreadCache.java:147) ~[netty-buffer-4.1.15.Final.jar:4.1.15.Final]
at io.netty.buffer.PoolThreadCache.<init>(PoolThreadCache.java:82) ~[netty-buffer-4.1.15.Final.jar:4.1.15.Final]
at io.netty.buffer.PooledByteBufAllocator$PoolThreadLocalCache.initialValue(PooledByteBufAllocator.java:427) ~[netty-buffer-4.1.15.Final.jar:4.1.15.Final]
at io.netty.buffer.PooledByteBufAllocator$PoolThreadLocalCache.initialValue(PooledByteBufAllocator.java:412) ~[netty-buffer-4.1.15.Final.jar:4.1.15.Final]
at io.netty.util.concurrent.FastThreadLocal.initialize(FastThreadLocal.java:155) ~[netty-all-4.1.1.Final.jar:4.1.1.Final]
at io.netty.util.concurrent.FastThreadLocal.get(FastThreadLocal.java:149) ~[netty-all-4.1.1.Final.jar:4.1.1.Final]
at io.netty.util.concurrent.FastThreadLocal.get(FastThreadLocal.java:135) ~[netty-all-4.1.1.Final.jar:4.1.1.Final]
at io.netty.buffer.PooledByteBufAllocator.newDirectBuffer(PooledByteBufAllocator.java:319) ~[netty-buffer-4.1.15.Final.jar:4.1.15.Final]
at io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:181) ~[netty-buffer-4.1.15.Final.jar:4.1.15.Final]
at io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:172) ~[netty-buffer-4.1.15.Final.jar:4.1.15.Final]
at com.lambdaworks.redis.protocol.CommandHandler.<init>(CommandHandler.java:80) ~[lettuce-4.4.1.Final.jar:na]
at com.lambdaworks.redis.RedisClient.connectStandaloneAsync(RedisClient.java:483) ~[lettuce-4.4.1.Final.jar:na]
at com.lambdaworks.redis.RedisClient.connectStandalone(RedisClient.java:465) ~[lettuce-4.4.1.Final.jar:na]
at com.lambdaworks.redis.RedisClient.connect(RedisClient.java:406) ~[lettuce-4.4.1.Final.jar:na]
at com.lambdaworks.redis.masterslave.MasterSlave.connectMasterSlave(MasterSlave.java:204) ~[lettuce-4.4.1.Final.jar:na]
at com.lambdaworks.redis.masterslave.MasterSlave.connect(MasterSlave.java:118) ~[lettuce-4.4.1.Final.jar:na]
at com.benmu.lettuce.wrapper.RedisClientBuilder.initClient(RedisClientBuilder.java:96) ~[lettuce-wrapper-1.1.4.jar:na]
。。。
看了下,主要是netty的jar 加载问题,因为Linux上面老项目能正常启动。
二 分析
从报错日志来看,就是java.lang.NoSuchMethodError 对应的引用类:
netty-buffer-4.1.15.Final.jar
里面的io.netty.buffer.PoolThreadCache
private abstract static class MemoryRegionCache<T> {
private final int size;
private final Queue<Entry<T>> queue;
private final SizeClass sizeClass;
private int allocations;
MemoryRegionCache(int size, SizeClass sizeClass) {
this.size = MathUtil.safeFindNextPositivePowerOfTwo(size);
queue = PlatformDependent.newFixedMpscQueue(this.size);
this.sizeClass = sizeClass;
}
就是这个MathUtil.safeFindNextPositivePowerOfTwo(size); 报的异常。
看下引用类:
import io.netty.util.internal.MathUtil;
对应的jar是个:nettty-all:4.1.1.Final.jar
看下这个类,mathUtil是米有safeFindNextPositivePowerOfTwo 方法的。
所以,原因就定位问题了。
怎么找对应依赖的jar呢?
mvn dependency:tree
[INFO] +- com.benmu.redis:arc-client:jar:1.1.2:compile
[INFO] | +- io.netty:netty-all:jar:4.1.1.Final:compile
怎么修复呢?
pom排除掉。
<exclusions>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</exclusion>
</exclusions>
如果没有引用,还可以指定下新版本。
<dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.15.Final</version> </dependency>
因为是老项目,引用多,升级client版本代价大,所以指定了匹配的版本。这里不做强制要求。可以根据自己的项目来。