我是靠谱客的博主 鳗鱼抽屉,最近开发中收集的这篇文章主要介绍redis链接Jedis的配置,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

用java连接虚拟机中的Redis
package redis.test;

import redis.clients.jedis.Jedis;

public class TestPing {

    @SuppressWarnings("resource")
    public static void main(String[] args) {

        Jedis jedis = new Jedis("192.168.161.129", 6381);

        System.out.println(jedis.ping());

    }
}

总结以下连接步骤:
一. 在虚拟机centos上根据ifconfig命令确定虚拟机ip
[root@localhost /]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.161.129  netmask 255.255.255.0  broadcast 192.168.161.255
        inet6 fe80::857b:658f:eda:a36d  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:65:7d:3b  txqueuelen 1000  (Ethernet)
        RX packets 475  bytes 35307 (34.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 238  bytes 22676 (22.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 731  bytes 122187 (119.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 731  bytes 122187 (119.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:6d:4b:72  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0



其中ip为ens33中的inet 192.168.161.129,new Jedis时使用该ip。
二. 关闭centos7默认防火墙,启用iptables防火墙
1. 关闭firewall:
systemctl stop firewalld.service   #停止firewall
systemctl disable firewalld.service   #禁止firewall开机启动
firewall-cmd --state   #查看默认防火墙状态(关闭后显示not running,开启后显示running)

2. 安装iptables防火墙
yum -y install iptables-services #安装iptables防火墙
1
3. 配置防火墙需要过滤的端口,即Reids使用的端口
vi /etc/sysconfig/iptables #编辑防火墙配置文件
1
配置文件内容如下:

# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT

-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT


-A INPUT -p tcp -m state --state NEW -m tcp --dport 6379 -j ACCEPT

添加到
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT

的上面或下面,切记不要添加到最后一行,否则防火墙重启后不生效。
6379为Redis的端口号。
之后保存退出,执行以下命令:
systemctl restart iptables.service   #最后重启防火墙使配置生效
systemctl enable iptables.service    #设置防火墙开机启动
1
2
最后重启虚拟机。
若端口未配置,会报以下错误:
Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect timed out
    at redis.clients.jedis.Connection.connect(Connection.java:207)
    at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93)
    at redis.clients.jedis.Connection.sendCommand(Connection.java:126)
    at redis.clients.jedis.Connection.sendCommand(Connection.java:121)
    at redis.clients.jedis.BinaryClient.ping(BinaryClient.java:106)
    at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:195)
    at redis.test.TestPing.main(TestPing.java:12)
Caused by: java.net.SocketTimeoutException: connect timed out
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    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.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at redis.clients.jedis.Connection.connect(Connection.java:184)
    ... 6 more

三. 修改对应的redis.conf配置文件
1. 注释掉bind 127.0.0.1
#bind 127.0.0.1
1
如果未注释,会报以下错误:
Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect
    at redis.clients.jedis.Connection.connect(Connection.java:207)
    at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93)
    at redis.clients.jedis.Connection.sendCommand(Connection.java:126)
    at redis.clients.jedis.Connection.sendCommand(Connection.java:121)
    at redis.clients.jedis.BinaryClient.ping(BinaryClient.java:106)
    at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:195)
    at redis.test.TestPing.main(TestPing.java:12)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    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.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at redis.clients.jedis.Connection.connect(Connection.java:184)
    ... 6 more
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2. 关闭protected-mode
protected-mode no
1
如果未关闭,会报以下错误:
Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
    at redis.clients.jedis.Protocol.processError(Protocol.java:127)
    at redis.clients.jedis.Protocol.process(Protocol.java:161)
    at redis.clients.jedis.Protocol.read(Protocol.java:215)
    at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340)
    at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:239)
    at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:196)
    at redis.test.TestPing.main(TestPing.java:12)
最后感谢作者:https://blog.csdn.net/xw12138/article/details/78065905

最后

以上就是鳗鱼抽屉为你收集整理的redis链接Jedis的配置的全部内容,希望文章能够帮你解决redis链接Jedis的配置所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(61)

评论列表共有 0 条评论

立即
投稿
返回
顶部