开发者社区 > 云原生 > Serverless > 正文

搭建基于 express 的 Serverless Web 应用?

搭建基于 express 的 Serverless Web 应用?

展开
收起
小天使爱美 2020-03-27 17:02:56 1305 0
1 条回答
写回答
取消 提交回答
  • 函数计算平台针对 Java 语言推出的 Java HTTP 触发器功能,能够无缝迁移传统的 Java Web 应用。支持基于 Servlet 协议的 Web 框架所开发应用,比如常用的 Spring、SpringBoot、Struts2等。本文介绍如何使用 Java HTTP 触发器来快速迁移 SpringBoot 应用,并使用函数计算提供的 fun 工具 做到真正一键部署。

    您可以直接下载 SpringBoot 集成到函数计算示例代码 测试运行

    迁移 SpingBoot 到函数计算需以下步骤:

    添加 FC 入口函数 添加 SpringBootServletInitializer 入口 配置打包方式 部署服务和函数 测试运行

    1. 添加 FC 入口函数 在 SpringBoot 工程中添加 FC 的函数入口和函数初始化入口,代码如下:

    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(); } }

    1. 添加 SpringBootServletInitializer 入口 要让 FC 成功加载 SpringBoot,需要为 SpringBoot 配置 ServletInitializer 接口来指向 Application 入口类:

    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); } }

    1. 配置打包方式 请参考 Java 代码打包 在 pom.xml 中增加如下配置:

    <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}
    1. 部署服务和函数 最后,只需在 FC 平台创建服务和函数,以及 HTTP 触发器即可。可使用 fun 工具 来自动化创建过程,对应的 yml 配置如下:

    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 即可部署成功

    1. 测试函数运行 使用 curl 命令访问上述 deploy 生成的 url 地址:

    curl https://{account_id}.{region}.fc.aliyuncs.com/2016-08-15/proxy/demo-springboot-service/demo-springboot/ 成功返回 SpringBoot 页面springboot

    2020-03-27 17:32:14
    赞同 展开评论 打赏

快速交付实现商业价值。

相关产品

  • 函数计算
  • 相关电子书

    更多
    Hologres Serverless之路:揭秘弹性计算组 立即下载
    Serverless开发平台: 让研发效能再提升 立即下载
    Serverless 引领云上研发新范式 立即下载