zoukankan      html  css  js  c++  java
  • spring boot集成redis

    package com.qmtt.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.Message;
    import org.springframework.data.redis.connection.MessageListener;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.data.redis.listener.ChannelTopic;
    import org.springframework.data.redis.listener.RedisMessageListenerContainer;
    import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
    
    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    @Configuration
    public class RedisConfig {
        @Bean
        public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
            StringRedisTemplate template = new StringRedisTemplate(factory);
            setSerializer(template);// 设置序列化工具
            template.afterPropertiesSet();
            return template;
        }
    
        private void setSerializer(StringRedisTemplate template) {
            @SuppressWarnings({ "rawtypes", "unchecked" })
            Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
            ObjectMapper om = new ObjectMapper();
            om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
            om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
            jackson2JsonRedisSerializer.setObjectMapper(om);
            template.setValueSerializer(jackson2JsonRedisSerializer);
        }
    
        // 订阅pubsub:queue
        @Bean
        RedisMessageListenerContainer redisContainer(RedisConnectionFactory factory) {
            final RedisMessageListenerContainer container = new RedisMessageListenerContainer();
            container.setConnectionFactory(factory);
            MessageListenerAdapter listener = new MessageListenerAdapter(new RedisMessageListener());
            container.addMessageListener(listener, new ChannelTopic("pubsub:queue"));
            return container;
        }
    
        public class RedisMessageListener implements MessageListener {
            @Override
            public void onMessage(final Message message, final byte[] pattern) {
                System.out.println("Message received: " + message.toString());
            }
        }
    }
    #redis相关配置
    # Redis数据库索引(默认为0)
    spring.redis.database=0
    # Redis服务器地址
    spring.redis.host=127.0.0.1
    # Redis服务器连接端口
    spring.redis.port=6379
    # Redis服务器连接密码(默认为空)
    spring.redis.password=111
    # 连接池最大连接数(使用负值表示没有限制)
    spring.redis.pool.max-active=100
    # 连接池最大阻塞等待时间(使用负值表示没有限制)
    spring.redis.pool.max-wait=-1
    # 连接池中的最大空闲连接
    spring.redis.pool.max-idle=20
    # 连接池中的最小空闲连接
    spring.redis.pool.min-idle=10
    # 连接超时时间(毫秒)
    spring.redis.timeout=0

    有追求,才有动力!

    向每一个软件工程师致敬!

    by wujf

    mail:921252375@qq.com

  • 相关阅读:
    看了陈安之的文字 无论怎样 都要记住的是 你仍然是你自己 改变是应用他人的方法提高自己 改变是做更优秀更独特的自己
    流行的Ajax应用演示和源码下载(转)
    WEB2.0概念诠释(根据网络资料归纳)之一
    ASP.NET中文件上传下载方法集合(较为详细的介绍 转)
    建立梦想清单
    Ajax的应用
    Ajax的问题
    WEB2.0概念诠释(根据网络资料归纳)之三
    IIS突然挂掉
    VS2008软件90天过期,解决升级问题。
  • 原文地址:https://www.cnblogs.com/wujf/p/8473892.html
Copyright © 2011-2022 走看看