使用go-zero框架创建微服务Demo

本文将基于 go-zero微服务框架实现一个用户名信息查询的后台。

系统架构

整体架构如图所示:

arch

项目开发

  • 项目开发过程,主要参考以下两篇文章:
  1. go-zero 实战之 blog 系统
  2. go-zero官方之快速开发

详情可移步查看,在此不做赘述。

  • 项目仓库如下:

项目地址

项目测试

search without auth

curl -k -v -X GET localhost:8889/users/name/bzz -d ‘{“name”: “bzz”}’ -H ‘content-type: application/json’

  • Trying 127.0.0.1…
  • TCP_NODELAY set
  • Connected to localhost (127.0.0.1) port 8889 (#0)

GET /users/name/bzz HTTP/1.1 Host: localhost:8889 User-Agent: curl/7.64.1 Accept: / content-type: application/json Content-Length: 15

  • upload completely sent off: 15 out of 15 bytes < HTTP/1.1 401 Unauthorized < X-Trace-Id: 9ad30d3480e83dcdee10c6fb7329397f < Date: Mon, 06 Dec 2021 02:08:43 GMT < Content-Length: 0 <
  • Connection #0 to host localhost left intact
  • Closing connection 0

get token with user doesn’t exist

curl -k -v -X POST localhost:8888/users -d ‘{“name”: “bzz”, “password”: “bzz@1234”}’ -H ‘content-type: application/json’ Note: Unnecessary use of -X or –request, POST is already inferred.

  • Trying 127.0.0.1…
  • TCP_NODELAY set
  • Connected to localhost (127.0.0.1) port 8888 (#0)

POST /users HTTP/1.1 Host: localhost:8888 User-Agent: curl/7.64.1 Accept: / content-type: application/json Content-Length: 39

  • upload completely sent off: 39 out of 39 bytes < HTTP/1.1 400 Bad Request < Content-Type: text/plain; charset=utf-8 < X-Content-Type-Options: nosniff < X-Trace-Id: e06405160158d7921bf260eefed335ec < Date: Mon, 06 Dec 2021 02:41:27 GMT < Content-Length: 19 < 用户名不存在
  • Connection #0 to host localhost left intact
  • Closing connection 0

register user

curl -k -v -X POST localhost:8888/users/register -d ‘{“name”: “bzz”, “password”: “bzz@1234”}’ -H ‘content-type: application/json’ Note: Unnecessary use of -X or –request, POST is already inferred.

  • Trying 127.0.0.1…
  • TCP_NODELAY set
  • Connected to localhost (127.0.0.1) port 8888 (#0)

POST /users/register HTTP/1.1 Host: localhost:8888 User-Agent: curl/7.64.1 Accept: / content-type: application/json Content-Length: 39

  • upload completely sent off: 39 out of 39 bytes < HTTP/1.1 200 OK < Content-Type: application/json < X-Trace-Id: 92ffdf82559e78789c80510b02bcaea2 < Date: Mon, 06 Dec 2021 02:42:58 GMT < Content-Length: 8 <
  • Connection #0 to host localhost left intact {“id”:1}* Closing connection 0

login with wrong password

curl -k -v -X POST localhost:8888/users -d ‘{“name”: “bzz”, “password”: “bzz@12345”}’ -H ‘content-type: application/json’ Note: Unnecessary use of -X or –request, POST is already inferred.

  • Trying 127.0.0.1…
  • TCP_NODELAY set
  • Connected to localhost (127.0.0.1) port 8888 (#0)

POST /users HTTP/1.1 Host: localhost:8888 User-Agent: curl/7.64.1 Accept: / content-type: application/json Content-Length: 40

  • upload completely sent off: 40 out of 40 bytes < HTTP/1.1 400 Bad Request < Content-Type: text/plain; charset=utf-8 < X-Content-Type-Options: nosniff < X-Trace-Id: 491f3dbca9c2619ab3019c957c1e17ed < Date: Mon, 06 Dec 2021 02:43:57 GMT < Content-Length: 22 < 用户密码不正确
  • Connection #0 to host localhost left intact
  • Closing connection 0

login with correct password

curl -k -v -X POST localhost:8888/users -d ‘{“name”: “bzz”, “password”: “bzz@1234”}’ -H ‘content-type: application/json’ Note: Unnecessary use of -X or –request, POST is already inferred.

  • Trying 127.0.0.1…
  • TCP_NODELAY set
  • Connected to localhost (127.0.0.1) port 8888 (#0)

POST /users HTTP/1.1 Host: localhost:8888 User-Agent: curl/7.64.1 Accept: / content-type: application/json Content-Length: 39

  • upload completely sent off: 39 out of 39 bytes < HTTP/1.1 200 OK < Content-Type: application/json < X-Trace-Id: 37cad9c97a2fc98b57f202b761b7110c < Date: Mon, 06 Dec 2021 02:44:26 GMT < Content-Length: 155 <
  • Connection #0 to host localhost left intact {“token”:“eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2Mzg4NDUwNjYsImlhdCI6MTYzODc1ODY2NiwidXNlcklkIjoxfQ.NxDKvzC6xcY5jSoDuti87Kina5KWsWhdRd_aZwqOjM0”}* Closing connection 0

search with auth

curl -k -v -X GET localhost:8889/users/name/bzz -d ‘{“name”: “bzz”}’ -H ‘content-type: application/json’ -H ‘authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2Mzg4NDUwNjYsImlhdCI6MTYzODc1ODY2NiwidXNlcklkIjoxfQ.NxDKvzC6xcY5jSoDuti87Kina5KWsWhdRd_aZwqOjM0’

  • Trying 127.0.0.1…
  • TCP_NODELAY set
  • Connected to localhost (127.0.0.1) port 8889 (#0)

GET /users/name/bzz HTTP/1.1 Host: localhost:8889 User-Agent: curl/7.64.1 Accept: / content-type: application/json authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2Mzg4NDUwNjYsImlhdCI6MTYzODc1ODY2NiwidXNlcklkIjoxfQ.NxDKvzC6xcY5jSoDuti87Kina5KWsWhdRd_aZwqOjM0 Content-Length: 15

  • upload completely sent off: 15 out of 15 bytes < HTTP/1.1 200 OK < Content-Type: application/json < X-Trace-Id: 50ff60790d881753e0fcbd51cf6f2454 < Date: Mon, 06 Dec 2021 02:46:14 GMT < Content-Length: 23 <
  • Connection #0 to host localhost left intact {“password”:“bzz@1234”}* Closing connection 0

总结

使用 go-zero框架可快速生成 restfulrpc的脚手架代码。go-zero框架也提供了丰富的中间件、微服务治理能力的支持。后续将基于此demo逐步进行能力拓展。

参考