cloudevents 学习二 java 代码集成试用

主要是测试cloudevents 与java 的集成(client 使用nodejs)

nodejs client

const axios = require("axios").default;
const { emitterFor, Mode, HTTP,CloudEvent } = require("cloudevents");
 
const type = "userlogin"
const source = "itmd_cust"
const subject = "demo"
const datacontenttype = "application/json"
const data  = {
    name:"dalongdemo",
    age:333
}
const ce = new CloudEvent({ type, source, data:data ,datacontenttype,subject });
const message = HTTP.binary(ce); // Or HTTP.structured(ce)
console.log(message.headers)
function sendWithAxios2(message) {
    // Do what you need with the message headers
    // and body in this function, then send the
    // event
    axios({
        method: "post",
        url: "http://localhost:8080/echo2",
        data: message.body,
        headers: message.headers,
      });
 
  }
const emit2 = emitterFor(sendWithAxios2, { mode: Mode.BINARY });
emit2(ce);

java 集成

主要集成了spring jackson

  • pom 文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.dalong</groupId>
    <artifactId>batchapp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>batchapp</name>
    <description>mybatch app</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.3.156</version>
        </dependency>
        <dependency>
            <groupId>io.cloudevents</groupId>
            <artifactId>cloudevents-json-jackson</artifactId>
            <version>2.2.0</version>
        </dependency>
        <dependency>
            <groupId>io.cloudevents</groupId>
            <artifactId>cloudevents-http-basic</artifactId>
            <version>2.2.0</version>
        </dependency>
        <dependency>
            <groupId>io.cloudevents</groupId>
            <artifactId>cloudevents-spring</artifactId>
            <version>2.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
 
</project>
  • convert 配置
    CloudEventHandlerConfiguration
 
@Configuration
public class CloudEventHandlerConfiguration implements WebMvcConfigurer {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(0,new CloudEventHttpMessageConverter());
    }
}
  • controller 定义
package com.dalong.batchapp;
 
import com.fasterxml.jackson.databind.ObjectMapper;
import io.cloudevents.CloudEvent;
import io.cloudevents.core.builder.CloudEventBuilder;
import io.cloudevents.core.data.PojoCloudEventData;
import io.cloudevents.jackson.PojoCloudEventDataMapper;
import io.cloudevents.spring.http.CloudEventHttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
 
import java.net.URI;
import java.util.UUID;
 
import static io.cloudevents.core.CloudEventUtils.mapData;
 
@RestController
public class EventController {
    private static final  ObjectMapper objectMapper  = new ObjectMapper();
    @PostMapping("/echo2")
    public void ce(@RequestBody CloudEvent event) {
       //  此处集成jackson 以及cloudevent 数据格式,方便消息体处理
        CloudEvent event1 = CloudEventBuilder.from(event)
                .withId(UUID.randomUUID().toString())
                .withSource(URI.create("https://spring.io/foos"))
                .withType("io.spring.event.Foo")
                .withData(event.getData().toBytes())
                .build();
        PojoCloudEventData<MyUser> cloudEventData = mapData(
                event,
                PojoCloudEventDataMapper.from(objectMapper,MyUser.class)
        );
        MyUser user = cloudEventData.getValue();
        System.out.println(user.toString());
    }
    @PostMapping("/echo")
    public ResponseEntity<MyUser> echo(@RequestBody MyUser foo, @RequestHeader HttpHeaders headers) {
        CloudEvent attributes = CloudEventHttpUtils.fromHttp(headers)
                .withId(UUID.randomUUID().toString())
                .withSource(URI.create("https://spring.io/foos"))
                .withType("io.spring.event.Foo")
                .build();
        System.out.println(foo.toString());
        System.out.println(headers.toString());
        HttpHeaders outgoing = CloudEventHttpUtils.toHttp(attributes);
        return ResponseEntity.ok().headers(outgoing).body(foo);
    }
}

效果

  • node client

 

 

  • spring boot 消息

 

 

参考资料

https://stackoverflow.com/questions/3907929/should-i-declare-jacksons-objectmapper-as-a-static-field
https://cloudevents.github.io/sdk-java/json-jackson.html
https://cloudevents.io/
https://cloudevents.github.io/sdk-java/http-basic.html
https://cloudevents.github.io/sdk-java/spring.html

posted on 2021-08-04 19:39  荣锋亮  阅读(852)  评论(0编辑  收藏  举报

导航