您的位置:首页 > 数据库 > Redis

java操作redis

2018-11-08 11:17 78 查看

Jedis客户端

是redis提供的Java访问redis的客户端jar包.
是一个java编写的类驱动技术.

单机版访问

在这里插入代码片package com.bjsxt.test.jedis;

import org.junit.Test;

import redis.clients.jedis.Jedis;

/**
* 访问单机版
*/
public class TestSingle {

@Test
public void testJedisSingle() throws Exception {
// 1. 创建连接, 通过IP+Port访问服务器
Jedis jedis = new Jedis("192.168.120.129", 6379);

// 2. 数据新增, 所有的数据访问方法都是和对应的命令完全一致.
// 如: 新增 命令 set,  新增数据方法为 set方法
// 所有方法的返回值,对应redis命令中的返回信息.
// 如: set命令成功返回ok, set方法返回字符串
jedis.set("age", "20");
jedis.set("gender", "男");

// 3. 查询数据
String name = jedis.get("name");

System.out.println("name : " + name);

// 3. 回收资源, 退出
jedis.quit();

// jedis.close(); 关闭客户端连接
}

集群版访问

package com.bjsxt.test.jedis;

import java.util.HashSet;
import java.util.Set;

import org.junit.Test;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

/**
* 访问集群版
*/
public class TestJedisCluster {

@Test
public void testCluster() throws Exception{

Set<HostAndPort> nodes = new HashSet<>();
nodes.add(new HostAndPort("192.168.120.129", 7001));
nodes.add(new HostAndPort("192.168.120.129", 7002));
nodes.add(new HostAndPort("192.168.120.129", 7003));
nodes.add(new HostAndPort("192.168.120.129", 7004));
nodes.add(new HostAndPort("192.168.120.129", 7005));
nodes.add(new HostAndPort("192.168.120.129", 7006));

// 1. 创建连接, 需要一个Set<HostAndPort>, 提供集群中所有的节点IP+Port.
JedisCluster cluster = new JedisCluster(nodes);

// 2. 访问, 命令即方法
cluster.set("testCluster", "testCluster");

System.out.println(cluster.get("a"));

// 3. 回收资源
cluster.close();
}

Spring容器管理Jedis访问

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<!-- 配置jedis连接池配置信息. -->
<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>
<!-- jedis整合spring单机版连接池 -->
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg name="host" value="192.168.120.129"/>
<constructor-arg name="port" value="6379"/>
<constructor-arg name="poolConfig" ref="jedisPoolConfig"/>
</bean>

<!-- jedisCluster -->
<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
<constructor-arg name="poolConfig" ref="jedisPoolConfig"/>
<constructor-arg name="nodes">
<set>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.120.129"/>
<constructor-arg name="port" value="7001"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.120.129"/>
<constructor-arg name="port" value="7002"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.120.129"/>
<constructor-arg name="port" value="7003"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.120.129"/>
<constructor-arg name="port" value="7004"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.120.129"/>
<constructor-arg name="port" value="7005"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.120.129"/>
<constructor-arg name="port" value="7006"/>
</bean>
</set>
</constructor-arg>
</bean>

<!-- 配置Jedis对象生命周期 -->
<!-- <bean id="jedis" scope="prototype" class="redis.clients.jedis.Jedis">
<constructor-arg name="host" value="192.168.120.129"/>
<constructor-arg name="port" value="6379"/>
</bean> -->

<!-- 配置Cluster -->
<!-- <bean id="cluster" scope="prototype" class="redis.clients.jedis.JedisCluster">
<constructor-arg name="nodes">
<set>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.120.129"/>
<constructor-arg name="port" value="7001"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.120.129"/>
<constructor-arg name="port" value="7002"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.120.129"/>
<constructor-arg name="port" value="7003"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.120.129"/>
<constructor-arg name="port" value="7004"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.120.129"/>
<constructor-arg name="port" value="7005"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.120.129"/>
<constructor-arg name="port" value="7006"/>
</bean>
</set>
</constructor-arg>
</bean> -->

</beans>

测试代码

package com.bjsxt.test.jedis;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;

public class TestSpring {

// 集群版连接池
@Test
public void testClusterPool(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext-jedis.xml");

JedisCluster cluster = (JedisCluster) context.getBean("jedisCluster");

System.out.println(cluster.get("testCluster"));

// 不能关闭客户端对象, 因为cluster对象默认支持连接池.
// cluster.close();
}

// 集群版
@Test
public void testCluster(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext-jedis.xml");

JedisCluster cluster = (JedisCluster) context.getBean("cluster");

System.out.println(cluster.get("testCluster"));

cluster.close();
}

// 单机版连接池
@Test
public void testSinglePool(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext-jedis.xml");

JedisPool pool = (JedisPool) context.getBean("jedisPool");

Jedis jedis = pool.getResource();

System.out.println(jedis.get("name"));

jedis.close();
}

// 单机版
@Test
public void testSingle(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext-jedis.xml");

Jedis jedis = (Jedis) context.getBean("jedis");

System.out.println(jedis.get("name"));

jedis.close();
}

}
阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: