• Redis开启远程连接


    Redis开启远程连接

    1 开启远程连接

    redis默认是不支持远程连接,需要手动开启,在redis.conf文件中,找到下方法代码:

    #bind 127.0.0.1
    
    • 1

    这里只允许127.0.0.1登录,注释掉

    开启密码校验,去掉requirepass的注析

    requirepass password
    //设置密码为password(自定义的,随便设)
    
    • 1
    • 2

    改完之后,保存退出,启动redis

    redis-server redis.conf
    
    • 1

    2 Jedis连接Redis

    首先创建一个maven项目

    Jedis的GitHub地址:https://github.com/xetorthio/jedis
    
    • 1

    项目创建成功后,添加Jedis依赖

    
        redis.clients
        jedis
        3.2.0
        jar
        compile
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    创建测试方法

    public class MyJedis {
        public static void main(String[] args) {
            //1.构造一个Jedis对象,因为这里使用的默认端口6379,所以不用配置端口,要填的是远程服务器的端口号
            Jedis jedis = new Jedis("127.0.0.1");
            //密码
            jedis.auth("password");
            //测试是否连接成功
            String ping = jedis.ping();
            //返回pong表示成功
            System.out.println(ping);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    对于Jedis而言,一旦连接上redis服务端,剩下的操作就很简单了

    在Jedis中,由于方法的API和Redis的命令高度一致,所以,Jedis中的方法见名知意,直接使用即可

    3 Jedis优化连接

    3.1 Jedis连接池

    在实际应用中,Jedis实例我们一般都是通过连接池来获取,由于Jedis对象不是线程安全的,所以,当我们使用Jdeis对象时,从连接池获取Jedis,使用完成后,再还给连接池。

    public class JedisPoolTest {
        public static void main(String[] args) {
            Jedis jedis = null;
            //1.构造一个jedis连接池
            JedisPool jedisPool = new JedisPool("127.0.0.1", 6379);
            //2.从连接池中获取一个Jedis连接
            jedis = jedisPool.getResource();
            jedis.auth("password");
            try {
                //3.Jedis操作
                String ping = jedis.ping();
                System.out.println(ping);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                //4.归还连接
                if (jedis != null)
                    jedis.close();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    通过finally确保jedis一定被关闭

    利用JDK1.7中的try-with-resource特性,可以对上面的代码进行改造:

    public class JedisPoolTest {
        public static void main(String[] args) {
            //1.构造一个jedis连接池
            JedisPool jedisPool = new JedisPool("127.0.0.1", 6379);
            //2.从连接池中获取一个Jedis连接
            //版本低,可以在Project Structure的Modules的Language level哪里改为8
            try(Jedis jedis = jedisPool.getResource()){
                //3.Jedis操作
                jedis.auth("password");
                String ping = jedis.ping();
                System.out.println(ping+":");
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 版本低,可以在Project Structure的Modules的Language level哪里改为8

    增加约束,封装

    public interface CallWithJedis {
        void call(Jedis jedis);
    }
    
    
    public class Redis {
        private JedisPool pool;
        public Redis(){
            GenericObjectPoolConfig config=new GenericObjectPoolConfig();
            //连接池最大空闲数
            config.setMaxIdle(300);
            //最大连接数
            config.setMaxTotal(1000);
            //连接最大等待时间,如果是-1表示没有限制
            config.setMaxWaitMillis(30000);
            //空闲时检查有效性
            config.setTestOnBorrow(true);
            /**
             * 1.redis地址
             * 2.redis
             * 3.连接超时时间
             * 4.密码
             */
            pool=new JedisPool(config,"127.0.0.1",6379,30000,"password");
        }
        public void exectu(CallWithJedis callWithJedis){
            try(Jedis jedis=pool.getResource()){
                callWithJedis.call(jedis);
            }
        }
    }
    
    
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    
    public class JedisPoolTest {
        public static void main(String[] args) {
            Redis redis = new Redis();
            redis.exectu(jedis -> {
                System.out.println(jedis.ping());
            });
        }
    }
    
    
    
    在这里插入代码片
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    4 lettuce

    github:github.com/lettuce-io/lettuce-core

    lettuce和Jedis的一个比较

    1. Jedis在实现的过程中是直接连接Reids的,在多个线程之间共享一个Jedis实例,这是线程不安全的,如果想在多线程场景下使用Jedis,就得使用连接池,这样,每个线程都有自己的Jedis实例。(会浪费)
    2. Lettuce基于Netty NIO框架来构建的,所以克服了Jedis中线程不安全的问题,Lettuce支持同步、异步以及响应式调用,多个线程可用共享一个连接实例。

    使用Lettuce,首先创建一个普通的maven项目,添加Lettuce依赖

        
            
                io.lettuce
                lettuce-core
                5.2.2.RELEASE
            
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    实例:

    import io.lettuce.core.RedisClient;
    import io.lettuce.core.api.StatefulRedisConnection;
    import io.lettuce.core.api.sync.RedisCommands;
    
    public class LettuceTest {
        public static void main(String[] args) {
            RedisClient redisClient = RedisClient.create("redis://password@127.0.0.1");
            StatefulRedisConnection connect = redisClient.connect();
            RedisCommands sync = connect.sync();
            sync.set("name","java");
            String name=sync.get("name");
            System.out.println(name);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    这里的密码传递方式,密码直接写下连接地址里面

  • 相关阅读:
    如何进行任务优先级的确定
    WPF篇(10)-Label标签+TextBlock文字块+TextBox文本框+RichTextBox富文本框
    【LeetCode高频SQL50题-基础版】打卡第5天:第26~30题
    vue3项目实战中的接口调用方法(一)async/await用法 对axios二次封装 实现异步请求
    字节跳动数据库的过去、现状与未来
    前端项目中,强缓存和协商缓存的配置
    RT-Thread Studio学习(十二)W25Q128(SPI)的读写
    JSP ssh 校园二手商品拍卖系统myeclipse开发mysql数据库MVC模式java编程网页设计
    C语言三位数求解(ZZULIOJ1076:三位数求解)
    使用PasteSpider把你的代码升级到服务器的Docker/Podman上,K8S太庞大,PasteSpider极易上手!
  • 原文地址:https://blog.csdn.net/m0_67402564/article/details/126596121