Spring Boot okhhtp3模拟服务器:连接被拒绝

7rtdyuoh  于 2022-11-05  发布在  Spring
关注(0)|答案(2)|浏览(243)

由于我已将spring-boot-dependencies版本从2.2.5.RELEASE升级到2.4.0,因此使用MockWebServer的所有测试都失败,并出现以下错误:
org.springframework.web.reactive.function.client.WebClientRequestException: Connection refused: localhost/0:0:0:0:0:0:0:1:56338; nested exception is io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/0:0:0:0:0:0:0:1:56338
有什么线索吗?使用了错误的依赖性/弃用的东西?
Maven依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.4.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
    <version>2.4.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>2.4.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <scope>test</scope>
    <version>5.7.0</version>
</dependency>
<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>mockwebserver</artifactId>
    <scope>test</scope>
    <version>3.14.9</version>
</dependency>
<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <scope>test</scope>
    <version>3.14.9</version>
</dependency>

我已经在我的当前代码中编写了一个简单的测试用例,但是我总是遇到同样的问题...(感谢https://stackoverflow.com/a/62528728/1638483

import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.web.reactive.function.client.WebClient;

public class QuoteClient {

    private final WebClient webClient;

    public QuoteClient(WebClient.Builder builder, String baseUrl) {
        this.webClient = builder.baseUrl(baseUrl).build();
    }

    public JsonNode getData() {
        return this.webClient
                .get()
                .retrieve()
                .bodyToMono(JsonNode.class)
                .block();
    }
}

import com.fasterxml.jackson.databind.JsonNode;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.web.reactive.function.client.WebClient;

import static org.junit.Assert.assertNotNull;

public class QuotesClientTest {
    private QuoteClient quotesClient;
    private MockWebServer server;

    @BeforeEach
    public void setup() {
        this.server = new MockWebServer();
        this.quotesClient = new QuoteClient(WebClient.builder(), server.url("/").toString());
    }

    @Test
    public void test() {
        server.enqueue(new MockResponse()
                .setStatus("HTTP/1.1 200")
                .setBody("{\"bar\":\"barbar\",\"foo\":\"foofoo\"}")
                .addHeader("Content-Type", "application/json"));

        JsonNode data = quotesClient.getData();
        assertNotNull(data);

        System.out.println(data);

    }
}

堆栈追踪

org.springframework.web.reactive.function.client.WebClientRequestException: Connection refused: localhost/0:0:0:0:0:0:0:1:56382; nested exception is io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/0:0:0:0:0:0:0:1:56382

       at org.springframework.web.reactive.function.client.ExchangeFunctions$DefaultExchangeFunction.lambda$wrapException$9(ExchangeFunctions.java:137)
       Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
   |_ checkpoint ⇢ Request to GET null [DefaultWebClient]
Stack trace:
       at org.springframework.web.reactive.function.client.ExchangeFunctions$DefaultExchangeFunction.lambda$wrapException$9(ExchangeFunctions.java:137)
       at reactor.core.publisher.MonoErrorSupplied.subscribe(MonoErrorSupplied.java:70)
       at reactor.core.publisher.Mono.subscribe(Mono.java:3987)
       at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onError(FluxOnErrorResume.java:103)
       at reactor.core.publisher.FluxPeek$PeekSubscriber.onError(FluxPeek.java:221)
       at reactor.core.publisher.FluxPeek$PeekSubscriber.onError(FluxPeek.java:221)
       at reactor.core.publisher.FluxPeek$PeekSubscriber.onError(FluxPeek.java:221)
//...
       at reactor.netty.transport.TransportConnector$MonoChannelPromise.tryFailure(TransportConnector.java:464)
       at reactor.netty.transport.TransportConnector$MonoChannelPromise$1.tryFailure(TransportConnector.java:515)
       at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.fulfillConnectPromise(AbstractNioChannel.java:321)
       at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:337)
       at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:707)
       at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)
       at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)
       at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
       at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
       at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
       at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
       at java.lang.Thread.run(Thread.java:748)
   Suppressed: java.lang.Exception: #block terminated with an error
       at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:99)
       at reactor.core.publisher.Mono.block(Mono.java:1679)
       at QuoteClient.getData(QuoteClient.java:23)
       at QuotesClientTest.test(QuotesClientTest.java:30)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
       at java.lang.reflect.Method.invoke(Method.java:498)
       at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:688)
       at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
       at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
//...
       at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:108)
       at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
       at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
       at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
       at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
       at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:96)
       at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:75)
   Caused by: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: localhost/0:0:0:0:0:0:0:1:56382
   Caused by: java.net.ConnectException: Connection refused
       at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
       at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
       at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:330)
       at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334)
       at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:707)
       at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)
       at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)
       at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
       at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
       at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
       at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
       at java.lang.Thread.run(Thread.java:748)
yjghlzjz

yjghlzjz1#

2.4.5版本(我没有测试中间版本)开始,这个问题似乎已经通过使用 spring-boot-dependencies 得到了解决。
最新消息:
请注意您的本地主机文件...
我更新了我的,添加了.local作为localhost<myHostname>的后缀,因为今天早上问题再次出现(没有任何更改-_-)

127.0.0.1   localhost.local,<myHostname>.local
255.255.255.255 broadcasthost
::1             localhost.local,<myHostname>.local
whhtz7ly

whhtz7ly2#

在我的例子中,我在hosts文件中注解了这一行。

127.0.0.1 localhost

不加评论就解决了我的问题。

相关问题