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. Cocos2d-x怎样控制动作速度

    基本动作和组合动作实现了针对精灵的各种运动和动画效果的改变.但这种改变速度匀速的.线性的.通过ActionEase及其的派生类和Speed 类我们能够使精灵以非匀速或非线性速度运动,这样看起了效果更加 ...

  2. 10.cocos2dx C++为Sprite添加触摸事件监听器

    1.首先头文件定义事件处理的函数原型 private: bool onTouchBegan(Touch* tTouch,Event* eEvent);//手指按下事件 void onTouchMove ...

  3. CSS动态实现文本框清除按钮的隐藏与显示

    当前现代浏览器中,Chrome浏览器下type=search的输入框会有清除按钮的动态呈现,不过搜索input框尺寸不太好控制(padding无视):FireFox浏览器貌似任何类型的输入框都无动于衷 ...

  4. 带你底层看Sqoop如何转换成MapReduce作业运行的(代码程序)

    补充 其实啊,我们知道,sqoop在运行的时候,最终会去转换成mapreduce作业,这个很简单,不多赘述.直接贴出来. 具体这些怎么运行的,见我如下这篇博客.这里只做一个引子. Sqoop Impo ...

  5. Python线程池任务

    #!/usr/bin/env python # -*- coding:utf-8 -*- from concurrent.futures import ThreadPoolExecutor #线程池, ...

  6. cron 简单任务调度 go

    package main import ( "github.com/robfig/cron" "log" ) func main() { i := 0 c := ...

  7. POJ——T 2891 Strange Way to Express Integers

    http://poj.org/problem?id=2891 Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 16849   ...

  8. RecyclerView具体解释

    public class RecyclerView extends ViewGroup implements ScrollingView, NestedScrollingChild { 由上面的继承结 ...

  9. 【Cocos2d-x 017】 多分辨率适配全然解析

    转:http://blog.csdn.net/w18767104183/article/details/22668739 文件夹从Cocos2d-x 2.0.4開始,Cocos2d-x提出了自己的多分 ...

  10. js21---单体(单例)模式

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...