我使用Spring Boot(版本1.5.1 RELEASE)、Spring Cloud(Edgware.SR3)、Spring Cloud Starter Config(版本1.4.3 RELEASE)和Netflix 的 Eureka naming server (Spring Cloud Starter Netflix Eureka Client/Server 1.4.4 RELEASE):创建了两个微服务。
- 一个Eureka服务器(一个用于注册微服务的服务)。
- 一个Eureka客户端(一个用于保存/更新操作的数据库服务)。
Eureka客户端是一个简单的Restful服务,有一个单一的@PostMapping
注解方法,接受一个可序列化的对象作为参数(@RequestBody
MyObject对象)。
Eureka客户端成功地在Eureka服务器上注册了自己。
然后,在Spring Boot Webflux
的帮助下,我尝试在WebClient
类的帮助下访问我的Eureka客户端,该类是在Spring 5中引入的,是AsyncRestTemplate
类的一种改进。我的做法如下。
WebClient webClient = WebClient.create();
reactor.core.publisher.Mono result = (Mono) webClient.post()
.uri("http://localhost:8080/eureka-client")
.body(BodyInserters.fromObject(//myComplexObject here))
.retrieve()
.bodyToMono(String.class);
result.subscribe();
调试时,我发现在上述代码的最后一行之后,我得到了OOM异常。
io.netty.handler.codec.EncoderException: java.lang.OutOfMemoryError
下面是完整的堆栈跟踪:
[reactor-http-nio-4] WARN io.netty.util.concurrent.AbstractEventExecutor - A task raised an exception. Task: reactor.ipc.netty.channel.ContextHandler$$Lambda$401/11401686@10ae5a2
reactor.core.Exceptions$ErrorCallbackNotImplemented: io.netty.handler.codec.EncoderException: java.lang.OutOfMemoryError
Caused by: io.netty.handler.codec.EncoderException: java.lang.OutOfMemoryError
at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:106)
at io.netty.channel.CombinedChannelDuplexHandler.write(CombinedChannelDuplexHandler.java:348)
at io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:738)
at io.netty.channel.AbstractChannelHandlerContext.invokeWrite(AbstractChannelHandlerContext.java:730)
at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:816)
at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:723)
at reactor.ipc.netty.channel.ChannelOperationsHandler.doWrite(ChannelOperationsHandler.java:291)
at reactor.ipc.netty.channel.ChannelOperationsHandler.drain(ChannelOperationsHandler.java:465)
at reactor.ipc.netty.channel.ChannelOperationsHandler.flush(ChannelOperationsHandler.java:191)
at io.netty.channel.AbstractChannelHandlerContext.invokeFlush0(AbstractChannelHandlerContext.java:776)
at io.netty.channel.AbstractChannelHandlerContext.invokeWriteAndFlush(AbstractChannelHandlerContext.java:802)
at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:814)
at io.netty.channel.AbstractChannelHandlerContext.writeAndFlush(AbstractChannelHandlerContext.java:794)
at io.netty.channel.AbstractChannelHandlerContext.writeAndFlush(AbstractChannelHandlerContext.java:831)
at io.netty.channel.DefaultChannelPipeline.writeAndFlush(DefaultChannelPipeline.java:1051)
at io.netty.channel.AbstractChannel.writeAndFlush(AbstractChannel.java:300)
at reactor.ipc.netty.http.HttpOperations.lambda$then$0(HttpOperations.java:135)
at reactor.ipc.netty.FutureMono.lambda$deferFuture$0(FutureMono.java:68)
at reactor.ipc.netty.FutureMono$DeferredFutureMono.subscribe(FutureMono.java:134)
at reactor.core.publisher.Mono.subscribe(Mono.java:3008)
at reactor.core.publisher.MonoIgnoreThen$ThenIgnoreMain.drain(MonoIgnoreThen.java:167)
at reactor.core.publisher.MonoIgnoreThen.subscribe(MonoIgnoreThen.java:56)
at reactor.core.publisher.Mono.subscribe(Mono.java:3008)
at reactor.core.publisher.FluxConcatIterable$ConcatIterableSubscriber.onComplete(FluxConcatIterable.java:141)
at reactor.core.publisher.FluxConcatIterable.subscribe(FluxConcatIterable.java:60)
at reactor.core.publisher.MonoSourceFlux.subscribe(MonoSourceFlux.java:47)
at reactor.ipc.netty.channel.ChannelOperations.applyHandler(ChannelOperations.java:380)
at reactor.ipc.netty.http.client.HttpClientOperations.onHandlerStart(HttpClientOperations.java:501)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:463)
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:886)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.OutOfMemoryError
at sun.misc.Unsafe.allocateMemory(Native Method)
at io.netty.util.internal.PlatformDependent0.allocateDirectNoCleaner(PlatformDependent0.java:430)
at io.netty.util.internal.PlatformDependent.allocateDirectNoCleaner(PlatformDependent.java:596)
at io.netty.buffer.PoolArena$DirectArena.allocateDirect(PoolArena.java:764)
at io.netty.buffer.PoolArena$DirectArena.newChunk(PoolArena.java:740)
at io.netty.buffer.PoolArena.allocateNormal(PoolArena.java:244)
at io.netty.buffer.PoolArena.allocate(PoolArena.java:214)
at io.netty.buffer.PoolArena.allocate(PoolArena.java:146)
at io.netty.buffer.PooledByteBufAllocator.newDirectBuffer(PooledByteBufAllocator.java:324)
at io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:185)
at io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:176)
at io.netty.buffer.AbstractByteBufAllocator.buffer(AbstractByteBufAllocator.java:113)
at io.netty.handler.codec.http.HttpObjectEncoder.encode(HttpObjectEncoder.java:92)
at io.netty.handler.codec.http.HttpClientCodec$Encoder.encode(HttpClientCodec.java:167)
at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:88)
... 32 more
我的classpath里有jackson库,并且依靠spring来序列化我的复杂对象,同时把它传递给Restful服务。我不确定是什么原因导致了这种情况。有什么指点吗?
StackOverflow: java.lang.OutOfMemoryError on using Spring Boot WebFlux Starter - Stack Overflow