Redis学习笔记(4)—— Jedis入门
一、Jedis介绍
Redis不仅是使用命令来操作,现在基本上主流的语言都有客户端支持,比如Java、C、C#、C++、php、Node、js、Go等。
在官方网站里列的一些Java客户端,有jedis、Redisson、Jredis等,其中官方推荐使用jedis,在企业中用的最多的就是Jedis。
二、Java连接Redis
2.1 导入jar包
- commons-pool2-2.3.jar
- jedis-2.7.0.jar
2.2 单实例连接
@Test public void testJedisSingle() throws Exception { // 1.设置ip地址和端口号 Jedis jedis = new Jedis("192.168.25.129", 6379); // 2.设置数据 jedis.set("name", "zhangsan"); // 3.获得数据 String name = jedis.get("name"); System.out.println(name); // 4.释放资源 jedis.close(); }
注:如果执行上面代码时抛出如下异常
redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeOutException: connect timed out
必须关闭Linux防火墙
service iptables stop
2.3 连接池连接
@Test public void testJedisPool() throws Exception { // 1.获得连接池配置对象,设置配置项 JedisPoolConfig config = new JedisPoolConfig(); // 1.1 最大连接数 config.setMaxTotal(30); // 1.2 最大空闲连接数 config.setMaxIdle(10); // 2.获得连接池 JedisPool jedisPool = new JedisPool(config, "192.168.25.129", 6379); // 3.获得核心对象 Jedis jedis = null; try { jedis = jedisPool.getResource(); // 4.设置数据 jedis.set("name", "lisi"); // 5.获得数据 String name = jedis.get("name"); System.out.println(name); } catch (Exception e) { e.printStackTrace(); } finally { if (jedis != null) { jedis.close(); } // 虚拟机关闭时,释放pool资源 if (jedisPool != null) { jedisPool.close(); } } }
2.4 集群连接
@Test public void testJedisCluster() throws Exception { // 第一步:使用JedisCluster对象。需要一个Set<HostAndPort>参数。Redis节点的列表。 Set<HostAndPort> nodes = new HashSet<>(); nodes.add(new HostAndPort("192.168.25.129", 7001)); nodes.add(new HostAndPort("192.168.25.129", 7002)); nodes.add(new HostAndPort("192.168.25.129", 7003)); nodes.add(new HostAndPort("192.168.25.129", 7004)); nodes.add(new HostAndPort("192.168.25.129", 7005)); nodes.add(new HostAndPort("192.168.25.129", 7006)); // 第二步:直接使用JedisCluster对象操作redis。在系统中单例存在。 JedisCluster jedisCluster = new JedisCluster(nodes); jedisCluster.set("name", "zhangsan"); String name = jedisCluster.get("name"); // 第三步:打印结果 System.out.println(name); // 第四步:系统关闭前,关闭JedisCluster对象。 jedisCluster.close(); }
三、接口封装
常用的操作redis的方法提取出一个接口,分别对应单机版和集群版创建两个实现类。redis的操作在项目中应用较广泛,可将接口和实现类放在commons的工具类中。
3.1 接口定义
public interface JedisClient { String set(String key, String value); String get(String key); Boolean exists(String key); Long expire(String key, int seconds); Long ttl(String key); Long incr(String key); Long hset(String key, String field, String value); String hget(String key, String field); Long hdel(String key, String... field); }
3.2 单机版实现类
public class JedisClientPool implements JedisClient { private JedisPool jedisPool; public JedisPool getJedisPool() { return jedisPool; } public void setJedisPool(JedisPool jedisPool) { this.jedisPool = jedisPool; } @Override public String set(String key, String value) { Jedis jedis = jedisPool.getResource(); String result = jedis.set(key, value); jedis.close(); return result; } @Override public String get(String key) { Jedis jedis = jedisPool.getResource(); String result = jedis.get(key); jedis.close(); return result; } @Override public Boolean exists(String key) { Jedis jedis = jedisPool.getResource(); Boolean result = jedis.exists(key); jedis.close(); return result; } @Override public Long expire(String key, int seconds) { Jedis jedis = jedisPool.getResource(); Long result = jedis.expire(key, seconds); jedis.close(); return result; } @Override public Long ttl(String key) { Jedis jedis = jedisPool.getResource(); Long result = jedis.ttl(key); jedis.close(); return result; } @Override public Long incr(String key) { Jedis jedis = jedisPool.getResource(); Long result = jedis.incr(key); jedis.close(); return result; } @Override public Long hset(String key, String field, String value) { Jedis jedis = jedisPool.getResource(); Long result = jedis.hset(key, field, value); jedis.close(); return result; } @Override public String hget(String key, String field) { Jedis jedis = jedisPool.getResource(); String result = jedis.hget(key, field); jedis.close(); return result; } @Override public Long hdel(String key, String... field) { Jedis jedis = jedisPool.getResource(); Long result = jedis.hdel(key, field); jedis.close(); return result; } }
配置文件applicationContext-redis.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"> <!-- 配置redis单机版连接 --> <bean id="jedisClientPool" class="cn.e3mall.common.jedis.JedisClientPool"> <property name="jedisPool" ref="jedisPool"/> </bean> <bean id="jedisPool" class="redis.clients.jedis.JedisPool"> <constructor-arg name="host" value="192.168.25.129"/> <constructor-arg name="port" value="6379"/> </bean> </beans>
3.3 集群版实现类
public class JedisClientCluster implements JedisClient { private JedisCluster jedisCluster; public JedisCluster getJedisCluster() { return jedisCluster; } public void setJedisCluster(JedisCluster jedisCluster) { this.jedisCluster = jedisCluster; } @Override public String set(String key, String value) { return jedisCluster.set(key, value); } @Override public String get(String key) { return jedisCluster.get(key); } @Override public Boolean exists(String key) { return jedisCluster.exists(key); } @Override public Long expire(String key, int seconds) { return jedisCluster.expire(key, seconds); } @Override public Long ttl(String key) { return jedisCluster.ttl(key); } @Override public Long incr(String key) { return jedisCluster.incr(key); } @Override public Long hset(String key, String field, String value) { return jedisCluster.hset(key, field, value); } @Override public String hget(String key, String field) { return jedisCluster.hget(key, field); } @Override public Long hdel(String key, String... field) { return jedisCluster.hdel(key, field); } }
spring的配置:
<!-- 配置redis集群版的连接 --> <bean id="jedisClientCluster" class="cn.e3mall.common.jedis.JedisClientCluster"> <property name="jedisCluster" ref="jedisCluster"></property> </bean> <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster"> <constructor-arg> <set> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.129"></constructor-arg> <constructor-arg name="port" value="7001"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.129"></constructor-arg> <constructor-arg name="port" value="7002"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.129"></constructor-arg> <constructor-arg name="port" value="7003"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.129"></constructor-arg> <constructor-arg name="port" value="7004"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.129"></constructor-arg> <constructor-arg name="port" value="7005"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.129"></constructor-arg> <constructor-arg name="port" value="7006"></constructor-arg> </bean> </set> </constructor-arg> </bean>
注意:单机版和集群版不能共存,使用单机版时注释集群版的配置。使用集群版,把单机版注释。
3.4 封装代码测试
@Test public void testJedisClient() throws Exception { //初始化Spring容器 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-redis.xml"); //从容器中获得JedisClient对象 JedisClient jedisClient = applicationContext.getBean(JedisClient.class); jedisClient.set("first", "100"); String result = jedisClient.get("first"); System.out.println(result); }