开发者社区> 问答> 正文

Java HTTP 触发器一键部署 SpringBoot 应用?

Java HTTP 触发器一键部署 SpringBoot 应用?

展开
收起
小天使爱美 2020-03-27 17:02:42 1460 0
1 条回答
写回答
取消 提交回答
    1. 添加 FC 入口函数 在 Spring 工程中添加 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 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. 配置打包方式 请参考 Java 代码打包,在 pom.xml 中增加如下配置:
    com.santi demo-spring-hello war 1.0 1.8 com.aliyun.fc.runtime fc-java-core 1.3.0 com.aliyun.fc.runtime fc-java-common 2.1.0 org.apache.maven.plugins maven-compiler-plugin 2.3.2 ${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-fc-service demo-fc-service: Type: 'Aliyun::Serverless::Service' Properties: Description: 'Hello FC' Policies: - AliyunOSSFullAccess - AliyunLogFullAccess InternetAccess: true # Create function demo-spring demo-spring: Type: 'Aliyun::Serverless::Function' Properties: Initializer: com.aliyun.fc.example.FcHandler::initialize Handler: com.aliyun.fc.example.FcHandler::handleRequest Runtime: java8 CodeUri: './target/demo-spring-hello-1.0.war' MemorySize: 256 Timeout: 60 InitializationTimeout: 60 EnvironmentVariables: 'USER_CONTEXT_PATH': '/2016-08-15/proxy/demo-fc-service/demo-spring' '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-fc-service/demo-spring/ 成功返回 Spring 页面spring

    2020-03-27 17:31:22
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
从《阿里巴巴Java开发手册》编写推广谈技术成长 立即下载
阿里巴巴HTTP 2.0实践及无线通信协议的演进之路 立即下载
CDN助力企业网站进入HTTPS时代 立即下载