之前的接口请求过程是这样的
- 用户打开
APP
请求业务API
- 业务
API
请求推荐算法(容错) - 推荐算法请求内网服务
- 内网服务提供一些业务的接口数据给推荐算法(例如运营手动配置的推荐)
1 2 3 |
user -> API -> recommend service -> inner service user <- API <- recommend service <- inner service |
因为之前API
和inner service
都是使用Laravel
构建, API
服务器可以多加几台负载均衡, 但是inner service
只有单机, 所以趁现在使用Golang
重构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
和Golang/gin
的inner service
性能对比
PHP
已开启OPcache
PHP
提供服务的端口为8888
Golang
提供服务的端口为9998
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# 服务器配置: 两核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) # Golang 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) # Golang ab -n 10000 -c 100 127.0.0.1:9998/api/v0/albums Requests per second: 4191.87 [#/sec] (mean) |