Java操作redis【二十】
1. 首先需要将 redis 的绑定地址为 127.0.0.1 去掉,同时将 redis 的保护模式去掉,并且开放 6379 端口。
如果是保护模式需要输入密码才能连接。
(1) 去掉绑定地址:
(2) 去掉保护模式:
(3)linux 中开放 6379 端口
1 2 | /sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT iptables-save |
2.Java 操作 redis
(1) 导包:
(2) 基本的测试
// 通过 Java 程序访问 redis @Test public void test1() { //1、获得连接对象 Jedis jedis = new Jedis("192.168.43.101", 6379);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2、获得数据</span> String username = jedis.get("str3"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(username); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">3、存储</span> jedis.set("addr", "北京"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.get(</span>"addr"<span style="color: rgba(0, 0, 0, 1)">)); Set</span><String> keys = jedis.keys("*"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (String s:keys){ System.out.println(</span>"key---"+s+" value---"+<span style="color: rgba(0, 0, 0, 1)">jedis.get(s)); } }</span></pre>
(3) 使用 JedisPoolConfig 操作 redis
//通过 jedis 的 pool 获得 jedis 连接对象 @Test public void test2(){ //0、创建池子的配置对象 JedisPoolConfig poolConfig = new JedisPoolConfig(); poolConfig.setMaxIdle(30);//最大闲置个数 poolConfig.setMinIdle(10);//最小闲置个数 poolConfig.setMaxTotal(50);//最大连接数</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1、创建一个redis的连接池</span> JedisPool pool = <span style="color: rgba(0, 0, 255, 1)">new</span> JedisPool(poolConfig, "192.168.43.101", 6379<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">2、从池子中获取redis的连接资源</span> Jedis jedis =<span style="color: rgba(0, 0, 0, 1)"> pool.getResource(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">3、操作数据库</span> jedis.set("xxx","yyyy"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(jedis.get(</span>"xxx"<span style="color: rgba(0, 0, 0, 1)">)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">4、获得数据</span> String username = jedis.get("str3"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(username); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">4、关闭资源</span>
jedis.close();
pool.close();}</span></pre>
3. 封装 redis 连接操作 redis:
(1)src 目录下 redis.properties
redis.maxIdle=30 redis.minIdle=10 redis.maxTotal=100 redis.url=192.168.43.101 redis.port=6379
(2) 封装连接并进行测试:
package RedisTest;import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;public class JedisPoolUtils {
</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> JedisPool pool = <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)">{ </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">加载配置文件</span> InputStream in = JedisPoolUtils.<span style="color: rgba(0, 0, 255, 1)">class</span>.getClassLoader().getResourceAsStream("redis.properties"<span style="color: rgba(0, 0, 0, 1)">); Properties pro </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Properties(); </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { pro.load(in); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IOException e) { e.printStackTrace(); } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获得池子对象</span> JedisPoolConfig poolConfig = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JedisPoolConfig(); poolConfig.setMaxIdle(Integer.parseInt(pro.get(</span>"redis.maxIdle").toString()));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">最大闲置个数</span> poolConfig.setMinIdle(Integer.parseInt(pro.get("redis.minIdle").toString()));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">最小闲置个数</span> poolConfig.setMaxTotal(Integer.parseInt(pro.get("redis.maxTotal").toString()));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">最大连接数</span> pool = <span style="color: rgba(0, 0, 255, 1)">new</span> JedisPool(poolConfig,pro.getProperty("redis.url") , Integer.parseInt(pro.get("redis.port"<span style="color: rgba(0, 0, 0, 1)">).toString())); } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获得jedis资源的方法</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> Jedis getJedis(){ </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> pool.getResource(); } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) { Jedis jedis </span>=<span style="color: rgba(0, 0, 0, 1)"> getJedis(); System.out.println(jedis.get(</span>"str3"<span style="color: rgba(0, 0, 0, 1)">)); }
}
总结: jedis 封装了很多对 redis 操作的方法,方法的名字都是以 redis 中的方法命名的,我们可以直接使用。
例如: