redis学习(二):java使用Jedis连接CentOS-7中的redis数据库

环境:

Linux系统:VMware 14 中 CentOS 7 64 位

redis版本:redis-stable.tar.gz(当前版本是:redis-cli 4.0.9)

一、在java中使用Jedis连接redis数据库,实现步骤:

1、需要加入Jedis的jar包:

<dependency>
	<groupId>redis.clients</groupId>
	<artifactId>jedis</artifactId>
	<version>2.9.0</version>
</dependency>

2、java代码:

@Test
public void redisTest2(){
    //连接本地的 Redis 服务
     Jedis jedis = new Jedis("192.168.138.131",6379);
     //查看服务是否运行
     System.out.println("服务正在运行: "+jedis.ping());
     jedis.set("a", "小王");
     System.out.println(jedis.get("a"));
     jedis.close();
}

在Linux系统上面执行ifconfig命令获取Linux系统的ip,如下:

[root@localhost /]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.138.131  netmask 255.255.255.0  broadcast 192.168.138.255
        inet6 fe80::2114:b02c:ad47:d0d3  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:b7:e5:7d  txqueuelen 1000  (Ethernet)
        RX packets 53168  bytes 73659986 (70.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 25995  bytes 1948044 (1.8 MiB)
        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 124  bytes 27019 (26.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 124  bytes 27019 (26.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost /]# 
其中ip为ens33中的inet 192.168.138.131,new Jedis时使用该ip,后面的是端口号,默认的是6379。

3、运行时出现错误,是因为Linux防火墙的问题。

二、配置Linux防火墙

1、关闭centos7默认防火墙,启用iptables防火墙。(在根目录下执行)

systemctl stop firewalld.service   #停止firewall
systemctl disable firewalld.service   #禁止firewall开机启动
firewall-cmd --state   #查看默认防火墙状态(关闭后显示not running,开启后显示running)

2. 安装iptables防火墙

yum -y install iptables-services #安装iptables防火墙

3. 配置防火墙需要过滤的端口,即Reids使用的端口。

编译:/etc/sysconfig/iptables  配置文件

配置文件内容如下:
# 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的端口号。

4、保存,执行以下命令(在根目录执行):

systemctl restart iptables.service   #最后重启防火墙使配置生效
systemctl enable iptables.service    #设置防火墙开机启动

5、重新启动虚拟机。

6、修改redis安装目录下面的redis.conf文件,即usr/local/redis/bin/

1)注释掉 bind 127.0.0.1

#bind 127.0.0.1

2)关闭 protected-mode,把yes改成no

protected-mode no

3)重新启动redis

参考文章:https://blog.csdn.net/xw12138/article/details/78065905

猜你喜欢

转载自blog.csdn.net/java_xuetu/article/details/80300028