• springboot整合redis单机及集群


    一、单机配置

    properties配置

    #单机redis
    spring.redis.host=127.0.0.1
    spring.redis.port=6379
    spring.redis.password=redis

    启动类加

    @EnableCaching

    具体的方法上加

    @Cacheable(value="userList")

    这样的话,redis 中key值即为userList,value 为方法的返回值。pojo可能会需要序列化。

    二、集群配置

    properties配置

    #集群redis节点以及端口
    #spring.redis.cluster.nodes=192.168.1.127:7550,192.168.1.127:7551,192.168.1.127:7552,192.168.1.127:7553

    RedisClusterConfig

    package com.dc.sb.web.redis;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import redis.clients.jedis.HostAndPort;
    import redis.clients.jedis.JedisCluster;
    
    import java.util.HashSet;
    import java.util.Set;
    
    /**
     * redis 集群配置类
     * 需要时直接   @Autowired JedisCluster
     * @author DUCHONG
     * @since 2018-12-17 17:28
     **/
    @Configuration
    public class RedisClusterConfig {
    
        @Value("spring.redis.cluster.nodes")
        private String redisNodes;
    
        @Bean
        public JedisCluster getJedisCluster(){
    
            Set<HostAndPort> nodes=new HashSet<HostAndPort>() ;
    
            String [] redisnodes=redisNodes.split(",");
            for (String redisnode : redisnodes) {
    
                String [] arr=redisnode.split(":");
                HostAndPort hostAndPort=new HostAndPort(arr[0],Integer.parseInt(arr[1]));
                nodes.add(hostAndPort);
            }
    
            return new JedisCluster(nodes);
        }
    }

    使用时直接

    @Autowired 
    private JedisCluster jedisCluster
  • 相关阅读:
    【机器学习】算法原理详细推导与实现(一):线性回归
    《0~3岁孩子的 正面管教》——备忘
    马歇尔·卢森堡《非暴力沟通》——备忘
    李笑来《财富自由之路》——备忘
    select、poll、epoll之间的区别总结[整理]
    堆和栈区别
    Linux 文件系统剖析
    Inside The C++ Object Model(五)
    Inside The C++ Object Model(四)
    Inside The C++ Object Model(三)
  • 原文地址:https://www.cnblogs.com/geekdc/p/10132884.html
Copyright © 2020-2023  润新知