redis主題
01_Redis介紹和安裝運(yùn)行
02_Jedis的介紹和使用
03_Redis數(shù)據(jù)類型和數(shù)據(jù)操作的命令
04_Redis集群
Jedis
Jedis介紹
Redis不僅是使用命令來操作,現(xiàn)在基本上主流的語言都有客戶端支持,比如java、C、C#、C++、php、Node.js、Go等。
在官方網(wǎng)站里列一些Java的客戶端,有Jedis、Redisson、Jredis、JDBC-Redis、等其中官方推薦使用Jedis和Redisson。 在企業(yè)中用的最多的就是Jedis,下面我們就重點(diǎn)學(xué)習(xí)下Jedis。
Jedis同樣也是托管在github上,github地址;
Jedis使用
導(dǎo)包
commons-pool2-2.3.jar jedis-2.7.0.jar junit-4.9.jar如果是maven工程
pom坐標(biāo):
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.0</version>
</dependency>
- 單實(shí)例連接
通過創(chuàng)建單實(shí)例jedis對(duì)象連接redis服務(wù),如下代碼:
@Test
public void jedis01() throws Exception{
//創(chuàng)建和redis的連接
Jedis jedis = new Jedis("192.168.93.88", 6379);
//存入
jedis.set("key2", "2");
//取出
System.out.println(jedis.get("key2"));
//關(guān)閉
jedis.close();
}
如果連接失敗,可能的原因:
沒有注釋redis.conf中的
bind 127.0.0沒有取消保護(hù)模式,在redis.conf中將
protected-mode yes改為protected-mode no沒有開放redis的端口6379(默認(rèn)端口),在linux中
/sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT使用連接池連接
@Test
public void jedis02()
{
//創(chuàng)建連接池
JedisPool pool = new JedisPool("你的ip", 6379);
//獲取連接
Jedis jedis = pool.getResource();
//存入
jedis.set("str2","abcd");
//取出
String val = jedis.get("str2");
System.out.println(val);
//使用連接時(shí),連接使用完后一定要關(guān)閉,關(guān)閉后連接會(huì)自動(dòng)回到連接池供別人使用,如果一直不關(guān)閉則連接被耗盡之后就會(huì)死機(jī)
jedis.close();
pool.close();
}
- jedis與spring整合
- 導(dǎo)入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 - 新建一個(gè)source folder,命名為config
- 在config中創(chuàng)建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">
<!-- 最大連接數(shù) -->
<property name="maxTotal" value="30" />
<!-- 最大空閑連接數(shù) -->
<property name="maxIdle" value="10" />
<!-- 每次釋放連接的最大數(shù)目 -->
<property name="numTestsPerEvictionRun" value="1024" />
<!-- 釋放連接的掃描間隔(毫秒) -->
<property name="timeBetweenEvictionRunsMillis" value="30000" />
<!-- 連接最小空閑時(shí)間 -->
<property name="minEvictableIdleTimeMillis" value="1800000" />
<!-- 連接空閑多久后釋放, 當(dāng)空閑時(shí)間>該值 且 空閑連接>最大空閑連接數(shù) 時(shí)直接釋放 -->
<property name="softMinEvictableIdleTimeMillis" value="10000" />
<!-- 獲取連接時(shí)的最大等待毫秒數(shù),小于零:阻塞不確定的時(shí)間,默認(rèn)-1 -->
<property name="maxWaitMillis" value="1500" />
<!-- 在獲取連接的時(shí)候檢查有效性, 默認(rèn)false -->
<property name="testOnBorrow" value="true" />
<!-- 在空閑時(shí)檢查有效性, 默認(rèn)false -->
<property name="testWhileIdle" value="true" />
<!-- 連接耗盡時(shí)是否阻塞, false報(bào)異常,ture阻塞直到超時(shí), 默認(rèn)true -->
<property name="blockWhenExhausted" value="false" />
</bean>
<!-- redis單機(jī) 通過連接池 -->
<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"));
//執(zhí)行完成后,spring會(huì)幫我們關(guān)閉jedis
}
}
連接redis碰到的各種問題
http://blog.csdn.net/yingxiake/article/details/51472810