java连接redis

  (1)首先引入jar包

      <dependency>
        <groupId>commons-pool</groupId>
        <artifactId>commons-pool</artifactId>
        <version>1.6</version>
      </dependency>
      <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.0</version>
      </dependency>

  (2)由于我的redis是安装在虚拟机上的linux,所以必须关闭防火墙(或者指定开放端口),不然java程序连接服务器的返回连接被拒绝。

      Redhat使用了SELinux来增强安全,关闭的办法为:

      1. 永久有效
      修改 /etc/selinux/config 文件中的 SELINUX="" 为 disabled ,然后重启。
      2. 即时生效
      setenforce 0
      关闭防火墙的方法为:
      1. 永久性生效
      开启:chkconfig iptables on 
      关闭:chkconfig iptables off
      2. 即时生效,重启后失效
      开启:service iptables start
      关闭:service iptables stop 
  (3)java代码如下:
      
原文地址:https://www.cnblogs.com/gdpuzxs/p/6624101.html