在微服务中获得连接超时错误

时间:2018-06-13 03:45:25

标签: spring-boot postman microservices

我使用Java 8SpringBoot 2构建了一个微服务。从这个微服务,我正在尝试使用另一个REST API服务。但是,我在Chrome上收到以下错误。我已经禁用了Windows防火墙和McAfee防病毒防火墙,但仍然遇到了同样的错误。我可以使用Postman工具直接调用REST API,但不能通过我的微服务调用。

错误: -

  

java.lang.IllegalStateException:底层HTTP客户端已完成   没有发出回应。

     

2018-06-12 15:21:29.300 ERROR 17996 --- [ctor-http-nio-3]   .a.w.r.e.DefaultErrorWebExceptionHandler:无法处理请求   [获取http://localhost:8080/category/search]   io.netty.channel.AbstractChannel $ AnnotatedConnectException:Connection   超时:暂无信息:test.usdemo.xyz.com/92.54.41.24:443   at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)   〜[na:1.8.0_171] at   sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)   〜[na:1.8.0_171] at   io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:325)   〜[netty-transport-4.1.24.Final.jar:4.1.24.Final] at   io.netty.channel.nio.AbstractNioChannel $ AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:340)   〜[netty-transport-4.1.24.Final.jar:4.1.24.Final] at   io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:633)   〜[netty-transport-4.1.24.Final.jar:4.1.24.Final] at   io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:580)   〜[netty-transport-4.1.24.Final.jar:4.1.24.Final] at   io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:497)   〜[netty-transport-4.1.24.Final.jar:4.1.24.Final] at   io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:459)   〜[netty-transport-4.1.24.Final.jar:4.1.24.Final] at   io.netty.util.concurrent.SingleThreadEventExecutor $ 5.run(SingleThreadEventExecutor.java:884)   〜[netty-common-4.1.24.Final.jar:4.1.24.Final] at   java.lang.Thread.run(Thread.java:748)~ [na:1.8.0_171]引起:   java.net.ConnectException:连接超时:没有进一步   信息...省略了10个常见帧

控制器类: -

@RestController
public class CategorySearchController {

    private final CategorySearchService categorySearchService;

    @Autowired
    public CategorySearchController(CategorySearchService categorySearchService) {
        this.categorySearchService = categorySearchService;
    }


    @GetMapping(path = "/search-category")
    public Mono<CategoryResponse> searchCategories(SearchRequest categorySearchRequest){
        return categorySearchService
                .searchCategories()
                .switchIfEmpty(Mono.error(
                        new EntityNotFoundException("No category matching " + categorySearchRequest.getSearchTerm() + " was found")));
    }

}

服务类: -

@Service
public class CategorySearchServiceImpl implements CategorySearchService {
    private String baseUrl = "https://demo0954903.mockable.io";

    @Override
    public Mono<CategoryResponse> searchCategories() {
        WebClient webClient = WebClient.create(baseUrl);
        return webClient.
                 get()
                .uri("/category-search")
                .retrieve()
                .bodyToMono(CategoryResponse.class);
    }
}

1 个答案:

答案 0 :(得分:0)

我找到了解决这个问题的方法。我需要在webclient中添加代理,如下所示: -

private final WebClient webClient;
ReactorClientHttpConnector connector = new ReactorClientHttpConnector(
                options -> options.httpProxy(addressSpec -> {
                return addressSpec.host(PROXY_HOST).port(PROXY_PORT);
                }));

        this.webClient = webClientBuilder.baseUrl(BASE_URL).clientConnector(connector).build();