Spring Boot Integrated Redis

Source: Internet
Author: User
Tags propertyaccessor redis server

 PackageCom.qmtt.config;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;ImportOrg.springframework.data.redis.connection.Message;ImportOrg.springframework.data.redis.connection.MessageListener;Importorg.springframework.data.redis.connection.RedisConnectionFactory;Importorg.springframework.data.redis.core.RedisTemplate;Importorg.springframework.data.redis.core.StringRedisTemplate;ImportOrg.springframework.data.redis.listener.ChannelTopic;ImportOrg.springframework.data.redis.listener.RedisMessageListenerContainer;ImportOrg.springframework.data.redis.listener.adapter.MessageListenerAdapter;ImportOrg.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;ImportCom.fasterxml.jackson.annotation.JsonAutoDetect;ImportCom.fasterxml.jackson.annotation.PropertyAccessor;Importcom.fasterxml.jackson.databind.ObjectMapper; @Configuration Public classRedisconfig {@Bean PublicRedistemplate<string, string>redistemplate (Redisconnectionfactory Factory) {stringredistemplate template=Newstringredistemplate (Factory); Setserializer (template);//setting up the serialization toolTemplate.afterpropertiesset (); returntemplate; }    Private voidSetserializer (stringredistemplate template) {@SuppressWarnings ({"Rawtypes", "unchecked"}) Jackson2jsonredisserializer Jackson2jsonredisserializer=NewJackson2jsonredisserializer (Object.class); Objectmapper om=NewObjectmapper ();        Om.setvisibility (Propertyaccessor.all, JsonAutoDetect.Visibility.ANY);        Om.enabledefaulttyping (ObjectMapper.DefaultTyping.NON_FINAL);        Jackson2jsonredisserializer.setobjectmapper (OM);    Template.setvalueserializer (Jackson2jsonredisserializer); }    //Subscribe to Pubsub:queue@Bean Redismessagelistenercontainer Rediscontainer (Redisconnectionfactory factory) {FinalRedismessagelistenercontainer container =NewRedismessagelistenercontainer ();        Container.setconnectionfactory (Factory); Messagelisteneradapter Listener=NewMessagelisteneradapter (NewRedismessagelistener ()); Container.addmessagelistener (Listener,NewChanneltopic ("Pubsub:queue")); returncontainer; }     Public classRedismessagelistenerImplementsMessageListener {@Override Public voidOnMessage (FinalMessage message,Final byte[] pattern) {System.out.println ("Message Received:" +message.tostring ()); }    }}

#redis相关配置 # Redis Database index (default 0) spring.redis.database=0# Redis server address spring.redis.host=127.0.0.1  # Redis Server connection Port Spring.redis.port=6379# Redis Server connection password (default is empty) Spring.redis.password=111# Connection pool Maximum number of connections (using negative values for No limit) Spring.redis.pool.max-active=100# Connection pool maximum blocking wait time (using negative values means there is no limit) Spring.redis.pool.max -wait=-1# Maximum idle connection in connection pool Spring.redis.pool.max-idle=20# Minimum idle connection in connection pool spring.redis.pool.min-idle =10# Connection time-out (ms) Spring.redis.timeout=0

Spring Boot Integrated Redis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.