zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    node-sass安装失败问题
    通过JS下载 or 唤起App
    获取地址栏参数
    JS获取浏览器可视区域的尺寸
    Pyhton2.x 和Python3.x
    导入一个AndroidStudio工程作为一个Library Module
    win10 右键菜单添加Git Hash Here
    CTRL-Space always toggles Chinese IME (Windows 7、10)
    DOM解析XML报错:Content is not allowed in prolog
    重复安装相同包名APK出现的问题。
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/15100484.html
Copyright © 2011-2022 走看看