美文网首页
2019-01-03 秒杀接口优化-springboot集成r

2019-01-03 秒杀接口优化-springboot集成r

作者: 培根好吃 | 来源:发表于2019-01-03 21:54 被阅读0次

1.添加依赖 配置文件

<dependency>
       <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-amqp</artifactId>
   </dependency>



2. 改linux里面rabbitmq的配置 rabbitmq /etc/rabbitmq 里面 vi rabbitmq.config。再然后重启rabbitmq


[{rabbit,[{loopback_users,[]}]}].

2.

@Configuration
public class MQConfig {
   public final static String QUEUE="queue";
   @Bean
   public Queue queue() {
       return new Queue(QUEUE,true);
   }
}


@Service
public class MQSender {
   private static Logger logger=LoggerFactory.getLogger(MQReceiver.class);
   @Autowired
   AmqpTemplate amqpTemplate;
   public void send(Object message) {
       String beanToString = RedisService.beanToString(message);
       logger.info("send message:"+beanToString);
       amqpTemplate.convertAndSend(MQConfig.QUEUE,beanToString);
   }
}



@Service
public class MQReceiver {

   private static Logger logger=LoggerFactory.getLogger(MQReceiver.class);
   @RabbitListener(queues=MQConfig.QUEUE)
   public void receive(String message) {
       logger.info("receive message:"+message);
   }
}


@RequestMapping("/mq")
   @ResponseBody
   Result<String> rabbit(){
       sender.send("hello mq");
       return Result.success("hello world");
   }

相关文章

网友评论

      本文标题:2019-01-03 秒杀接口优化-springboot集成r

      本文链接:https://www.haomeiwen.com/subject/unnqrqtx.html