最近更新时间:2023-02-03 17:38:12
<properties>
<quarkus-plugin.version>1.10.3.Final</quarkus-plugin.version>
<quarkus.platform.artifact-id>quarkus-universe-bom</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
<quarkus.platform.version>1.10.3.Final</quarkus.platform.version>
<project.version>2.3.0</project.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven-surefire-plugin.version>2.22.1</maven-surefire-plugin.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>${quarkus.platform.artifact-id}</artifactId>
<version>${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client</artifactId>
</dependency>
<dependency>
<groupId>io.cloudevents</groupId>
<artifactId>cloudevents-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.cloudevents</groupId>
<artifactId>cloudevents-http-restful-ws</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.cloudevents</groupId>
<artifactId>cloudevents-json-jackson</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<!-- lombok log annotations -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>compile</scope>
</dependency>
</dependencies>
在resource目录下新建application.properties文件,用于http server端口、打包方式、log日志配置
# Configuration file
# server port
quarkus.http.port=8080
#Uber-Jar Creation
quarkus.package.type=uber-jar
# log config
quarkus.log.console.format=%d{HH:mm:ss,SSS} %-5p [%c{2.}] (%t) %s%e%n
quarkus.log.console.level=INFO
quarkus.log.console.color=false
创建一个EventResource 类,提供一个简单的接口服务,用于接收cloudevents事件。
package io.cloudevents.examples.quarkus.resources;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.cloudevents.CloudEvent;
import io.cloudevents.examples.quarkus.model.Ks3CloudEventData;
import io.cloudevents.jackson.PojoCloudEventDataMapper;
import lombok.extern.slf4j.Slf4j;
import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
@Path("/")
@Slf4j
public class EventResource {
@Inject
ObjectMapper mapper;
@Context
UriInfo uriInfo;
@POST
@Path("event-invoke")
public Response create(CloudEvent event) {
if (event == null || event.getData() == null) {
throw new BadRequestException("Invalid data received. Null or empty event");
}
log.info("ak: {}", event.getExtension("ak"));
Ks3CloudEventData eventData = PojoCloudEventDataMapper
.from(mapper, Ks3CloudEventData.class)
.map(event.getData())
.getValue();
log.info("Received eventData: {}", eventData);
return Response
.ok(uriInfo.getAbsolutePathBuilder().build(event.getId()))
.build();
}
}
创建一个HealthResource类,用户程序的健康检查。
package io.cloudevents.examples.quarkus.resources;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
@Path("/health")
public class HealthResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public CompletionStage<String> hello() {
//使用异步响应
return CompletableFuture.supplyAsync(() -> "Hands up!");
}
}
您可以自定义其他的path处理业务逻辑。
在pom.xml文件中添加quarkus-maven-plugin插件。
<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>1.10.3.Final</version>
<executions>
<execution>
<goals>
<goal>generate-code</goal>
<goal>generate-code-tests</goal>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
使用下面命令将代码和及其依赖打包成可执行的jar包,编译后的jar包位于项目文件内的target目录内。
maven package
编译好的jar包需用zip工具进行压缩,用于后续控制台的代码包上传。
build.gradle配置文件如下:
build.gradle:
plugins {
id 'java'
id 'io.quarkus' version '1.10.3.Final'
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
implementation enforcedPlatform("io.quarkus:quarkus-universe-bom:1.10.3.Final")
implementation 'io.quarkus:quarkus-resteasy-jackson:1.10.3.Final'
implementation 'io.quarkus:quarkus-rest-client:1.10.3.Final'
implementation 'io.cloudevents:cloudevents-api:2.3.0'
implementation 'io.cloudevents:cloudevents-http-restful-ws:2.3.0'
implementation 'io.cloudevents:cloudevents-json-jackson:2.3.0'
implementation 'org.projectlombok:lombok:1.18.22'
testImplementation 'io.quarkus:quarkus-junit5:1.10.3.Final'
testImplementation 'io.rest-assured:rest-assured:4.3.2'
implementation "org.projectlombok:lombok:1.18.22"
annotationProcessor "org.projectlombok:lombok:1.18.22"
}
group = 'io.cloudevents'
version = '2.3.0'
description = 'cloudevents-restful-ws-quarkus-example'
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
compileJava {
options.encoding = 'UTF-8'
options.compilerArgs << '-parameters'
}
compileTestJava {
options.encoding = 'UTF-8'
}
在项目的根目录执行下面命令,可在本地执行应用程序
gradle quarkusDev
在项目的根目录下执行下面命令打可将项目打包成一个包含所有依赖的应用程序
gradle build
编译后的jar包位于项目文件内的build目录下(文件名带runner字段)
build/cloudevents-restful-ws-quarkus-example-2.3.0-runner.jar
如果显示编译失败,请根据输出的编译错误信息调整代码。
纯净模式