You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@eventmesh.apache.org by mi...@apache.org on 2021/12/02 13:19:10 UTC

[incubator-eventmesh] branch cloudevents updated: cloudevents protocol http pub/sub for sdk (#625)

This is an automated email from the ASF dual-hosted git repository.

mikexue pushed a commit to branch cloudevents
in repository https://gitbox.apache.org/repos/asf/incubator-eventmesh.git


The following commit(s) were added to refs/heads/cloudevents by this push:
     new f90714f  cloudevents protocol http pub/sub for sdk (#625)
f90714f is described below

commit f90714fe34185cc5434971019de47c1ecd7bd8f9
Author: mike_xwm <mi...@126.com>
AuthorDate: Thu Dec 2 21:19:06 2021 +0800

    cloudevents protocol http pub/sub for sdk (#625)
    
    cloudevents protocol http pub/sub for sdk
---
 .../http/body/message/ReplyMessageRequestBody.java     |  2 +-
 .../common/protocol/http/common/ProtocolKey.java       |  2 ++
 .../http/demo/sub/controller/SubController.java        |  3 ++-
 .../cloudevents/CloudEventsProtocolAdaptor.java        |  5 ++++-
 .../http/SendMessageBatchV2ProtocolResolver.java       | 14 ++++++++++++--
 .../http/SendMessageRequestProtocolResolver.java       | 18 ++++++++++++++----
 .../client/http/producer/CloudEventProducer.java       | 17 ++++++++++++++++-
 7 files changed, 51 insertions(+), 10 deletions(-)

diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/ReplyMessageRequestBody.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/ReplyMessageRequestBody.java
index a6a456b..90d83c1 100644
--- a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/ReplyMessageRequestBody.java
+++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/body/message/ReplyMessageRequestBody.java
@@ -32,7 +32,7 @@ public class ReplyMessageRequestBody extends Body {
 
     public static final String ORIGTOPIC     = "origtopic";
     public static final String BIZSEQNO      = "bizseqno";
-    public static final String UNIQUEID      = "uniqueId";
+    public static final String UNIQUEID      = "uniqueid";
     public static final String CONTENT       = "content";
     public static final String EXTFIELDS     = "extFields";
     public static final String PRODUCERGROUP = "producergroup";
diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/ProtocolKey.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/ProtocolKey.java
index eb79f04..e032f1f 100644
--- a/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/ProtocolKey.java
+++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/http/common/ProtocolKey.java
@@ -38,6 +38,8 @@ public class ProtocolKey {
         public static final String IP = "ip";
         public static final String USERNAME = "username";
         public static final String PASSWD = "passwd";
+        public static final String BIZSEQNO = "bizseqno";
+        public static final String UNIQUEID = "uniqueid";
     }
 
 
diff --git a/eventmesh-examples/src/main/java/org/apache/eventmesh/http/demo/sub/controller/SubController.java b/eventmesh-examples/src/main/java/org/apache/eventmesh/http/demo/sub/controller/SubController.java
index 2f0089c..74a5a6e 100644
--- a/eventmesh-examples/src/main/java/org/apache/eventmesh/http/demo/sub/controller/SubController.java
+++ b/eventmesh-examples/src/main/java/org/apache/eventmesh/http/demo/sub/controller/SubController.java
@@ -43,7 +43,8 @@ public class SubController {
 
     @RequestMapping(value = "/test", method = RequestMethod.POST)
     public String subTest(@RequestBody String message) {
-        log.info("=======receive message======= {}", JsonUtils.serialize(message));
+        log.info("=======receive message======= {}", message);
+
         subService.consumeMessage(message);
 
         Map<String, Object> map = new HashMap<>();
diff --git a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/CloudEventsProtocolAdaptor.java b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/CloudEventsProtocolAdaptor.java
index 6e540e6..15f5161 100644
--- a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/CloudEventsProtocolAdaptor.java
+++ b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/CloudEventsProtocolAdaptor.java
@@ -19,6 +19,7 @@ package org.apache.eventmesh.protocol.cloudevents;
 
 import io.cloudevents.CloudEvent;
 import io.cloudevents.core.provider.EventFormatProvider;
+import io.cloudevents.jackson.JsonFormat;
 
 import org.apache.commons.lang3.StringUtils;
 import org.apache.eventmesh.common.Constants;
@@ -36,6 +37,7 @@ import org.apache.eventmesh.protocol.cloudevents.resolver.http.SendMessageBatchV
 import org.apache.eventmesh.protocol.cloudevents.resolver.http.SendMessageRequestProtocolResolver;
 import org.apache.eventmesh.protocol.cloudevents.resolver.tcp.TcpMessageProtocolResolver;
 
+import java.nio.charset.StandardCharsets;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -104,7 +106,8 @@ public class CloudEventsProtocolAdaptor<T extends ProtocolTransportObject>
                 final Map<String, Object> map = new HashMap<>();
                 @Override
                 public Map<String, Object> toMap() {
-                    map.put("content", JsonUtils.serialize(cloudEvent));
+                    byte[] eventByte = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE).serialize(cloudEvent);
+                    map.put("content", new String(eventByte, StandardCharsets.UTF_8));
                     return map;
                 }
             };
diff --git a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageBatchV2ProtocolResolver.java b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageBatchV2ProtocolResolver.java
index a863bb5..a90484b 100644
--- a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageBatchV2ProtocolResolver.java
+++ b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageBatchV2ProtocolResolver.java
@@ -20,13 +20,16 @@ package org.apache.eventmesh.protocol.cloudevents.resolver.http;
 import io.cloudevents.CloudEvent;
 import io.cloudevents.SpecVersion;
 import io.cloudevents.core.builder.CloudEventBuilder;
+import io.cloudevents.core.provider.EventFormatProvider;
 import io.cloudevents.core.v03.CloudEventV03;
 import io.cloudevents.core.v1.CloudEventV1;
+import io.cloudevents.jackson.JsonFormat;
 
 import org.apache.commons.lang3.StringUtils;
 
 import org.apache.eventmesh.common.protocol.http.body.Body;
 import org.apache.eventmesh.common.protocol.http.body.message.SendMessageBatchV2RequestBody;
+import org.apache.eventmesh.common.protocol.http.body.message.SendMessageRequestBody;
 import org.apache.eventmesh.common.protocol.http.common.ProtocolKey;
 import org.apache.eventmesh.common.protocol.http.common.ProtocolVersion;
 import org.apache.eventmesh.common.protocol.http.header.Header;
@@ -34,6 +37,8 @@ import org.apache.eventmesh.common.protocol.http.header.message.SendMessageBatch
 import org.apache.eventmesh.common.utils.JsonUtils;
 import org.apache.eventmesh.protocol.api.exception.ProtocolHandleException;
 
+import java.nio.charset.StandardCharsets;
+
 public class SendMessageBatchV2ProtocolResolver {
     public static CloudEvent buildEvent(Header header, Body body) throws ProtocolHandleException {
         try {
@@ -55,11 +60,13 @@ public class SendMessageBatchV2ProtocolResolver {
             ProtocolVersion version = sendMessageBatchV2RequestHeader.getVersion();
             String language = sendMessageBatchV2RequestHeader.getLanguage();
 
+            String producerGroup = sendMessageBatchV2RequestBody.getProducerGroup();
             String content = sendMessageBatchV2RequestBody.getMsg();
 
             CloudEvent event = null;
             if (StringUtils.equals(SpecVersion.V1.toString(), protocolVersion)) {
-                event = JsonUtils.deserialize(content, CloudEventV1.class);
+                event = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE)
+                    .deserialize(content.getBytes(StandardCharsets.UTF_8));
                 event = CloudEventBuilder.from(event)
                     .withExtension(ProtocolKey.REQUEST_CODE, code)
                     .withExtension(ProtocolKey.ClientInstanceKey.ENV, env)
@@ -74,9 +81,11 @@ public class SendMessageBatchV2ProtocolResolver {
                     .withExtension(ProtocolKey.PROTOCOL_TYPE, protocolType)
                     .withExtension(ProtocolKey.PROTOCOL_DESC, protocolDesc)
                     .withExtension(ProtocolKey.PROTOCOL_VERSION, protocolVersion)
+                    .withExtension(SendMessageBatchV2RequestBody.PRODUCERGROUP, producerGroup)
                     .build();
             } else if (StringUtils.equals(SpecVersion.V03.toString(), protocolVersion)) {
-                event = JsonUtils.deserialize(content, CloudEventV03.class);
+                event = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE)
+                    .deserialize(content.getBytes(StandardCharsets.UTF_8));
                 event = CloudEventBuilder.from(event)
                     .withExtension(ProtocolKey.REQUEST_CODE, code)
                     .withExtension(ProtocolKey.ClientInstanceKey.ENV, env)
@@ -91,6 +100,7 @@ public class SendMessageBatchV2ProtocolResolver {
                     .withExtension(ProtocolKey.PROTOCOL_TYPE, protocolType)
                     .withExtension(ProtocolKey.PROTOCOL_DESC, protocolDesc)
                     .withExtension(ProtocolKey.PROTOCOL_VERSION, protocolVersion)
+                    .withExtension(SendMessageBatchV2RequestBody.PRODUCERGROUP, producerGroup)
                     .build();
             }
             return event;
diff --git a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageRequestProtocolResolver.java b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageRequestProtocolResolver.java
index 6c80dd7..f8e0791 100644
--- a/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageRequestProtocolResolver.java
+++ b/eventmesh-protocol-plugin/eventmesh-protocol-cloudevents/src/main/java/org/apache/eventmesh/protocol/cloudevents/resolver/http/SendMessageRequestProtocolResolver.java
@@ -20,8 +20,11 @@ package org.apache.eventmesh.protocol.cloudevents.resolver.http;
 import io.cloudevents.CloudEvent;
 import io.cloudevents.SpecVersion;
 import io.cloudevents.core.builder.CloudEventBuilder;
+import io.cloudevents.core.provider.EventFormatProvider;
 import io.cloudevents.core.v03.CloudEventV03;
 import io.cloudevents.core.v1.CloudEventV1;
+import io.cloudevents.jackson.JsonFormat;
+
 import org.apache.commons.lang3.StringUtils;
 import org.apache.eventmesh.common.protocol.http.body.Body;
 import org.apache.eventmesh.common.protocol.http.body.message.SendMessageRequestBody;
@@ -32,6 +35,8 @@ import org.apache.eventmesh.common.protocol.http.header.message.SendMessageReque
 import org.apache.eventmesh.common.utils.JsonUtils;
 import org.apache.eventmesh.protocol.api.exception.ProtocolHandleException;
 
+import java.nio.charset.StandardCharsets;
+
 public class SendMessageRequestProtocolResolver {
 
     public static CloudEvent buildEvent(Header header, Body body) throws ProtocolHandleException {
@@ -54,12 +59,14 @@ public class SendMessageRequestProtocolResolver {
             ProtocolVersion version = sendMessageRequestHeader.getVersion();
             String language = sendMessageRequestHeader.getLanguage();
 
+            String producerGroup = sendMessageRequestBody.getProducerGroup();
             String content = sendMessageRequestBody.getContent();
 
             CloudEvent event = null;
             if (StringUtils.equals(SpecVersion.V1.toString(), protocolVersion)) {
-                event = JsonUtils.deserialize(content, CloudEventV1.class);
-                event = CloudEventBuilder.from(event)
+                event = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE)
+                    .deserialize(content.getBytes(StandardCharsets.UTF_8));
+                event = CloudEventBuilder.v1(event)
                         .withExtension(ProtocolKey.REQUEST_CODE, code)
                         .withExtension(ProtocolKey.ClientInstanceKey.ENV, env)
                         .withExtension(ProtocolKey.ClientInstanceKey.IDC, idc)
@@ -73,10 +80,12 @@ public class SendMessageRequestProtocolResolver {
                         .withExtension(ProtocolKey.PROTOCOL_TYPE, protocolType)
                         .withExtension(ProtocolKey.PROTOCOL_DESC, protocolDesc)
                         .withExtension(ProtocolKey.PROTOCOL_VERSION, protocolVersion)
+                        .withExtension(SendMessageRequestBody.PRODUCERGROUP, producerGroup)
                         .build();
             } else if (StringUtils.equals(SpecVersion.V03.toString(), protocolVersion)) {
-                event = JsonUtils.deserialize(content, CloudEventV03.class);
-                event = CloudEventBuilder.from(event)
+                event = EventFormatProvider.getInstance().resolveFormat(JsonFormat.CONTENT_TYPE)
+                    .deserialize(content.getBytes(StandardCharsets.UTF_8));
+                event = CloudEventBuilder.v03(event)
                         .withExtension(ProtocolKey.REQUEST_CODE, code)
                         .withExtension(ProtocolKey.ClientInstanceKey.ENV, env)
                         .withExtension(ProtocolKey.ClientInstanceKey.IDC, idc)
@@ -90,6 +99,7 @@ public class SendMessageRequestProtocolResolver {
                         .withExtension(ProtocolKey.PROTOCOL_TYPE, protocolType)
                         .withExtension(ProtocolKey.PROTOCOL_DESC, protocolDesc)
                         .withExtension(ProtocolKey.PROTOCOL_VERSION, protocolVersion)
+                        .withExtension(SendMessageRequestBody.PRODUCERGROUP, producerGroup)
                         .build();
             }
             return event;
diff --git a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/producer/CloudEventProducer.java b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/producer/CloudEventProducer.java
index 49905ad..01d6ca7 100644
--- a/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/producer/CloudEventProducer.java
+++ b/eventmesh-sdk-java/src/main/java/org/apache/eventmesh/client/http/producer/CloudEventProducer.java
@@ -14,13 +14,16 @@ import org.apache.eventmesh.common.protocol.http.common.ProtocolKey;
 import org.apache.eventmesh.common.protocol.http.common.ProtocolVersion;
 import org.apache.eventmesh.common.protocol.http.common.RequestCode;
 import org.apache.eventmesh.common.utils.JsonUtils;
+import org.apache.eventmesh.common.utils.RandomStringUtils;
 
 import java.io.IOException;
+import java.nio.charset.StandardCharsets;
 
 import com.google.common.base.Preconditions;
 
 import io.cloudevents.CloudEvent;
 import io.cloudevents.core.builder.CloudEventBuilder;
+import io.cloudevents.core.provider.EventFormatProvider;
 import io.netty.handler.codec.http.HttpMethod;
 import lombok.extern.slf4j.Slf4j;
 
@@ -101,17 +104,27 @@ class CloudEventProducer extends AbstractHttpClient implements EventMeshProtocol
     }
 
     private RequestParam buildCommonPostParam(CloudEvent cloudEvent) {
+        byte[] bodyByte = EventFormatProvider.getInstance().resolveFormat(cloudEvent.getDataContentType())
+            .serialize(cloudEvent);
+        String content = new String(bodyByte, StandardCharsets.UTF_8);
+
         RequestParam requestParam = new RequestParam(HttpMethod.POST);
         requestParam
+            .addHeader(ProtocolKey.ClientInstanceKey.ENV, eventMeshHttpClientConfig.getEnv())
+            .addHeader(ProtocolKey.ClientInstanceKey.IDC, eventMeshHttpClientConfig.getIdc())
+            .addHeader(ProtocolKey.ClientInstanceKey.IP, eventMeshHttpClientConfig.getIp())
+            .addHeader(ProtocolKey.ClientInstanceKey.PID, eventMeshHttpClientConfig.getPid())
+            .addHeader(ProtocolKey.ClientInstanceKey.SYS, eventMeshHttpClientConfig.getSys())
             .addHeader(ProtocolKey.ClientInstanceKey.USERNAME, eventMeshHttpClientConfig.getUserName())
             .addHeader(ProtocolKey.ClientInstanceKey.PASSWD, eventMeshHttpClientConfig.getPassword())
             .addHeader(ProtocolKey.LANGUAGE, Constants.LANGUAGE_JAVA)
             .addHeader(ProtocolKey.PROTOCOL_TYPE, PROTOCOL_TYPE)
             .addHeader(ProtocolKey.PROTOCOL_DESC, PROTOCOL_DESC)
             .addHeader(ProtocolKey.PROTOCOL_VERSION, cloudEvent.getSpecVersion().toString())
+
             // todo: move producerGroup tp header
             .addBody(SendMessageRequestBody.PRODUCERGROUP, eventMeshHttpClientConfig.getProducerGroup())
-            .addBody(SendMessageRequestBody.CONTENT, JsonUtils.serialize(cloudEvent));
+            .addBody(SendMessageRequestBody.CONTENT, content);
         return requestParam;
     }
 
@@ -125,6 +138,8 @@ class CloudEventProducer extends AbstractHttpClient implements EventMeshProtocol
             .withExtension(ProtocolKey.LANGUAGE, Constants.LANGUAGE_JAVA)
             .withExtension(ProtocolKey.PROTOCOL_DESC, cloudEvent.getSpecVersion().name())
             .withExtension(ProtocolKey.PROTOCOL_VERSION, cloudEvent.getSpecVersion().toString())
+            .withExtension(ProtocolKey.ClientInstanceKey.BIZSEQNO, RandomStringUtils.generateNum(30))
+            .withExtension(ProtocolKey.ClientInstanceKey.UNIQUEID, RandomStringUtils.generateNum(30))
             .build();
     }
 

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: commits-help@eventmesh.apache.org