lettuce--Advanced Redis client
redis官方提供的java client:

git地址:https://github.com/mp911de/lettuce
Advanced Redis client for thread-safe sync, async, and reactive usage. Supports Cluster, Sentinel, Pipelining, and codecs.http://redis.paluch.biz
Introduction
Lettuce is a scalable thread-safe Redis client for synchronous, asynchronous and reactive usage. Multiple threads may share one connection if they avoid blocking and transactional operations such as BLPOP and MULTI/EXEC. lettuce is built with netty. Supports advanced Redis features such as Sentinel, Cluster, Pipelining, Auto-Reconnect and Redis data models.
This version of lettuce has been tested against Redis and 3.0.
- lettuce 3.x works with Java 6, 7 and 8, lettuce 4.x requires Java 8
- synchronous, asynchronous and reactive usage
- Redis Sentinel
- Redis Cluster
- SSL and Unix Domain Socket connections
- Streaming API
- CDI and Spring integration
- Codecs (for UTF8/bit/JSON etc. representation of your data)
- multiple Command Interfaces
几个常见的使用方法:
1. 连接单机
package com.lambdaworks.examples; import com.lambdaworks.redis.RedisClient;
import com.lambdaworks.redis.RedisConnection;
import com.lambdaworks.redis.RedisURI; /**
* @author <a href="mailto:mpaluch@paluch.biz">Mark Paluch</a>
* @since 18.06.15 09:17
*/
public class ConnectToRedis { public static void main(String[] args) {
// Syntax: redis://[password@]host[:port][/databaseNumber]
RedisClient redisClient = new RedisClient(RedisURI.create("redis://password@localhost:6379/0"));
RedisConnection<String, String> connection = redisClient.connect(); System.out.println("Connected to Redis"); connection.close();
redisClient.shutdown();
}
}
2. 连接集群
package com.lambdaworks.examples; import com.lambdaworks.redis.RedisURI;
import com.lambdaworks.redis.cluster.RedisAdvancedClusterConnection;
import com.lambdaworks.redis.cluster.RedisClusterClient; /**
* @author <a href="mailto:mpaluch@paluch.biz">Mark Paluch</a>
* @since 18.06.15 09:17
*/
public class ConnectToRedisCluster { public static void main(String[] args) {
// Syntax: redis://[password@]host[:port]
RedisClusterClient redisClient = new RedisClusterClient(RedisURI.create("redis://password@localhost:7379"));
RedisAdvancedClusterConnection<String, String> connection = redisClient.connectCluster(); System.out.println("Connected to Redis"); connection.close();
redisClient.shutdown();
}
}
3. 连接sentinel
package com.lambdaworks.examples; import com.lambdaworks.redis.*; /**
* @author <a href="mailto:mpaluch@paluch.biz">Mark Paluch</a>
* @since 18.06.15 09:17
*/
public class ConnectToRedisUsingRedisSentinel { public static void main(String[] args) {
// Syntax: redis-sentinel://[password@]host[:port][,host2[:port2]][/databaseNumber]#sentinelMasterId
RedisClient redisClient = new RedisClient(
RedisURI.create("redis-sentinel://localhost:26379,localhost:26380/0#mymaster"));
RedisConnection<String, String> connection = redisClient.connect(); System.out.println("Connected to Redis using Redis Sentinel"); connection.close();
redisClient.shutdown();
}
}
4.安全的连接
package com.lambdaworks.examples; import com.lambdaworks.redis.*; /**
* @author <a href="mailto:mpaluch@paluch.biz">Mark Paluch</a>
* @since 18.06.15 09:17
*/
public class ConnectToRedisSSL { public static void main(String[] args) {
// Syntax: rediss://[password@]host[:port][/databaseNumber]
// Adopt the port to the stunnel port in front of your Redis instance
RedisClient redisClient = new RedisClient(RedisURI.create("rediss://password@localhost:6443/0"));
RedisConnection<String, String> connection = redisClient.connect(); System.out.println("Connected to Redis using SSL"); connection.close();
redisClient.shutdown();
}
}
5. spring集成
package com.lambdaworks.examples; import com.lambdaworks.redis.*;
import org.springframework.beans.factory.annotation.Autowired; /**
* @author <a href="mailto:mpaluch@paluch.biz">Mark Paluch</a>
* @since 18.06.15 09:31
*/
public class MySpringBean { private RedisClient redisClient; @Autowired
public void setRedisClient(RedisClient redisClient) {
this.redisClient = redisClient;
} public String ping() { RedisConnection<String, String> connection = redisClient.connect();
String result = connection.ping();
connection.close();
return result;
}
}
使用代码如下:
package com.lambdaworks.examples; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.lambdaworks.redis.RedisClient;
import com.lambdaworks.redis.RedisConnection; /**
* @author <a href="mailto:mpaluch@paluch.biz">Mark Paluch</a>
* @since 18.06.15 09:17
*/
public class SpringExample { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"com/lambdaworks/examples/SpringTest-context.xml"); RedisClient client = context.getBean(RedisClient.class); RedisConnection<String, String> connection = client.connect();
System.out.println("PING: " + connection.ping());
connection.close(); MySpringBean mySpringBean = context.getBean(MySpringBean.class);
System.out.println("PING: " + mySpringBean.ping()); context.close();
} }
参考文献:
【1】https://github.com/mp911de/lettuce
lettuce--Advanced Redis client的更多相关文章
- HAProxy advanced Redis health check---ref
http://blog.exceliance.fr/2014/01/02/haproxy-advanced-redis-health-check/ HAProxy advanced Redis hea ...
- 深入浅出 Redis client/server交互流程
综述 最近笔者阅读并研究redis源码,在redis客户端与服务器端交互这个内容点上,需要参考网上一些文章,但是遗憾的是发现大部分文章都断断续续的非系统性的,不能给读者此交互流程的整体把握.所以这里我 ...
- Redis 详解 (一) StackExchange.Redis Client
这期我们来看StackExchange.Redis,这是redis 的.net客户端之一.Redis是一个开源的内存数据存储,可以用来做数据库,缓存或者消息代理服务.目前有不少人在使用ServiceS ...
- Advanced REST client
好用的测试工具,老是忘记名字chrome插件 Advanced REST client
- Advanced REST client的使用说明
1. 为什么要使用REST Client 在实际企业开发过程中经常会有这样的需求: 1.我当前开发的这个系统是需要调用其他系统的接口,也就是我们需要频繁的测试接口,尝试不同的入参参数去查看返回结果, ...
- 谷歌(Chrome)安装Advanced REST Client插件
进入Extensions(工具——>扩展程序) 点击Get More extensions或新建标签页点击网上应用店 如果加载太慢,出现chrome网上应用店无法打开,显示暂时无法加载该应用的画 ...
- 发送请求工具—Advanced REST Client
Advanced REST Client是Chrome浏览器下的一个插件,通过它能够发送http.https.WebSocket请求.在Chrome商店下搜索Advanced REST Client, ...
- redis client protocol 分解
在官方网站http://redis.io/topics/protocol我们必须redis通信协议做说明. 根据以下某些原因.我想解决redis client protocol: 1.足够了解通信协议 ...
- StackExchange.Redis Client
StackExchange.Redis Client 这期我们来看StackExchange.Redis,这是redis 的.net客户端之一.Redis是一个开源的内存数据存储,可以用来做数据库,缓 ...
随机推荐
- leetCode 27.Remove Element (删除元素) 解题思路和方法
Remove Element Given an array and a value, remove all instances of that value in place and return th ...
- JavaScript中操作对象的属性
1.操作对象的属性 注意: 标签属性与DOM对象属性的相应关系: 绝大部分2者是同样的.如:imgobj.src属性相应<img src="" >中src属性,但也有例 ...
- ListView-添加head跟foot item 问题
今天在使用ListView 的 addFooterView 的方法时候,遇到了一个问题.当我代码中执行了如下的操作 ListView listView = new ListView(this); li ...
- go语言的一个gui 开源 项目 https://github.com/andlabs/ui
go语言的一个gui 开源 项目 https://github.com/andlabs/ui 1 安装 mingw-w64 链接地址: http://mingw-w64.sourceforge. ...
- 手机端使用rem的适配
<html> <body> <!-- http://www.w3cfuns.com/notes/29143/79dafb7c07f6865f435af641869d312 ...
- Postman APP
http://chromecj.com/web-development/2017-12/870.html Postman 工具模拟http各种协议请求.
- 压状态bfs
一般地图很小,状态不多,可以装压或者hash,构造压缩或hash的函数,构造还原地图的函数,然后就无脑bfs(感觉就是SPFA) 题目: 1.玩具游戏:二进制压缩状态 #include<cstd ...
- 【习题 8-1 UVA - 1149】Bin Packing
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每个背包只能装两个东西. 而且每个东西都要被装进去. 那么我们随意考虑某个物品.(不必要求顺序 这个物品肯定要放进某个背包里面的. ...
- VS:"64位调试操作花费的时间比预期要长"的一解决途径
解决的方法之中的一个: 在命令提示符那里打入例如以下命令: netsh winsock reset catalog netsh int ip reset reset.log hit 重新启动电脑后,就 ...
- poj 2480 Longge's problem 积性函数性质+欧拉函数
题意: 求f(n)=∑gcd(i, N) 1<=i <=N. 分析: f(n)是积性的数论上有证明(f(n)=sigma{1<=i<=N} gcd(i,N) = sigma{d ...