简体   繁体   English

在运行时设置 Dapr @Topic 字段

[英]Setting Dapr @Topic fields at runtime

Stack: Java Jakarta EE10 Wildfly27 Docker Dapr堆栈:Java Jakarta EE10 Wildfly27 Docker Dapr

The issue is configuration of Topics, dynamically.问题是主题的动态配置。 I have several environments TEST/DEV/PROD and i want to use different topics for the different environments, but the same image.我有几个环境 TEST/DEV/PROD,我想为不同的环境使用不同的主题,但使用相同的图像。 Ideally i would like to set the topic via Environment variables.理想情况下,我想通过环境变量设置主题。

So why not do this declarative?那么为什么不做这个声明呢? Well as i run Dapr on Azure Container Service (ACA) and it does NOT(!) support declarative config(!)好吧,当我在 Azure 容器服务(ACA)上运行 Dapr 时,它不(!)支持声明式配置(!)

So the way to do this in my subscriber is via the Annotation @Topic所以在我的订户中执行此操作的方法是通过注释@Topic

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

In the sample above "messagebus" is the default value and "myAppProperty" is the name of the application Property.在上面的示例中,“messagebus”是默认值,“myAppProperty”是应用程序属性的名称。

Had i used Spring this would have worked but we are using Jakarta EE10 running on Wildfly 27如果我使用 Spring 这会有效,但我们使用的是在 Wildfly 27 上运行的 Jakarta EE10

How on earth can i get this to work?我到底怎样才能让它发挥作用?

Dapr Java SDK only provides a native integration for Spring Boot; Dapr Java SDK 只为Spring Boot提供原生集成; where Dapr-specific annotations such as Topic gets processed and their configuration synthesized into the respective action taking into consideration property resolution, which in your case, would be the topic subscription creation.其中处理特定于 Dapr 的注释(例如Topic )并将它们的配置合成到相应的操作中,同时考虑到属性解析,在您的情况下,这将是主题订阅创建。

To have the a subscription created dynamically toward the config-wired topic values, you can use the Dapr SDK programmatically to create subscription topics by exposing a route over the /dapr/subscribe endpoint:要针对配置有线主题值动态创建订阅,您可以使用 Dapr SDK 以编程方式通过在/dapr/subscribe端点上公开路由来创建订阅主题:

@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();
   }
}

Here is the Topic configuration template (Inspired from Spring Boot integration DaprTopicConfiguration):下面是 Topic 配置模板(灵感来自 Spring Boot integration 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;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM