Gin框架 重父、Go Micro集成
- 初始化Gin引擎
- 注冊路由
- 運行路由
- 添加路由handle方法中摄闸,
- 創(chuàng)建服務
- 注冊微服務客戶端
- 調(diào)用服務
- 響應Response
新建http/main.go
image.png
package main
import (
"github.com/asim/go-micro/v3"
"github.com/gin-gonic/gin"
helloworld "helloworld/proto"
)
func main() {
// 初始化引擎
gin.SetMode(gin.DebugMode)
router := gin.Default()
// 注冊異常捕獲組件
router.Use(gin.Recovery())
//注冊路由
router.GET("/", handle)
//運行路由
router.Run(":8080")
}
func handle(c *gin.Context) {
// 創(chuàng)建服務
service := micro.NewService(
micro.Name("go.micro.web.helloworld"),
)
service.Init()
// 創(chuàng)建微服務客戶端
client := helloworld.NewHelloworldService("helloworld", service.Client())
// 調(diào)用服務
rsp, err := client.Call(c, &helloworld.Request{
Name: "world!",
})
if err != nil {
c.JSON(200, gin.H{"code": 500, "msg": err.Error()})
return
}
c.JSON(200, gin.H{"code": 200, "msg": rsp.Msg})
}
服務端贬蛙,為項目根目錄下 helloworld/main.go
運行服務
image.png
image.png