API-GATEWAY 不起作用。抛出 java.net.UnknownHostException:无法解决

Posted

技术标签:

【中文标题】API-GATEWAY 不起作用。抛出 java.net.UnknownHostException:无法解决【英文标题】:API-GATEWAY doesn't work. Throwing java.net.UnknownHostException: failed to resolve 【发布时间】:2021-07-30 09:10:54 【问题描述】:

我使用的是 Spring 版本 - 2.4.5

Application.properties:

spring.application.name = api-gateway
server.port = 8765
#Had to put the below as I was facing No spring.config.import pro#perty has been defined and this has been put in currency exchang#e, currency converter and naming server as well
spring.cloud.config.enabled=false 

eureka.client.service-url.defaultZone = http://localhost:8761/eureka

spring.cloud.gateway.discovery.locator.enabled=true

#Tried the below two, but couldnt resolve the issue
eureka.instance.hostname=localhost
spring.cloud.gateway.discovery.locator.lower-case-service-id=true

所有依赖项都正确导入。

POM.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.in28minutes.microservices</groupId>
    <artifactId>api-gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>api-gateway</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
        <spring-cloud.version>2020.0.2</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>$spring-cloud.version</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

但在点击 api-gateway 时仍面临以下错误:

http://localhost:8765/currency-exchange/currency-exchange/from/USD/to/INR(或)

http://localhost:8765/CURRENCY-EXCHANGE/currency-exchange/from/USD/to/INR

    Whitelabel Error Page
This application has no configured error view, so you are seeing this as a fallback.

Sat May 08 14:03:05 IST 2021
[d1829095-3] There was an unexpected error (type=Internal Server Error, status=500).
failed to resolve 'XYZ.abc.def.com' after 2 queries
java.net.UnknownHostException: failed to resolve 'XYZ.abc.def.com' after 2 queries 
    at io.netty.resolver.dns.DnsResolveContext.finishResolve(DnsResolveContext.java:1013)
    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
    |_ checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
    |_ checkpoint ⇢ org.springframework.boot.actuate.metrics.web.reactive.server.MetricsWebFilter [DefaultWebFilterChain]
    |_ checkpoint ⇢ HTTP GET "/CURRENCY-EXCHANGE/currency-exchange/from/USD/to/INR" [ExceptionHandlingWebHandler]
Stack trace:
        at io.netty.resolver.dns.DnsResolveContext.finishResolve(DnsResolveContext.java:1013)
        at io.netty.resolver.dns.DnsResolveContext.tryToFinishResolve(DnsResolveContext.java:966)
        at io.netty.resolver.dns.DnsResolveContext.query(DnsResolveContext.java:414)
        at io.netty.resolver.dns.DnsResolveContext.onResponse(DnsResolveContext.java:625)
        at io.netty.resolver.dns.DnsResolveContext.access$400(DnsResolveContext.java:63)
        at io.netty.resolver.dns.DnsResolveContext$2.operationComplete(DnsResolveContext.java:458)
        at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:578)
        at io.netty.util.concurrent.DefaultPromise.notifyListeners0(DefaultPromise.java:571)
        at io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:550)
        at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:491)
        at io.netty.util.concurrent.DefaultPromise.setValue0(DefaultPromise.java:616)
        at io.netty.util.concurrent.DefaultPromise.setSuccess0(DefaultPromise.java:605)
        at io.netty.util.concurrent.DefaultPromise.trySuccess(DefaultPromise.java:104)
        at io.netty.resolver.dns.DnsQueryContext.trySuccess(DnsQueryContext.java:201)
        at io.netty.resolver.dns.DnsQueryContext.finish(DnsQueryContext.java:193)
        at io.netty.resolver.dns.DnsNameResolver$DnsResponseHandler.channelRead(DnsNameResolver.java:1264)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
        at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
        at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
        at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
        at io.netty.channel.nio.AbstractNioMessageChannel$NioMessageUnsafe.read(AbstractNioMessageChannel.java:97)
        at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:719)
        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.base/java.lang.Thread.run(Thread.java:832)

【问题讨论】:

看来是DNS问题,我们必须在所有appliation.properties文件中启用下面的,它解决了我的问题----eureka.instance.prefer-ip-address=true ***.com/questions/65401512/… - 作为参考 【参考方案1】:

显然是通过添加

eureka.instance.prefer-ip-address=true
eureka.instance.hostname=localhost

在所有微服务中(减去名称服务) 接缝工作。

我还添加了@依赖项

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

【讨论】:

以上是关于API-GATEWAY 不起作用。抛出 java.net.UnknownHostException:无法解决的主要内容,如果未能解决你的问题,请参考以下文章

我的查询根本不起作用并抛出错误

在 iOS 上启动屏幕后 React Native 白色闪烁:官方解决方案不起作用,抛出错误(为啥??)

codeception 中的 fillField 方法不起作用抛出 DOMXPath::query(): Invalid expression

PHP & WP:当从 DB 抛出错误时,尝试 catch 不起作用

Laravel 8 多对多关系不起作用(没有抛出错误)

具有 kerberos 安全性的 Sqoop 在 cron 中不起作用,抛出 GSSException