凹凸租车前端egg服务插件
介绍
插件整合了error log, access log, sentry, validator,并会注册健康接口/health
如何使用
首先
$ yarn add egg-at-plugin
然后进行配置
// config/config.{env}.jsconfigsentry =dsn: 'xxx'// 添加sentry配置, 如果配置不存在则不进行注册// config/plugin.jsexportsatPlugin =enable: truepackage: 'egg-at-plugin'
最后就得到了所有的功能啦~
接入
当前validator功能是需要服务自行接入的, 默认约定实现Validator Service, 实现validate方法, 方法签名为(path:string, method: string, data: any)
path为当前controller注册的路由, method为当前controller使用的http方法, data为当前参数所在区域(requestBody为body, requestQuery为不含数组的http query, requestQueries为含复数的http query)
举个🌰
// app/lib/service/validator.jsmoduleexports ={// some valiation here}// app/controller/home.js// 如果当前controller 传入参数为 { light: 'blue' }, 调用方法为POST, 参数存在于HTTP body中async {const value = thisctxvalidatorconst light = value;// some bll here}// 如果当前controller 传入参数为 { knife: 'blue' }, 调用方法为GET, 参数存在于HTTP queryasync {const value = thisctxvalidatorconst knife = value;// some bll here}// 如果当前controller 传入参数为 { clothes: 'blue' }, 调用方法为GET, 参数存在于HTTP query 但是存在数组形式的参数async {const value = thisctxvalidatorconst clothes = value;// some bll here}
sentry接入:
// config/config.{env}.jsexportssentry =dsn: 'https://819e74a6e948468b9740680cfa87986b:38aaa0c0d51e463597493c250ff11f83@sentry.io/246025'{ // 可选// return 为 true 上发sentry记录, 其余忽略不进行上传, 默认只上传http code 500以上的错误及无http code的错误return !errstatus || errstatus >= 500};// 主动上发sentry error ()ctx // 会自动集成当前环境上下文// 或者appRavenappRaven
eureka接入:
// config/config.{env}.jsexportseureka =instance:app: 'auto-node-eureka-test'instanceId: 'egg-eureka-pro'hostName: 'localhost'ipAddr: '127.0.0.1'port:$: 8080'@enabled': 'true'homePageUrl: '/health'statusPageUrl: '/health'healthCheckUrl: '/health'secureVipAddress: '127.0.0.1'vipAddress: '127.0.0.1'dataCenterInfo:'@class': 'com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo'name: 'MyOwn'eureka:servicePath: '/eureka/apps/'host: '121.199.4.107'port: 1246
TODO
- 完善的单元测试
- 接入apollo(egg的启动顺序坑爹啊)
- 继续完善