Go-Micro客户端请求报500错误的解决方法

Go-Micro客户端请求报500错误的解决方法

1.服务端代码

复制代码
package main

import (
    "github.com/gin-gonic/gin"
    "github.com/micro/go-micro/registry"
    "github.com/micro/go-micro/web"
    "github.com/micro/go-plugins/registry/consul"
    "net/http"
)

func main() {
   
    consulReg := consul.NewRegistry(registry.Addrs(":8500"))
    engine := gin.Default()
    engine.POST("/hello", func(c *gin.Context) {
   
        c.JSON(http.StatusOK, gin.H{
   
            "msg": "hello,world",
        })
    })
    service := web.NewService(
        web.Name("cas"),
        web.Address(":8001"),
        web.Registry(consulReg),
        web.Handler(engine),
    )
    service.Init()
    service.Run()
}

2.客户端代码

javascript 复制代码
复制代码
package main

import (
    "context"
    "github.com/micro/go-micro/client"
    "github.com/micro/go-micro/client/selector"
    "github.com/micro/go-micro/registry"
    "github.com/micro/go-plugins/client/http"
    "github.com/micro/go-plugins/registry/consul"
    "log"
)

func main() {
   
    consulReg := consul.NewRegistry(registry.Addrs(":8500"))
    selector := selector.NewSelector(
        selector.Registry(consulReg),
        selector.SetStrategy(selector.RoundRobin),
    )
    httpClient := http.NewClient(
        // 选择器
        client.Selector(selector),
        // 响应格式默认格式protobuf,设置为json
        client.ContentType("application/json"),
        )
    req := map[string]string{
   }
    request := httpClient.NewRequest("cas", "/hello", req)
    rsp := map[string]interface{
   }{
   }
    err := httpClient.Call(context.Background(), request, &rsp)
    if err != nil {
   
        log.Fatalf("request err: %+v", err)
    }
    log.Printf("%+v",rsp)
}
javascript 复制代码
javascript 复制代码
 

3.发起请求报错

客户端请求报错如下:
代码语言:javascript
复制

javascript 复制代码
{"id":"go.micro.client","code":500,"detail":"none available","status":"Internal Server Error"}

4.问题分析

1.顺着客户端调用的Call()方法,进入源码github.com\micro\go-plugins\client\http\http.go,找到获取服务节点的方法:

复制代码
// get next nodes from the selector
    next, err := h.next(req, callOpts)

2.再继续查看next()方法,找到第63行,这里为Selector节点选择器添加了过滤器,传递了两个参数"protocol", "http",可以发现是个键值对:

复制代码
// only get the things that are of mucp protocol
	selectOptions := append(opts.SelectOptions, selector.WithFilter(
		selector.FilterLabel("protocol", "http"),
	))

3.进一步进入FilterLabel()方法,在第41行可以发现,上一步传的两个参数在这里做了校验,分别作为的Metadata(元数据)的map的键和值,相当于验证协议需要为http:

javascript 复制代码
复制代码
    if node.Metadata[key] == val {
   
                    nodes = append(nodes, node)
                }
javascript 复制代码
javascript 复制代码

4.回到http.go的69行,如果不满足http协议,则获取服务节点失败,返回我们所遇到的这个err:

复制代码
// get next nodes from the selector
    next, err := h.opts.Selector.Select(service, selectOptions...)
    if err != nil && err == selector.ErrNotFound {
   
        return nil, errors.NotFound("go.micro.client", err.Error())
    } else if err != nil {
   
        return nil, errors.InternalServerError("go.micro.client", err.Error())
    }

到这里其实已经可以基本确定我们遇到的问题了:在使用go-plugins插件进行服务调用时,在服务发现时为选择器添加了过滤,限定了请求协议,要求Metadata的键值必须为"protocol":"http",否则返回的服务节点切片长度将为0。

5.解决方法

因此解决方法则是在服务端进行服务注册的时候,为注册的服务添加上Metadata配置,指定请求协议为http:

复制代码
    service := web.NewService(
        web.Name("cas"),
        web.Address(":8001"),
        web.Registry(consulReg),
        web.Handler(engine),
        // 为注册的服务添加Metadata,指定请求协议为http
        web.Metadata(map[string]string{
   "protocol" : "http"}),
    )

在指定了服务的请求协议后,成功解决该问题~😄

相关推荐
为何创造硅基生物6 小时前
C语言 结构体内存对齐规则(通俗易懂版)
c语言·开发语言
吃好睡好便好6 小时前
在Matlab中绘制横直方图
开发语言·学习·算法·matlab
星寂樱易李6 小时前
iperf3 + Python-- 网络带宽、网速、网络稳定性
开发语言·网络·python
仰泳之鹅6 小时前
【C语言】自定义数据类型2——联合体与枚举
c语言·开发语言·算法
之歆7 小时前
DAY_12JavaScript DOM 完全指南(二):实战与性能篇
开发语言·前端·javascript·ecmascript
姚不倒7 小时前
Go语言进阶:接口、错误处理与并发编程(goroutine/channel/context)
云原生·golang
candyTong7 小时前
Claude Code 的 Edit 工具是怎么工作的
javascript·后端·架构
cen__y8 小时前
Linux12(Git01)
linux·运维·服务器·c语言·开发语言·git
AI人工智能+电脑小能手8 小时前
【大白话说Java面试题 第65题】【JVM篇】第25题:谈谈对 OOM 的认识
java·开发语言·jvm
GetcharZp8 小时前
GitHub 2.4 万 Star!D2 正在重新定义程序员画图方式
后端