在运行时设置Dapr @Topic字段



Stack: Java Jakarta EE10 Wildfly27 Docker Dapr

问题是动态地配置Topics。我有几个环境TEST/DEV/PROD,我想为不同的环境使用不同的主题,但相同的图像。理想情况下,我想通过环境变量设置主题。

那么为什么不使用声明式呢?我在Azure容器服务(ACA)上运行Dapr,它不支持声明性配置(!)

在我的订阅者中这样做是通过Annotation @Topic

像这样@Topic(name = "cache_update", pubsubName = "${myAppProperty:messagebus}")

在上面的示例中"messagebus"是默认值,myAppProperty"是应用程序属性的名称。

如果我使用Spring,这将工作,但我们正在使用雅加达EE10运行在Wildfly 27

我到底怎么才能让这个工作?

更新:

好的,非常感谢你的这一点,但我仍然有点不清楚如何写子部分(这将暴露一个POST端点?所以我的端点看起来像这样吗?

(我。e不需要@Topic注释?)

@Path("/handleTopicRoute")
@RequestScoped 
public class DaprServiceImpl extends AbstractCmsService implements DaprService { 
public Response receiveMessage(CloudEvent<CacheUpdate> event) { 
return getResponse(daprSubscriptionController.updateCache(event.getData())); 
}

所以Dapr通过你给我看的端点找到我到主题的映射端点?

Dapr Java SDK仅为Spring Boot提供本地集成;其中处理特定于dpar的注释,如Topic,并将其配置合成为考虑属性解析的相应操作,在您的示例中,将是主题订阅创建。

要对配置连接的主题值动态创建订阅,您可以通过在/dapr/subscribe端点上公开路由,以编程方式使用Dapr SDK来创建订阅主题:

@Path("/dapr")
public class DaprController {
private static final DefaultObjectSerializer SERIALIZER = new DefaultObjectSerializer();
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/subscribe")
public Response subscribeTopics() {
String topic = "TOPIC_NAME"; // you can resolve this value out of environment variables, system properties or MicroProfile config is you are using Wildfly-extras
String pubsub = "PUBSUB"; // resolve this values as well through any external meaning
TopicConfig config = new TopicConfig(pubsub, topic, "/handleTopicRoute", Collections.emptyList());
return Response.ok(SERIALIZER.serialize(Collections.singleton(config))).build();
}
}

下面是主题配置模板(灵感来自Spring Boot集成DaprTopicConfiguration):

public class TopicConfig {
private final String pubsubName;
private final String topic;
private final String route;
private final Map<String, String> metadata;
public TopicConfig(String pubsubName, String topic, String route, Map<String, String> metadata) {
this.pubsubName = pubsubName;
this.topic = topic;
this.route = route;
this.metadata = Collections.unmodifiableMap(metadata);
}
public String getPubsubName() {
return pubsubName;
}
public String getTopic() {
return topic;
}
public String getRoute() {
return route;
}
public Map<String, String> getMetadata() {
return metadata;
}
}

最新更新