Monolithic Service

[!TIP] This document is machine-translated by Google. If you find grammatical and semantic errors, and the document description is not clear, please PR

Forward

Since go-zero integrates web/rpc, some friends in the community will ask me whether go-zero is positioned as a microservice framework. The answer is no. Although go-zero integrates many functions, you can use any one of them independently, or you can develop a single service.

It is not that every service must adopt the design of the microservice architecture.

Create greet service

  1. $ mkdir go-zero-demo
  2. $ cd go-zero-demo
  3. $ go mod init go-zero-demo
  4. $ goctl api new greet
  5. $ go mod tidy
  6. Done.

Take a look at the structure of the greet service

  1. $ cd greet
  2. $ tree
  3. .
  4. ├── etc
  5. └── greet-api.yaml
  6. ├── go.mod
  7. ├── greet.api
  8. ├── greet.go
  9. └── internal
  10. ├── config
  11. └── config.go
  12. ├── handler
  13. ├── greethandler.go
  14. └── routes.go
  15. ├── logic
  16. └── greetlogic.go
  17. ├── svc
  18. └── servicecontext.go
  19. └── types
  20. └── types.go

It can be observed from the above directory structure that although the greet service is small, it has “all internal organs”. Next, we can write business code in greetlogic.go.

Write logic

  1. $ vim greet/internal/logic/greetlogic.go
  1. func (l *GreetLogic) Greet(req types.Request) (*types.Response, error) {
  2. return &types.Response{
  3. Message: "Hello go-zero",
  4. }, nil
  5. }

Start and access the service

  • Start service

    1. $ cd greet
    2. $ go run greet.go -f etc/greet-api.yaml
    3. Starting server at 0.0.0.0:8888...
  • Access service

    1. $ curl -i -X GET http://localhost:8888/from/you
    2. HTTP/1.1 200 OK
    3. Content-Type: application/json
    4. Date: Sun, 07 Feb 2021 04:31:25 GMT
    5. Content-Length: 27
    6. {"message":"Hello go-zero"}

Source code

greet source code

Guess you wants