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. For this point, you can take a look at the fourth issue of the author (kevin) OpenTalk , Which has a detailed explanation on this.

Create greet service

  1. $ cd ~/go-zero-demo
  2. $ goctl api new greet
  3. Done.

Take a look at the structure of the greet service

  1. $ cd greet
  2. $ tree
  1. .
  2. ├── etc
  3. └── greet-api.yaml
  4. ├── go.mod
  5. ├── greet.api
  6. ├── greet.go
  7. └── internal
  8. ├── config
  9. └── config.go
  10. ├── handler
  11. ├── greethandler.go
  12. └── routes.go
  13. ├── logic
  14. └── greetlogic.go
  15. ├── svc
  16. └── servicecontext.go
  17. └── types
  18. └── 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 ~/go-zero-demo/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 ~/go-zer-demo/greet
    2. $ go run greet.go -f etc/greet-api.yaml
    1. Starting server at 0.0.0.0:8888...
  • Access service

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

Source code

greet source code

Guess you wants