温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

用redis做消息队列有用吗

发布时间:2021-06-23 10:23:31 来源:亿速云 阅读:182 作者:chen 栏目:大数据

本篇内容主要讲解“用redis做消息队列有用吗”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“用redis做消息队列有用吗”吧!

      我觉得redis消息队列不太好,虽然有消息队列的功能,也能做延迟,但是不建议使用redis做消息队列。

1、pom文件
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、application.properties
spring.redis.database=15
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
spring.redis.timeout=10000ms
3、实体类
/**
 * @Author:MuJiuTian
 * @Description: 全局topicname的定义
 * @Date: Created in 上午10:54 2019/9/26
 */
public class TopicName {

    public static String topic_name_test = "loving";
}
4、Service层(生产者)
@Service
public class PublishService {

    @Autowired
    StringRedisTemplate redisTemplate;

    public void sendMessage(String channel, Object message){
        redisTemplate.convertAndSend(channel, message);
    }
}
5、Controller层
/**
 * @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";
    }
}
6、配置类
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;
    }
}
7、消费者
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);
    }
}
8、测试

用redis做消息队列有用吗

用redis做消息队列有用吗

到此,相信大家对“用redis做消息队列有用吗”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI