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