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

本文涉及的产品
简介: 前言 阿里云函数计算 Function Compute(FC) 本文介绍如何使用 Java HTTP 触发器来快速迁移 SpringBoot 应用 demo-springboot-hello,并使用函数计算提供的 fun 工具 来快速部署和测试。

前言

阿里云函数计算 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 工具 能极大简化创建服务和函数的流程。示例代码中已经包含了 fun 工具部署对应的配置文件 template.yml。该配置默认的会使用 HelloSpringBoot.java 来创建服务 test-java 以及函数 demo-springboot,并且会自动创建绑定函数的 HTTP 触发器。

  1. 修改示例代码中 HelloSpringBoot.java 对应的 userContextPath
  2. 在示例代码根目录执行 mvn clean package 打包
  3. 在示例代码根目录执行 fun deploy 部署
    image

测试函数运行

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

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

成功返回 SpringBoot 页面
image

相关实践学习
基于函数计算一键部署掌上游戏机
本场景介绍如何使用阿里云计算服务命令快速搭建一个掌上游戏机。
建立 Serverless 思维
本课程包括: Serverless 应用引擎的概念, 为开发者带来的实际价值, 以及让您了解常见的 Serverless 架构模式
目录
相关文章
|
14天前
|
Java
java原生发送http请求
java原生发送http请求
|
22天前
|
Java API Spring
SpringBoot项目调用HTTP接口5种方式你了解多少?
SpringBoot项目调用HTTP接口5种方式你了解多少?
71 2
|
1月前
|
Web App开发 监控 Java
|
1月前
|
弹性计算 前端开发 小程序
微信小程序上传文件至阿里云OSS直传(java后端签名+前端直传)
当前的通用文件上传方式是通过前端上传到服务器,再由服务器转存至对象存储。这种方式在处理小文件时效率尚可,但大文件上传因受限于服务器带宽,速度较慢。例如,一个100MB的文件在5Mbps带宽的阿里云ECS上上传至服务器需160秒。为解决此问题,可以采用后端签名的方式,使微信小程序直接上传文件到阿里云OSS,绕过服务器中转。具体操作包括在JAVA后端引入相关依赖,生成签名,并在微信小程序前端使用这个签名进行文件上传,注意设置正确的请求头和formData参数。这样能提高大文件上传的速度。
|
7天前
|
网络协议 Java API
深度剖析:Java网络编程中的TCP/IP与HTTP协议实践
【4月更文挑战第17天】Java网络编程重在TCP/IP和HTTP协议的应用。TCP提供可靠数据传输,通过Socket和ServerSocket实现;HTTP用于Web服务,常借助HttpURLConnection或Apache HttpClient。两者结合,构成网络服务基础。Java有多种高级API和框架(如Netty、Spring Boot)简化开发,助力高效、高并发的网络通信。
|
7天前
|
监控 Serverless API
阿里云函数计算的工作原理与事件驱动模型密切相关
【4月更文挑战第17天】阿里云函数计算的工作原理与事件驱动模型密切相关
61 4
|
7天前
|
消息中间件 运维 Serverless
阿里云函数计算是一种FaaS(Function as a Service)云服务
【4月更文挑战第17天】阿里云函数计算是一种FaaS(Function as a Service)云服务
48 3
|
7天前
|
自然语言处理 Cloud Native Serverless
通义灵码牵手阿里云函数计算 FC ,打造智能编码新体验
近日,通义灵码正式进驻函数计算 FC WebIDE,让使用函数计算产品的开发者在其熟悉的云端集成开发环境中,无需再次登录即可使用通义灵码的智能编程能力,实现开发效率与代码质量的双重提升。
95393 2

相关产品

  • 函数计算