结合@SqsListener和@RequestMapping

时间:2018-04-11 11:28:49

标签: spring amazon-web-services spring-boot amazon-sqs spring-cloud-aws

我们目前正在将当前架构迁移到基于Spring-AWS的微服务中。我的任务之一是研究我们的微服务如何相互通信。 我的目标是建立一个RESTful HTTP端点和SQS生产者和消费者的混合系统。

例如,我有以下代码:

@SqsListener("request_queue")
@SendTo("response_queue")
@PostMapping("/send")
public Object send(@RequestBody Request request, @Header("SenderId") String senderId) {
    if (senderId != null && !senderId.trim().isEmpty()) {
        logger.info("SQS Message Received!");
        logger.info("Sender ID: ".concat(senderId));
        request = new Gson().fromJson(payload, Request.class);
    }

    Response response = processRequest(request); // Process request

    return response;
}

理论上,此方法应该能够处理以下内容:

  1. 通过HTTP
  2. 接收请求对象
  3. 继续轮询 request_queue ,查看包含请求对象的消息
  4. 作为HTTP端点,该方法不返回任何错误。但是,作为SQS侦听器,它会遇到以下异常:

    org.springframework.messaging.converter.MessageConversionException: 
        Cannot convert from [java.lang.String] to [com.oriente.salt.Request] for 
        GenericMessage [payload={"source":"QueueTester","message":"This is a wonderful 
        message send by queue from Habanero to Salt. Spicy.","msisdn":"+639772108550"}, 
        headers={LogicalResourceId=salt_queue, ApproximateReceiveCount=1, 
        SentTimestamp=1523444620218, ....
    

    我试图用@Payload注释请求参数,但无济于事。目前我还通过Java设置了AWS配置,如下所示:

    ConsuerAWSSQSConfig.java

    @Configuration
    public class ConsumerAWSSQSConfig {
    
    @Bean
    public SimpleMessageListenerContainer simpleMessageListenerContainer() {
        SimpleMessageListenerContainer msgListenerContainer = simpleMessageListenerContainerFactory()
                .createSimpleMessageListenerContainer();
        msgListenerContainer.setMessageHandler(queueMessageHandler());
        return msgListenerContainer;
    }
    
    @Bean
    public SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory() {
        SimpleMessageListenerContainerFactory msgListenerContainerFactory = new SimpleMessageListenerContainerFactory();
        msgListenerContainerFactory.setAmazonSqs(amazonSQSClient());
        return msgListenerContainerFactory;
    }
    
    @Bean
    public QueueMessageHandler queueMessageHandler() {
        QueueMessageHandlerFactory queueMsgHandlerFactory = new QueueMessageHandlerFactory();
        queueMsgHandlerFactory.setAmazonSqs(amazonSQSClient());
        QueueMessageHandler queueMessageHandler = queueMsgHandlerFactory.createQueueMessageHandler();
        List<HandlerMethodArgumentResolver> list = new ArrayList<>();
        HandlerMethodArgumentResolver resolver = new PayloadArgumentResolver(new MappingJackson2MessageConverter());
        list.add(resolver);
        queueMessageHandler.setArgumentResolvers(list);
        return queueMessageHandler;
    }
    
    @Lazy
    @Bean(name = "amazonSQS", destroyMethod = "shutdown")
    public AmazonSQSAsync amazonSQSClient() {
        AmazonSQSAsync awsSQSAsync = AmazonSQSAsyncClientBuilder.standard().withRegion(Regions.AP_SOUTHEAST_1).build();
        return awsSQSAsync;
    }
    }
    

    你们有什么想法?

0 个答案:

没有答案