当前位置: 首页 > 知识库问答 >
问题:

API网关不起作用。抛出java.net.未知主机异常:无法解决

水渊
2023-03-14

我使用的是Spring版本-2.4.5

pplication.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网关时仍面临以下错误:

http://localhost:8765/currency-兑换/货币兑换/自/美元/至/印度卢比(或)

http://localhost:8765/CURRENCY-兑换/货币兑换/自/美元/至/印度卢比

    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)

共有2个答案

赵智
2023-03-14

我经历了同样的训练,面临着同样的问题。

然而仔细看;我发现我在货币兑换中定义了一些额外的属性\application.properties.发表评论解决了这个问题。

   #spring.profiles.active=dev
   #spring.cloud.config.profile=dev
   #eureka.instance.hostname=localhost
   #spring.cloud.discovery.enabled=true

这确实是调查工作。但我知道有时候这真的很疯狂。

曹旭东
2023-03-14

显然通过添加

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

在所有的微服务(减去namesr副)接缝中工作。

我还添加了@依赖项

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
 类似资料:
  • 您好,我正在尝试获取rss源,代码如下: 它由和AsyncTask包装,我得到了这个日志: 有人知道吗?我也试过google news rss feed(https://news.google.com/news/rss/headlines/section/topic/WORLD?ned=us 方法parseFeed():

  • 我收到这个错误消息"java.net.未知主机异常:无法解决主机"api.themoviedb.org":没有与主机名相关联的地址"。我读到的所有stackoverflow帖子都说清单文件可能是错误的,但我认为在这一点上是正确的,所以我不确定如何继续。 AndroidManifest。xml: MainActivity.java: }

  • 在AWS EC2 T2上部署停靠的Spring Boot应用程序时,我遇到了这个“java.net.UnknownHostException:host.docker.internal:Name或service not known”问题。微实例。由于此错误,spring启动应用程序无法启动。 但奇怪的是,我没有在应用程序的任何地方使用变量“host.docker.internal”:不在代码中,不在

  • 问题内容: 我正在尝试通过android应用程序中的HTTP Post请求访问textalertapp.com。但是我收到了未知主机错误。谁能帮我解决这个问题。 代码是 问题答案: 我只是在清单文件中添加了这一行。问题解决了

  • 我的机器运行的是hbase-0.94.16服务器,因此当我尝试使用hbase definitive primitive guide中的文档来安装hbase hush服务器时,它出现了以下异常 14/05/22 11:03:43信息zookeeper.zookeeper:客户端环境:java.io.tmpdir=/tmp 14/05/22 11:03:43信息zookeeper.zookeeper:

  • 我在JMeter上运行测试脚本时遇到过这种错误? java.net.unknownHostException:h2ebiz-testat java.net.inet6addressimpl.lookupAllHostAddr(本机方法)位于java.net.inetAddress.2.lookupAllHostAddr(未知源)位于java.net.inetAddress.getAddress.g