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. jquery05 继承

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  2. 深入解析开源项目之Universal-Image-Loader(二)内存---缓存篇

    珍惜作者劳动成果,如需转载,请注明出处. http://blog.csdn.net/zhengzechuan91/article/details/50292871 Universal-Image-Lo ...

  3. 71.lambda表达式的递归

    #include <iostream> #include <functional> using namespace std; void main() { //&调用外部 ...

  4. touch---创建文件或更改文件日期

  5. 洛谷 P1757 通天之分组背包

    P1757 通天之分组背包 题目背景 直达通天路·小A历险记第二篇 题目描述 自01背包问世之后,小A对此深感兴趣.一天,小A去远游,却发现他的背包不同于01背包,他的物品大致可分为k组,每组中的物品 ...

  6. WordPress出现Briefly unavailable for scheduled maintenance. Check back in a minute. 的解决方法

    WordPress出现 Briefly unavailable for scheduled maintenance. Check back in a minute. 解决方法: 登入FTP,然后把Wo ...

  7. Word Ladder II [leetcode]

    本题有几个注意点: 1. 回溯找路径时.依据路径的最大长度控制回溯深度 2. BFS时,在找到end单词后,给当前层做标记find=true,遍历完当前层后结束.不须要遍历下一层了. 3. 能够将字典 ...

  8. 停止使用域名 boypay.net

    停止使用域名 boypay.net boypay.net 这个域名当时注册的时候打算开发网店--" 情侣商城",表面意思是 "男生支付",情侣和婚姻是人生中必须 ...

  9. Android HttpLoggingInterceptor的用法简介

    该拦截器用于记录应用中的网络请求的信息. 示例 OkHttpClient client = new OkHttpClient(); HttpLoggingInterceptor logging = n ...

  10. jQ-多选按钮实现单选按钮的功能以及input按钮的优化

    css: .displayN{display: none;} label {font-size:12px;cursor:pointer;} label i {font-size:12px;font-s ...