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. (Python)numpy的argmax用法

      解释 还是从一维数组出发.看下面的例子. import numpy as np a = np.array([3, 1, 2, 4, 6, 1]) print(np.argmax(a))4 argm ...

  2. jenkins 修改工作目录

    修改Jenkins路径 Jenkins的默认安装路径是/var/lib/jenkins 现在由于这个根目录的磁盘太小,所以切换到/data 目录下. Jenkins目录.端口.工作目录等信息在/etc ...

  3. EasyMock使用总结

    最重要的事说在前面:遇到一个你不熟悉的知识,一定要去官网仔仔细细的看官方文档!一定要仔仔细细!一定要!(尔康鼻孔脸..) 正篇: 一.使用 首先,当然是添加依赖,有人用maven,有人用ant或者ma ...

  4. Spring的国际化(转载)

    1:在MyEclipse下面创建一个test的Web  Project,然后添加Spring相关的文件,在src根目录下创建applicationContext.xml文件. applicationC ...

  5. 在winsshd 中添加id_rsa.pub 实现Windows 服务器主机自动信任Linux 客户端

    文章一. 生成密钥: 在Linux主机(ssh客户端),通过ssh-keygen在建立SSH keys# ssh-keygen -t rsa (连续三次回车,即在本地生成了公钥和私钥,不设置密码)将在 ...

  6. Django1.8:403错误:CSRF verification failed. Request aborted.

    问题:Django 403错误:CSRF verification failed. Request aborted.     原因:需要加cookie验证 解决方法: 1.在view.py中增加 fr ...

  7. raptor

    raptor - 必应词典 美['ræptər]英['ræptə(r)] n.猛禽:攫禽 网络迅猛龙:雷电威龙:决战侏罗纪

  8. ios runtime简单实用(添加动态属性)

    #import "Person.h" @interface Person (PersonCategory)   // 添加Person中没有的name属性 @property (n ...

  9. 快速排序C++实现

    #include<iostream> using namespace std;class quicksort{ public: int quicks(int *a,int low,int ...

  10. JAVA知识积累 JSP第一篇【JSP介绍、工作原理、生命周期、语法、指令、行为】

    什么是JSP JSP全名为Java Server Pages,java服务器页面.JSP是一种基于文本的程序,其特点就是HTML和Java代码共同存在! 为什么需要JSP JSP是为了简化Servle ...