最近更新时间:2023-02-03 17:38:12
Spring Boot 2.5.5以前版本不支持Java17,需要使用高于此版本的Spring Boot进行开发。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
<relativePath/>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.ksyun.kcf</groupId>
<artifactId>cloudevents-spring-boot-example</artifactId>
<version>1.0</version>
<properties>
<spring-boot.version>2.5.5</spring-boot.version>
<spring.version>5.2.8.RELEASE</spring.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>17</java.version>
<cloudevents.version>2.3.0</cloudevents.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>io.cloudevents</groupId>
<artifactId>cloudevents-core</artifactId>
<version>${cloudevents.version}</version>
</dependency>
<!-- To use the json format and the cloudevent data mapper -->
<dependency>
<groupId>io.cloudevents</groupId>
<artifactId>cloudevents-json-jackson</artifactId>
<version>${cloudevents.version}</version>
</dependency>
<dependency>
<groupId>io.cloudevents</groupId>
<artifactId>cloudevents-http-restful-ws</artifactId>
<version>${cloudevents.version}</version>
</dependency>
<!-- lombok log annotations -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
<dependency>
<!-- 排除 spring-boot-starter-logging -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 其他依赖省略 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
</dependencies>
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
在resource目录下的application.properties文件中配置监听端口号等信息。
server.port=8080
创建一个MainResource类,提供几个简单的接口服务。
package com.example.demo.controller;
import com.example.demo.model.Bucket;
import com.example.demo.model.Ks3CloudEventData;
import com.example.demo.model.ResponseData;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import java.util.UUID;
import static io.cloudevents.core.CloudEventUtils.mapData;
@Path("/")
@Slf4j
public class MainResource {
public static final String EVENT_TYPE = "kcf";
@Autowired
ObjectMapper objectMapper;
@POST
@Path("event-invoke")
public Response fcEventInvoke(CloudEvent inputEvent) throws Exception {
log.info("receive event message, event type: {}", inputEvent.getType());
//将data字符数据序列化对象
PojoCloudEventData<Ks3CloudEventData> cloudEventData = mapData(inputEvent, PojoCloudEventDataMapper.from(objectMapper, Ks3CloudEventData.class));
log.info("cloudeventdata: {}", new String(cloudEventData.toBytes()));
Ks3CloudEventData userData = cloudEventData.getValue();
log.info("user data: {}", userData);
CloudEvent outputEvent = CloudEventBuilder.from(inputEvent)
.withData("event invoke".getBytes())
.withExtension("path", "/event-invoke")
.build();
log.info("cloudevent output: {}", outputEvent);
return Response.ok(outputEvent).build();
}
@GET
@Path("health")
public Response Health() throws Exception {
log.info("Health Up");
return Response.ok("Up").build();
}
@POST
@Path("http-invoke")
@Produces({"application/json"})
public Response fcHttpPostInvoke(Bucket bucket) throws Exception {
log.info("receive http post message: {}", bucket);
ResponseData responseData = new ResponseData();
responseData.setRequestId(UUID.randomUUID().toString());
return Response.ok(responseData).build();
}
@GET
@Path("http-invoke")
public Response fcHttpGetInvoke() throws Exception {
log.info("receive http get message");
return Response.ok("Get message Ok").build();
}
@PUT
@Path("http-invoke/{id}")
public Response fcHttpPutInvoke(@PathParam("id") String id) throws Exception {
log.info("receive http put message: {}", id);
return Response.ok("Put message Ok").build();
}
@DELETE
@Path("http-invoke/{id}")
public Response fcHttpDeleteInvoke(@PathParam("id") String id) throws Exception {
log.info("receive http delete message: {}", id);
return Response.ok("Delete message Ok").build();
}
@PATCH
@Path("http-invoke/{id}")
public Response fcHttpPatchInvoke(@PathParam("id") String id) throws Exception {
log.info("receive http patch message: {}", id);
return Response.ok("Patch message Ok").build();
}
}
其中health路径用于用户程序的健康检查,event-invoke用于事件函数请求响应逻辑,http-invoke用于http请求响应逻辑,您可以自定义其他path。
用于将cloudevents消息进行serializes/deserializes。
package com.example.demo.controller;
import io.cloudevents.http.restful.ws.CloudEventsProvider;
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JerseyConfiguration extends ResourceConfig {
public JerseyConfiguration() {
// Configure Jersey to load the CloudEventsProvider (which serializes/deserializes CloudEvents)
// and our resource
registerClasses(CloudEventsProvider.class, MainResource.class);
}
}
在pom.xml文件中添加spring-boot-maven-plugin插件(打包插件您可以自行选择,此处将spring-boot-maven-plugin插件作为示例)。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.example.demo.Application</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
使用下面命令将代码和及其依赖打包成可执行的jar包,编译后的jar包位于项目文件内的target目录内。
maven package
编译好的jar包需用zip工具进行压缩,用于后续控制台的代码包上传。
build.gradle配置文件如下
plugins {
id 'org.springframework.boot' version '2.5.5'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example'
version = '1.0-SNAPSHOT'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
dependencies{
implementation 'org.springframework.boot:spring-boot-starter-jersey'
compileOnly 'org.projectlombok:lombok:'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springFramework.boot:spring-boot-starter-test:2.5.5'
implementation 'io.cloudevents:cloudevents-core:2.3.0'
implementation 'io.cloudevents:cloudevents-json-jackson:2.3.0'
implementation 'io.cloudevents:cloudevents-http-restful-ws:2.3.0'
}
tasks.named('test'){
useJUnitPlatform()
}
在项目的根目录下执行下面命令打包,可将spring boot项目打包成一个包含所有依赖的应用程序。
gradle bootJar
编译后的jar包位于项目文件夹内的build/libs
目录下。
如果显示编译失败,请根据输出的编译错误信息调整代码。
纯净模式