spring server cannot connect to redis using jedis client - java

I am getting this issue when I deploy redis server and spring server in two different servers and try to connect to spring server through my application.
App Details:
We have mobile application which publish gps coordinates to spring server using stomp. In the spring server we create jedis pubsub connection and publish those gps data to our web application and web users subscribe to those jedis pubsub connections.
Library versions:
stomp:1.7.1
jedis: 2.8.1
spring: 4.3.0
Working scenarios:
*Deploy spring server in my local machine and redis server in remote production server.
*Deploy spring server in remote server and redis server in same remote server where spring server is deployed.
Partially Working scenarios:
*Deploy spring server in remote server and redis server in different remote server where spring server is deployed. In this scenario I monitor redis server using redis cli and I can see the "HGETALL", "PUBLISH" key words with its data. But the same time I got following error in spring server:
Caused by:
java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at redis.clients.jedis.Connection.connect(Connection.java:158)
... 4 more
redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused (Connection refused)
at redis.clients.jedis.Connection.connect(Connection.java:164)
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:80)
at redis.clients.jedis.Connection.setTimeoutInfinite(Connection.java:68)
at redis.clients.jedis.Jedis.subscribe(Jedis.java:2626)
at shipxpress.pubsubservice.controllers.SubscriberThread.run(MainController.java:227)
The spring server can successfully depoyed to the server and exception occurs when jedis try to publish or subscripe to the connection.
I can succesfully ping to redis server from the server where we deploy the spring server through the redis-cli and the redis server prtected-mode is no and and bind ip is set to 0.0.0.0
Links:
I went through following links but no luck
Redis bind to more than one IP
https://github.com/xetorthio/jedis/issues/1405
Cannot connect to redis using jedis

This issue means that the Spring remote server is not able to communicate with the other remote server(Redis server) on the default Redis port.
Maybe you could confirm this by trying to access the Remote Redis server from the other remote Server using netstat from the console.

I had the same issue, and the simplest solution was
Find the following line in your redis.conf file and comment it out:
bind 127.0.0.1
By adding a # in front of it:
# bind 127.0.0.1
and change the line protected-mode yes to be protected-mode no
save your redis.conf and restart redis using the config file
redis-server /configFileLocation

Related

Spring Boot UnknownHostException while connecting to RDS throgh VPC peering

I am getting the UnknownHostException while trying to deploy a Spring Boot application to a K8s Cluster in AWS EKS.
Scenario:
I have a AWS EKS Cluster running on a VPC (A) and the RDS is running on a VPC (B).
I have created the VPC peering connection between the two VPCs, enabling DNS resolution and configuring the routing tables accordingly.
I also created a K8s Service mapping the external RDS endpoint.
If I try to connect to the RDS instance from EKS Cluster using a busybox like this:
$ kubectl run -i --tty --rm debug --image=busybox --restart=Never -- sh
with any of the following options, it works:
/ # nc mysql-service 3306
/ # nc mysql-service.default.svc.cluster.local 3306
/ # nc xxxxxxx.xxxxxxx.us-east-1.rds.amazonaws.com 3306
But when I deploy my Spring Boot application it doesn't work.
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:54)
at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:93)
at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:133)
at com.mysql.cj.core.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:149)
at com.mysql.cj.mysqla.io.MysqlaSocketConnection.connect(MysqlaSocketConnection.java:83)
at com.mysql.cj.mysqla.MysqlaSession.connect(MysqlaSession.java:144)
at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:1676)
... 58 common frames omitted
Caused by: java.net.UnknownHostException: mysql-service
Any suggestions?
Thanks!
I figured it out! The whole VPC Peering configuration is working. The K8s external service is also working. The problem was in my Spring Boot application.
I had configured the data source URL with an environment variable which was causing the error. For some reason, Spring doesn't resolve the host configured with an environment variable.
I just changed the spring configuration from:
spring:
datasource:
url: jdbc:mysql://${DB_HOST}:3306/db_name
to:
spring:
datasource:
url: jdbc:mysql://mysql-service:3306/db_name
and now it works!

Connecting Azure app service to MongoDB Atlas across vnet peering connection

So I have an Azure App Service and I want it to be able to connect to mongo db atlas. For our Atlas setup there is a peering connection between our managed environment on azure (and the vnet it all sits in) and our mongo db cluster.
The mongo cluster is setup with "Connect via peering only" which we can't change. This means we can't connect to our db with any old public ip address -It has to come from the vnet it's peered with. The vms (from within the peered vnet) are able to access the db no problem.
I tried with the app service to connect to the db with the same connection string that worked for the vm. It returned a the error:
java.net.ConnectException: Connection refused (Connection refused)}}, {address=MYMONGOADDRESS.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}]
This is expected due to the reasons above. However I setup the vnet integration for the app service
. I created a dedicated subnet as described for the app service and whitelisted the ip range in mongo. However I still couldn't connect to the db after using this vnet integration. I also got a very similar error but slightly different.
{java.net.SocketTimeoutException: connect timed out}}, {address=MYMONGOADDRESS.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}]
Where instead of a connection refused I'm not getting a connection time out. I've repeated this a few times and get the same result.
Anyone know why these errors are different and any help in being able to connect to the db from my azure app service?
Is there any firewall or network security group? What the network peering status is? If it shows « initiated » then this means it has not been completed. Network peering has to be done on each ends. If it shows « connected », then I would look for firewall or network security groups.
See this
https://docs.atlas.mongodb.com/security-vpc-peering/#vpc-peering
No idea if this can apply to your case but this question was the first and only that came up when I was searching for something similar.
In my case I have a MongoDB Atlas deployed on Azure and an app running in AKS. I set up peering as documented but still could not connect from the app container in AKS to the MongoDB. Turns out the problem was an incorrect connection string.
Atlas has a different URL when connecting directly and when connecting through a peer network:
# Direct connection
mongo "mongodb+srv://<cluster>.<something>.mongodb.net/<dbname>"
# Peered connection (note the -pri)
mongo "mongodb+srv://<cluster>-pri.<something>.mongodb.net/<dbname>"
As an additional note, I configured the Atlas Network Acces IP address list with the CIDR of the Azure VN Subnet used in AKS. (Under Virtual Network > Subnets > IPv4)

Connection refused when connecting to RabbitMQ via IP address instead of localhost

I'm trying to set up a RabbitMQ server (version 3.7.4, installed via homebrew on macOS 10.13.4) such that my colleagues can work with it via the internal network. We are using the official Java client (com.rabbitmq:amqp-client:5.2.0). Unfortunately, they only get a ConnectException with the message Connection refused:
java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.rabbitmq.client.impl.SocketFrameHandlerFactory.create(SocketFrameHandlerFactory.java:60)
at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:62)
at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:134)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:997)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:956)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:914)
at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1068)
at my.own.private.RabbitMqConnector.connect(RabbitMqConnector.java:29)
We already set up another user account (not guest) which has admin privileges. With almost the same Java code I can connect locally (setting the host to localhost) but not if I use my IP address. According to the RabbitMQ docs, the RabbitMQ server binds to all network interfaces by default (otherwise I would have said that it's a problem with address binding). I disabled my firewall so that shouldn't be the problem.
Here's the respective code:
final ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setUsername("username");
connectionFactory.setPassword("password");
connectionFactory.setHost("10.10.33.12");
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/");
try (final Connection connection = connectionFactory.newConnection()) {
// doo stuff with the connection
}
I finally found the actual problem and it's not related to Java or how I configured the ConnectionFactory. It was indeed a problem how the RabbitMQ package for homebrew is configured. In the rabbitmq-env.conf file, there was the following setting
NODE_IP_ADDRESS=127.0.0.1
This setting caused RabbitMQ to only listen on 127.0.0.1 for incoming connections. Once I removed the setting and restarted the service, I could connect from other machines as well.
Thank you all for your help!
The issue is not with your code. I would suggest you to check such things:
RabbitMQ Server is up and running
Your custom user can access virtual host "/"
Check firewall settings if any, probably it's refusing connection with remote host
One of these should be the cause, hope it helps!

Java SFTP client application in Cloud Foundary Environment

I have a java application ,which trasfters a file to SFTP server.
Our application use jsch for implementation of sftp client in java.
Jsch requires below parameters to connect to a SFTP server:
Host name of SFTP server: XX.XX.XXX.XXX
Port : 22
Username : abc
Password : ****
My application is working fine and transferring the file when I deploy it on Unix boxes or run in my local machine.
Now I am going to deploy the application on Cloud Foundry, but this is giving me ConnectException.
2018-03-14T19:07:40.41+0530 [APP/PROC/WEB/0] OUT com.jcraft.jsch.JSchException: java.net.ConnectException: Connection refused (Connection refused)
I have updated the security group :
{
"destination": "XX.XX.XXX.XXX",
"protocol": "tcp",
"ports": "22"
}
PS:
"protocol": "sftp" is invalid ,so I gave "protocol": "tcp"
Cloud foundary(Private cloud) and SFTP server both are in my local premises.
I read about volume services but ,I did not find any clue. We do not have marketplace accessible, so If volume service can help then we will have to build those service.
Queries:
How can I access my SFTP server via a application which is deployed on Cloud Foundry?

Communication between two REST Services

I have two REST Services (developed using Spring Boot), one for Authentication and one that returns Data. The Data service validates a token against the Authentication service when data i requested. The validation is done by the Data service sending a request to the Authentication service containing an ID like this https://name.com/auth/key?id=[ID]. I have tested the validation by having the Authentication service deployed as a WAR-file on a Tomcat server https://name.com/auth/ and having the Data service running local on my computer. This setup worked fine. Now I have deployed the Data service as WAR-file to the same Tomcat server https://name.com/data/. The issue now is that the Data service is not able to connect to the Authentication service giving the following error message
HTTP Status 500 - I/O error on GET request for "https://name.com/auth/key": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
Why is the connection refused when both WAR-files are running on the same Tomcat server?
The issue was by using the internal IP addresses rather then the external.

Categories