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.

几个常见的使用方法:

1. 连接单机

package com.lambdaworks.examples;

import com.lambdaworks.redis.RedisClient;
import com.lambdaworks.redis.RedisConnection;
import com.lambdaworks.redis.RedisURI;

/**
 * @author <a href="mailto:[email protected]">Mark Paluch</a>
 * @since 18.06.15 09:17
 */
public class ConnectToRedis {

    public static void main(String[] args) {
        // Syntax: redis://[[email protected]]host[:port][/databaseNumber]
        RedisClient redisClient = new RedisClient(RedisURI.create("redis://[email protected]: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:[email protected]">Mark Paluch</a>
 * @since 18.06.15 09:17
 */
public class ConnectToRedisCluster {

    public static void main(String[] args) {
        // Syntax: redis://[[email protected]]host[:port]
        RedisClusterClient redisClient = new RedisClusterClient(RedisURI.create("redis://[email protected]: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:[email protected]">Mark Paluch</a>
 * @since 18.06.15 09:17
 */
public class ConnectToRedisUsingRedisSentinel {

    public static void main(String[] args) {
        // Syntax: redis-sentinel://[[email protected]]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:[email protected]">Mark Paluch</a>
 * @since 18.06.15 09:17
 */
public class ConnectToRedisSSL {

    public static void main(String[] args) {
        // Syntax: rediss://[[email protected]]host[:port][/databaseNumber]
        // Adopt the port to the stunnel port in front of your Redis instance
        RedisClient redisClient = new RedisClient(RedisURI.create("rediss://[email protected]: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:[email protected]">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:[email protected]">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

时间: 2024-08-07 21:16:41

lettuce--Advanced Redis client的相关文章

Redis client Python usage

mport redis r_server = redis.Redis('localhost') #this line creates a new Redis object and #connects to our redis server r_server.set('test_key', 'test_value') #with the created redis object we can #submits redis commands as its methods print 'previou

Advanced Rest Client调试RESTFul

Advanced REST client 基于浏览器的Rest Client工具 在chrome或者firefox浏览器都有很多插件,我一般都是使用chrome浏览器,在chrome的webstore中可以搜索到自己想要的插件.这里就讲讲Advance REST Client,Postman-REST Client,DEV HTTP CLIENT,Simple REST Client 网页开发者辅助程序来创建和测试自定义HTTP请求.它是一款非常强大,使用简单的客户端测试工具,得到了程序员的好评

redis client protocol 解析

在官网http://redis.io/topics/protocol有对redis通信协议有做说明. 基于下面的一些原因,我想解析redis client protocol: 1.足够了解通信协议,有助于做出更好的系统设计. 2.学习RESP的设计思想,不仅能扩展我的思维,也许将来能应用于我的代码中. 3.因为有些人想将redis client直接并入自己已有的系统中:包括我在内.这个将在我下一篇文章再做说明. 下面我翻译一下http://redis.io/topics/protocol一些我认

Googel 浏览器 模拟发送请求工具--Advanced REST Client

Advanced REST Client是 Chrome 浏览器下的一个插件,通过它可以发送 http.https.WebSocket 请求.在 Chrome 商店下搜索 Advanced REST Client,即可找到 如果搜索不到的可到CSDN 下载: http://download.csdn.net/detail/li1669852599/9547748 1.下载插件:Advanced Rest Client 2.因为最新版的Chrome不支持本地安装插件,所以我们要使能开发者模式 3.

StackExchange.Redis Client

StackExchange.Redis Client 这期我们来看StackExchange.Redis,这是redis 的.net客户端之一.Redis是一个开源的内存数据存储,可以用来做数据库,缓存或者消息代理服务.目前有不少人在使用ServiceStack.Redis这个.net客户端,但是这个的最新版本目前已经变成了商业软件.对于ServiceStack.Redis这种行为,我们没有什么好说的,留给我们的选择是使用低版本的开源版本或者转向其他的客户端. 要说到StackExchange.

发送请求工具—Advanced REST Client

Advanced REST Client是Chrome浏览器下的一个插件,通过它能够发送http.https.WebSocket请求.在Chrome商店下搜索Advanced REST Client,就可以找到 Advanced REST Client的界面 Advanced REST Client在google code的地址:http://code.google.com/p/chrome-rest-client/ 以下来介绍Advanced REST Client发送请求的方法 1. 发送h

redis client 2.0.0 pipeline 的list的rpop bug

描写叙述: redis client 2.0.0 pipeline 的list的rpop 存在严重bug,rpop list的时候,假设list已经为空的时候,rpop出来的Response依旧不为null,导致吊response.get()方法抛异常 代码: @Test public void testRedisPipeline(){ Jedis jedis = null; try{ jedis = new Jedis("127.0.0.1",6379); Pipeline pipe

【轮子狂魔】手把手教你自造Redis Client

为什么做Redis Client? Redis Client顾名思义,redis的客户端,主要是封装了一些对于Redis的操作. 而目前用的比较广泛的 ServiceStack.Redis 不学好,居然开始收费了. 作为轮子狂魔,是可忍孰不可忍啊.于是我决定自己造轮子了. Redis通信协议 先给个Redis官方的通信协议地址:http://redisdoc.com/topic/protocol.html 关键是我截图的部分,我们可以得到以下几个信息: 1.tcp协议 2.默认端口6379 3.

redis client protocol 实现

在官网中http://redis.io/clients有许多已经实现好的redis client:有需要可以参考一下. 其实前一篇http://blog.csdn.net/yitouhan/article/details/46612925 redis client protocol 解析,我已经对RESP做主要的解析. 下面是解析RESP的所有函数,其中对外函数是RedisProtocol::Decode: https://github.com/XJM2013/GameEngine/blob/m