在localstack中测试我的应用程序时遇到问题。要求很简单。我想通过SQS订阅来订阅AWS-SNS中的一个主题。在我配置@SQSListener时执行执行时,在localstack中,它尝试使用AWS验证凭据,并且出现以下错误消息:
org.springframework.beans.factory.BeanCreationException:在类路径资源[org / springframework / cloud / aws / messaging / config / annotation / SqsConfiguration.class]中创建名称为'simpleMessageListenerContainer'的bean时出错:调用init方法失败嵌套异常是com.amazonaws.SdkClientException:无法从链中任何提供程序加载AWS凭证:
我尝试为localstack设置一个虚拟凭据,但同样没有帮助。我尝试在网上搜索,但找不到与此相关的任何帖子。
@Slf4j
@Configuration
@Profile("local")
@EnableSns
@EnableSqs
public class LocalStackConfiguration {
@Value("${sqsClassifierQueueName}")
private String sqsClassifierQueueName;
@Value("${sqsConsQueueNames}")
private List<String> sqsConsQueueNames;
@Bean(name = "amazonSqs")
public AmazonSQS configureSQSClient()
throws MalformedURLException
{
log.info("Loading local SQS client");
BasicAWSCredentials awsCreds = new BasicAWSCredentials("user", "password");
AmazonSQS sqsClient = AmazonSQSClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:4576", "us-east-1"))
.build();
sqsClient.createQueue(sqsClassifierQueueName);
sqsConsQueueNames.forEach(queueName -> sqsClient.createQueue(queueName));
//log.info("Device Registry queue Created: " + sqsClient.createQueue(REGISTRY_QUEUE_URL.substring(REGISTRY_QUEUE_URL.lastIndexOf('/') + 1)));
return sqsClient;
}
@Bean(name = "amazonSns")
public AmazonSNS configureSNSClient() {
AmazonSNS snsClient= AmazonSNSClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("user", "password")))
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:4575", "us-east-1"))
.build();
return snsClient;
}
}
@Component
@DependsOn(value = {"amazonSns", "amazonSqs"})
public class SNSSubscription {
@Qualifier("amazonSns")
@Autowired
private AmazonSNS sns;
@Qualifier("amazonSqs")
@Autowired
private AmazonSQS sqs;
@Value("${snsClassifierTopicName}")
private String snsClassifierTopicName;
@Value("${sqsClassifierQueueName}")
private String sqsClassifierQueueName;
@PostConstruct
public void subscribeToTopics() {
subscribeToClassifierTopics();
}
private void subscribeToClassifierTopics() {
SNSSubscriptionUtils.subscribeToSQS(sns, sqs, snsClassifierTopicName, sqsClassifierQueueName);
}
@SqsListener(value="${snsClassifierTopicName}")
public void receiveMessage(Object obj){
System.out.println(obj);
}
}
public class SNSSubscriptionUtils {
public static void subscribeToSQS(AmazonSNS sns, AmazonSQS sqs, String topicName, String queueName) {
String snsTopicArn = getSnsTopicArn(sns, topicName);
String queueUrl = SQSUtils.getQueueUrl(sqs, queueName);
Topics.subscribeQueue(sns, sqs, snsTopicArn, queueUrl);
}
public static String getSnsTopicArn(AmazonSNS sns, String topicName) {
String topicArn = sns.createTopic(topicName).getTopicArn();
return topicArn;
}
}
public class SQSUtils {
public static String getQueueUrl(AmazonSQS sqs, String queueName) {
GetQueueUrlRequest queueUrlRequest = new GetQueueUrlRequest(queueName);
return sqs.getQueueUrl(queueUrlRequest).getQueueUrl();
}
}
答案 0 :(得分:0)
使用主要注释对配置的bean进行注释。
@Bean(name = "amazonSqs")
@Primary
public AmazonSQS configureSQSClient()