我在项目中安装了PubSub依赖项。
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-pubsub</artifactId>
</dependency>
我实现了TopicChannel
和SubscriptionChannel
:
@Configuration
@Slf4j
public class SubscriberChannel {
private final String subscription = "test-sub";
@Autowired
private DeviceRepository deviceRepository;
@Autowired
private RfidReaderRepository rfidReaderRepository;
@Autowired
private BagDetectionRepository bagDetectionRepository;
@Autowired
private PersonDetectionRepository personDetectionRepository;
@Bean
@ServiceActivator(inputChannel = "pubsubInputChannel")
public MessageHandler messageReceiver() {
return message -> {
String payload = (String) message.getPayload();
long start = System.currentTimeMillis();
log.info("Message arrived! Payload: " + payload);
BasicAcknowledgeablePubsubMessage originalMessage =
message.getHeaders().get(GcpPubSubHeaders.ORIGINAL_MESSAGE, BasicAcknowledgeablePubsubMessage.class);
originalMessage.ack();
};
}
@Bean
public PubSubInboundChannelAdapter messageChannelAdapter(
@Qualifier("pubsubInputChannel") MessageChannel inputChannel,
PubSubTemplate pubSubTemplate) {
PubSubInboundChannelAdapter adapter =
new PubSubInboundChannelAdapter(pubSubTemplate, subscription);
adapter.setErrorChannelName("pubsubErrors");
adapter.setOutputChannel(inputChannel);
adapter.setAckMode(AckMode.MANUAL);
return adapter;
}
@ServiceActivator(inputChannel = "pubsubErrors")
public void pubsubErrorHandler(Message<MessagingException> exceptionMessage) {
BasicAcknowledgeablePubsubMessage originalMessage =
(BasicAcknowledgeablePubsubMessage) exceptionMessage.getPayload().getFailedMessage()
.getHeaders().get(GcpPubSubHeaders.ORIGINAL_MESSAGE);
originalMessage.nack();
}
@Bean
public MessageChannel pubsubInputChannel() {
return new DirectChannel();
}
@Bean
@InboundChannelAdapter(channel = "pubsubInputChannel", poller = @Poller(fixedDelay = "100"))
public MessageSource<Object> pubsubAdapter(PubSubTemplate pubSubTemplate) {
PubSubMessageSource messageSource = new PubSubMessageSource(pubSubTemplate, subscription);
messageSource.setAckMode(AckMode.MANUAL);
messageSource.setPayloadType(String.class);
messageSource.setBlockOnPull(true);
messageSource.setMaxFetchSize(20 * 1024 * 1024);
return messageSource;
}
}
主题频道
@Configuration
public class TopicChannel {
private final String topic = "testt";
@Bean
@ServiceActivator(inputChannel = "pubsubOutputChannel")
public MessageHandler messageSender(PubSubTemplate pubsubTemplate) {
return new PubSubMessageHandler(pubsubTemplate, topic);
}
@MessagingGateway(defaultRequestChannel = "pubsubOutputChannel")
public interface PubsubOutboundGateway {
void sendToPubsub(String text);
}
}
当运行应用程序时,我得到这个错误
org.springframework.beans.factory.不满足DependencyException:创建类中定义了名称为"messageChannelAdapter"的bean时出错路径资源[com/payfree/bftConfiguration/pubsub/SubscriberChannel.class]:通过方法表达的不满足依赖关系"messageChannelAdapter"参数1;嵌套异常为org.springframework.beans.factory.不满足DependencyException:创建在类路径中定义了名称为"pubSubTemplate"的bean时出错资源[com/google/cloud/spring/autoconfig/pubsub/GcpPubSubAutoConfiguration.class]:通过方法"pubSubTemplate"表达的不满足的依赖项参数0;嵌套异常为org.springframework.beans.factory.不满足DependencyException:创建中定义的名称为"pubSubPublisherTemplate"的bean时出错类路径资源[com/google/cloud/spring/autoconfig/pubsub/GcpPubSubAutoConfiguration.class]:通过方法表达的不满足依赖关系"pubSubPublisherTemplate"参数0;嵌套异常为org.springframework.beans.factory.BeanCreationException:错误正在创建在类中定义了名称为"defaultPublisherFactory"的bean路径资源[com/google/cloud/spring/autoconfig/pubsub/GcpPubSubAutoConfiguration.class]:通过工厂方法实例化Bean失败;嵌套异常为org.springframework.beans.BeanInstanceException:未能实例化[com.google.cloud.spring.publsub.support.PublisherFactory]:工厂方法"defaultPublisherFactory"引发异常;嵌套的异常为java.lang.IollegalArgumentException:项目ID不能为null或为空。
如何修复此错误?
使用gcloud cli 设置项目ID
gcloud config set project <your-project-id>
参考:https://cloud.google.com/sdk/gcloud/reference/config/set
如果系统上尚未安装gcloud cli,请安装。链接:https://cloud.google.com/sdk/docs/install