ToB企服应用市场:ToB评测及商务社交产业平台

标题: SpringBoot整合RabbitMQ [打印本页]

作者: 滴水恩情    时间: 2022-9-16 17:16
标题: SpringBoot整合RabbitMQ
  1. <dependencies>
  2.     <dependency>
  3.         <groupId>org.springframework.boot</groupId>
  4.         <artifactId>spring-boot-starter-actuator</artifactId>
  5.     </dependency>
  6.     <dependency>
  7.         <groupId>org.springframework.cloud</groupId>
  8.         <artifactId>spring-cloud-starter-bus-amqp</artifactId>
  9.     </dependency>
  10.     <dependency>
  11.         <groupId>com.alibaba</groupId>
  12.         <artifactId>fastjson</artifactId>
  13.     </dependency>
  14. </dependencies>
复制代码
  1. import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
  2. import org.springframework.amqp.support.converter.MessageConverter;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. @Configuration
  6. public class RabbitConfig {
  7.     /**
  8.      * 配置消息转换器,默认是字符串转换器
  9.      * @return MessageConverter
  10.      */
  11.     @Bean
  12.     public MessageConverter messageConverter() {
  13.         return new Jackson2JsonMessageConverter();
  14.     }
  15. }
复制代码
  1. public class RabbitConstant {
  2.     /** 短信发送 */
  3.     public static final String EXCHANGE_DIRECT_MSM = "exchange.direct.msm";
  4.     public static final String ROUTING_MSM = "msm";
  5.     public static final String QUEUE_MSM = "queue.msm";
  6. }
复制代码
  1. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Service;
  4. @Service
  5. public class RabbitService {
  6.     @Autowired
  7.     private RabbitTemplate rabbitTemplate;
  8.     /**
  9.      * 生产消息到RabbitMQ中
  10.      * @param exchange 交换机
  11.      * @param routingKey 路由Key
  12.      * @param message 任意类型的消息
  13.      * @return boolean
  14.      */
  15.     public boolean sendMessage(String exchange, String routingKey, Object message) {
  16.         rabbitTemplate.convertAndSend(exchange, routingKey, message);
  17.         return true;
  18.     }
  19. }
复制代码
  1. #rabbitmq地址
  2. spring.rabbitmq.host=your_ip
  3. spring.rabbitmq.port=5672
  4. spring.rabbitmq.username=your_username
  5. spring.rabbitmq.password=your_password
复制代码
  1. @Service
  2. public class MsmServiceImpl implements MsmService {
  3.     @Override
  4.     public boolean orderConfirm(Map<String, Object> param) {
  5.         if (StringUtils.isEmpty(param.get("phone"))) return false;
  6.         // 整合阿里云短信服务,设置相关参数
  7.         DefaultProfile profile = DefaultProfile.
  8.                 getProfile(ConstantPropertiesUtils.REGION_ID,
  9.                         ConstantPropertiesUtils.ACCESS_KEY,
  10.                         ConstantPropertiesUtils.ACCESS_SECRET);
  11.         IAcsClient client = new DefaultAcsClient(profile);
  12.         
  13.         SendSmsRequest request = new SendSmsRequest();
  14.         request.setPhoneNumbers(param.get("phone"));//接收短信的手机号码
  15.         request.setSignName(ConstantPropertiesUtils.SIGN_NAME);//短信签名名称
  16.         request.setTemplateCode(ConstantPropertiesUtils.TEMPLATE_CODE);//短信模板CODE
  17.         
  18.         // 使用json格式   {"msg":"下单成功"}
  19.         request.setTemplateParam(JSONObject.toJSONString(param));//短信模板变量对应的实际值
  20.         
  21.         try {
  22.             SendSmsResponse response = client.getAcsResponse(request);
  23.             // 发送短信,尽量打印出来是否发送成功
  24.             new Gson().toJson(response);
  25.         } catch (ClientException e) {
  26.             e.printStackTrace();
  27.             return false;
  28.         }
  29.         return true;
  30.     }
  31. }
复制代码
  1. import com.rabbitmq.client.Channel;
  2. import com.xsha.msg.service.MsgService;
  3. import com.xsha.rabbit.constant.RabbitConstant;
  4. import org.springframework.amqp.core.Message;
  5. import org.springframework.amqp.rabbit.annotation.Exchange;
  6. import org.springframework.amqp.rabbit.annotation.Queue;
  7. import org.springframework.amqp.rabbit.annotation.QueueBinding;
  8. import org.springframework.amqp.rabbit.annotation.RabbitListener;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.stereotype.Component;
  11. @Component
  12. public class MsgReceive {
  13.     @Autowired
  14.     private MsgService msgService;
  15.     /** 消息监听:监听到RabbitMQ中有消息就消费,并通过手机发送短信通知 */
  16.     @RabbitListener(bindings = @QueueBinding(
  17.             value = @Queue(value = RabbitConstant.QUEUE_MSM, durable = "true"),
  18.             exchange = @Exchange(value = RabbitConstant.EXCHANGE_DIRECT_MSM),
  19.             key = {RabbitConstant.ROUTING_MSM}
  20.     ))
  21.     public void orderConfirm(Map<String, Object> param, Message message, Channel channel) {
  22.         msgService.orderConfirm(param);
  23.     }
  24. }
复制代码
  1. #rabbitmq地址
  2. spring.rabbitmq.host=your_ip
  3. spring.rabbitmq.port=5672
  4. spring.rabbitmq.username=your_username
  5. spring.rabbitmq.password=your_password
复制代码
  1. @Service
  2. public class OrderServiceImpl implements OrderService {
  3.     @Autowired
  4.     private RabbitService rabbitService;
  5.     @Override
  6.     public void saveOrder(String userId, String id) {
  7.         
  8.         UserInfo userInfo = userInfoService.selectById(userId);
  9.         String phone = userInfo.getPhone();
  10.         // 短信信息封装(根据业务需求,封装重要信息)
  11.         Map<String,Object> param = new HashMap<String,Object>(){{
  12.             put("title", "消息标题");
  13.             put("phone", phone);
  14.             put("message", "下单成功");
  15.             put("name", userInfo.getName());
  16.             put("currentTime", new DateTime().toString("yyyy-MM-dd HH:mm"));
  17.         }};
  18.         // 生产消息
  19.         rabbitService.sendMessage(RabbitConstant.EXCHANGE_DIRECT_MSM, RabbitConstant.ROUTING_MSM, param);
  20.     }
  21. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4