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的更多相关文章

  1. Redis JAVA客户端 Jedis常用方法

    Jedis 是 Redis 官方首选的 Java 客户端开发包 (redis的java版本的客户端实现) #MAVEN配置 <dependency> <groupId>redi ...

  2. Redis c/c++, java client连接

    Redis 介绍 redis这个想必大家都了解,关于redis的安装參考这里,redis使用文档參见这里,英文文档. Redis Cclient的用法 Redis的cclient Hiredis使用比 ...

  3. 使用Redis的Java客户端Jedis

    转载自:http://aofengblog.blog.163.com/blog/static/631702120147298317919/ 前一篇文章<Redis命令指南>讲解了通过命令行 ...

  4. [转载] 使用Redis的Java客户端Jedis

    转载自http://aofengblog.blog.163.com/blog/static/631702120147298317919/ 在实际的项目开发中,各种语言是使用Redis的客户端库来与Re ...

  5. 3、redis之java client环境搭建

    JAVA Client环境搭建 POM: <dependency> <groupId>redis.clients</groupId> <artifactId& ...

  6. 【转载】Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式…)介绍

    转载地址:http://blog.csdn.net/truong/article/details/46711045 关键字:Redis的Java客户端Jedis的八种调用方式(事务.管道.分布式…)介 ...

  7. Java中Jedis连接Linux上的Redis出现connect time out(解决方案)

    我的代码: /** * * <p>Title: testJedis</p> * <p>Description: 测试单机版的redis连接(每连接一次构建一个对象) ...

  8. Java 使用Jedis连接Redis数据库(-)

    redis 安装: Linux 安装redis 1)下载jar包: 使用Jedis需要以下两个jar包: jedis-2.8.0.jar commons-pool2-2.4.2.jar 2)测试red ...

  9. redis安装 phpredis Jedis 扩展的实现及注意事项,php,java,python相关插件安装实例代码和文档推荐

    redis安装 phpredis Jedis 扩展的实现及注意事项,php,java,python相关插件安装实例代码和文档推荐 1.Redis 官方网站下载: http://redis.io/dow ...

随机推荐

  1. VC中使用ADO操作数据库的方法

    源地址:http://blog.csdn.net/xiaobai1593/article/details/7459862 准备工作: (1).引入ADO类 #import "c:\progr ...

  2. Python基础杂点

    Black Hat Python Python Programming for Hackers and Pentesters by  Justin Seitz December 2014, 192 p ...

  3. Delphi 动态数组合并

    TIntArray = array of Integer; function MergeArray(const ArrayA, ArrayB: TIntArray): TIntArray; var i ...

  4. 常用类一一基本数据类型的包装类(WrapperClass)一一Byte Short nteger Long Float Double Character Boolean

    为什么需要包装类? JAVA是支持跨平台的.可以在服务器 也可以在手机上运行 基本数据类型 在栈中  效率更高 包装类 将数据类型转换成对象 在 堆中  利于操作 package cn.bjsxt.w ...

  5. MongoDB 分片副本集集群搭建

    配置准备 三台机器: A(193.168.10.101) B(193.168.10.102) C(193.168.10.103) MongoDB 安装目录:/usr/local/mongodb Mon ...

  6. window环境mysql解压版配置

    1.下载并解压 到官网下载mysql-5.5.10-win32.zip,然后将mysql解压到任意路径,如:C:\mysql-5.5.10-win32 2.设置环境变量 打开计算机->属性-&g ...

  7. 浅谈QT打印功能实现

    QT作为一款轻量级的集成开发环境,其设计的目标是使开发人员利用QT这个应用程序框架更加快速及轻易的开发应用程序.要达到此目的,要求QT必须能够跨平台,QT能够在32位及64位的Linux,MAC OS ...

  8. 【校招面试 之 剑指offer】第10-1题 斐波那契数列

    递归以及非递归实现: #include<iostream> using namespace std; long long fun(long long n){ if(n == 0){ ret ...

  9. vsftp上传文件出现553 Could not create file

    没有权限创建文件或是目录,原因是selinux引起的登陆问题. 通过如下命令查看状态: > sestatus -b|grep ftp 设置allow_ftpd_full_access为on. 在 ...

  10. 用python做数值计算

    http://sebug.net/paper/books/scipydoc/scipy_intro.html http://www.cnblogs.com/weilq/p/3432817.html h ...