下面是micro官方已经写的一些优秀的例子
当然你也可以直接看awesome-micro
架构
req -> gateway -> api -> srv
srv为rpc服务, 通过api层抽象出接口, 再用gateway转发请求.
优点:
- gateway 实际为 micro api, 具有micro原生的服务发现的能力.
- 服务扩展时只需拓展api和srv层, gateway无需更改.
gateway的auth通过api plugin实现, 并使用了 casbin 做鉴权. 由于micro.Auth还未完成,所以只能自己写auth. 注意:
Header中的Authorization必须被重写为 'Authorization: Bearer xxx' 的形式.
看micro初始化时的源码, 你可以看到micro.Auth会被使用,它要求Authorization必须
为以上形式, xxx 可以为任何值, 因为不起作用(当micro.Auth完成后推荐使用它, 不用
自己写token相关逻辑了😀).
Run the Custom gateway
make run_gateway secret="go-micro-demo"
Run the user-srv
make run_srv
Run the api layer
make run_api
// curl -H 'Content-Type: application/json' -H 'Authorization: Bearer Token' -d '{"data": "John"}' http://localhost:8080/user/call
curl -H 'Content-Type: application/json' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsImV4cCI6MTYwMDQ0MDU0MiwiaWF0IjoxNjAwNDMwNTQyLCJ0eXAiOiJKV1QifQ.eyJpZCI6MTIzNDU2LCJ1c2VybmFtZSI6Ikppbm9mIiwieGgiOiJKaW5vZiJ9.Gv2DjRTjoyOcPr26SgKQkKfbxS5UAgbhWlAnvC7TGRI' -d '{"data": "John"}' http://localhost:8080/api/user/call
{"status":0,"message":"成功调用User.Call","data":"Hello jinof your data has been received"}
curl -H 'Content-Type: application/json' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsImV4cCI6MTYwMDQ0MDU0MiwiaWF0IjoxNjAwNDMwNTQyLCJ0eXAiOiJKV1QifQ.eyJpZCI6MTIzNDU2LCJ1c2VybmFtZSI6Ikppbm9mIiwieGgiOiJKaW5vZiJ9.Gv2DjRTjoyOcPr26SgKQkKfbxS5UAgbhWlAnvC7TGRI' -d '{"data": "John"}' http://localhost:8080/api/user/pub