天天看点

玩转阿里云函数计算(二)----Java Http 触发器极速迁移传统 SpringBoot 应用

前言

阿里云函数计算

Function Compute(FC)

本文介绍如何使用 Java HTTP 触发器来快速迁移 SpringBoot 应用

demo-springboot-hello

,并使用函数计算提供的

fun 工具

来快速部署和测试。

继续本文之前,建议先阅读

玩转阿里云函数计算(一)----Java Http 触发器极速迁移传统 Spring 应用

开始迁移

一、打包需要迁移的 SpringBoot 工程为 war 包

您可以直接使用 SpringBoot 的示例代码:

demo-springboot-hello 示例代码

在源码根目录执行 maven 打包命令

maven clean package

。打包成功后,在 target 目录下生成

demo-springboot-hello-1.0.0.war

这个文件。

注意,如果是您自己的 SpringBoot 工程,需要像上述示例工程一样新增 SpringBoot 的 Servlet 初始化函数入口,以保证打包后的 war 包被正常加载初始化:

public class SpringBootStartApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }
}           

二、在函数计算平台创建 Java 函数

在本地创建 maven 工程,并创建入口函数类

HelloSpringBoot.java

,将应用 war 包拷贝到工程

src/main/resources

目录。

public class HelloSpringBoot implements FunctionInitializer, HttpRequestHandler {
    private FcAppLoader fcAppLoader = new FcAppLoader();

    private String key = "demo-springboot-hello-1.0.0.war";
    
    private String userContextPath = "/2016-08-15/proxy/${YourServiceName}/${YourFunctionName}";
    
    @Override
    public void initialize(Context context) throws IOException {
        FunctionComputeLogger fcLogger = context.getLogger();
        
        fcAppLoader.setFCContext(context);
        
        // Load code from local project
        fcAppLoader.loadCodeFromLocalProject(key);
        
        // Init webapp from code
        long timeBegin = System.currentTimeMillis();
        fcLogger.info("Begin load webapp");
        boolean initSuccess = fcAppLoader.initApp(userContextPath, HelloSpringBoot.class.getClassLoader());
        if(! initSuccess) {
            throw new IOException("Init web app failed");
        }
        fcLogger.info("End load webapp, elapsed: " + (System.currentTimeMillis() - timeBegin) + "ms");
    }
    
    @Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response, Context context)
            throws IOException, ServletException {
        try {
            fcAppLoader.forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}           

其中引用的 maven 库:

<dependency>
    <groupId>com.aliyun.fc.runtime</groupId>
    <artifactId>fc-java-core</artifactId>
    <version>1.3.0</version>
</dependency>

 <dependency>
    <groupId>com.aliyun.fc.runtime</groupId>
    <artifactId>fc-java-common</artifactId>
    <version>1.0.0</version>
</dependency>           

将上述的 maven 工程打包,并在函数计算平台创建服务和函数,这里需要注意的点:

  1. 需要将您创建的服务名和函数名,填充到上述代码中:
    private String userContextPath = "/2016-08-15/proxy/${YourServiceName}/${YourFunctionName}";           
  2. 创建函数时除了需要设置函数入口外,还需要设置初始化入口指向上述代码的

    initialize

    函数。

您可以直接使用示例代码

fc-java-demo 示例代码

使用 fun 工具简化部署流程

使用函数计算提供的

能极大简化创建服务和函数的流程。示例代码中已经包含了 fun 工具部署对应的配置文件

template.yml

。该配置默认的会使用

HelloSpringBoot.java

来创建服务

test-java

以及函数

demo-springboot

,并且会自动创建绑定函数的 HTTP 触发器。

  1. 修改示例代码中

    HelloSpringBoot.java

    对应的

    userContextPath

  2. 在示例代码根目录执行

    mvn clean package

    打包
  3. fun deploy

    部署
    玩转阿里云函数计算(二)----Java Http 触发器极速迁移传统 SpringBoot 应用

测试函数运行

使用 curl 命令访问上述 deploy 生成的 url 地址:

curl https://{accountID}.{region}.fc.aliyuncs.com/2016-08-15/proxy/test-java/demo-springboot/           

成功返回 SpringBoot 页面

玩转阿里云函数计算(二)----Java Http 触发器极速迁移传统 SpringBoot 应用