Serverless

BaaS:后端服務化,例如CDN服務、對象存儲服務、日志服務、云監(jiān)控、云數(shù)據(jù)庫、消息隊列等
FaaS:函數(shù)服務化,即云計算里的函數(shù)計算服務,函數(shù)可以調用BaaS,函數(shù)之間也可以相互調用
Serverless:通過靜態(tài)文件托管到OSS、后端程序部署到FaaS,F(xiàn)aaS操作BaaS,可以實現(xiàn)一個完整的應用,并且不需要維護任何服務器

阿里云函數(shù)計算的限制:
1、函數(shù)最大允許運行時間600秒
2、函數(shù)長時間(幾分鐘到幾十分鐘不等)沒被觸發(fā),資源(容器)會被釋放;再次觸發(fā),函數(shù)需重新加載(冷啟動),耗時較長;可以通過定時觸發(fā)解決
3、Spring項目中的@Scheduled定時任務無法長時間保持運行,需要改造成函數(shù)計算的定時觸發(fā)器
4、函數(shù)會并發(fā)執(zhí)行,需要考慮數(shù)據(jù)一致性問題

SpringBoot項目 部署到 阿里云函數(shù)計算

1、pom.xml

<packaging>jar</packaging>
<properties>
    <project.java.version>1.8</project.java.version>
    <tomcat.version>8.5.20</tomcat.version>
</properties>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
 <!-- 阿里云函數(shù)計算相關依賴 -->
 <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>2.2.1</version>
</dependency>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
                        <includeScope>runtime</includeScope>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>${project.java.version}</source>
                <target>${project.java.version}</target>
            </configuration>
        </plugin>
    </plugins>
</build>

2、SpringBootStartApplication.java

public class SpringBootStartApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(MyApplication.class);  // SpringBoot入口類
    }
}

3、FcHandler.java

public class FcHandler implements FunctionInitializer, HttpRequestHandler {
    private AppLoader fcAppLoader = new FcAppLoader();
    private String userContextPath = System.getenv("USER_CONTEXT_PATH");  // 保持默認即可
    private String appBaseDir = System.getenv("APP_BASE_DIR"); // 默認為/temp,保持默認即可

    @Override
    public void initialize(Context context) throws IOException {
        FunctionComputeLogger fcLogger = context.getLogger(); // 日志工具
        fcAppLoader.setFCContext(context);
        if (appBaseDir != null) fcAppLoader.setBaseDir(appBaseDir);
        fcAppLoader.loadCodeFromLocalProject("");
        boolean initSuccess = fcAppLoader.initApp(userContextPath, FcHandler.class.getClassLoader());
        if (!initSuccess) {
            throw new IOException("Init web app failed");
        }
    }

    @Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response, Context context) throws IOException, ServletException {
        try {
            fcAppLoader.forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

4、服務配置
專有網絡配置:配置了VPC后,才能訪問VPC內的BaaS
日志配置:配置了日志項目后,程序中寫日志才有地方看
權限配置:訪問任何BaaS,都需要添加對應的權限
5、函數(shù)配置
觸發(fā)器:HTTP觸發(fā)器
認證方式:anonymous // 匿名,無認證
函數(shù)入口:com...FcHandler::handleRequest // 即上面的FcHandler類里的方法
函數(shù)初始化入口:com.
..FcHandler::initialize // 即上面的FcHandler類里的方法

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容