作者: DecentAnt
首先要弄清楚, 阿里云的函数计算项目和 SpringBoot 项目是两个完全独立的项目体系, 阿里云函数计算项目的打包后的大小不能超过 50M, 而 SpringBoot 的项目大小无所谓.
SpringBoot 项目
首先是 SpringBoot 项目, 这个项目和一般的 SpringBoot 项目一样, 但是数据库必须是阿里云可以连接到的数据库, 可以是阿里云内网数据库 (我没试过), 也可以是公网能够访问的数据库 (我用的就是这种), 项目配置和普通的 SpringBoot 项目一模一样, 不用什么修改.
为了测试方便, 我只用了一个汽车品牌表来做了这个实验, 项目内容非常简单, 其中有两个接口, 一个是 CarBrandAdd 另一个是 CarBrandList, 请求方式是 POST+Body(raw application/JSON), 也就是直接用 Body 中的 JSON 字符串进行请求.
两个 Controller 如下:
其中 Service 中的业务逻辑我就不贴出来了, 就是最最简单的添加和列表操作
application.properties 中数据库配置如下:
也就是最一般的数据库配置罢了
之后就是用 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 打包, 或者输入:
mvn package
最后将这个 war 包上传到阿里云的 OSS 上去
阿里云函数计算项目
这个项目是用来调用 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 一样的打包插件即可
阿里云增加函数计算
新建一个函数计算服务:
创建完成服务后, 创建一个函数
选择 java8, 使用空白函数
触发器配置中选择 HTTP 触发器, 注意: HTTP 触发器只能在创建函数时创建! 创建完成函数后无法再创建了!!
我这里使用 POST 请求
基础管理配置中上传函数计算打完了 war 包 (不是 SpringBoot 的 war 包!!)
最关键的是环境配置, 注意: 一定要打开 "是否配置函数初始化入口"
(其中马赛克掉的是包名)
权限配置可以跳过, 最后创建即可.
执行函数计算
填入相关请求路径信息, 然后点击执行即可, 注意类型不要选错了
查看数据库是否添加成功:
测试列表接口:
注意: 如果要换函数计算的 war 的话, 需要选择文件后, 点击保存才行, 否则无法生效.
来源: https://yq.aliyun.com/articles/706331