You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Quarkus/Funqy时,能否用Java/Kotlin SDK无HTTP发送Knative事件?

在Quarkus应用内自动生成Knative CloudEvent的可行方案

推荐方案:使用Quarkus Knative Eventing扩展

Quarkus提供了专门的Knative Eventing扩展,能让你以原生方式发送CloudEvent,无需手动构造HTTP请求细节。

  1. 添加依赖
    Maven坐标:

    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-knative-eventing</artifactId>
    </dependency>
    
  2. 示例代码
    注入CloudEventSender直接发送事件:

    import io.quarkus.knative.eventing.sink.CloudEventSender;
    import io.cloudevents.CloudEvent;
    import io.cloudevents.core.builder.CloudEventBuilder;
    import jakarta.inject.Inject;
    import jakarta.ws.rs.GET;
    import jakarta.ws.rs.Path;
    import java.net.URI;
    
    @Path("/auto-event")
    public class AutoEventProducer {
    
        @Inject
        CloudEventSender eventSender;
    
        @GET
        @Path("/send")
        public void generateAndSendEvent() {
            // 构造符合规范的CloudEvent
            CloudEvent event = CloudEventBuilder.v1()
                    .withId("auto-" + System.currentTimeMillis())
                    .withType("myOwnEvent")
                    .withSource(URI.create("quarkus-auto-producer"))
                    .withDataContentType("application/json")
                    .withData("\"AutoGeneratedEvent\"".getBytes())
                    .build();
    
            // 发送到Broker
            eventSender.send(event);
        }
    }
    
  3. 配置Broker地址
    在application.properties中指定你的Broker入口:

    quarkus.knative.eventing.sink.url=http://broker-ingress.knative-eventing.svc.cluster.local/knativetutorial/default
    

替代方案:用CloudEvents SDK封装HTTP请求

如果不想用Quarkus扩展,可以用官方CloudEvents SDK构造事件,配合Quarkus REST Client发送,比手动写curl风格代码更简洁。

  1. 添加依赖

    <dependency>
        <groupId>io.cloudevents</groupId>
        <artifactId>cloudevents-core</artifactId>
    </dependency>
    <dependency>
        <groupId>io.cloudevents</groupId>
        <artifactId>cloudevents-http-restful-ws</artifactId>
    </dependency>
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-rest-client</artifactId>
    </dependency>
    
  2. 示例代码

    import io.cloudevents.CloudEvent;
    import io.cloudevents.core.builder.CloudEventBuilder;
    import io.cloudevents.http.restful.ws.HttpMessageFactory;
    import jakarta.inject.Inject;
    import jakarta.ws.rs.POST;
    import jakarta.ws.rs.Path;
    import org.eclipse.microprofile.rest.client.inject.RestClient;
    import java.net.URI;
    
    @Path("/sdk-event")
    public class SdkEventProducer {
    
        @RestClient
        BrokerClient brokerClient;
    
        @POST
        public void sendAutoEvent() {
            CloudEvent event = CloudEventBuilder.v1()
                    .withId("sdk-auto-" + System.currentTimeMillis())
                    .withType("myOwnEvent")
                    .withSource(URI.create("quarkus-sdk-producer"))
                    .withDataContentType("application/json")
                    .withData("\"AutoEventFromSDK\"".getBytes())
                    .build();
    
            // 自动生成符合要求的HTTP请求头和实体
            var request = HttpMessageFactory.createRequest(event, URI.create("http://broker-ingress.knative-eventing.svc.cluster.local/knativetutorial/default"));
            brokerClient.sendEvent(request.getHeaders(), request.getEntity());
        }
    
        // REST Client接口定义
        public interface BrokerClient {
            @POST
            void sendEvent(@jakarta.ws.rs.core.HttpHeaders headers, byte[] eventData);
        }
    }
    
  3. 配置REST Client

    quarkus.rest-client."com.example.SdkEventProducer.BrokerClient".url=http://broker-ingress.knative-eventing.svc.cluster.local/knativetutorial/default
    

关于fabric8io Kubernetes Client的说明

fabric8io客户端主要用于操作Kubernetes/Knative资源(如Broker、Trigger、Pod等),本身没有专门的CloudEvent发送API。你可以用它管理Knative资源,但发送事件仍需通过HTTP接口。


总结

Knative Broker的核心接收机制就是HTTP,不存在绕开HTTP的方案,但你可以通过Quarkus扩展或CloudEvents SDK封装请求细节,让代码更简洁、更符合Quarkus原生开发风格,避免手动构造HTTP头和URL的繁琐操作。

内容的提问来源于stack exchange,提问作者Sherlock Wayne

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 12:03:38