使用lettuce连接redis单机和群集

pom.xml文件:

<dependency>
    <groupId>org.springframework.datagroupId>
    <artifactId>spring-data-redisartifactId>
    <version>2.0.7.RELEASEversion>
dependency>
<dependency>
    <groupId>io.lettucegroupId>
    <artifactId>lettuce-coreartifactId>
    <version>5.0.4.RELEASEversion>
dependency>

Java片段

import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisFuture;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.async.RedisAsyncCommands;
import io.lettuce.core.api.sync.RedisCommands;
import io.lettuce.core.cluster.RedisClusterClient;
import io.lettuce.core.cluster.api.StatefulRedisClusterConnection;
import io.lettuce.core.cluster.api.async.RedisAdvancedClusterAsyncCommands;
import io.lettuce.core.cluster.api.sync.RedisAdvancedClusterCommands;

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

/**
 * 使用Lettuce 操作redis单机版和redis集群
 */
public class LettucecClient {
    public static void main(String[] args) {
        operCluster();
    }

    public static void operSingle(){
        RedisClient client = RedisClient.create(RedisURI.create("redis://127.0.0.1:7001"));
        StatefulRedisConnection connect = client.connect();

        /**
         * 同步调用
         */
        RedisCommands commands = connect.sync();
        commands.set("hello","hello world");
        String str = commands.get("hello");
        System.out.println(str);

        /**
         * 异步调用
         */
        RedisAsyncCommands asyncCommands = connect.async();
        RedisFuture future = asyncCommands.get("hello");
        try {
            String str1 = future.get();
            System.out.println(str1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }


        connect.close();
        client.shutdown();
    }

    public static void operCluster(){
        List list = new ArrayList<>();
        list.add(RedisURI.create("redis://127.0.0.1:7001"));
        list.add(RedisURI.create("redis://127.0.0.1:7002"));
        list.add(RedisURI.create("redis://127.0.0.1:7003"));
        list.add(RedisURI.create("redis://127.0.0.1:7004"));
        list.add(RedisURI.create("redis://127.0.0.1:7005"));
        list.add(RedisURI.create("redis://127.0.0.1:7006"));
        RedisClusterClient client = RedisClusterClient.create(list);
        StatefulRedisClusterConnection connect = client.connect();

        /**
         * 同步执行命令
         */
        RedisAdvancedClusterCommands commands = connect.sync();
        commands.set("hello","hello world");
        String str = commands.get("hello");
        System.out.println(str);

        /**
         * 异步执行命令
         */
        RedisAdvancedClusterAsyncCommands asyncCommands = connect.async();
        RedisFuture future = asyncCommands.get("hello");

        try {
            String str1 = future.get();
            System.out.println(str1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        connect.close();
        client.shutdown();
    }
}

springboot 调用:

import io.lettuce.core.RedisURI;
import io.lettuce.core.cluster.ClusterClientOptions;
import io.lettuce.core.cluster.RedisClusterClient;
import io.lettuce.core.cluster.api.StatefulRedisClusterConnection;
import io.lettuce.core.resource.ClientResources;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class NewRedisClientApplication {

    @Bean(name="clusterRedisUri")
    RedisURI clusterRedisUri(){
       return RedisURI.builder().withHost("127.0.0.1").withPort(7001)
               .withHost("127.0.0.1").withPort(7002)
               .withHost("127.0.0.1").withPort(7003)
               .withHost("127.0.0.1").withPort(7004)
               .withHost("127.0.0.1").withPort(7005)
               .withHost("127.0.0.1").withPort(7006).build();
    }
    //配置集群选项,自动重连,最多重定型1次
    @Bean
    ClusterClientOptions clusterClientOptions(){
        return ClusterClientOptions.builder().autoReconnect(true).maxRedirects(1).build();
    }

    //创建集群客户端
    @Bean
    RedisClusterClient redisClusterClient(ClientResources clientResources, ClusterClientOptions options,RedisURI clusterRedisUri){
        RedisClusterClient redisClusterClient = RedisClusterClient.create(clientResources,clusterRedisUri);
        redisClusterClient.setOptions(options);
        return redisClusterClient;
    }

    //集群连接
    @Bean(destroyMethod = "close")
    StatefulRedisClusterConnection statefulRedisClusterConnection(RedisClusterClient redisClusterClient){
        return redisClusterClient.connect();
    }
}

测试

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
        StatefulRedisClusterConnection connection = (StatefulRedisClusterConnection) context.getBean("statefulRedisClusterConnection");

        RedisAdvancedClusterCommands clusterCommands = connection.sync();
        String str = clusterCommands.get("hello");

        System.out.println(str);
    }
}

你可能感兴趣的:(Java基础,Spring)