阿里云函数计算配合SpringBoot项目

本文涉及的产品
简介: 作者:DecentAnt首先要弄清楚,阿里云的函数计算项目和SpringBoot项目是两个完全独立的项目体系,阿里云函数计算项目的打包后的大小不能超过50M,而SpringBoot的项目大小无所谓。

作者:DecentAnt

首先要弄清楚,阿里云的函数计算项目和SpringBoot项目是两个完全独立的项目体系,阿里云函数计算项目的打包后的大小不能超过50M,而SpringBoot的项目大小无所谓。

  1. SpringBoot项目
    首先是SpringBoot项目,这个项目和一般的SpringBoot项目一样,但是数据库必须是阿里云可以连接到的数据库,可以是阿里云内网数据库(我没试过),也可以是公网能够访问的数据库(我用的就是这种),项目配置和普通的SpringBoot项目一模一样,不用什么修改。

为了测试方便,我只用了一个汽车品牌表来做了这个实验,项目内容非常简单,其中有两个接口,一个是CarBrandAdd另一个是CarBrandList,请求方式是POST+Body(raw application/json),也就是直接用Body中的json字符串进行请求。
两个Controller如下:
image1
image2

其中Service中的业务逻辑我就不贴出来了,就是最最简单的添加和列表操作

application.properties中数据库配置如下:
image3

也就是最一般的数据库配置罢了

之后就是用Maven打包了,我这里使用了idea的Maven打包方式,注意要打成war包:
war
我这里采用了阿里云文档中推荐的打包插件:(经过测试发现不用这种也是可以的,只是这样打包包的体积比较小罢了)

<plugins>
    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
            <appendAssemblyId>false</appendAssemblyId> <!-- this is used for not append id to the jar name -->
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id> <!-- this is used for inheritance merges -->
                <phase>package</phase> <!-- bind to the packaging phase -->
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>
</plugins>

然后就可以打包了,点击IDEA的Maven打包,或者输入:
image4
mvn package
image5

最后将这个war包上传到阿里云的OSS上去

  1. 阿里云函数计算项目
    这个项目是用来调用SpringBoot项目用的,其实相当于客户端的作用

创建一个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>

然后创建HelloWeb类,实现FunctionInitializer, HttpRequestHandler接口,我这里使用OSS的方式调用SpringBoot的war包,这里只要填写正确就行。
其中最让我纠结的就是userContextPath这个参数,结果发现这个参数填什么都没问题……根本不用管它。

public class HelloWeb implements FunctionInitializer, HttpRequestHandler {

private FcAppLoader fcAppLoader = new FcAppLoader();
private String ossEndPoint = "${YourEndPoint}";
private String bucket = "${YourBucket}";
private String key = "alifc.war";

//    private String userContextPath = "/2016-08-15/proxy/{YourServideName}/{YourFunctionName}";
private String userContextPath = "/2016-08-15/proxy/helloweb.LATEST/testweb3/carBrandList";

@Override
public void initialize(Context context) throws IOException {
    FunctionComputeLogger fcLogger = context.getLogger();
    fcAppLoader.setFCContext(context);
    fcLogger.info("Begin load code: "+key);
    // Load code from OSS
    fcAppLoader.loadCodeFromOSS(ossEndPoint, bucket, key);
    fcLogger.info("End load code");
    // Init webapp from code
    long timeBegin = System.currentTimeMillis();
    fcLogger.info("Begin load webapp");
    fcAppLoader.initApp(userContextPath, HelloWeb.class.getClassLoader());
    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);
        String requestPath = (String) request.getAttribute("FC_REQUEST_PATH");
        String requestURI = (String) request.getAttribute("FC_REQUEST_URI");
        String requestClientIP = (String) request.getAttribute("FC_REQUEST_CLIENT_IP");

        FunctionComputeLogger logger = context.getLogger();
        logger.info("requestPath is "+requestPath);
        logger.info("requestURI is "+requestURI);
        logger.info("requestClientIP is "+requestClientIP);

        String body = String.format("Path: %s\n Uri: %s\n IP: %s\n", requestPath, requestURI, requestClientIP);
        OutputStream out = response.getOutputStream();
        out.write((body).getBytes());
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

}
这个项目已经建立完成了。

然后同样的使用Maven进行打包,打包插件使用和SpringBoot一样的打包插件即可
image8

  1. 阿里云增加函数计算
    新建一个函数计算服务:

image9

创建完成服务后,创建一个函数
选择java8,使用空白函数
image10

触发器配置中选择HTTP触发器,注意:HTTP触发器只能在创建函数时创建!创建完成函数后无法再创建了!!
我这里使用POST请求
image11

基础管理配置中上传函数计算打完了war包(不是SpringBoot的war包!!)
最关键的是环境配置,注意:一定要打开“是否配置函数初始化入口”
image12
image13
image14

(其中马赛克掉的是包名)

权限配置可以跳过,最后创建即可。

  1. 执行函数计算
    填入相关请求路径信息,然后点击执行即可,注意类型不要选错了

image15

查看数据库是否添加成功:
image16

测试列表接口:
_

注意:如果要换函数计算的war的话,需要选择文件后,点击保存才行,否则无法生效。

相关实践学习
基于函数计算一键部署掌上游戏机
本场景介绍如何使用阿里云计算服务命令快速搭建一个掌上游戏机。
建立 Serverless 思维
本课程包括: Serverless 应用引擎的概念, 为开发者带来的实际价值, 以及让您了解常见的 Serverless 架构模式
目录
相关文章
|
10天前
|
Java Linux
Springboot 解决linux服务器下获取不到项目Resources下资源
Springboot 解决linux服务器下获取不到项目Resources下资源
|
18天前
|
Java API Spring
SpringBoot项目调用HTTP接口5种方式你了解多少?
SpringBoot项目调用HTTP接口5种方式你了解多少?
65 2
|
18天前
|
前端开发 JavaScript Java
6个SpringBoot 项目拿来就可以学习项目经验接私活
6个SpringBoot 项目拿来就可以学习项目经验接私活
29 0
|
27天前
|
监控 关系型数据库 Serverless
Serverless 应用引擎常见问题之函数计算3.0的项目提示未知错误如何解决
Serverless 应用引擎(Serverless Application Engine, SAE)是一种完全托管的应用平台,它允许开发者无需管理服务器即可构建和部署应用。以下是Serverless 应用引擎使用过程中的一些常见问题及其答案的汇总:
31 5
|
30天前
|
前端开发 Java 关系型数据库
SpringBoot+MyBatis 天猫商城项目
SpringBoot+MyBatis 天猫商城项目
51 1
|
21天前
|
Java Maven 微服务
springboot项目开启远程调试-jar包
springboot项目开启远程调试-jar包
17 0
|
2天前
|
消息中间件 运维 Serverless
阿里云函数计算是一种FaaS(Function as a Service)云服务
【4月更文挑战第17天】阿里云函数计算是一种FaaS(Function as a Service)云服务
12 3
|
3天前
|
自然语言处理 Cloud Native Serverless
通义灵码牵手阿里云函数计算 FC ,打造智能编码新体验
近日,通义灵码正式进驻函数计算 FC WebIDE,让使用函数计算产品的开发者在其熟悉的云端集成开发环境中,无需再次登录即可使用通义灵码的智能编程能力,实现开发效率与代码质量的双重提升。
|
3天前
|
人工智能 Serverless 数据处理
利用阿里云函数计算实现 Serverless 架构的应用
阿里云函数计算是事件驱动的Serverless服务,免服务器管理,自动扩展资源。它降低了基础设施成本,提高了开发效率,支持Web应用、数据处理、AI和定时任务等多种场景。通过实例展示了如何用Python实现图片压缩应用,通过OSS触发函数自动执行。阿里云函数计算在云计算时代助力企业实现快速迭代和高效运营。
3 0
|
4天前
|
NoSQL 关系型数据库 MySQL
阿里云服务器部署项目流程
本文主要讲解阿里云服务器的部署,如何选择配置等