02_Jedis的介绍和使用

redis主题

01_Redis介绍和安装运行
02_Jedis的介绍和使用
03_Redis数据类型和数据操作的命令
04_Redis集群

Jedis

Jedis介绍

Redis不仅是使用命令来操作,现在基本上主流的语言都有客户端支持,比如java、C、C#、C++、php、Node.js、Go等。
  在官方网站里列一些Java的客户端,有Jedis、Redisson、Jredis、JDBC-Redis、等其中官方推荐使用Jedis和Redisson。 在企业中用的最多的就是Jedis,下面我们就重点学习下Jedis。
Jedis同样也是托管在github上,github地址

Jedis使用

  • 导包
    commons-pool2-2.3.jar jedis-2.7.0.jar junit-4.9.jar

  • 如果是maven工程
    pom坐标:

<dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.7.0</version>
</dependency>
  • 单实例连接
    通过创建单实例jedis对象连接redis服务,如下代码:
    @Test
    public void jedis01() throws Exception{
        //创建和redis的连接
        Jedis jedis = new Jedis("192.168.93.88", 6379);
        
        //存入
        jedis.set("key2", "2");
        //取出
        System.out.println(jedis.get("key2"));
        //关闭
        jedis.close();
    }
  • 如果连接失败,可能的原因:

  • 没有注释redis.conf中的bind 127.0.0

  • 没有取消保护模式,在redis.conf中将protected-mode yes改为protected-mode no

  • 没有开放redis的端口6379(默认端口),在linux中/sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT

  • 使用连接池连接


    @Test
    public void jedis02()
    {
        //创建连接池
        JedisPool pool = new JedisPool("你的ip", 6379);
        //获取连接
        Jedis jedis = pool.getResource();
        //存入
        jedis.set("str2","abcd");
        //取出
        String val = jedis.get("str2");
        System.out.println(val);
        
        //使用连接时,连接使用完后一定要关闭,关闭后连接会自动回到连接池供别人使用,如果一直不关闭则连接被耗尽之后就会死机
        jedis.close();
        pool.close();
    }
  • jedis与spring整合
  • 导入spring的jar包
    commons-logging-1.1.1.jar jstl-1.2.jar spring-aop-3.2.0.RELEASE.jar spring-aspects-3.2.0.RELEASE.jar spring-beans-3.2.0.RELEASE.jar spring-context-3.2.0.RELEASE.jar spring-context-support-3.2.0.RELEASE.jar spring-core-3.2.0.RELEASE.jar spring-expression-3.2.0.RELEASE.jar spring-jdbc-3.2.0.RELEASE.jar spring-orm-3.2.0.RELEASE.jar spring-test-3.2.0.RELEASE.jar spring-tx-3.2.0.RELEASE.jar spring-web-3.2.0.RELEASE.jar spring-webmvc-3.2.0.RELEASE.jar
  • 新建一个source folder,命名为config
  • 在config中创建spring配置文件ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
    <!-- 连接池配置 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- 最大连接数 -->
        <property name="maxTotal" value="30" />
        <!-- 最大空闲连接数 -->
        <property name="maxIdle" value="10" />
        <!-- 每次释放连接的最大数目 -->
        <property name="numTestsPerEvictionRun" value="1024" />
        <!-- 释放连接的扫描间隔(毫秒) -->
        <property name="timeBetweenEvictionRunsMillis" value="30000" />
        <!-- 连接最小空闲时间 -->
        <property name="minEvictableIdleTimeMillis" value="1800000" />
        <!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
        <property name="softMinEvictableIdleTimeMillis" value="10000" />
        <!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
        <property name="maxWaitMillis" value="1500" />
        <!-- 在获取连接的时候检查有效性, 默认false -->
        <property name="testOnBorrow" value="true" />
        <!-- 在空闲时检查有效性, 默认false -->
        <property name="testWhileIdle" value="true" />
        <!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
        <property name="blockWhenExhausted" value="false" />
    </bean>
    <!-- redis单机 通过连接池 -->
    <bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="close">
        <constructor-arg name="poolConfig" ref="jedisPoolConfig"/>
        <constructor-arg name="host" value="你的ip"/>
        <constructor-arg name="port" value="6379"/>
    </bean>
</beans>
  • 编写测试类JedisSpringTest.java
package cn.huachao.jedis;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
public class JedisSpringTest {
    private ApplicationContext applicationContext;
    @Before
    public void init()
    {
        String configLocation = "classpath:ApplicationContext.xml";
        applicationContext = new ClassPathXmlApplicationContext(configLocation);
    }
    @Test
    public void Run1()
    {
        //获取连接池
        JedisPool pool = (JedisPool) applicationContext.getBean("jedisPool");
        //获取连接
        Jedis jedis = pool.getResource();
        //存入
        jedis.set("str1", "jiushini");
        //取出
        System.out.println(jedis.get("str1"));
        //执行完成后,spring会帮我们关闭jedis
    }
}

连接redis碰到的各种问题
http://blog.csdn.net/yingxiake/article/details/51472810

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容