Spring Function应用示例

最近更新时间:2022-04-25 16:19:52

查看PDF

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>

通过 spring-boot-maven-plugin 构建一个包含所有依赖的 jar 包(FatJar),执行命令打包

maven package

编译后的jar包位于项目文件内的target目录内,可以通过java -jar 【name】运行。

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目录下。
如果显示编译失败,请根据输出的编译错误信息调整代码。

文档内容是否对您有帮助?

根本没帮助
文档较差
文档一般
文档不错
文档很好

在文档使用中是否遇到以下问题

内容不全,不深入
内容更新不及时
描述不清晰,比较混乱
系统或功能太复杂,缺乏足够的引导
内容冗长

更多建议

0/200

评价建议不能为空

提交成功!

非常感谢您的反馈,我们会继续努力做到更好!

问题反馈