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.

几个常见的使用方法:

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

【2】http://redis.paluch.biz

lettuce--Advanced Redis client的更多相关文章

  1. HAProxy advanced Redis health check---ref

    http://blog.exceliance.fr/2014/01/02/haproxy-advanced-redis-health-check/ HAProxy advanced Redis hea ...

  2. 深入浅出 Redis client/server交互流程

    综述 最近笔者阅读并研究redis源码,在redis客户端与服务器端交互这个内容点上,需要参考网上一些文章,但是遗憾的是发现大部分文章都断断续续的非系统性的,不能给读者此交互流程的整体把握.所以这里我 ...

  3. Redis 详解 (一) StackExchange.Redis Client

    这期我们来看StackExchange.Redis,这是redis 的.net客户端之一.Redis是一个开源的内存数据存储,可以用来做数据库,缓存或者消息代理服务.目前有不少人在使用ServiceS ...

  4. Advanced REST client

    好用的测试工具,老是忘记名字chrome插件 Advanced REST client

  5. Advanced REST client的使用说明

    1.  为什么要使用REST Client 在实际企业开发过程中经常会有这样的需求: 1.我当前开发的这个系统是需要调用其他系统的接口,也就是我们需要频繁的测试接口,尝试不同的入参参数去查看返回结果, ...

  6. 谷歌(Chrome)安装Advanced REST Client插件

    进入Extensions(工具——>扩展程序) 点击Get More extensions或新建标签页点击网上应用店 如果加载太慢,出现chrome网上应用店无法打开,显示暂时无法加载该应用的画 ...

  7. 发送请求工具—Advanced REST Client

    Advanced REST Client是Chrome浏览器下的一个插件,通过它能够发送http.https.WebSocket请求.在Chrome商店下搜索Advanced REST Client, ...

  8. redis client protocol 分解

    在官方网站http://redis.io/topics/protocol我们必须redis通信协议做说明. 根据以下某些原因.我想解决redis client protocol: 1.足够了解通信协议 ...

  9. StackExchange.Redis Client

    StackExchange.Redis Client 这期我们来看StackExchange.Redis,这是redis 的.net客户端之一.Redis是一个开源的内存数据存储,可以用来做数据库,缓 ...

随机推荐

  1. IOS学习之斯坦福大学IOS开发课程笔记(第六课)

    转载请注明出处 http://blog.csdn.net/pony_maggie/article/details/28398697 作者:小马 这节课主要讲述多个MVC是怎样协同工作的.到眼下为止.全 ...

  2. c++笔试题:不使用第三个变量来交换俩个变量的数值

    题目:将a 与 b的值互换. 通常我们的做法是(尤其是在学习阶段):定义一个新的变量,借助它完成交换.代码如下:      int a,b;      a; b:      int t;      t ...

  3. Enity Framework已经是.NET下最主要的ORM了

    Enity Framework已经是.NET下最主要的ORM了.而ORM从一个Mapping的概念开始,到现在已经得到了一定的升华,特别是EF等对ORM框架面向对象能力的升华.切实地说,就是ORM让数 ...

  4. 51Nod 1006 最长公共子序列Lcs问题 模板题

    给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). 比如两个串为:   abcicba abdkscab   ab是两个串的子序列,abc也是,abca也是,其中abca是这两个 ...

  5. ZJOI2017线段树

    ZJOI2017线段树 题意: ​ 给你一颗广义线段树,太长了,自己去看. 题解: ​ 直接上zkw那一套,把闭区间换成开区间,就是把取\([l,r]\),变成取\([l-1,l-1],[r+1,r+ ...

  6. 【DRF认证】

    目录 认证组件的详细用法 本文详细讲述了DRF认证组件的原理以及用法. @ * 源码剖析** 上一篇博客讲解DRF版本的时候我们都知道了,在dispatch方法里执行了initial方法来初始化我们的 ...

  7. POJ 1442 Black Box treap求区间第k大

    题目来源:POJ 1442 Black Box 题意:输入xi 输出前xi个数的第i大的数 思路:试了下自己的treap模版 #include <cstdio> #include < ...

  8. Android经常使用自己定义控件(二)

           经常使用的Android自己定义控件分享 http://www.see-source.com//androidwidget/list.html?type=&p=1

  9. 亚马逊AWS的route53的收费,月费最低 0.9 USD

    亚马逊AWS的route53的收费Amazon Route 53 定价 https://aws.amazon.com/cn/route53/pricing/ 一文中,对于一些术语的解释第一项收费--域 ...

  10. php实现希尔排序(总结)

    php实现希尔排序(总结) 一.总结 1.希尔排序的算法思路:分组排序, 缩小增量排序,插入排序 2.算法思路: 循环非常好写 有几次gap:log2(n) 每次gap有几组:gap组 每组有几个元素 ...