前言

API gatewayRPC service

问题举例

HTTP
package main

import (
  "fmt"
  "net/http"
)

func morning(w http.ResponseWriter, req *http.Request) {
  fmt.Fprintln(w, "morning!")
}

func evening(w http.ResponseWriter, req *http.Request) {
  fmt.Fprintln(w, "evening!")
}

type Morning struct{}

func (m Morning) Start() {
  http.HandleFunc("/morning", morning)
  http.ListenAndServe("localhost:8080", nil)
}

func (m Morning) Stop() {
  fmt.Println("Stop morning service...")
}

type Evening struct{}

func (e Evening) Start() {
  http.HandleFunc("/evening", evening)
  http.ListenAndServe("localhost:8081", nil)
}

func (e Evening) Stop() {
  fmt.Println("Stop evening service...")
}

func main() {
  // todo: start both services here
}
morningmorning!eveningevening

第一次尝试

main
func main() {
  var morning Morning
  morning.Start()
  defer morning.Stop()

  var evening Evening
  evening.Start()
  defer evening.Stop()
}
curl
$ curl -i http://localhost:8080/morning
HTTP/1.1 200 OK
Date: Mon, 18 Apr 2022 02:10:34 GMT
Content-Length: 9
Content-Type: text/plain; charset=utf-8

morning!
$ curl -i http://localhost:8081/evening
curl: (7) Failed to connect to localhost port 8081 after 4 ms: Connection refused
morningevening
main
func main() {
  fmt.Println("Start morning service...")
  var morning Morning
  morning.Start()
  defer morning.Stop()

  fmt.Println("Start evening service...")
  var evening Evening
  evening.Start()
  defer evening.Stop()
}

重新启动

$ go run main.go
Start morning service...
Start morning service…eveningmorning.Start()goroutine

第二次尝试

WaitGroupWaitGroupwait
func main() {
  var wg sync.WaitGroup
  wg.Add(2)

  go func() {
    defer wg.Done()
    fmt.Println("Start morning service...")
    var morning Morning
    defer morning.Stop()
    morning.Start()
  }()

  go func() {
    defer wg.Done()
    fmt.Println("Start evening service...")
    var evening Evening
    defer evening.Stop()
    evening.Start()
  }()

  wg.Wait()
}

启动试试

$ go run main.go
Start evening service...
Start morning service...
curl
$ curl -i http://localhost:8080/morning
HTTP/1.1 200 OK
Date: Mon, 18 Apr 2022 02:28:33 GMT
Content-Length: 9
Content-Type: text/plain; charset=utf-8

morning!
$ curl -i http://localhost:8081/evening
HTTP/1.1 200 OK
Date: Mon, 18 Apr 2022 02:28:36 GMT
Content-Length: 9
Content-Type: text/plain; charset=utf-8

evening!
WaitGroup
wait
go-zero

第三次尝试

go-zeroServiceGroup
import "github.com/zeromicro/go-zero/core/service"

// more code

func main() {
  group := service.NewServiceGroup()
  defer group.Stop()
  group.Add(Morning{})
  group.Add(Evening{})
  group.Start()
}
WaitGroupServiceGroupStopdefer
ServiceGroupStart/Stopgraceful shutdownSIGTERMStopHTTPserver.ShutdowngRPCserver.GracefulStop()

总结

ServiceGroup
$ cloc core/service/servicegroup.go
------------------------------------------------------------------
Language        files          blank        comment           code
------------------------------------------------------------------
Go                 1             22             14             82
------------------------------------------------------------------
go-zeroServiceGroup

我的项目地址

https://github.com/zeromicro/go-zero

go-zero

微信交换群

关注『微服务实际』公众号并点击 交换群 获取社区群二维码。