Solon v2.7.6

FaaS - 常见问答

</> markdown

v2.6.3 后支持

1、开发 solon faas 服务简单吗?

很难!入门简单,成体系很难:

  • 几乎没有代码提示
  • 没法单步调试(只能写完,运行看错误信息)

它适合做些灵活的(或动态的),应紧的,很小的,散乱的需求。场景匹配了就好用,不匹配就难用。

2、能不能指定目录提供 faas 服务?

能。比如我们指定 faas 目录,提供服务:

@SolonMain
public class App {
    public static void main(String[] args) {
        Solon.start(App.class, args, app->{
            //让 Luffy 的处理,接管所有 http 请求处理
            app.http("/faas/**", new LuffyHandler());
            
            //添加文件目录加载器(根目录不要变)
            app.context().wrapAndPut(JtFunctionLoader.class, new JtFunctionLoaderFile("./luffy/"));
        });
    }
}

对文件目录也相应调整下,把函数文件也存到 faas 二级目录下:

/luffy/faas/

3、能不能把 faas 文件放到数据库里?

能。参考 JtFunctionLoaderFile 写个函数加载器。示例:

//随便写的哦,自己要做好缓存和更新处理
public class JtFunctionLoaderDb implements JtFunctionLoader {
    @Override
    public AFileModel fileGet(String path) throws Exception {
        return db.table("faas_file")
                 .whereEq("path", path)
                 .selectItem("*", AFileModel.class);
    }
}

4、如果有控制台管理 faas 文件,更新后怎么清除缓存?

调用接口

JtRun.dele(path);

5、能不能在 java 里调用 faas 文件?

能。可以使用多种不同的接口:

//调用并返回结果
Object rst = JtRun.call("/faas/demo.js", ctx);

//执行
JtRun.exec("/faas/demo.js", ctx);

示例,在定时任务里调用 faas 文件:

@Scheduled(fixedRate = 1000 * 3)
public class Job1 implements Runnable {
    @Override
    public void run() {
        JtRun.exec("/faas/demo_job.js");
    }
}

示例,在控制器里调用 faas 文件:

@Controller
public class Job1  {
    @Mapping("/hello")
    public Object hello() {
        return JtRun.call("/faas/demo.js", ctx);
    }
}