jfinal 使用 lettuce 整合 redis5.0.x集群

1.redis5.0.x集群搭建,参考慕课网免费课程https://www.imooc.com/learn/1089,我看完自己搭建的过程有个注意点:开放你redis实例端口号+10000的端口。

2.jfinal引入lettuce5.0.5,我试了最新版lettuce5.2.x和lettuce5.1.x不稳定,会报错,建议使用lettuce5.0.x,jfinal我使用的是jfinal4.7
<dependency>
  <groupId>io.lettuce</groupId>
  <artifactId>lettuce-core</artifactId>
  <version>5.0.5.RELEASE</version>
</dependency>

3.redis集群连接类:PoolLettuceCluster.java

import io.lettuce.core.RedisURI;
import io.lettuce.core.cluster.RedisClusterClient;
import io.lettuce.core.cluster.api.StatefulRedisClusterConnection;
import io.lettuce.core.cluster.api.sync.RedisAdvancedClusterCommands;

import java.util.ArrayList;
import java.util.List;

public class PoolLettuceCluster {
    static RedisClusterClient client;
    static StatefulRedisClusterConnection<String, String> connect;
    public static RedisAdvancedClusterCommands<String, String> openCluster(){
        List<RedisURI> list = new ArrayList<>();
        list.add(RedisURI.create("redis://127.0.0.1:5001"));
        list.add(RedisURI.create("redis://127.0.0.1:5002"));
        list.add(RedisURI.create("redis://127.0.0.1:5003"));
        list.add(RedisURI.create("redis://127.0.0.1:5004"));
        list.add(RedisURI.create("redis://127.0.0.1:5005"));
        list.add(RedisURI.create("redis://127.0.0.1:5006"));
        client = RedisClusterClient.create(list);
        connect = client.connect();

        RedisAdvancedClusterCommands<String, String> commands = connect.sync();
        return commands;
    }
    public static void closeCluster(){
        connect.close();
        client.shutdown();
        return ;
    }
}

4.使用set、get方法
RedisAdvancedClusterCommands<String, String> commands = PoolLettuceCluster.openCluster();//打开redis集群连接类
commands.set("token","hello world!");
String str = commands.get("token");
PoolLettuceCluster.closeCluster();//关闭redis集群连接类

5.redis其他方法我就先不研究了大家可以去看lettuce文档


评论区

JFinal

2019-10-31 18:50

感觉是极简设计,代码量很少,用起来应该会很舒适,谢谢分享,赞

热门分享

扫码入社