《SpringBoot2.X心法總綱》redis
我以爲redis消息隊列不太好,雖然有消息隊列的功能,也能作延遲,可是不建議使用redis作消息隊列。spring
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
spring.redis.database=15 spring.redis.host=localhost spring.redis.port=6379 spring.redis.password= spring.redis.timeout=10000ms
/** * @Author:MuJiuTian * @Description: 全局topicname的定義 * @Date: Created in 上午10:54 2019/9/26 */ public class TopicName { public static String topic_name_test = "loving"; }
@Service public class PublishService { @Autowired StringRedisTemplate redisTemplate; public void sendMessage(String channel, Object message){ redisTemplate.convertAndSend(channel, message); } }
/** * @Author:MuJiuTian * @Description: 測試redis消息隊列 * @Date: Created in 下午1:49 2019/9/10 */ @RestController @RequestMapping(value = "/mq") public class RedisMqController { @Autowired PublishService publishService; @GetMapping(value = "/sendMessage") public String sendMessage(){ publishService.sendMessage(TopicName.topic_name_test,"i love you aoxin"); return "SUCCESS"; } }
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.listener.PatternTopic; import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; @Configuration @AutoConfigureAfter({Receiver.class}) public class SubscriberConfig { /** * 綁定消息監聽者和接收監聽的方法 */ @Bean public MessageListenerAdapter listenerAdapter(Receiver receiver){ //若是使用此方法,那麼Receiver則 implements MessageListener return new MessageListenerAdapter(receiver); // 若是使用此方法,那麼Receiver則 不用implements MessageListener,能夠自定義方法處理消息,下面的第二個參數爲方法名 //return new MessageListenerAdapter(receiver,"receiveMessage"); } /** * 建立消息監聽容器 */ @Bean public RedisMessageListenerContainer getRedisMessageListenerContainer(RedisConnectionFactory redisConnectionFactory, MessageListenerAdapter messageListenerAdapter) { RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer(); redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory); redisMessageListenerContainer.addMessageListener(messageListenerAdapter, new PatternTopic(TopicName.topic_name_test)); return redisMessageListenerContainer; } }
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.connection.Message; import org.springframework.data.redis.connection.MessageListener; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.stereotype.Component; /** * @Author:MuJiuTian * @Description: 消息接受而後處理 * @Date: Created in 下午1:57 2019/9/10 */ @Component public class Receiver implements MessageListener{ @Autowired StringRedisTemplate redisTemplate; /* public void receiveMessage(String message) { System.out.println(message); }*/ @Override public void onMessage(Message message, byte[] bytes) { RedisSerializer<String> valueSerializer = redisTemplate.getStringSerializer(); String deserialize = valueSerializer.deserialize(message.getBody()); System.out.println(deserialize); } }