|
@@ -1154,6 +1154,88 @@ func main() {
|
|
|
}
|
|
}
|
|
|
```
|
|
```
|
|
|
|
|
|
|
|
|
|
+### Run multiple service using Gin
|
|
|
|
|
+
|
|
|
|
|
+See the [question](https://github.com/gin-gonic/gin/issues/346) and try the folling example:
|
|
|
|
|
+
|
|
|
|
|
+[embedmd]:# (examples/multiple-service/main.go go)
|
|
|
|
|
+```go
|
|
|
|
|
+package main
|
|
|
|
|
+
|
|
|
|
|
+import (
|
|
|
|
|
+ "log"
|
|
|
|
|
+ "net/http"
|
|
|
|
|
+ "time"
|
|
|
|
|
+
|
|
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
|
|
+ "golang.org/x/sync/errgroup"
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+var (
|
|
|
|
|
+ g errgroup.Group
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+func router01() http.Handler {
|
|
|
|
|
+ e := gin.New()
|
|
|
|
|
+ e.Use(gin.Recovery())
|
|
|
|
|
+ e.GET("/", func(c *gin.Context) {
|
|
|
|
|
+ c.JSON(
|
|
|
|
|
+ http.StatusOK,
|
|
|
|
|
+ gin.H{
|
|
|
|
|
+ "code": http.StatusOK,
|
|
|
|
|
+ "error": "Welcome server 01",
|
|
|
|
|
+ },
|
|
|
|
|
+ )
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ return e
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func router02() http.Handler {
|
|
|
|
|
+ e := gin.New()
|
|
|
|
|
+ e.Use(gin.Recovery())
|
|
|
|
|
+ e.GET("/", func(c *gin.Context) {
|
|
|
|
|
+ c.JSON(
|
|
|
|
|
+ http.StatusOK,
|
|
|
|
|
+ gin.H{
|
|
|
|
|
+ "code": http.StatusOK,
|
|
|
|
|
+ "error": "Welcome server 02",
|
|
|
|
|
+ },
|
|
|
|
|
+ )
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ return e
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func main() {
|
|
|
|
|
+ server01 := &http.Server{
|
|
|
|
|
+ Addr: ":8080",
|
|
|
|
|
+ Handler: router01(),
|
|
|
|
|
+ ReadTimeout: 5 * time.Second,
|
|
|
|
|
+ WriteTimeout: 10 * time.Second,
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ server02 := &http.Server{
|
|
|
|
|
+ Addr: ":8081",
|
|
|
|
|
+ Handler: router02(),
|
|
|
|
|
+ ReadTimeout: 5 * time.Second,
|
|
|
|
|
+ WriteTimeout: 10 * time.Second,
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ g.Go(func() error {
|
|
|
|
|
+ return server01.ListenAndServe()
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ g.Go(func() error {
|
|
|
|
|
+ return server02.ListenAndServe()
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ if err := g.Wait(); err != nil {
|
|
|
|
|
+ log.Fatal(err)
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+```
|
|
|
|
|
+
|
|
|
### Graceful restart or stop
|
|
### Graceful restart or stop
|
|
|
|
|
|
|
|
Do you want to graceful restart or stop your web server?
|
|
Do you want to graceful restart or stop your web server?
|