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 ...
随机推荐
- 转载:mysql数据同步redis
from: http://www.cnblogs.com/zhxilin/archive/2016/09/30/5923671.html 在服务端开发过程中,一般会使用MySQL等关系型数据库作为最终 ...
- ubuntu upstart启动流程分析
ubuntu自从6.10版本之后就使用了较新的upstart机制来进行系统的初始化. upstart是一种基于事件驱动的服务启动机制,可以使多个系统任务在保持依赖关系的前提下并发启动(据说这样这样启动 ...
- 将Ctrl+Alt+Delete键进行屏蔽,防止误操作重启服务器
[root@bgw-t ~]# vi /etc/init/control-alt-delete.conf #exec /sbin/shutdown -r now "Control-Alt- ...
- liblas 1.8.1编译安装
liblas https://github.com/libLAS/libLAS/issues/102 https://liblas.org/start.html 源码 https://github.c ...
- Python的isdigit()和isalpha()
提供一个参考链接<isalpha() Method> 使用isdigit()判断是否是全数字: if word.encode( 'UTF-8' ).isdigit() 使用isalpha ...
- CentOS开机卡在进度条,无法正常开机的排查办法
CentOS开机的时候卡在进度条一直进不去 重启,按f5键进度条/命令行界面方式切换,确认卡问题后处理就好 我这边卡在redis服务,设置为开机启动但是一直服务启动不起来 重启按住"e&qu ...
- scala--函数式对象
函数式对象 这次写点关于函数式对象的吧 class Rational(n:Int, d:Int) { // n,d 为类参数,scala会创造出同样带有这两个参数的主构造器.如果这个类没有主体,可以不 ...
- Ubuntu 安装 Elasticsearch
1.安装java 注意:最新版本的elasticsearch(5.6.2)要求安装java8 1.sudo apt-add-repository ppa:webupd8team/java 2.sudo ...
- Scripting API Samples
Scripting API Samples Tomáš Matoušek edited this page on Jan 31 · 32 revisions Home API Changes Bu ...
- 【Java】JavaIO(二)、节点流
一.InputStream & outputStream Java字节流主要是以InputStream (输入流),outputStream(输出流)为基类,本身是抽象类不能创建实例,但是是字 ...