├── .gitignore ├── README.md ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── demo │ │ └── redis │ │ ├── DemoRedisApp.java │ │ ├── config │ │ └── RedisConfig.java │ │ ├── pojo │ │ ├── Result.java │ │ ├── ShortMessage.java │ │ ├── SimpleMessage.java │ │ └── User.java │ │ └── service │ │ ├── ConsumerImpl.java │ │ ├── ConsumerImpl2.java │ │ ├── ConsumerImpl3.java │ │ ├── DemoService.java │ │ ├── DemoServiceImpl.java │ │ ├── ProduceService.java │ │ └── ProduceServiceImpl.java └── resources │ └── application.properties └── test └── java └── com └── demo └── redis └── AppTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.ear 17 | *.zip 18 | *.tar.gz 19 | *.rar 20 | 21 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 22 | hs_err_pid* 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # demo-messageQueue-springboot-redis 2 | springboot集成redis做消息队列测试demo 3 | ## 发布者和订阅者模式: 4 | 发布者发送消息到队列,每个订阅者都能收到一样的消息。 5 | 主要使用了redis提供的rightPopAndLeftPush获取队列数据,如果队列没有数据则阻塞等待,也就是监听。 6 | ## 生产者和消费者模式: 7 | 生产者将消息放入队列,多个消费者共同监听,谁先抢到资源,谁就从队列中取走消息去处理。注意,每个消息只能最多被一个消费者接收。 8 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | demo-redis 6 | redis 7 | 0.0.1-SNAPSHOT 8 | jar 9 | 10 | redis 11 | http://maven.apache.org 12 | 13 | 14 | org.springframework.boot 15 | spring-boot-starter-parent 16 | 1.5.3.RELEASE 17 | 18 | 19 | 20 | UTF-8 21 | 22 | 23 | 24 | 25 | junit 26 | junit 27 | test 28 | 29 | 30 | com.alibaba 31 | fastjson 32 | 1.2.41 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-starter 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-starter-test 41 | test 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-starter-data-redis 46 | 47 | 48 | 49 | com.fasterxml.jackson.core 50 | jackson-databind 51 | 52 | 53 | com.fasterxml.jackson.core 54 | jackson-core 55 | 56 | 57 | com.fasterxml.jackson.core 58 | jackson-annotations 59 | 60 | 61 | 62 | 63 | 64 | 65 | org.springframework.boot 66 | spring-boot-maven-plugin 67 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /src/main/java/com/demo/redis/DemoRedisApp.java: -------------------------------------------------------------------------------- 1 | package com.demo.redis; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | 7 | @SpringBootApplication 8 | public class DemoRedisApp 9 | { 10 | public static void main(String[] args) { 11 | SpringApplication.run(DemoRedisApp.class, args); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/demo/redis/config/RedisConfig.java: -------------------------------------------------------------------------------- 1 | package com.demo.redis.config; 2 | 3 | 4 | 5 | import java.lang.reflect.Method; 6 | 7 | import org.springframework.beans.factory.annotation.Qualifier; 8 | import org.springframework.beans.factory.annotation.Value; 9 | import org.springframework.cache.CacheManager; 10 | import org.springframework.cache.annotation.CachingConfigurerSupport; 11 | import org.springframework.cache.annotation.EnableCaching; 12 | import org.springframework.cache.interceptor.KeyGenerator; 13 | import org.springframework.context.annotation.Bean; 14 | import org.springframework.context.annotation.Configuration; 15 | import org.springframework.data.redis.cache.RedisCacheManager; 16 | import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; 17 | import org.springframework.data.redis.core.RedisTemplate; 18 | import org.springframework.data.redis.listener.PatternTopic; 19 | import org.springframework.data.redis.listener.RedisMessageListenerContainer; 20 | import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; 21 | import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; 22 | import org.springframework.data.redis.serializer.StringRedisSerializer; 23 | 24 | import com.fasterxml.jackson.annotation.PropertyAccessor; 25 | import com.demo.redis.service.ConsumerImpl; 26 | import com.demo.redis.service.ConsumerImpl2; 27 | import com.fasterxml.jackson.annotation.JsonAutoDetect; 28 | import com.fasterxml.jackson.databind.ObjectMapper; 29 | 30 | import redis.clients.jedis.JedisPoolConfig; 31 | 32 | @Configuration 33 | @EnableCaching 34 | public class RedisConfig extends CachingConfigurerSupport{ 35 | 36 | @Override 37 | @Bean 38 | public KeyGenerator keyGenerator() { 39 | return new KeyGenerator() { 40 | @Override 41 | public Object generate(Object target, Method method, Object... params) { 42 | StringBuilder sb = new StringBuilder(); 43 | sb.append(target.getClass().getName()); 44 | sb.append(method.getName()); 45 | for (Object obj : params) { 46 | sb.append(obj.toString()); 47 | } 48 | return sb.toString(); 49 | } 50 | }; 51 | } 52 | 53 | @SuppressWarnings("rawtypes") 54 | @Bean 55 | public CacheManager cacheManager(RedisTemplate redisTemplate) { 56 | RedisCacheManager rcm = new RedisCacheManager(redisTemplate); 57 | //设置缓存过期时间 58 | rcm.setDefaultExpiration(60);//秒 59 | return rcm; 60 | } 61 | 62 | @Bean(name="jedisPoolConfig") 63 | JedisPoolConfig jedisPoolConfig( 64 | @Value("${redis.pool.maxTotal}") int maxTotal, 65 | @Value("${redis.pool.maxIdle}") int maxIdle, 66 | @Value("${redis.pool.minIdle}") int minIdle, 67 | @Value("${redis.pool.maxWaitMillis}") int maxWaitMillis, 68 | @Value("${redis.pool.minEvictableIdleTimeMillis}") long minEvictableIdleTimeMillis, 69 | @Value("${redis.pool.numTestsPerEvictionRun}") int numTestsPerEvictionRun, 70 | @Value("${redis.pool.timeBetweenEvictionRunsMillis}") long timeBetweenEvictionRunsMillis 71 | ){ 72 | JedisPoolConfig jpc = new JedisPoolConfig(); 73 | jpc.setMaxTotal(maxTotal); 74 | jpc.setMaxIdle(maxIdle); 75 | jpc.setMinIdle(minIdle); 76 | jpc.setMaxWaitMillis(maxWaitMillis); 77 | jpc.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); 78 | jpc.setNumTestsPerEvictionRun(numTestsPerEvictionRun); 79 | jpc.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); 80 | return jpc; 81 | } 82 | 83 | @Bean(name="jedisConnectionFactory") 84 | JedisConnectionFactory jedisConnectionFactory( 85 | @Qualifier("jedisPoolConfig") JedisPoolConfig poolConfig, 86 | @Value("${redis.timeout}") int timeout, 87 | @Value("${redis.host}") String host, 88 | @Value("${redis.database}") int database, 89 | @Value("${redis.port}") int port 90 | ){ 91 | JedisConnectionFactory jcf = new JedisConnectionFactory(poolConfig); 92 | jcf.setHostName(host); 93 | jcf.setDatabase(database); 94 | jcf.setPort(port); 95 | jcf.setTimeout(timeout); 96 | jcf.setUsePool(true); 97 | return jcf; 98 | } 99 | 100 | @Bean(name="redisTemplate") 101 | RedisTemplate redisTemplate( 102 | @Qualifier("jedisConnectionFactory") JedisConnectionFactory connectionFactory 103 | ){ 104 | RedisTemplate rt = new RedisTemplate(); 105 | rt.setConnectionFactory(connectionFactory); 106 | rt.setKeySerializer(new StringRedisSerializer()); 107 | rt.setHashKeySerializer(new StringRedisSerializer()); 108 | Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); 109 | ObjectMapper om = new ObjectMapper(); 110 | om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); 111 | om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); 112 | jackson2JsonRedisSerializer.setObjectMapper(om); 113 | rt.setValueSerializer(jackson2JsonRedisSerializer); 114 | rt.setHashValueSerializer(jackson2JsonRedisSerializer); 115 | rt.setEnableTransactionSupport(true); 116 | rt.afterPropertiesSet(); 117 | return rt; 118 | } 119 | @Bean 120 | RedisMessageListenerContainer container( 121 | @Qualifier("jedisConnectionFactory") JedisConnectionFactory connectionFactory, 122 | @Value("${redis.channel}") String channel, 123 | //监听方法一 124 | @Qualifier("listenerAdapter") MessageListenerAdapter listenerAdapter, 125 | //监听方法二 126 | @Qualifier("listenerAdapter2") MessageListenerAdapter listenerAdapter2 127 | ) { 128 | 129 | RedisMessageListenerContainer container = new RedisMessageListenerContainer(); 130 | container.setConnectionFactory(connectionFactory); 131 | // container.addMessageListener(listenerAdapter, new PatternTopic(channel)); 132 | // container.addMessageListener(listenerAdapter2, new PatternTopic(channel)); 133 | 134 | return container; 135 | } 136 | @Bean(name="listenerAdapter") 137 | MessageListenerAdapter listenerAdapter(ConsumerImpl consumer) { 138 | return new MessageListenerAdapter(consumer, "onMessage"); 139 | } 140 | @Bean(name="listenerAdapter2") 141 | MessageListenerAdapter listenerAdapter2(ConsumerImpl2 consumer) { 142 | return new MessageListenerAdapter(consumer, "onMessage"); 143 | } 144 | 145 | } 146 | -------------------------------------------------------------------------------- /src/main/java/com/demo/redis/pojo/Result.java: -------------------------------------------------------------------------------- 1 | package com.demo.redis.pojo; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Result implements Serializable{ 6 | 7 | /** 8 | * @Fields serialVersionUID : TODO 9 | */ 10 | private static final long serialVersionUID = 7442924209132197855L; 11 | private int code; 12 | private boolean result; 13 | private String message; 14 | private String error; 15 | private Object data; 16 | public int getCode() { 17 | return code; 18 | } 19 | public void setCode(int code) { 20 | this.code = code; 21 | } 22 | public boolean isResult() { 23 | return result; 24 | } 25 | public void setResult(boolean result) { 26 | this.result = result; 27 | } 28 | public String getMessage() { 29 | return message; 30 | } 31 | public void setMessage(String message) { 32 | this.message = message; 33 | } 34 | public String getError() { 35 | return error; 36 | } 37 | public void setError(String error) { 38 | this.error = error; 39 | } 40 | public Object getData() { 41 | return data; 42 | } 43 | public void setData(Object data) { 44 | this.data = data; 45 | } 46 | 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/demo/redis/pojo/ShortMessage.java: -------------------------------------------------------------------------------- 1 | package com.demo.redis.pojo; 2 | 3 | import java.io.Serializable; 4 | 5 | public class ShortMessage implements Serializable{ 6 | 7 | /** 8 | * @Fields serialVersionUID : TODO 9 | */ 10 | private static final long serialVersionUID = -55006871872797947L; 11 | private String message; 12 | private String ie; 13 | private int type; 14 | private int num; 15 | 16 | public String getMessage() { 17 | return message; 18 | } 19 | public void setMessage(String message) { 20 | this.message = message; 21 | } 22 | public int getType() { 23 | return type; 24 | } 25 | public void setType(int type) { 26 | this.type = type; 27 | } 28 | public String getIe() { 29 | return ie; 30 | } 31 | public void setIe(String ie) { 32 | this.ie = ie; 33 | } 34 | public int getNum() { 35 | return num; 36 | } 37 | public void setNum(int num) { 38 | this.num = num; 39 | } 40 | 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/demo/redis/pojo/SimpleMessage.java: -------------------------------------------------------------------------------- 1 | package com.demo.redis.pojo; 2 | 3 | import java.io.Serializable; 4 | 5 | public class SimpleMessage implements Serializable{ 6 | 7 | /** 8 | * @Fields serialVersionUID : TODO 9 | */ 10 | private static final long serialVersionUID = 4993548158627952034L; 11 | private String message; 12 | private int type; 13 | 14 | public String getMessage() { 15 | return message; 16 | } 17 | public void setMessage(String message) { 18 | this.message = message; 19 | } 20 | public int getType() { 21 | return type; 22 | } 23 | public void setType(int type) { 24 | this.type = type; 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/demo/redis/pojo/User.java: -------------------------------------------------------------------------------- 1 | package com.demo.redis.pojo; 2 | 3 | import java.io.Serializable; 4 | import java.util.Random; 5 | 6 | public class User implements Serializable { 7 | 8 | /** 9 | * @Fields serialVersionUID : TODO 10 | */ 11 | private static final long serialVersionUID = -5663893354640565613L; 12 | private long id; 13 | private String name; 14 | private int age; 15 | 16 | public User(){ 17 | 18 | } 19 | public User(long id,String name,int age){ 20 | this.id = id; 21 | this.name = name; 22 | this.age = age; 23 | } 24 | 25 | public User(long id){ 26 | this.id = id; 27 | this.name = id+"随机"; 28 | this.age = (new Random()).nextInt((int)id); 29 | } 30 | 31 | public long getId() { 32 | return id; 33 | } 34 | public void setId(long id) { 35 | this.id = id; 36 | } 37 | public String getName() { 38 | return name; 39 | } 40 | public void setName(String name) { 41 | this.name = name; 42 | } 43 | public int getAge() { 44 | return age; 45 | } 46 | public void setAge(int age) { 47 | this.age = age; 48 | } 49 | @Override 50 | public String toString() { 51 | StringBuilder sb = new StringBuilder(); 52 | sb.append("["); 53 | sb.append("id=\""); 54 | sb.append(this.id); 55 | sb.append("\",name=\""); 56 | sb.append(this.name); 57 | sb.append("\",age=\""); 58 | sb.append(this.age); 59 | sb.append("\"]"); 60 | return sb.toString(); 61 | } 62 | 63 | 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/com/demo/redis/service/ConsumerImpl.java: -------------------------------------------------------------------------------- 1 | package com.demo.redis.service; 2 | 3 | 4 | import org.slf4j.Logger; 5 | import org.slf4j.LoggerFactory; 6 | import org.springframework.data.redis.connection.Message; 7 | import org.springframework.data.redis.connection.MessageListener; 8 | import org.springframework.stereotype.Service; 9 | 10 | import com.alibaba.fastjson.JSON; 11 | import com.demo.redis.pojo.User; 12 | 13 | @Service 14 | public class ConsumerImpl implements MessageListener{ 15 | 16 | private static final Logger logger = LoggerFactory.getLogger(ConsumerImpl.class); 17 | 18 | @Override 19 | public void onMessage(Message message, byte[] pattern) { 20 | try { 21 | logger.info("接收到消息---------------------------"); 22 | logger.info(JSON.toJSONString(message)); 23 | logger.info(new String(pattern,"utf-8")); 24 | byte[] msg= message.getBody(); 25 | String obj = new String(msg,"utf-8"); 26 | logger.info(obj); 27 | String userStr = obj.substring(obj.indexOf(",")+1,obj.length()-1); 28 | logger.info(userStr); 29 | User user=JSON.parseObject(userStr, User.class ); 30 | logger.info(user.toString()); 31 | } catch (Exception e) { 32 | logger.info("接收消息异常-------------------"); 33 | logger.info(e.getMessage()); 34 | } 35 | 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/demo/redis/service/ConsumerImpl2.java: -------------------------------------------------------------------------------- 1 | package com.demo.redis.service; 2 | 3 | 4 | import java.io.Serializable; 5 | 6 | import org.slf4j.Logger; 7 | import org.slf4j.LoggerFactory; 8 | import org.springframework.stereotype.Service; 9 | 10 | import com.alibaba.fastjson.JSON; 11 | import com.demo.redis.pojo.User; 12 | 13 | @Service 14 | public class ConsumerImpl2{ 15 | 16 | private static final Logger logger = LoggerFactory.getLogger(ConsumerImpl2.class); 17 | 18 | public void onMessage(String message) { 19 | //发布订阅模式 20 | //发送消息是发送的是user对象转换成的字符串String 21 | try { 22 | logger.info("接收到消息---------------------------"); 23 | logger.info("message:"+message); 24 | message = message.replace("\\", ""); 25 | message = message.substring(1, message.length()-1); 26 | logger.info("message:"+message); 27 | //很多时候都执行不到下面的代码就结束了 28 | User user2=JSON.parseObject(message,User.class ); 29 | logger.info("user2:"+user2.toString()); 30 | } catch (Exception e) { 31 | logger.info("接收消息异常-------------------"); 32 | logger.info(e.getMessage()); 33 | } 34 | 35 | } 36 | 37 | public void onMessage1(Serializable message) { 38 | //发布订阅模式 39 | //发送消息是发送的是User对象 40 | try { 41 | logger.info("接收到消息---------------------------"); 42 | String obj = message.toString(); 43 | logger.info(obj); 44 | String userStr = obj.substring(obj.indexOf(",")+1,obj.length()-1); 45 | logger.info(userStr); 46 | User user=JSON.parseObject(userStr,User.class ); 47 | logger.info(user.toString()); 48 | } catch (Exception e) { 49 | logger.info("接收消息异常-------------------"); 50 | logger.info(e.getMessage()); 51 | } 52 | 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/com/demo/redis/service/ConsumerImpl3.java: -------------------------------------------------------------------------------- 1 | package com.demo.redis.service; 2 | 3 | 4 | import java.io.Serializable; 5 | import java.util.concurrent.TimeUnit; 6 | 7 | import org.slf4j.Logger; 8 | import org.slf4j.LoggerFactory; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.beans.factory.annotation.Value; 11 | import org.springframework.data.redis.core.RedisTemplate; 12 | import org.springframework.stereotype.Service; 13 | 14 | import com.alibaba.fastjson.JSON; 15 | import com.demo.redis.pojo.User; 16 | 17 | @Service 18 | public class ConsumerImpl3{ 19 | 20 | private static final Logger logger = LoggerFactory.getLogger(ConsumerImpl3.class); 21 | 22 | @Autowired 23 | RedisTemplate redisTemplate; 24 | @Value("${redis.lpush.tmp.key}") 25 | String lpushTempKey; 26 | @Value("${redis.lpush.key}") 27 | String lpushKey; 28 | 29 | public void onMessage() { 30 | //生产者消费者模式 31 | //一直发送请求 32 | while(true){ 33 | //一直请求 34 | // Object obj = redisTemplate.opsForList().rightPopAndLeftPush(lpushKey, lpushTempKey); 35 | 36 | //每十秒请求一次 37 | Object obj = redisTemplate.opsForList().rightPopAndLeftPush(lpushKey, lpushTempKey, 10, TimeUnit.SECONDS); 38 | 39 | logger.info("3--------------消息处理---------------------"); 40 | //存在数据 41 | if(null!=obj){ 42 | logger.info("ConsumerImpl3消息处理---------------------"); 43 | String string = obj.toString(); 44 | logger.info("message:"+string); 45 | 46 | boolean succ = true; 47 | if(succ){ 48 | //处理成功 49 | redisTemplate.opsForList().rightPop(lpushTempKey); 50 | logger.info("success"); 51 | }else{ 52 | //处理失败 53 | redisTemplate.opsForList().rightPopAndLeftPush(lpushTempKey, lpushKey); 54 | logger.info("fail,弹回任务队列"); 55 | } 56 | } 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/demo/redis/service/DemoService.java: -------------------------------------------------------------------------------- 1 | package com.demo.redis.service; 2 | 3 | 4 | import com.demo.redis.pojo.User; 5 | 6 | public interface DemoService { 7 | 8 | public User findUser(long id); 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/demo/redis/service/DemoServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.demo.redis.service; 2 | 3 | import org.springframework.cache.annotation.Cacheable; 4 | import org.springframework.stereotype.Service; 5 | 6 | import com.demo.redis.pojo.User; 7 | 8 | @Service 9 | public class DemoServiceImpl implements DemoService { 10 | 11 | @Override 12 | @Cacheable(value = "usercache",keyGenerator = "keyGenerator") 13 | public User findUser(long id){ 14 | System.out.println("findUser------无缓存的时候调用这里"); 15 | return new User(id); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/demo/redis/service/ProduceService.java: -------------------------------------------------------------------------------- 1 | package com.demo.redis.service; 2 | 3 | import com.demo.redis.pojo.User; 4 | 5 | public interface ProduceService { 6 | 7 | public void produceMsg(User user); 8 | public void produceStr(String json); 9 | public void lpush(User user); 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/demo/redis/service/ProduceServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.demo.redis.service; 2 | 3 | import java.util.concurrent.CountDownLatch; 4 | 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.beans.factory.annotation.Value; 9 | import org.springframework.data.redis.core.RedisTemplate; 10 | import org.springframework.stereotype.Service; 11 | 12 | import com.demo.redis.pojo.User; 13 | 14 | @Service 15 | public class ProduceServiceImpl implements ProduceService { 16 | 17 | private static final Logger logger = LoggerFactory.getLogger(ProduceServiceImpl.class); 18 | 19 | 20 | @Autowired 21 | RedisTemplate redisTemplate; 22 | 23 | @Value("${redis.channel}") 24 | String channel; 25 | @Value("${redis.lpush.key}") 26 | String lpushKey; 27 | 28 | @Override 29 | public void produceMsg(User user) { 30 | redisTemplate.convertAndSend(channel, user);//发布订阅模式 31 | } 32 | 33 | @Override 34 | public void lpush(User user) { 35 | redisTemplate.opsForList().leftPush(lpushKey, user); 36 | } 37 | 38 | @Override 39 | public void produceStr(String json) { 40 | redisTemplate.convertAndSend(channel, json); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #redis做消息队列测试demo 2 | spring.application.name=demo-redis 3 | 4 | redis.host=localhost 5 | redis.port=6379 6 | redis.database=1 7 | redis.password= 8 | redis.timeout=15000 9 | redis.pool.maxTotal=8 10 | redis.pool.maxIdle=8 11 | redis.pool.minIdle=0 12 | redis.pool.maxWaitMillis=10000 13 | redis.pool.minEvictableIdleTimeMillis=300000 14 | redis.pool.numTestsPerEvictionRun=3 15 | redis.pool.timeBetweenEvictionRunsMillis=60000 16 | redis.pool.maxActive=8 17 | redis.pool.maxWait=-1 18 | #哨兵 19 | #redis.sentinel.master= 20 | #哨兵的配置列表 21 | #redis.sentinel.nodes= 22 | redis.channel="channel" 23 | redis.lpush.key="lpushList" 24 | redis.lpush.tmp.key="lpushTempList" 25 | -------------------------------------------------------------------------------- /src/test/java/com/demo/redis/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.demo.redis; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.test.context.SpringBootTest; 7 | import org.springframework.test.context.junit4.SpringRunner; 8 | 9 | import com.alibaba.fastjson.JSON; 10 | import com.alibaba.fastjson.JSONObject; 11 | import com.demo.redis.pojo.User; 12 | import com.demo.redis.service.ConsumerImpl3; 13 | import com.demo.redis.service.DemoService; 14 | import com.demo.redis.service.ProduceService; 15 | 16 | @RunWith(SpringRunner.class) 17 | @SpringBootTest(classes=DemoRedisApp.class) 18 | public class AppTest { 19 | 20 | @Autowired 21 | ProduceService pro; 22 | @Autowired 23 | ConsumerImpl3 consumer; 24 | @Autowired 25 | DemoService demo; 26 | 27 | @Test 28 | public void rpop(){ 29 | consumer.onMessage(); 30 | } 31 | 32 | @Test 33 | public void lpush(){ 34 | User user = new User(20); 35 | String json = JSON.toJSONString(user); 36 | System.out.println(json); 37 | pro.lpush(user); 38 | } 39 | 40 | @Test 41 | public void produceStr(){ 42 | User user = new User(20); 43 | String json = JSON.toJSONString(user); 44 | System.out.println(json); 45 | pro.produceStr(json); 46 | } 47 | 48 | @Test 49 | public void produceUser(){ 50 | User user = new User(20); 51 | out(user); 52 | pro.produceMsg(user); 53 | } 54 | 55 | @Test 56 | public void cache(){ 57 | out(demo.findUser(13)); 58 | out(demo.findUser(15)); 59 | } 60 | 61 | 62 | 63 | public void out(Object object){ 64 | System.out.println("***********************结果***************************"); 65 | System.out.println(JSONObject.toJSONString(object)); 66 | } 67 | } 68 | --------------------------------------------------------------------------------