java.lang.IllegalArgumentException:项目ID不能为空

java.lang.IllegalArgumentException: The project ID can't be null or empty

我在我的项目中安装了 PubSub 依赖。

<dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>spring-cloud-gcp-starter-pubsub</artifactId>
    </dependency>

我实现了 TopicChannel 和订阅频道

  @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.UnsatisfiedDependencyException:创建名称为 'messageChannelAdapter' 的 bean 在 class 路径资源 [com/payfree/bftConfiguration/pubsub/SubscriberChannel.class] 中定义时出错:通过方法 [[=] 表达的依赖关系不满足=26=]参数1;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:在 class 路径资源 [com/google/cloud/spring/autoconfigure/pubsub/GcpPubSubAutoConfiguration.class] 中定义名称 'pubSubTemplate' 创建 bean 时出错:通过方法 [=28 表示的不满足依赖关系=]参数0;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:在 class 路径资源 [com/google/cloud/spring/autoconfigure/pubsub/GcpPubSubAutoConfiguration.class] 中定义名称 'pubSubPublisherTemplate' 创建 bean 时出错:通过方法 [=30 表示的不满足依赖关系=]参数0;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在 class 路径资源 [com/google/cloud/spring/autoconfigure/pubsub/GcpPubSubAutoConfiguration.class] 中定义名称 'defaultPublisherFactory' 创建 bean 时出错:通过工厂方法实例化 Bean 失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [com.google.cloud.spring.pubsub.support.PublisherFactory]:工厂方法 'defaultPublisherFactory' 抛出异常;嵌套异常是 java.lang.IllegalArgumentException:项目 ID 不能为 null 或为空。

我该如何解决这个错误?

使用 gcloud-cli

设置项目 ID
gcloud config set project <your-project-id>

参考:https://cloud.google.com/sdk/gcloud/reference/config/set

如果您的系统上尚未安装 gcloud-cli,请安装。 Link: https://cloud.google.com/sdk/docs/install