Lettuce基本使用(2) --集群

使用Lettuce  操作 Redis3.2.6集群

Lettuce使用Netty作为底层通信框架

 

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import com.lambdaworks.redis.RedisFuture;
import com.lambdaworks.redis.RedisURI;
import com.lambdaworks.redis.cluster.RedisClusterClient;
import com.lambdaworks.redis.cluster.api.StatefulRedisClusterConnection;
import com.lambdaworks.redis.cluster.api.async.RedisAdvancedClusterAsyncCommands;
import com.lambdaworks.redis.cluster.api.sync.RedisAdvancedClusterCommands;

public class LettuceClusterClient {

	public static void main(String[] args) {
		ArrayList list = new ArrayList<>();
		list.add(RedisURI.create("redis://192.168.37.128:7000"));
		list.add(RedisURI.create("redis://192.168.37.128:7001"));
		list.add(RedisURI.create("redis://192.168.37.128:7002"));
		list.add(RedisURI.create("redis://192.168.37.128:7003"));
		list.add(RedisURI.create("redis://192.168.37.128:7004"));
		list.add(RedisURI.create("redis://192.168.37.128:7005"));
		RedisClusterClient client = RedisClusterClient.create(list);
		//RedisClusterClient client = RedisClusterClient.create("redis://192.168.37.128:7000");
		StatefulRedisClusterConnection connect = client.connect();
		
		/* 同步执行的命令 */
		RedisAdvancedClusterCommands commands = connect.sync();
		String str = commands.get("test2");
		System.out.println(str);
		
		/*  异步执行的命令  */
//		RedisAdvancedClusterAsyncCommands commands= connect.async();
//		RedisFuture future = commands.get("test2");
//		try {
//			String str = future.get();
//			System.out.println(str);
//		} catch (InterruptedException e) {
//			e.printStackTrace();
//		} catch (ExecutionException e) {
//			e.printStackTrace();
//		}
		
		connect.close();
		client.shutdown();
	}
}

    相同的代码,使用不同的Lettuce版本执行,可能会报异常,主要是引入的Netty版本有光,只需把Netty的版本修改成相同的即可 


	io.netty
	netty-all
        
	
	4.0.36.Final

你可能感兴趣的:(Redis)