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 ...
随机推荐
- .net数据库连接防注入参数查询 命令执行 读取 备份 导出导入转化XML格式
ADO.NET是一组类库,让我们通过程序的方式访问数据库.SYSTEM.DATA这个类提供了统一的接口访问Oracle MSSQL Access.像SYSTEM.IO类操作文件一样. **connec ...
- 前端编程工具WebStorm 10 工具的快捷使用方式
1.如果是一个空白的文档,要想快速生成HTML的基本结构,可以写一个! 然后按一下tab键,如果是写的一个标签的名字,则会生成基本的标签结构. 2.h1{}:{}中写要显示的文本 3.h1[]:[]中 ...
- ndarray
ndarray ndarray-多维数组对象 ndarray数组分两部分 实际数据 描述数据的元数据(数据类型/维度等) ndarray所有元素类型相同,下标从0开始 import numpy as ...
- stl-stack+括号配对问题
栈:stl的一种容器,遵循先进后出原则,,只能在栈的顶部操作,就像放盘子一样,洗好的盘子叠在上面,需要用时也是先从顶部拿.不允许被遍历,没有迭代器 基本操作: 1.头文件#include<sta ...
- dns server 配置
# cat /etc/named.conf//// named.conf//// Provided by Red Hat bind package to configure the ISC BIND ...
- FastDFSClient工具类 文件上传下载
package cn.itcast.fastdfs.cliennt; import org.csource.common.NameValuePair; import org.csource.fastd ...
- Windows下搭建appium(Android版)
1.安装node.js 说明:安装node.js是为了可以使用它的npm,可以用npm install很方便的安装它包含的包,appium server使用node.js编写的 下载地址:https: ...
- C语言实现大数四则运算
一.简介 众所周知,C语言中INT类型是有限制,不能进行超过其范围的运算,而如果采用float类型进行运算,由于float在内存中特殊的存储形式,又失去了计算的进度.要解决整个问题,一种解决方法是通过 ...
- ubuntu的文本界面修改字体大小
使用命令: dpkg-reconfigure console-setup
- ROI
1.ROI简介 ROI(region of interest),感兴趣区域.机器视觉.图像处理中,从被处理的图像以方框.圆.椭圆. 不规则多边形等方式勾勒出需要处理的区域,称为感兴趣区域,ROI.在H ...