学习体验下Koupleless框架
在挺久之前serverless架构还是很火的,大概是在这一波人工智能热潮之前,结合云原生,但似乎这个框架似乎没有一个大一统的,类似于Java中的spring,springboot这种框架
前阵子体验到了这款Koupleless框架,前身是sofa serverless,总结下来主要是对于它的模块级开发和部署的机制觉得非常不错
首先就是我们可以下一个samples来体验
可以通过git也可以去下载zip包(因为实在有点大,网络不好可能还是zip包比较快)1
git clone git@github.com:koupleless/samples.git
比较方便的是基于仓库下的springboot-samples/web/tomcat/样例来体验
目录下的 base-web-single-host 是基座应用biz1-web-single-host 和 biz2-web-single-host 是业务模块的demo
代码clone下来以后首先要构建这几个模块1
mvn -pl web/tomcat/biz1-web-single-host,web/tomcat/biz2-web-single-host -am clean package -DskipTests
这里会碰到几个问题,一个是需要maven版本 >= 3.9.x,于是我就单独下了个,另外就是会碰到1
2 Plugin com.alipay.sofa.koupleless:koupleless-base-build-plugin:1.4.2-SNAPSHOT or one of its dependencies could not be resolved:
[ERROR] Could not find artifact com.alipay.sofa.koupleless:koupleless-base-build-plugin:jar:1.4.2-SNAPSHOT in ark-snapshot (https://oss.sonatype.org/content/repositories/snapshots)
这个问题,然后就去这个仓库看了下缔约没这个版本的包,已经是正式包了,
找到这个版本是在根pom定义的1
<koupleless.runtime.version>1.4.2</koupleless.runtime.version>
把它改成正式版本构建就好了,当然也是对国内网络不太友好,需要等一会
还需要准备好模块包的构建工具,arkctl v0.2.1+
可以用go来安装1
go install github.com/koupleless/arkctl@v0.2.1
或者直接在这下载 https://github.com/koupleless/arkctl/releases
安装后需要设置下PATH,也可以直接用全路径,比如mac下安装完了是在用户目录下的/go/bin/arkctl
对了,还需要maven拉下依赖包,也可以用命令行的
然后就可以启动基座了,基座启动就类似于普通的springboot应用,
接下去启动biz模块1
~/go/bin/arkctl deploy web/tomcat/biz1-web-single-host/target/biz1-web-single-host-0.0.1-SNAPSHOT-ark-biz.jar
和1
~/go/bin/arkctl deploy web/tomcat/biz2-web-single-host/target/biz2-web-single-host-0.0.1-SNAPSHOT-ark-biz.jar
启动之后就可以用curl或者浏览器访问来验证,是否两个模块都被加载了
使用curl查看biz11
curl http://localhost:8080/biz1/
可以看到输出1
hello to biz1 deploy, run in com.alipay.sofa.ark.container.service.classloader.BizClassLoader@4850d96f%
再看下biz21
curl http://localhost:8080/biz2/
可以看到输出1
hello to biz2 deploy, run in com.alipay.sofa.ark.container.service.classloader.BizClassLoader@6c5136fe
结合代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23package com.alipay.sofa.web.biz1.rest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDate;
import java.time.LocalDateTime;
public class SampleController {
private ApplicationContext applicationContext;
public String hello() {
String appName = applicationContext.getId();
return String.format("hello to %s deploy, run in %s", appName, Thread.currentThread()
.getContextClassLoader());
}
可以看到这是biz1的输出1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23package com.alipay.sofa.web.biz2.rest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDate;
import java.time.LocalDateTime;
public class SampleController {
private ApplicationContext applicationContext;
public String hello() {
String appName = applicationContext.getId();
return String.format("hello to %s deploy, run in %s", appName, Thread.currentThread()
.getContextClassLoader());
}
biz2也会答应对应的
这里其实主体就是启动一个应用基座,模块都是直接在基座上热加载的,不用重新启动应用,对于需要快捷开发并行开发的还是很方便的,也是种变相的serverless下,模块的代码结构模式也跟普通springboot应用很类似,就常规的开发,但是部署启动都变成了秒级的,非常方便