Basic use of Lettuce(1)

      It's the first time to write a blog, please forgive me if it's not good. If there is something wrong/bad, please advise.

Taking advantage of the Chinese New Year, I learned to learn Redis, and now I record some of the learning results for easy reference, and also help everyone avoid detours

      -1- Description

            redis version: redis-3.2.6

            Environment: cluster, three masters and three backups

            lettuce version 4.3.0-final / 4.2.0-final

            The difference between these two versions is relatively large, and there may be problems with inconsistent versions in use, mainly related to the version of netty

      -2- maven dependencies

            

<!-- Lettuce Redis Client-->
<dependency>
	<groupId>biz.paluch.redis</groupId>
	<artifactId>lettuce</artifactId>
	<!--<version>4.2.0.Final</version>-->
	<version>4.3.0.Final</version>
</dependency>

 

 

      -2- Lettuce stand-alone client

           

import java.util.concurrent.ExecutionException;

import com.lambdaworks.redis.RedisClient;
import com.lambdaworks.redis.RedisFuture;
import com.lambdaworks.redis.RedisURI;
import com.lambdaworks.redis.api.StatefulRedisConnection;
import com.lambdaworks.redis.api.async.RedisAsyncCommands;
import com.lambdaworks.redis.api.sync.RedisCommands;

public class LettuceClient {

	public static void main(String[] args) {
		RedisClient client = RedisClient.create(RedisURI.create("redis://192.168.37.128:7000"));
		StatefulRedisConnection<String, String> connect = client.connect();
		
		/* Synchronous execution of commands */
		RedisCommands<String, String> commands = connect.sync();
		String str = commands.get("test1");
		//String str2 = commands.get("test2");//MOVED 8899 192.168.37.128:7001;;;;client is a stand-alone version
		System.out.println(str);
		
		/* Asynchronously executed command */
//		RedisAsyncCommands<String, String> commands = connect.async();
//		RedisFuture<String> future = commands.get("test1");
//		try {
//			String str = future.get();
//			System.out.println(str);
//		} catch (InterruptedException e) {
// e.printStackTrace ();
//		} catch (ExecutionException e) {
// e.printStackTrace ();
//		}
		
		connect.close();
		client.shutdown();
	}
}

 A single-machine client connects to Redis in a cluster environment. If the hash slot corresponding to the key is not on the host created by the current client, it will report MOVE.... 192.168.37.128:7001 Exception

            

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326765309&siteId=291194637