question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot reconnect to Nats cluster

See original GitHub issue

I have a Nats cluster setup. When the Nats server that is being used by the java app is terminated, the app tries to reconnect but it throws the following exception.

java.io.IOException: java.net.URISyntaxException: Illegal character in scheme name at index 0: 172.18.0.101:4222
	at io.nats.client.impl.SocketDataPort.connect(SocketDataPort.java:60)
	at io.nats.client.impl.NatsConnection.tryToConnect(NatsConnection.java:299)
	at io.nats.client.impl.NatsConnection.reconnect(NatsConnection.java:225)
	at io.nats.client.impl.NatsConnection.closeSocket(NatsConnection.java:471)
	at io.nats.client.impl.NatsConnection.lambda$handleCommunicationIssue$2(NatsConnection.java:428)
	at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.net.URISyntaxException: Illegal character in scheme name at index 0: 172.18.0.101:4222
	at java.base/java.net.URI$Parser.fail(URI.java:2915)
	at java.base/java.net.URI$Parser.checkChars(URI.java:3086)
	at java.base/java.net.URI$Parser.checkChar(URI.java:3096)
	at java.base/java.net.URI$Parser.parse(URI.java:3111)
	at java.base/java.net.URI.<init>(URI.java:600)
	at io.nats.client.impl.SocketDataPort.connect(SocketDataPort.java:47)
	... 5 more

From what I can see, Nats publishes all addresses of Nats servers in the cluster in the following format ip-address:port without the scheme. Check the connect_urls values below.

{"server_id":"iHAhz9B35WdjjgYytJ35ze","version":"1.3.0","proto":1,"git_commit":"eed4fbc","go":"go1.11","host":"0.0.0.0","port":4222,"max_payload":1048576,"client_id":8,"connect_urls":["172.18.0.100:4222","172.18.0.101:4222","172.18.0.102:4222"]}

When the Nats client tries to reconnect, it will use one of those addresses and create a java.net.URI instance from it. Looks like the URI class does not accept a ip-address:port format. Running the code below, will throws the same exception.

URI url = new URI("172.18.0.1:4222");

However, if the nats scheme is added, the URI will work fine.

URI url = new URI("nats://172.18.0.1:4222");

Should the nats:// scheme be always used when building the server list in NatsConnection#getServers()?

Actually, it is not possible to a Nats server by addressing it without the scheme because looks like java.net.URI requires a scheme. None of the following code works.

Nats.connect("localhost:4222");
Nats.connect("127.0.0.1:4222");

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
sasburycommented, Nov 27, 2018

ok, it turns out blindly adding nats:// is not necessarily bad, because the place that was failing doesn’t care about the protocol. BUT, i did a new fix that is a bit more careful and checks the URI in all the places we use it. I am re-running tests and will commit momentarily into v2.4.0 branch. Hoping to release that branch this week, but if you get a chance to try before I release it would be awesome just in case the tests miss something.

Oh, i also fixed the unit test that was for servers in info to remove the nats:// to test this.

1reaction
sasburycommented, Nov 25, 2018

Should get this fixed this week, I have other tasks in there anyway

On Sun, Nov 25, 2018, 12:26 PM Ivan Kozlovic <notifications@github.com wrote:

@sasbury https://github.com/sasbury This is correct analysis. The server sends connect URLs of all servers in the cluster in the form IP:port, without a scheme (or username and password for that matter). The client library is responsible for adding it.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/nats-io/java-nats/issues/195#issuecomment-441470146, or mute the thread https://github.com/notifications/unsubscribe-auth/ADKH0esNe-152lNqL9WEETMLGuyhPqgnks5uyvzugaJpZM4YyCjY .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot re-connect to server after nats-streaming ... - GitHub
I've encountered the following issue while trying to reconnect to nats-streaming-server. Here are the steps to reproduce:.
Read more >
Automatic Reconnections - NATS Docs
Upon re-connection the client library will automatically re-establish all the subscriptions, there is nothing for the application programmer to do.
Read more >
can't connect to nats://nats.openfaas.svc.cluster.local:4222
Error configuring OpenFaas - can't connect to nats://nats.openfaas.svc.cluster.local:4222 · Ask Question. Asked 3 years, 3 months ago.
Read more >
"nats-connection-error" in Gorouter when connecting to non ...
When the connection is interrupted, the client will try to reconnect to any peer in the cluster. This can be problematic for various...
Read more >
nats - Go Packages
RetryOnFailedConnect sets the connection in reconnecting state right away if it can't connect to a server in the initial set. See RetryOnFailedConnect option ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found