AnnotatedConnectException: Connection refused: localhost/127.0.1.1:37655
See original GitHub issueWhen there are more than one entries for localhost in /etc/hosts
, connection is refused.
This happens only when using DnsAddressResolverGroup
. If DefaultAddressResolverGroup
is used there is no problem with connection establishment.
# /etc/hosts
127.0.1.1 localhost
127.0.0.1 localhost
If 127.0.0.1
is first then there is no problem
# /etc/hosts
127.0.0.1 localhost
127.0.1.1 localhost
Original issue https://github.com/reactor/reactor-netty/issues/1405
Expected behavior
Connection to be established regardless of the AddressResolverGroup
that is used.
Actual behavior
Exception is thrown with DnsAddressResolverGroup
:
AnnotatedConnectException: Connection refused: localhost/127.0.1.1:37655
Steps to reproduce
A reproducible example and steps are described in the issue https://github.com/reactor/reactor-netty/issues/1405
I added additional test to the existing test cases here https://github.com/hisener/playground/blob/master/spring-webclient-connection-failure-test/src/test/java/com/github/hisener/spring/webclient/WebClientTest.java
@RepeatedTest(10)
void nettyClient() throws Exception {
NioEventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group)
.channel(NioSocketChannel.class)
// There is no issue with DefaultAddressResolverGroup
//.resolver(DefaultAddressResolverGroup.INSTANCE)
.resolver(new DnsAddressResolverGroup(new DnsNameResolverBuilder(group.next()).channelType(NioDatagramChannel.class)))
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) {
socketChannel.pipeline()
.addLast(new HttpClientCodec());
}
});
Channel ch = bootstrap.connect(baseUrl.getHost(), baseUrl.getPort())
.sync()
.channel();
ch.close();
}
finally {
group.shutdownGracefully();
}
}
Netty version
4.1.55.Final-SNAPSHOT
JVM version (e.g. java -version
)
openjdk version “11.0.9.1” 2020-11-04 OpenJDK Runtime Environment 18.9 (build 11.0.9.1+1) OpenJDK 64-Bit Server VM 18.9 (build 11.0.9.1+1, mixed mode)
OS version (e.g. uname -a
)
Linux b732417723fd 5.4.39-linuxkit #1 SMP Fri May 8 23:03:06 UTC 2020 x86_64 GNU/Linux
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:10 (7 by maintainers)
Top GitHub Comments
@normanmaurer PTAL #11246 , I made some tricks to keep the backwards compatibility.
@normanmaurer The current Netty implementations returns always only one address for
localhost
whenresolveAll
is invoked. If Netty is able to return all resolved addresses forlocalhost
, Reactor Netty will be able to iterate over the result.https://github.com/netty/netty/blob/6724786dcc9fa38ba516dab97a04e2bbc17e81d9/resolver/src/main/java/io/netty/resolver/DefaultHostsFileEntriesResolver.java#L46-L63
https://github.com/netty/netty/blob/6724786dcc9fa38ba516dab97a04e2bbc17e81d9/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java#L1023
https://github.com/netty/netty/blob/6724786dcc9fa38ba516dab97a04e2bbc17e81d9/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java#L684-L697
Also JDK’s
HostsFileNameService
returns all entries IPv4/IPv6 forlocalhost
, while Netty’sHostsFileParser
always returns the first for IPv4/IPv6. https://github.com/openjdk/jdk/blob/e8eda655bb27ab1b8d806a64622ea7ee9719a85d/src/java.base/share/classes/java/net/InetAddress.java#L1047-L1054https://github.com/netty/netty/blob/6724786dcc9fa38ba516dab97a04e2bbc17e81d9/resolver/src/main/java/io/netty/resolver/HostsFileParser.java#L198-L209
Here is a simple test and its results:
Also I’m seeing that when I use JDK’s
PlatformNameService
, the result is sorted, while when I force the usage ofHostsFileNameService
, they are not.So I need a method that returns all entries for
localhost
like in JDK’s implementation (preferably sorted). If it is not possible to modify the current interface (io.netty.resolver.HostsFileEntriesResolver), is it possible to introduce a new one? May be evencompleteOncePreferredResolved
can be used.