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是一个开源的内存数据存储,可以用来做数据库,缓 ...
随机推荐
- linux 进程等待 wait 、 waitpid
waitpid() 与 wait() 功能相似,都是用户主进程等待子进程结束或中断. 可用于进程之间的同步 wait 函数原型 pid_t wait(int *status); 函数说明 wait() ...
- [BZOJ2143]飞飞侠 并查集优化最短路
链接 题解 首先很容易想到对每个点暴力跑Dijkstra,但是这样边数是 \(N^4\) 的,考虑优化 发现每次松弛的时候,都要把整个地图扫一遍,每个节点都要重复扫很多次,如果我们在一个点不会再被更新 ...
- 70.lambda表达式逻辑(二进制转换为为十进制)
#include <iostream> #include <cstring> using namespace std; void main() { auto fun = []( ...
- centos配置tomcat编辑修改
https://jingyan.baidu.com/article/6525d4b1382f0aac7d2e9421.html
- CSU 8月月赛 Decimal 小数化分数
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1303 这个OJ很容易跪所以我贴一下题目 Description 任意一个分数都是有理数,对于任意一 ...
- thinkphp5空控制器和空操作
thinkphp5空控制器和空操作 一.总结 1.空控制器和空操作用:空控制器和空操作都是为了防止网站上的用户恶意输入,网站上线的话必须加上, 2.空操作:空操作就是在一般的控制器里面加上一个 _em ...
- BZOJ3510首都(LCT)
Description 在X星球上有N个国家,每个国家占据着X星球的一座城市.由于国家之间是敌对关系,所以不同国家的两个城市是不会有公路相连的. X星球上战乱频发,如果A国打败了B国,那么B国将永远从 ...
- Linux文本编辑器
1.编辑模式 2.命令模式 3.底部命令模式 注意:如果发现编辑不了.可能是因为非法退出产生一个后缀名为.swp 的临时隐藏文件. 将其删除重新编辑即可!
- Windows平台上的pip安装
写在前面 pip 是 Python 的包管理工具,在 Python 开发中必不可少.作为一名python菜鸟,本文在踩坑无数的基础上尽可能详细地解释pip的安装过程.在安装之前需要明确两点: 1.pi ...
- 洛谷 P2384 最短路
洛谷 P2384 最短路 题目背景 狗哥做烂了最短路,突然机智的考了Bosh一道,没想到把Bosh考住了...你能帮Bosh解决吗? 他会给你10000000000000000000000000000 ...