咱们用一个系列来解说从需要到上线、从代码到k8s部署、从日志到监控等各个方面的微服务残缺实际。
整个我的项目应用了go-zero开发的微服务,根本蕴含了go-zero以及相干go-zero作者开发的一些中间件,所用到的技术栈根本是go-zero项目组的自研组件,根本是go-zero全家桶了。
实战我的项目地址:https://github.com/Mikaelemmm...
1、鉴权服务
1.1 identity-api
identity次要是用来做鉴权服务的,后面咱们nginx网关的时候有提到。在拜访一个资源的时候,nginx外部会先来identity-api中解析token,identity-api会去申请identity-rpc,所有的验证与颁发token,对立是在identity-rpc中做的
咱们会从header的Authorization中获取token,从x-Original-Uri获取拜访的资源门路
如果以后拜访的路由须要登陆:
- token解析失败:就会返回给前端http401错误码;
- token解析胜利:就会将解析进去的userId放入header的x-user中返回给auth模块,auth模块会把header传递给对应拜访的服务(usercenter), 这样咱们在usercenter间接就能够拿到该登陆用户的id了
如果以后拜访的路由不须要登陆:
前端header中传递了token
- 如果token校验失败:返回http401;
- 如果token校验胜利:就会将解析进去的userId放入header的x-user中返回给auth模块,auth模块会把header传递给对应拜访的服务(usercenter), 这样咱们在usercenter间接就能够拿到该登陆用户的id了
- 前端header中没传递token:userid 会传递 0 给后端服务
urlNoAuth办法判断以后资源是否在yml中配置能够不登陆
//以后url是否须要受权验证func (l *TokenLogic) urlNoAuth(path string) bool { for _, val := range l.svcCtx.Config.NoAuthUrls { if val == path { return true } } return false}
isPass办法就是去identity-rpc校验token,次要也是应用了go-zero的jwt的办法
1.2 identity-rpc
当咱们在注册、登陆胜利时候,用户服务会调用identity-rpc生成token,所以咱们对立在identity-rpc中颁发、校验token,这样就不必每个服务都要写个jwt去保护。
当identity-api申请进来时候,identity-api本人能够解析进去userid,然而咱们要测验这个token是否是过期,就要去后端rpc中的redis中去进行二次校验(当然如果你感觉这里多一次申请,你能够把这一步放到api里间接申请redis也能够),通过rpc的validateToken办法校验
message ValidateTokenReq { int64 userId = 1; string token = 2;}message ValidateTokenResp { bool ok = 1;}rpc validateToken(ValidateTokenReq) returns(ValidateTokenResp);
校验之前登陆、注册等受权时候颁发进来存在redis的token是否正确、过期。
这样api就能够返回给nginx的auth模块是否失败,如果失败auth会间接返回给前端http code 401(所以你们前端应该是先判断http状态码>=400全副异样,再判断业务错误码) , 如果胜利间接拜访后端服务了拿到数据间接返回给前端展现
2、装置goctl 与 protoc、protoc-gen-go
【注】这个跟鉴权没什么关系,只是前面写代码要用到,在这里最好给装置了
2.1 装置goctl
# for Go 1.15 and earlierGO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get -u github.com/zeromicro/go-zero/tools/goctl@latest# for Go 1.16 and laterGOPROXY=https://goproxy.cn/,direct go install github.com/zeromicro/go-zero/tools/goctl@latest
验证是否装置胜利
$ goctl --version
Goctl自定义模版template:将我的项目目录下的data/goctl文件夹内容copy到 HOME 目录的 .goctl 中,goctl在生成代码时候会优先依据这个模版下内容生成
$ cp -r data/goctl ~/.goctl
2.2 装置protoc
链接:https://github.com/protocolbu...
间接找到对应平台的protoc,我是mac intel芯片,所以间接找到 protoc-3.19.3-osx-x86_64.zip ,解压进去后进入该目录下的bin目录中,将protoc间接copy到你的gopath/bin目录下即可。
验证是否装置胜利
$ protoc --version
2.3 装置protoc-gen-go
$ GOPROXY=https://goproxy.cn/,direct go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
查看$GOPATH/bin下是否有protoc-gen-go即可
【注】:如果后续在应用goctl生成代码时候,遇到以下问题
protoc --proto_path=/Users/seven/Developer/goenv/go-zero-looklook/app/usercenter/cmd/rpc/pb usercenter.proto --go_out=plugins=grpc:/Users/seven/Developer/goenv/go-zero-looklook/app/usercenter/cmd/rpc --go_opt=Musercenter.proto=././pbgoctl: generation error: unsupported plugin protoc-gen-go which installed from the following source:google.golang.org/protobuf/cmd/protoc-gen-go, github.com/protocolbuffers/protobuf-go/cmd/protoc-gen-go;Please replace it by the following command, we recommend to use version before v1.3.5:go get -u github.com/golang/protobuf/protoc-gen-gogoctl version: 1.3.0 darwin/amd64
间接执行
$ GOPROXY=https://goproxy.cn/,direct go get -u github.com/golang/protobuf/protoc-gen-go
2.4 装置protoc-gen-go-grpc
$ GOPROXY=https://goproxy.cn/,direct go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
3、总结
总的来说,identity还算是比较简单的,整个流程如下:
用户发动申请资源 -> nginx网关->匹配到对应服务模块 -> auth模块->identity-api ->identity-rpc -> 用户申请的资源
我的项目地址
https://github.com/zeromicro/go-zero
欢送应用 go-zero
并 star 反对咱们!
微信交换群
关注『微服务实际』公众号并点击 交换群 获取社区群二维码。