我开发了google pubsub消费者,当我使用固定值“ my-alpha-pubsub-topic”时,它已完全运行

@ServiceActivator(inputChannel = "my-alpha-pubsub-topic" )
 public void messageReceiver(String payload) {
  LOGGER.info("Message arrived! Payload: " + payload);

 }


但是我在Kubernetes中的Container上运行它,我需要通过Environment从env参数传递inputChannel值来为1个容器构建支持多个环境

Alpha => inputChannel = "my-alpha-pubsub-topic"
Staging => inputChannel = "my-staging-pubsub-topic"
Production => inputChannel = "my-pubsub-topic"


购买我无法在inputChannel中传递参数。运行失败

@Value("${pubsub.name}")
 private String pubsubName;


@ServiceActivator(inputChannel = "${pubsubName}" )
 public void messageReceiver(String payload) {
  LOGGER.info("Message arrived! Payload: " + payload);

 }


我尝试这个失败

@ServiceActivator(inputChannel = "${pubsubName}" )
@ServiceActivator(inputChannel = "#{${pubsubName}}" )
@ServiceActivator(inputChannel = "${pubsub.name}" )


我如何通过属性或环境参数将值传递给inputChannel

最佳答案

问题是您不能使用
@ServiceActivator(inputChannel = "${pubsub.name}" )因为inputChannel需要指定spring bean的限定符而不是环境变量。
我想在代码的某个地方,您有一个带有限定符my-alpha-pubsub-topic的频道,这就是为什么它第一次起作用的原因。

以我的观点,使用spring概要文件创建所有这些bean并在每个环境中具有以下内容将更有意义

 @Profile("Staging")
 @ServiceActivator(inputChannel = "my-staging-pubsub-topic" )
  public void messageReceiver(String payload) {
  LOGGER.info("Message arrived! Payload: " + payload);
 }

08-03 19:25