Java客户端连接不了安装在CentOS7上的redis解决方案

今天初试了使用Java Redisson 客户端连接安装在CentOS上的redis, 发现一直连接不了, 报错信息如下:

Exception in thread "main" com.lambdaworks.redis.RedisException: Unable to connect
	at com.lambdaworks.redis.RedisClient.connect(RedisClient.java:188)
	at com.lambdaworks.redis.RedisClient.connectAsync(RedisClient.java:148)
	at com.lambdaworks.redis.RedisClient.connect(RedisClient.java:131)
	at org.redisson.connection.ConnectionManager.connection(ConnectionManager.java:119)
	at org.redisson.RedissonMap.put(RedissonMap.java:95)
	at redis.RedisExample.main(RedisExample.java:25)
Caused by: java.net.ConnectException: Connection timed out: no further information: /xxx.xxx.xxx.xxx:6379
	at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
	at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:735)
	at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:191)
	at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:241)
	at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:502)
	at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:452)
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:346)
	at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:101)
	at java.lang.Thread.run(Thread.java:744)

网上找了很多方法, 说关闭防火墙即可, 下面为操作防火墙的命令。

启动一个服务:systemctl start firewalld.service
关闭一个服务:systemctl stop firewalld.service
重启一个服务:systemctl restart firewalld.service
显示一个服务的状态:systemctl status firewalld.service
在开机时启用一个服务:systemctl enable firewalld.service
在开机时禁用一个服务:systemctl disable firewalld.service
查看服务是否开机启动:systemctl is-enabled firewalld.service;echo $?
查看已启动的服务列表:systemctl list-unit-files|grep enabled

我们选择第二个, 可是怎么关闭也不好使。还是和以前一样的报错。忽然想到安装在CentOS上的redis有一个redis.conf配置文件(我是在/etc/redis文件夹下)

使用命令打开

[root@zz /]# cd /etc/redis
[root@zz redis]# ll
总用量 48
-rw-r--r--. 1 root root 46736 3月  15 23:23 redis.conf
[root@zz redis]# vim redis.conf
进去之后直接敲 /bind 回车。这时候找到一段

# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 lookback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1
看最后一行, 将这个ip改成你们虚拟机的IP地址即可, 改完之后 按ESC, shift+:,wq+回车

查看虚拟机ip命令, 红色部分就是虚拟机的ip地址

[root@zz redis]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet xxx.xxx.xxx.xxx  netmask 255.255.255.0  broadcast xxx.xxx.xxx.255
        inet6 fe80::6beb:dc52:2424:5df9  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:10:97:1e  txqueuelen 1000  (Ethernet)
        RX packets 4108  bytes 266330 (260.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 532  bytes 46576 (45.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

开始测试,搞定!


总结就三步:

1:ifconfig查看虚拟机分配的ip
2:修改redis.conf中bind 指定虚拟机的ip
3:启动:redis-server (具体目录)/redis.conf


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值