Redis java client ==> Jedis
https://github.com/xetorthio/jedis
Jedis is a blazingly small and sane Redis java client.
Jedis was conceived to be EASY to use.
Jedis is fully compatible with redis 2.8.x and 3.0.x.
小、功能健全、简单易用、全面兼容
1. 单客户端
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.2</version>
</dependency>
Jedis jedis = new Jedis("localhost");
jedis.set("foo", "bar");
String value = jedis.get("foo");
2. 集群
Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
//Jedis Cluster will attempt to discover cluster nodes automatically
jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7379));
JedisCluster jc = new JedisCluster(jedisClusterNodes);
jc.set("foo", "bar");
String value = jc.get("foo");
Spring 集成
简单的
pom.xml
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
</dependencies>
beans.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: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/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <context:annotation-config />
<beans>
<bean id="jedis" class="redis.clients.jedis.Jedis">
<constructor-arg value="172.16.162.248"></constructor-arg>
</bean>
</beans> </beans>
@Autowired
private Jedis jedis;
Jedis Cluster
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.zno</groupId>
<artifactId>redisCluster</artifactId>
<version>0.0.1-SNAPSHOT</version> <dependencies> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency> <dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.2</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.3.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
beans-redis.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: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/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <context:annotation-config /> <bean class="redis.clients.jedis.JedisCluster">
<constructor-arg>
<set>
<ref bean="node1" />
<ref bean="node2" />
<ref bean="node3" />
<ref bean="node4" />
<ref bean="node5" />
<ref bean="node6" />
<ref bean="node7" />
<ref bean="node8" />
<ref bean="node9" />
</set>
</constructor-arg> </bean> <bean id="node1" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="172.16.20.46"></constructor-arg>
<constructor-arg name="port" value="7001"></constructor-arg>
</bean>
<bean id="node2" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="172.16.20.46"></constructor-arg>
<constructor-arg name="port" value="7002"></constructor-arg>
</bean>
<bean id="node3" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="172.16.20.46"></constructor-arg>
<constructor-arg name="port" value="7003"></constructor-arg>
</bean>
<bean id="node4" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="172.16.20.46"></constructor-arg>
<constructor-arg name="port" value="7004"></constructor-arg>
</bean>
<bean id="node5" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="172.16.20.46"></constructor-arg>
<constructor-arg name="port" value="7005"></constructor-arg>
</bean>
<bean id="node6" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="172.16.20.46"></constructor-arg>
<constructor-arg name="port" value="7006"></constructor-arg>
</bean>
<bean id="node7" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="172.16.20.46"></constructor-arg>
<constructor-arg name="port" value="7007"></constructor-arg>
</bean>
<bean id="node8" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="172.16.20.46"></constructor-arg>
<constructor-arg name="port" value="7008"></constructor-arg>
</bean>
<bean id="node9" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="172.16.20.46"></constructor-arg>
<constructor-arg name="port" value="7009"></constructor-arg>
</bean> </beans>
TestRedisCluster.java
package redisCluster; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import redis.clients.jedis.JedisCluster; @ContextConfiguration(locations = {"classpath:beans-redis.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class TestRedisCluster { @Autowired
private JedisCluster jedisCluster; @Test
public void tt(){
String key = "testkey";
jedisCluster.set(key, "1");
System.out.println(jedisCluster.get(key));
jedisCluster.del(key);
System.out.println(jedisCluster.get(key)); }
}
打印结果:
九月 , :: 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-redis.xml]
九月 , :: 下午 org.springframework.context.support.GenericApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@1a705d7: startup date [Wed Sep :: CST ]; root of context hierarchy null
Redis java client ==> Jedis的更多相关文章
- Redis JAVA客户端 Jedis常用方法
Jedis 是 Redis 官方首选的 Java 客户端开发包 (redis的java版本的客户端实现) #MAVEN配置 <dependency> <groupId>redi ...
- Redis c/c++, java client连接
Redis 介绍 redis这个想必大家都了解,关于redis的安装參考这里,redis使用文档參见这里,英文文档. Redis Cclient的用法 Redis的cclient Hiredis使用比 ...
- 使用Redis的Java客户端Jedis
转载自:http://aofengblog.blog.163.com/blog/static/631702120147298317919/ 前一篇文章<Redis命令指南>讲解了通过命令行 ...
- [转载] 使用Redis的Java客户端Jedis
转载自http://aofengblog.blog.163.com/blog/static/631702120147298317919/ 在实际的项目开发中,各种语言是使用Redis的客户端库来与Re ...
- 3、redis之java client环境搭建
JAVA Client环境搭建 POM: <dependency> <groupId>redis.clients</groupId> <artifactId& ...
- 【转载】Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式…)介绍
转载地址:http://blog.csdn.net/truong/article/details/46711045 关键字:Redis的Java客户端Jedis的八种调用方式(事务.管道.分布式…)介 ...
- Java中Jedis连接Linux上的Redis出现connect time out(解决方案)
我的代码: /** * * <p>Title: testJedis</p> * <p>Description: 测试单机版的redis连接(每连接一次构建一个对象) ...
- Java 使用Jedis连接Redis数据库(-)
redis 安装: Linux 安装redis 1)下载jar包: 使用Jedis需要以下两个jar包: jedis-2.8.0.jar commons-pool2-2.4.2.jar 2)测试red ...
- redis安装 phpredis Jedis 扩展的实现及注意事项,php,java,python相关插件安装实例代码和文档推荐
redis安装 phpredis Jedis 扩展的实现及注意事项,php,java,python相关插件安装实例代码和文档推荐 1.Redis 官方网站下载: http://redis.io/dow ...
随机推荐
- 更改linux swappiness 提高物理内存使用率
swappiness的值的大小对如何使用swap分区是有着很大的联系的. swappiness=0的时候表示最大限度使用物理内存,然后才是 swap空间,swappiness=100的时候表示积极的使 ...
- How to Pronounce the I in ING
How to Pronounce the I in ING Share Tweet Share Tagged With: ING Verbs The I in ING is the IH as in ...
- Quartz+TopShelf实现Windows服务作业调度
Quartz:首先我贴出来了两段代码(下方),可以看出,首先会根据配置文件(quartz.config),包装出一个Quartz.Core.QuartzScheduler instance,这是一个调 ...
- pyorient
简介 pyorient是orientdb的python库 该库提供两种访问orientdb的方式:1.client 的方式 2.ogm 的方式(类似于ORM) 由于OGM 封装了client,且由于O ...
- pyplot图像组件
pyplot图像组件 ax子对象的组件内容 Title 图表标题 plt.title() Axis 坐标范围,x轴,y轴 plt.axis() label 坐标轴标注 plt.xlabel() plt ...
- web中的中文乱码处理
1.页面设置pageEncoding="UTF-8" <%@ page contentType="text/html;charset=UTF-8" lan ...
- Java GC的原理
Java GC(garbage collec,垃圾收集,回收) GC是对JVM中的内存进行标记和回收,Sun公司的JDK用的虚拟机都是HotSpot 对象化的实例是放在heap堆内存中的,这里讲的分代 ...
- hibernate联合主键
@Entity @Table(name = "TABLE_NAME") @IdClass(PK.class) public class TableName implements S ...
- GridView控件中的一些常见问题
1. 无法获取模板列中的值,使用FindControl()方法无效: 给模板列中添加隐藏域,并给隐藏域绑定要获取的值,代码如下: <asp:HiddenField ID="hfIsFr ...
- 给乱序的链表排序 · Sort List, 链表重排reorder list LoLn...
链表排序 · Sort List [抄题]: [思维问题]: [一句话思路]: [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: quick ...