• redis<二>spring使用redis,配置远程登录和密码


    使用默认的redis配置

    1. 改pom, 加入redis依赖,版本可以不需要写,由spring的父工程控制。
    
    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-data-redisartifactId>
    dependency>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    1. 配配置类
    package com.fuzekun.demo1.configuration;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.RedisSerializer;
    
    @Configuration
    public class RedisConfig {
    
        @Bean
        public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
            RedisTemplate<String, Object> template = new RedisTemplate<>();
            template.setConnectionFactory(factory);
    
            // 设置key的序列化方式
            template.setKeySerializer(RedisSerializer.string());
            // 设置value的序列化方式
            template.setValueSerializer(RedisSerializer.json());
            // 设置hash的key的序列化方式
            template.setHashKeySerializer(RedisSerializer.string());
            // 设置hash的value的序列化方式
            template.setHashValueSerializer(RedisSerializer.json());
    
            template.afterPropertiesSet();
            return template;
        }
    
    }
    
    
    • 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
    1. 写测试类
    package com.fuzekun.demo1.utils;
    
    import com.fuzekun.demo1.Demo1Application;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.dao.DataAccessException;
    import org.springframework.data.redis.core.BoundValueOperations;
    import org.springframework.data.redis.core.RedisOperations;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.core.SessionCallback;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import java.util.concurrent.TimeUnit;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    @ContextConfiguration(classes = Demo1Application.class)
    public class RedisTests {
    
        @Autowired
        private RedisTemplate redisTemplate;
    
        @Test
        public void testStrings() {
            String redisKey = "test:count";
    
            redisTemplate.opsForValue().set(redisKey, 1);
    
            System.out.println(redisTemplate.opsForValue().get(redisKey));
            System.out.println(redisTemplate.opsForValue().increment(redisKey));
            System.out.println(redisTemplate.opsForValue().decrement(redisKey));
        }
    
        @Test
        public void testHashes() {
            String redisKey = "test:user";
    
            redisTemplate.opsForHash().put(redisKey, "id", 1);
            redisTemplate.opsForHash().put(redisKey, "username", "zhangsan");
    
            System.out.println(redisTemplate.opsForHash().get(redisKey, "id"));
            System.out.println(redisTemplate.opsForHash().get(redisKey, "username"));
        }
    
        @Test
        public void testLists() {
            String redisKey = "test:ids";
    
            redisTemplate.opsForList().leftPush(redisKey, 101);
            redisTemplate.opsForList().leftPush(redisKey, 102);
            redisTemplate.opsForList().leftPush(redisKey, 103);
    
            System.out.println(redisTemplate.opsForList().size(redisKey));
            System.out.println(redisTemplate.opsForList().index(redisKey, 0));
            System.out.println(redisTemplate.opsForList().range(redisKey, 0, 2));
    
            System.out.println(redisTemplate.opsForList().leftPop(redisKey));
            System.out.println(redisTemplate.opsForList().leftPop(redisKey));
            System.out.println(redisTemplate.opsForList().leftPop(redisKey));
        }
    
        @Test
        public void testSets() {
            String redisKey = "test:teachers";
    
            redisTemplate.opsForSet().add(redisKey, "刘备", "关羽", "张飞", "赵云", "诸葛亮");
    
            System.out.println(redisTemplate.opsForSet().size(redisKey));
            System.out.println(redisTemplate.opsForSet().pop(redisKey));
            System.out.println(redisTemplate.opsForSet().members(redisKey));
        }
    
        @Test
        public void testSortedSets() {
            String redisKey = "test:students";
    
            redisTemplate.opsForZSet().add(redisKey, "唐僧", 80);
            redisTemplate.opsForZSet().add(redisKey, "悟空", 90);
            redisTemplate.opsForZSet().add(redisKey, "八戒", 50);
            redisTemplate.opsForZSet().add(redisKey, "沙僧", 70);
            redisTemplate.opsForZSet().add(redisKey, "白龙马", 60);
    
            System.out.println(redisTemplate.opsForZSet().zCard(redisKey));
            System.out.println(redisTemplate.opsForZSet().score(redisKey, "八戒"));
            System.out.println(redisTemplate.opsForZSet().reverseRank(redisKey, "八戒"));
            System.out.println(redisTemplate.opsForZSet().reverseRange(redisKey, 0, 2));
        }
    
        @Test
        public void testKeys() {
            redisTemplate.delete("test:user");
    
            System.out.println(redisTemplate.hasKey("test:user"));
    
            redisTemplate.expire("test:students", 10, TimeUnit.SECONDS);
        }
    
        // 多次访问同一个key
        @Test
        public void testBoundOperations() {
            String redisKey = "test:count";
            BoundValueOperations operations = redisTemplate.boundValueOps(redisKey);
            operations.increment();
            operations.increment();
            operations.increment();
            operations.increment();
            operations.increment();
            System.out.println(operations.get());
        }
    
        // 编程式事务
        @Test
        public void testTransactional() {
            Object obj = redisTemplate.execute(new SessionCallback() {
                @Override
                public Object execute(RedisOperations operations) throws DataAccessException {
                    String redisKey = "test:tx";
    
                    operations.multi();
    
                    operations.opsForSet().add(redisKey, "zhangsan");
                    operations.opsForSet().add(redisKey, "lisi");
                    operations.opsForSet().add(redisKey, "wangwu");
    
                    System.out.println(operations.opsForSet().members(redisKey));
    
                    return operations.exec();
                }
            });
            System.out.println(obj);
        }
    
    }
    
    
    • 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
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137

    运行结果

    在这里插入图片描述

    自己定义RedisconnectionFactory

    用来配置登录的地址和登录的端口以及登录密码

    创建RedisConnectionFactory,由他创建RedisConnection。这个工厂会被直接注入到redisTemplate

    官方文档上说使用工厂创建的连接线程安全,并且可以被实例共享。那么就是说,是线程安全的单例模式(个人猜测)。

    1. 导入jedis依赖
    <dependencies>
    
      
    
      <dependency>
        <groupId>redis.clientsgroupId>
        <artifactId>jedisartifactId>
        <version>4.3.1version>
      dependency>
    
    dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    1. 编写配置类,获取连接工厂
    @Configuration
    class RedisConfiguration {
    
      @Bean
      public JedisConnectionFactory redisConnectionFactory() {
    															// 设置域名和端口
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration("server", 6379);
        config.setPassword("fdafda");							// 设置密码
        return new JedisConnectionFactory(config);
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    1. 将工厂创建的连接注入到模板配置中。仍旧是上面的配置类
    package com.fuzekun.demo1.configuration;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.connection.RedisSentinelConfiguration;
    import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
    import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.RedisSerializer;
    
    @Configuration
    public class RedisConfig {
    
    
        /**
         * Jedis
         */
    //    @Bean
    //    public RedisConnectionFactory jedisConnectionFactory() {
    //        RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
    //                .sentinel("node0", 6379);
    //        sentinelConfig.setPassword("1230");
    //        return new JedisConnectionFactory(sentinelConfig);
    //    }
    
        // 创建工厂,配置端口,账号,密码
        @Bean
        public JedisConnectionFactory redisConnectionFactory() {
            RedisStandaloneConfiguration config = new RedisStandaloneConfiguration("node0", 6379);
            config.setPassword("1230");
            return new JedisConnectionFactory(config);
        }
        // 将工厂注入到factory中
        @Bean
        public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
            RedisTemplate<String, Object> template = new RedisTemplate<>();
            template.setConnectionFactory(factory);
    
            // 设置key的序列化方式
            template.setKeySerializer(RedisSerializer.string());
            // 设置value的序列化方式
            template.setValueSerializer(RedisSerializer.json());
            // 设置hash的key的序列化方式
            template.setHashKeySerializer(RedisSerializer.string());
            // 设置hash的value的序列化方式
            template.setHashValueSerializer(RedisSerializer.json());
    
            template.afterPropertiesSet();
            return template;
        }
    
    }
    
    
    • 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
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    关于工厂模式的思考:

    为什么要用工厂模式?

    1. 控制对象创建的工序,方便对象的管理。
    2. 将对象和控制解耦,增加了灵活性,方便对象类的扩展。
    3. 使用配置文件,解决了对象的选择问题。

    为什么使用抽象工厂模式? 定义了产品一族。

  • 相关阅读:
    暴雨服务器:科技创新构建高效、高质、可持续的新质生产力
    PHP+学生成绩管理系统 毕业设计-附源码201829
    思维模型 鸟笼效应
    设计模式
    python中日期转字符串/字符串转日期
    手撕红黑树
    Rust 从入门到精通08-字符串
    开发工具创新升级,鲲鹏推进计算产业“竹林”式生长
    java基于springboot的民宿预约管理平台系统
    2024年【R1快开门式压力容器操作】模拟考试题及R1快开门式压力容器操作复审模拟考试
  • 原文地址:https://blog.csdn.net/fuzekun/article/details/127975070