全部文档
当前文档

暂无内容

如果没有找到您期望的内容,请尝试其他搜索词

文档中心

Spring Function应用示例

最近更新时间:2023-02-03 17:38:11

1. pom依赖

<properties>
   <spring-boot.version>2.4.3</spring-boot.version>
   <project.version>2.3.0</project.version>
</properties>

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
</dependencyManagement> 

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-function-web</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>io.cloudevents</groupId>
            <artifactId>cloudevents-spring</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>io.cloudevents</groupId>
            <artifactId>cloudevents-http-basic</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>io.cloudevents</groupId>
            <artifactId>cloudevents-json-jackson</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
        </dependency>
    </dependencies>

2. 添加启动类,注册序列化、反序列化配置类

package io.cloudevents.examples.spring;

import io.cloudevents.CloudEvent;
import io.cloudevents.core.builder.CloudEventBuilder;
import io.cloudevents.spring.messaging.CloudEventMessageConverter;
import io.cloudevents.spring.webflux.CloudEventHttpMessageReader;
import io.cloudevents.spring.webflux.CloudEventHttpMessageWriter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.codec.CodecCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.codec.CodecConfigurer;

import java.net.URI;
import java.util.UUID;
import java.util.function.Function;

@SpringBootApplication
public class DemoApplication {

 public static void main(String[] args) throws Exception {
  SpringApplication.run(DemoApplication.class, args);
 }

 /**
  * Configure a MessageConverter for Spring Cloud Function to pick up and use to
  * convert to and from CloudEvent and Message.
  */
 @Configuration
 public static class CloudEventMessageConverterConfiguration {
  @Bean
  public CloudEventMessageConverter cloudEventMessageConverter() {
   return new CloudEventMessageConverter();
  }
 }

 /**
  * Configure an HTTP reader and writer so that we can process CloudEvents over
     * HTTP via Spring Webflux.
  */
 @Configuration
 public static class CloudEventHandlerConfiguration implements CodecCustomizer {

  @Override
  public void customize(CodecConfigurer configurer) {
   configurer.customCodecs().register(new CloudEventHttpMessageReader());
   configurer.customCodecs().register(new CloudEventHttpMessageWriter());
  }

 }

}

3. 监听端口资源配置

在resource目录下的application.properties文件中配置监听端口号等信息。

server.port=8080

4. 提供接口服务

创建一个MainResource类,提供一个简单的接口服务

package io.cloudevents.examples.spring;

import io.cloudevents.CloudEvent;
import io.cloudevents.core.builder.CloudEventBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.URI;
import java.util.UUID;
import java.util.function.Function;
import java.util.function.Supplier;

@Slf4j
@Configuration
public class MainResource {

    @Bean
    public Function<CloudEvent, CloudEvent> events() {
        log.info("receive event");
        return event -> CloudEventBuilder.from(event)
                .withId(UUID.randomUUID().toString())
                .withSource(URI.create("https://spring.io/foos"))
                .withType("io.spring.event.Foo")
                .withData(event.getData().toBytes())
                .build();
    }

    @Bean
    public Supplier<String> health() {
        log.info("receive health check");
        return () -> "Hands up";
    }

}

其中events处理事件函数请求逻辑;health用于程序本身健康检测。您可以自定义其他function进行业务处理。

5. Maven编译打包

在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>io.cloudevents.examples.spring.DemoApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

使用下面命令将代码和及其依赖打包成可执行的jar包,编译后的jar包位于项目文件内的target目录内。

maven package

编译好的jar包需用zip工具进行压缩,用于后续控制台的代码包上传。

6. Gradle编译打包

build.gradle配置文件如下:

plugins {
    id 'org.springframework.boot' version '2.4.3'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}
group = 'com.example'
version = '1.0-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
 mavenCentral()
}

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-function-web:3.1.1'
    implementation 'org.springframework.boot:spring-boot-starter-webflux:2.4.3'
    implementation 'io.cloudevents:cloudevents-spring:2.3.0'
    implementation 'io.cloudevents:cloudevents-http-basic:2.3.0'
    implementation 'io.cloudevents:cloudevents-json-jackson:2.3.0'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test:2.4.3'
}

description = 'cloudevents-spring-function-example'

在项目的根目录下执行下面命令打包

gradle bootJar

编译后的jar包位于项目文件内的build/libs目录下。
如果显示编译失败,请根据输出的编译错误信息调整代码。

文档导读
纯净模式常规模式

纯净模式

点击可全屏预览文档内容
文档反馈