zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    第五周总结 8.11
    第四周总结 8.2
    第三周总结7.27
    PHP实验四
    PHP实验一
    PHP实验三
    软件工程课程总结
    《梦断代码》阅读笔记03
    找水王
    评价搜狗输入法
  • 原文地址:https://www.cnblogs.com/geekdc/p/10132884.html
Copyright © 2011-2022 走看看