java连接redis
java 连接 redis
简单连接
1、创建 Java Project 项目
- 添加 JUnit 测试环境:项目名 -- 右键 --Build Path--Add Library--JUnit--Next--Finish
- 在项目名下添加 lib 文件夹
2、添加 jar 包
- jedis 的 jar 包:jedis.jar
- jedis 连接池的 jar 包:commons-pool.jar
3、创建测试类 JedisClientTest.java
import org.junit.jupiter.api.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
public class JedisClientTest {
@Test
public void jedisClient() {
// 声明 Jedis 对象,通过该对象连接到 redis 服务器
Jedis jedis = new Jedis("127.0.0.1",6379);
// 设置密码
// jedis.auth("root");
// 通过 jedis 赋值
jedis.set("strTest","2222");
// 通过 redis 取值
String res = jedis.get("strTest");
System.out.println(res);
jedis.close();
}
<span class="hljs-comment">// 使用jedis连接池连接jedis服务器</span>
<span class="hljs-meta">@Test</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">jedisPool</span><span class="hljs-params">()</span> {
<span class="hljs-comment">// 创建Jedis连接池</span>
<span class="hljs-type">JedisPool</span> <span class="hljs-variable">jedisPool</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">JedisPool</span>(<span class="hljs-string">"127.0.0.1"</span>,<span class="hljs-number">6379</span>);
<span class="hljs-comment">// 通过jedis连接池获取jedis对象</span>
<span class="hljs-type">Jedis</span> <span class="hljs-variable">jedis</span> <span class="hljs-operator">=</span> jedisPool.getResource();
<span class="hljs-comment">// 设置值</span>
jedis.set(<span class="hljs-string">"pooltest"</span>,<span class="hljs-string">"sdsff"</span>);
<span class="hljs-comment">// 取值</span>
<span class="hljs-type">String</span> <span class="hljs-variable">res</span> <span class="hljs-operator">=</span> jedis.get(<span class="hljs-string">"pooltest"</span>);
System.out.println(res);
<span class="hljs-comment">// 关闭jedis</span>
jedis.close();
<span class="hljs-comment">// 关闭连接池:注意关闭连接池要在所有jedis都关闭之后载关闭</span>
jedisPool.close();
}
}
Spring 连接 Jedis
1、创建 Java Project 项目
- 添加 JUnit 测试环境:项目名 -- 右键 --Build Path--Add Library--JUnit--Next--Finish
- 在项目名下添加 lib 文件夹
2、添加 jar 包
aopalliance.jar
aspectjweaver.jar
cglib-nodep-2.2.jar
commons-collections-3.1.jar
commons-dbcp-1.2.2.jar
commons-logging.jar
commons-pool.jar
commons-pool2-2.3.jar
fastjson-1.1.28.jar
jackson-core-asl-1.9.7.jar
jackson-mapper-asl-1.9.7.jar
jedis-2.7.0.jar
jstl.jar
spring-aop-3.2.8.RELEASE.jar
spring-aspects-3.2.8.RELEASE.jar
spring-beans-3.2.8.RELEASE.jar
spring-context-3.2.8.RELEASE.jar
spring-core-3.2.8.RELEASE.jar
spring-expression-3.2.8.RELEASE.jar
spring-jdbc-3.2.8.RELEASE.jar
spring-tx-3.2.8.RELEASE.jar
spring-web-3.2.8.RELEASE.jar
spring-webmvc-3.2.8.RELEASE.jar
standard.jar
3、创建 applicationContext.xml 配置文件
-
其中包含以下内容:
- 创建 redis 的连接池,配置其参数
- 通过连接池获得连接池对象
-
具体配置如下:
<?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:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd ">
<span class="hljs-comment"><!-- 创建redis的连接池,配置其参数 --></span>
<span class="hljs-tag"><<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"jedisPoolConfig"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"redis.clients.jedis.JedisPoolConfig"</span>></span>
<span class="hljs-comment"><!-- 最大连接数 --></span>
<span class="hljs-tag"><<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"maxTotal"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"30"</span> /></span>
<span class="hljs-comment"><!-- 最大空闲连接数 --></span>
<span class="hljs-tag"><<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"maxIdle"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"10"</span> /></span>
<span class="hljs-comment"><!-- 每次释放连接的最大数目 --></span>
<span class="hljs-tag"><<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"numTestsPerEvictionRun"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"1024"</span> /></span>
<span class="hljs-comment"><!-- 释放连接的扫描间隔(毫秒) --></span>
<span class="hljs-tag"><<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"timeBetweenEvictionRunsMillis"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"30000"</span> /></span>
<span class="hljs-comment"><!-- 连接最小空闲时间 --></span>
<span class="hljs-tag"><<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"minEvictableIdleTimeMillis"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"1800000"</span> /></span>
<span class="hljs-comment"><!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 --></span>
<span class="hljs-tag"><<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"softMinEvictableIdleTimeMillis"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"10000"</span> /></span>
<span class="hljs-comment"><!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 --></span>
<span class="hljs-tag"><<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"maxWaitMillis"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"1500"</span> /></span>
<span class="hljs-comment"><!-- 在获取连接的时候检查有效性, 默认false --></span>
<span class="hljs-tag"><<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"testOnBorrow"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"false"</span> /></span>
<span class="hljs-comment"><!-- 在空闲时检查有效性, 默认false --></span>
<span class="hljs-tag"><<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"testWhileIdle"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"true"</span> /></span>
<span class="hljs-comment"><!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true --></span>
<span class="hljs-tag"><<span class="hljs-name">property</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"blockWhenExhausted"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"false"</span> /></span>
<span class="hljs-tag"></<span class="hljs-name">bean</span>></span>
<span class="hljs-comment"><!-- 通过连接池获得连接池对象 --></span>
<span class="hljs-tag"><<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"jedisPool"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"redis.clients.jedis.JedisPool"</span> <span class="hljs-attr">destroy-method</span>=<span class="hljs-string">"close"</span>></span>
<span class="hljs-tag"><<span class="hljs-name">constructor-arg</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"poolConfig"</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">"jedisPoolConfig"</span>/></span>
<span class="hljs-tag"><<span class="hljs-name">constructor-arg</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"host"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"127.0.0.1"</span>/></span>
<span class="hljs-tag"><<span class="hljs-name">constructor-arg</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"port"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"6379"</span>/></span>
<span class="hljs-tag"></<span class="hljs-name">bean</span>></span>
</beans>
4、创建测试文件 SpringJedisClientTest.java
- SpringJedisClientTest.java 代码如下:
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
/**
* 使用 spring 配置 redis
* @author Rainy
*/
public class SpringJedisClientTest {
@Test
public void springJedisPool() {
// 加载注配置文件
@SuppressWarnings("resource")
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
// 实例化 jedisPool
JedisPool jedisPool = (JedisPool) ac.getBean("jedisPool");
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.set("jedistest","张三");
String res = jedis.get("jedistest");
System.out.println(res);
}catch (Exception e) {
e.printStackTrace();
}finally {
if(jedis != null) {
jedis.close();
}
}
}
}
- 运行结果如下:
一月 06, 2019 10:48:28 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7ce6a65d: startup date [Sun Jan 06 22:48:28 CST 2019]; root of context hierarchy
一月 06, 2019 10:48:28 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
一月 06, 2019 10:48:28 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@b62fe6d: defining beans [jedisPoolConfig,jedisPool]; root of factory hierarchy
张三