我使用Java 17、Sping Boot v3.1.3和Spring Cloud堆栈来执行服务间通信。
当我向http://localhost:8081/api/order发出POST请求时收到此错误
订单服务错误:
java.net.UnknownHostException: Failed to resolve 'inventory-service' [A(1), AAAA(28)] after 6 queries
at io.netty.resolver.dns.DnsResolveContext.finishResolve(DnsResolveContext.java:1097) ~[netty-resolver-dns-4.1.97.Final.jar:4.1.97.Final]
at io.netty.resolver.dns.DnsResolveContext.tryToFinishResolve(DnsResolveContext.java:1044) ~[netty-resolver-dns-4.1.97.Final.jar:4.1.97.Final]
at io.netty.resolver.dns.DnsResolveContext.query(DnsResolveContext.java:432) ~[netty-resolver-dns-4.1.97.Final.jar:4.1.97.Final]
at io.netty.resolver.dns.DnsResolveContext.onResponse(DnsResolveContext.java:662) ~[netty-resolver-dns-4.1.97.Final.jar:4.1.97.Final]
at io.netty.resolver.dns.DnsResolveContext.access$500(DnsResolveContext.java:66) ~[netty-resolver-dns-4.1.97.Final.jar:4.1.97.Final]
at io.netty.resolver.dns.DnsResolveContext$2.operationComplete(DnsResolveContext.java:489) ~[netty-resolver-dns-4.1.97.Final.jar:4.1.97.Final]
at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:590) ~[netty-common-4.1.97.Final.jar:4.1.97.Final]
at io.netty.util.concurrent.DefaultPromise.notifyListeners0(DefaultPromise.java:583) ~[netty-common-4.1.97.Final.jar:4.1.97.Final]
at io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:559) ~[netty-common-4.1.97.Final.jar:4.1.97.Final]
at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:492) ~[netty-common-4.1.97.Final.jar:4.1.97.Final]
at io.netty.util.concurrent.DefaultPromise.setValue0(DefaultPromise.java:636) ~[netty-common-4.1.97.Final.jar:4.1.97.Final]
at io.netty.util.concurrent.DefaultPromise.setSuccess0(DefaultPromise.java:625) ~[netty-common-4.1.97.Final.jar:4.1.97.Final]
at io.netty.util.concurrent.DefaultPromise.trySuccess(DefaultPromise.java:105) ~[netty-common-4.1.97.Final.jar:4.1.97.Final]
at io.netty.resolver.dns.DnsQueryContext.trySuccess(DnsQueryContext.java:317) ~[netty-resolver-dns-4.1.97.Final.jar:4.1.97.Final]
at io.netty.resolver.dns.DnsQueryContext.finishSuccess(DnsQueryContext.java:309) ~[netty-resolver-dns-4.1.97.Final.jar:4.1.97.Final]
at io.netty.resolver.dns.DnsNameResolver$DnsResponseHandler.channelRead(DnsNameResolver.java:1392) ~[netty-resolver-dns-4.1.97.Final.jar:4.1.97.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:444) ~[netty-transport-4.1.97.Final.jar:4.1.97.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420) ~[netty-transport-4.1.97.Final.jar:4.1.97.Final]
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412) ~[netty-transport-4.1.97.Final.jar:4.1.97.Final]
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103) ~[netty-codec-4.1.97.Final.jar:4.1.97.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:444) ~[netty-transport-4.1.97.Final.jar:4.1.97.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420) ~[netty-transport-4.1.97.Final.jar:4.1.97.Final]
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412) ~[netty-transport-4.1.97.Final.jar:4.1.97.Final]
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410) ~[netty-transport-4.1.97.Final.jar:4.1.97.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:440) ~[netty-transport-4.1.97.Final.jar:4.1.97.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420) ~[netty-transport-4.1.97.Final.jar:4.1.97.Final]
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919) ~[netty-transport-4.1.97.Final.jar:4.1.97.Final]
at io.netty.channel.nio.AbstractNioMessageChannel$NioMessageUnsafe.read(AbstractNioMessageChannel.java:97) ~[netty-transport-4.1.97.Final.jar:4.1.97.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:788) ~[netty-transport-4.1.97.Final.jar:4.1.97.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:724) ~[netty-transport-4.1.97.Final.jar:4.1.97.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:650) ~[netty-transport-4.1.97.Final.jar:4.1.97.Final]
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562) ~[netty-transport-4.1.97.Final.jar:4.1.97.Final]
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997) ~[netty-common-4.1.97.Final.jar:4.1.97.Final]
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.97.Final.jar:4.1.97.Final]
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.97.Final.jar:4.1.97.Final]
at java.base/java.lang.Thread.run(Thread.java:1589) ~[na:na]
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>4.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
<version>3.1.6</version>
</dependency>
WebClientConfig类:
@Configuration
public class WebClientConfig {
@Bean
@LoadBalanced
public WebClient.Builder webClientBuilder() {
return WebClient.builder();
}
}
OrderService类:
private final WebClient.Builder webClientBuilder;
public void placeOrder(OrderRequest orderRequest) {
Order order = new Order();
order.setOrderNumber(UUID.randomUUID().toString());
List<OrderLineItems> orderLineItems = orderRequest.getOrderLineItemsDto()
.stream()
.map(this::mapToDto)
.toList();
order.setOrderLineItems(orderLineItems);
List<String> skuCodes = order.getOrderLineItems().stream()
.map(OrderLineItems::getSkuCode)
.toList();
InventoryResponse[] inventoryResponses = webClientBuilder.build().get()
.uri("http://inventory-service/api/inventory",
uriBuilder -> uriBuilder.queryParam("skuCode", skuCodes).build())
.retrieve()
.bodyToMono(InventoryResponse[].class)
.block();
application.properties:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/order-service
spring.datasource.username=username
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update
spring.application.name=order-service
server.port=8081
eureka.client.service-url.default-zone=http://localhost:8761/eureka
eureka.instance.prefer-ip-address=true
eureka.instance.hostname=localhost
application.properties从inventory-service:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3307/inventory-service
spring.datasource.username=root
spring.datasource.password=1234
spring.jpa.hibernate.ddl-auto=create-drop
spring.application.name=inventory-service
server.port=0
eureka.client.service-url.default-zone=http://localhost:8761/eureka
application.properties从discovery-server:
server.port=8761
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://localhost:${server.port}/eureka/
eureka.instance.prefer-ip-address=true
我真的不知道我做错了什么。
2条答案
按热度按时间zzzyeukh1#
您的错误是因为您的DNS没有将
inventory-service
解析为任何IPv4/6。我不知道这个服务是什么,但它是从你的
OrderService
调用/配置的:你必须适当地改变它:从您的配置中,我们可以看到您使用了来自类似命名服务的本地数据库:
如果两个服务都在同一台机器上启动,那么你可能需要将
http://inventory-service
更改为http://localhost:?/
:?
是因为您的库存服务application.properties使用server.port=0
;您可以尝试修复一个端口并在
OrderService
中使用它。但是,您的
discovery-server
可能意味着您的每个服务都使用它来“发现”实际的端点。对于这一点,我不能帮助,因为我不知道Eureka (我想这是发现服务的名称正在配置的属性)。
mspsb9vt2#
尝试用途:
而不是: