java 从本地向 SQS 队列发送消息失败
Sending message to a SQS queue fails from local in java
我无法从我的 java 应用程序向 sqs-queue 发送消息。
我是 运行 roribio16/alpine-sqs docker 我本地的 SQS 图片,我已经创建了一个标准队列。
代码片段:
@Data
@Configuration
@EnableSqs
public class SQSConfig {
private final String amazonAWSAccessKey;
private final String amazonAWSSecretKey;
private final String amazonSQSRegion;
public SQSConfig(@Value("${aws.accesskey}") String amazonAWSAccessKey,
@Value("${aws.secretkey}") String amazonAWSSecretKey,
@Value("${sqs.aws.region}") String amazonSQSRegion) {
this.amazonAWSAccessKey = amazonAWSAccessKey;
this.amazonAWSSecretKey = amazonAWSSecretKey;
this.amazonSQSRegion = amazonSQSRegion;
}
@Bean
public QueueMessagingTemplate queueMessagingTemplate() {
return new QueueMessagingTemplate(amazonSQSClient());
}
@Bean
@Primary
public AmazonSQSAsync amazonSQSClient() {
return AmazonSQSAsyncClientBuilder.standard()
.withRegion(amazonSQSRegion)
.withCredentials(new AWSStaticCredentialsProvider(new DefaultAWSCredentialsProviderChain().getCredentials()))
.build();
}
private AWSStaticCredentialsProvider amazonAWSCredentials() {
return new AWSStaticCredentialsProvider(new BasicAWSCredentials(amazonAWSAccessKey, amazonAWSSecretKey));
}
}
MessageProducerService:
@Component
public class SQSMessageProducerService {
@Value("${sqs.endpoint}")
private String amazonSQSEndpoint;
@Autowired
private QueueMessagingTemplate queueMessagingTemplate;
public void sendMessage(String messageBody) {
Message messageBuilt = MessageBuilder.withPayload(messageBody).build();
try {
queueMessagingTemplate.send(amazonSQSEndpoint, messageBuilt);
} catch (MessagingException e) {
System.out.println(e);
}
System.out.println("message sent: " + messageBody);
}
}
使用-
调用
sqsMessageProducerService.sendMessage(userEvent.toString());
错误堆栈-
InvalidAction; see the SQS docs. (Service: AmazonSQS; Status Code:
400; Error Code: InvalidAction; Request ID:
00000000-0000-0000-0000-000000000000)
来自属性文件的变量 -
sqs.endpoint=${SQS_ENDPOINT:http://localhost:9324/test-sns-queue}
sqs.aws.region=${SQS_AWS_REGION:us-west-2}
此外,SQS 实例在我的本地启动,可以使用 http://localhost:9325/ 访问。
我在这里缺少一些配置吗?
我认为问题可能在于端点是 http://localhost:9324/queue/test-sns-queue
终点有误请再试/queue/test-sns-queue
我无法从我的 java 应用程序向 sqs-queue 发送消息。 我是 运行 roribio16/alpine-sqs docker 我本地的 SQS 图片,我已经创建了一个标准队列。
代码片段:
@Data
@Configuration
@EnableSqs
public class SQSConfig {
private final String amazonAWSAccessKey;
private final String amazonAWSSecretKey;
private final String amazonSQSRegion;
public SQSConfig(@Value("${aws.accesskey}") String amazonAWSAccessKey,
@Value("${aws.secretkey}") String amazonAWSSecretKey,
@Value("${sqs.aws.region}") String amazonSQSRegion) {
this.amazonAWSAccessKey = amazonAWSAccessKey;
this.amazonAWSSecretKey = amazonAWSSecretKey;
this.amazonSQSRegion = amazonSQSRegion;
}
@Bean
public QueueMessagingTemplate queueMessagingTemplate() {
return new QueueMessagingTemplate(amazonSQSClient());
}
@Bean
@Primary
public AmazonSQSAsync amazonSQSClient() {
return AmazonSQSAsyncClientBuilder.standard()
.withRegion(amazonSQSRegion)
.withCredentials(new AWSStaticCredentialsProvider(new DefaultAWSCredentialsProviderChain().getCredentials()))
.build();
}
private AWSStaticCredentialsProvider amazonAWSCredentials() {
return new AWSStaticCredentialsProvider(new BasicAWSCredentials(amazonAWSAccessKey, amazonAWSSecretKey));
}
}
MessageProducerService:
@Component
public class SQSMessageProducerService {
@Value("${sqs.endpoint}")
private String amazonSQSEndpoint;
@Autowired
private QueueMessagingTemplate queueMessagingTemplate;
public void sendMessage(String messageBody) {
Message messageBuilt = MessageBuilder.withPayload(messageBody).build();
try {
queueMessagingTemplate.send(amazonSQSEndpoint, messageBuilt);
} catch (MessagingException e) {
System.out.println(e);
}
System.out.println("message sent: " + messageBody);
}
}
使用-
调用sqsMessageProducerService.sendMessage(userEvent.toString());
错误堆栈-
InvalidAction; see the SQS docs. (Service: AmazonSQS; Status Code: 400; Error Code: InvalidAction; Request ID: 00000000-0000-0000-0000-000000000000)
来自属性文件的变量 -
sqs.endpoint=${SQS_ENDPOINT:http://localhost:9324/test-sns-queue}
sqs.aws.region=${SQS_AWS_REGION:us-west-2}
此外,SQS 实例在我的本地启动,可以使用 http://localhost:9325/ 访问。 我在这里缺少一些配置吗?
我认为问题可能在于端点是 http://localhost:9324/queue/test-sns-queue
终点有误请再试/queue/test-sns-queue