• java项目集成2个redis


    一、集成redisjson

    1、引入pom文件。

     
        <dependency>
             <groupId>redis.clientsgroupId>
             <artifactId>jedisartifactId>
         dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2、编写配置类,直接上代码。

    import lombok.extern.log4j.Log4j2;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import redis.clients.jedis.DefaultJedisClientConfig;
    import redis.clients.jedis.HostAndPort;
    import redis.clients.jedis.UnifiedJedis;
    import redis.clients.jedis.providers.PooledConnectionProvider;
    
    /**
     * @ClassName 新版本redisjson配置
     * @Author will
     */
    @Log4j2
    @Configuration
    public class JedisConfig {
    
        @Value("${spring.redis.host}")
        private String host;
    
        @Value("${spring.redis.port}")
        private int port;
    
        @Value("${spring.redis.password}")
        private String password;
        @Value("${spring.redis.timeout}")
        private int timeout;
    
        @Bean
        public UnifiedJedis  unifiedJedis(){
            HostAndPort hostAndPort = new HostAndPort(host, port);
            DefaultJedisClientConfig jedisClientConfig = DefaultJedisClientConfig.builder()
                    .timeoutMillis(timeout)
                    .build();
            //UnifiedJedis unifiedJedis = new UnifiedJedis(hostAndPort, jedisClientConfig);
            PooledConnectionProvider provider = new PooledConnectionProvider(hostAndPort);
            UnifiedJedis unifiedJedis = new UnifiedJedis(provider);
            return unifiedJedis;
        }
    }
    
    
    • 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

    3、配置yml文件。

    spring:
     redis:
       port: 6379 #端口号
       password:  #输入redis数据库密码
       host: 192.168.XXX.XXX     #输入ip地址
       timeout: 10000
       jedis:
         pool:
           max-wait: -1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    二、集成另外一个版本redis

    1、pom文件就是上面的。

    2、配置类。

    @Log4j2
    @Configuration
    public class RedisClusterConfig {
    
        @Value("${redis.nodes}")
        private String nodes; //集群节点
    
        @Value("${redis.timeout}")
        private int timeout; //超时时间
    
        @Value("${redis.maxAttempts}")
        private int maxAttempts; //重试次数
    
        @Value("${redis.userName}")
        private String userName;
    
        @Value("${redis.password}")
        private String password;
    
        @Bean
        public JedisCluster jedisCluster(){
            Set nodesHostAndPort = new LinkedHashSet();
            String [] nodeArr = nodes.split(",");
            for(String node : nodeArr) {
                String ip = node.split(":")[0];
                Integer port = Integer.parseInt(node.split(":")[1]);
                nodesHostAndPort.add(new HostAndPort(ip, port));
            }
    
            DefaultJedisClientConfig.Builder builder = DefaultJedisClientConfig.builder().timeoutMillis(timeout);
            if(StringUtils.isNotEmpty(userName)){
                builder.user(userName);
            }
            if(StringUtils.isNotEmpty(password)){
                builder.password(password);
            }
            JedisClientConfig clientConfig =  builder.build();
            JedisCluster cluster = new JedisCluster(nodesHostAndPort, 300, 3);
            return cluster;
        }
    }
    
    • 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

    3、配置文件。

    # redis集群配置
    # 集群地址
    redis.nodes = 192.168.10.XXX:6381,192.168.10.XXX:6382,192.168.10.XXX:6383
    # 连接超时时间(毫秒)
    redis.timeout = 300
    # 超时重试次数
    redis.maxAttempts = 3
    redis.userName =
    redis.password =
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    4.测试

    @RunWith(SpringRunner.class)
    @Log4j2
    @SpringBootTest(classes = SdeApplication.class)
    public class RedisTest {
    
        @Resource
        private RedisService redisService;  //redisjson
    
        @Resource
        private JedisCluster jedisCluster;  //老版本的集群redis
    
        @Test
        public void testRedis() {
            //测试redisjson
            redisService.setByJson("ResourceState",2);
            Integer resourceState = redisService.getByJson("ResourceState", Integer.class);
            log.info("resourceState="+resourceState);
    
            //测试老版本的集群redis
            jedisCluster.set("test-one","测试");
            String name = jedisCluster.get("test-one");
            log.info("test-one:"+name);
            jedisCluster.del("test-one");
            name = jedisCluster.get("test-one");
            log.info("删除后test-one:"+name);
        }
    
    }
    
    • 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
  • 相关阅读:
    LeetCode ——160. 相交链表,142. 环形链表 II
    渗透测试与HTTP中的PUT请求
    Linux项目自动化构建工具-make/Makefile
    Daffodil for Visual Studio
    图像压缩(1)RGB888与RGB565图像
    怎样录屏没有外界杂音?3个十分好用的方法,码住收藏!
    ES6解析赋值
    漫谈:C语言 C++ 所有编程语言 =和==的麻烦
    Lambda 在集合中的应用
    使用YOLOv5的backbone网络识别图像天气 - P9
  • 原文地址:https://blog.csdn.net/wuyongde0922/article/details/126784447