搭建基于 express 的 Serverless Web 应用?
函数计算平台针对 Java 语言推出的 Java HTTP 触发器功能,能够无缝迁移传统的 Java Web 应用。支持基于 Servlet 协议的 Web 框架所开发应用,比如常用的 Spring、SpringBoot、Struts2等。本文介绍如何使用 Java HTTP 触发器来快速迁移 SpringBoot 应用,并使用函数计算提供的 fun 工具 做到真正一键部署。
您可以直接下载 SpringBoot 集成到函数计算示例代码 测试运行
迁移 SpingBoot 到函数计算需以下步骤:
添加 FC 入口函数 添加 SpringBootServletInitializer 入口 配置打包方式 部署服务和函数 测试运行
FcHandler.java
public class FcHandler implements FunctionInitializer, HttpRequestHandler { private AppLoader fcAppLoader = new FcAppLoader(); // Request url web path // 1. Without custom domain: /2016-08-15/proxy/${YourServiceName}/${YourFunctionName} // 2. With custom domain: your mapping settings path private String userContextPath = System.getenv("USER_CONTEXT_PATH"); // Webapp home directory after inited // Default "/tmp" private String appBaseDir = System.getenv("APP_BASE_DIR"); @Override public void initialize(Context context) throws IOException { FunctionComputeLogger fcLogger = context.getLogger(); // Config FcAppLoader fcAppLoader.setFCContext(context); if (appBaseDir != null) fcAppLoader.setBaseDir(appBaseDir); // Load code from /code fcLogger.info("Begin load code"); fcAppLoader.loadCodeFromLocalProject(""); fcLogger.info("End load code"); // Init webapp from code long timeBegin = System.currentTimeMillis(); fcLogger.info("Begin load webapp"); boolean initSuccess = fcAppLoader.initApp(userContextPath, FcHandler.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(); } }
Application.java
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } SpringBootStartApplication.java
public class SpringBootStartApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(Application.class); } }
<project.java.version>1.8</project.java.version> <tomcat.version>8.5.20</tomcat.version>
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat org.apache.maven.plugins maven-dependency-plugin copy-dependencies prepare-package copy-dependencies ${project.build.directory}/classes/lib runtime org.apache.maven.plugins maven-compiler-plugin ${project.java.version} ${project.java.version}ROSTemplateFormatVersion: '2015-09-01' Transform: 'Aliyun::Serverless-2018-04-03' Resources: # Create Service demo-springboot-service: Type: 'Aliyun::Serverless::Service' Properties: Description: 'Hello SpringBoot On FC' Policies: - AliyunOSSFullAccess - AliyunLogFullAccess LogConfig: Project: ${YOUR_LOPROJECT} # Need to modify Logstore: ${YOUR_LOGSTORE} # Need to modify InternetAccess: true # Create function demo-springboot: Type: 'Aliyun::Serverless::Function' Properties: Initializer: hello.FcHandler::initialize Handler: hello.FcHandler::handleRequest Runtime: java8 CodeUri: './target/demo-springboot-hello-1.0.0.jar' MemorySize: 256 Timeout: 60 InitializationTimeout: 60 EnvironmentVariables: 'USER_CONTEXT_PATH': '/2016-08-15/proxy/demo-sprinboot-service/demo-springboot' 'APP_BASE_DIR': '/tmp' # Create http trigger Events: http: # trigger name Type: HTTP # http trigger Properties: AuthType: ANONYMOUS Methods: ['GET', 'POST'] 执行
mvn clean package fun deploy 即可部署成功
curl https://{account_id}.{region}.fc.aliyuncs.com/2016-08-15/proxy/demo-springboot-service/demo-springboot/ 成功返回 SpringBoot 页面springboot
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。