之前的接口请求过程是这样的
- 用户打开
APP请求业务API - 业务
API请求推荐算法(容错) - 推荐算法请求内网服务
- 内网服务提供一些业务的接口数据给推荐算法(例如运营手动配置的推荐)
user -> API -> recommend service -> inner service
user <- API <- recommend service <- inner service
因为之前API和inner service都是使用Laravel构建, API服务器可以多加几台负载均衡, 但是inner service只有单机, 所以趁现在使用Go重构inner service
引用的库资源
- https://github.com/gin-gonic/gin
- https://github.com/go-gorm/gorm
缓存中间件https://github.com/gin-contrib/cachehttp 测试https://github.com/gavv/httpexpect环境变量载入https://github.com/joho/godotenv解析环境变量https://github.com/joeshaw/envdecode热加载代码https://github.com/cosmtrek/air
贴一下PHP/Laravel和Go/gin的inner service性能对比
PHP已开启OPcachePHP提供服务的端口为8888Go提供服务的端口为9998
# 服务器配置: 两核8G
top
%Cpu0
%Cpu1
KiB Mem : 7645252 total
# 未开启API中间件缓存
#####################
# PHP
ab -n 10000 -c 100 127.0.0.1:8888/api/v0/albums
Requests per second: 107.49 [#/sec] (mean)
# Go
ab -n 10000 -c 100 127.0.0.1:9998/api/v1/albums
Requests per second: 2719.86 [#/sec] (mean)
# 开启API中间件缓存(Redis)
#####################
# PHP
ab -n 10000 -c 100 127.0.0.1:8888/api/v0/albums
Requests per second: 124.89 [#/sec] (mean)
# Go
ab -n 10000 -c 100 127.0.0.1:9998/api/v0/albums
Requests per second: 4191.87 [#/sec] (mean)