我是靠谱客的博主 开朗过客,这篇文章主要介绍go micro v3 学习:gin框架集成,现在分享给大家,希望可以做个参考。

Gin框架 、Go Micro集成

  1. 初始化Gin引擎

  2. 注册路由

  3. 运行路由

  4. 添加路由handle方法中,

    • 创建服务

    • 注册微服务客户端

    • 调用服务

    • 响应Response


新建http/main.go

image.png

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package mainimport (     "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) {     // 创建服务     service := micro.NewService(         micro.Name("go.micro.web.helloworld"),     )     service.Init()     // 创建微服务客户端     client := helloworld.NewHelloworldService("helloworld", service.Client())     // 调用服务     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



最后

以上就是开朗过客最近收集整理的关于go micro v3 学习:gin框架集成的全部内容,更多相关go内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(634)

评论列表共有 0 条评论

立即
投稿
返回
顶部