偶尔报错 {"msg": "rpc error: code = Unimplemented desc = Method not found!"}

来源:25-3 登录逻辑完善

Josephhhhh

2021-08-27

跟老师一模一样的代码,每次发送login请求,如果是发送间隔长一点就能每次都返回200,如果连续不断点击发送,会返回200或者400 - {“msg”: “rpc error: code = Unimplemented desc = Method not found!”}
为什么呢

client (go) 端的代码


func PasswordLogin(ctx *gin.Context) {
	// form validation
	passwordLoginForm := forms.PasswordLoginForm{}
	// read body and assign to passwordLoginForm
	if err := ctx.ShouldBindJSON(&passwordLoginForm); err != nil {
		HandleValidatorError(ctx, err)
		return
	}

	// connect to grpc
	ip := global.ServerConfig.UserSrv.Host
	port := global.ServerConfig.UserSrv.Port
	conn, err := grpc.Dial(fmt.Sprintf("%s:%d", ip, port),
		grpc.WithInsecure(),
		grpc.WithUnaryInterceptor(middleware.RequestInterceptor))
	if err != nil {
		zap.S().Errorw("[GetUserList] faild to connect to user server",
			"msg", err.Error(),
		)
	}

	// generate grpc client
	userSrvClient := proto.NewUserClient(conn)

	// login
	userRsp, err := userSrvClient.GetUserByUsername(context.Background(), &proto.UsernameRequest{Username: passwordLoginForm.Username})
	if err != nil {
		if e, ok := status.FromError(err); ok {
			switch e.Code() {
			case codes.NotFound:
				ctx.JSON(http.StatusBadRequest, gin.H{
					"username": "Username doesn't exist",
				})
			default:
				ctx.JSON(http.StatusInternalServerError, gin.H{
					"username": "Failed Login",
				})
			}
		}
	} else {
		// validate password
		passwordValidationResponse, err := userSrvClient.ValidatePassword(context.Background(), &proto.PasswordValidationInfo{
			Password:          passwordLoginForm.Password,
			EncryptedPassword: userRsp.Password,
		})
		if err != nil {
			zap.S().Errorw("Failed Validating Password",
				"msg", err.Error(),
			)
			ctx.JSON(http.StatusBadRequest, gin.H{
				"msg": "Failed Validating Password",
			})
		} else {
			if passwordValidationResponse.Success {
				ctx.JSON(http.StatusOK, gin.H{
					"msg": "login success",
				})
			} else {
				ctx.JSON(http.StatusOK, gin.H{
					"password": "invalid password",
				})
			}

		}
	}

Server 端的代码

    @logger.catch
    def ValidatePassword(self, request: user_pb2.PasswordValidationInfo, context):
        """
        Validate password
        :param request:
        :param context:
        :return:
        """
        is_success = pbkdf2_sha256.verify(request.password, request.encryptedPassword)
        return user_pb2.PasswordValidationResponse(success=is_success)

我还在client端在invoke之前加了个interceptor

连续发送四次请求,前两次400, 后两次200
结果是

[GIN] 2021/08/27 - 20:02:04 | 400 |    4.698033ms |       127.0.0.1 | POST     "/u/v1/user/login"
2021-08-27T20:02:05.118+1000    INFO    middleware/interceptors.go:19   Sending request: /User/GetUserByUsername
2021-08-27T20:02:05.123+1000    INFO    middleware/interceptors.go:19   Sending request: /User/ValidatePassword
2021-08-27T20:02:05.124+1000    ERROR   api/user.go:180 Failed Validating Password      {"msg": "rpc error: code = Unimplemented desc = Method not found!"}
mxshop-api/user-web/api.PasswordLogin
        /home/josephli/go/src/mxshop-api/user-web/api/user.go:180
github.com/gin-gonic/gin.(*Context).Next
        /home/josephli/go/pkg/mod/github.com/gin-gonic/gin@v1.7.4/context.go:165
github.com/gin-gonic/gin.CustomRecoveryWithWriter.func1
        /home/josephli/go/pkg/mod/github.com/gin-gonic/gin@v1.7.4/recovery.go:99
github.com/gin-gonic/gin.(*Context).Next
        /home/josephli/go/pkg/mod/github.com/gin-gonic/gin@v1.7.4/context.go:165
github.com/gin-gonic/gin.LoggerWithConfig.func1
        /home/josephli/go/pkg/mod/github.com/gin-gonic/gin@v1.7.4/logger.go:241
github.com/gin-gonic/gin.(*Context).Next
        /home/josephli/go/pkg/mod/github.com/gin-gonic/gin@v1.7.4/context.go:165
github.com/gin-gonic/gin.(*Engine).handleHTTPRequest
        /home/josephli/go/pkg/mod/github.com/gin-gonic/gin@v1.7.4/gin.go:489
github.com/gin-gonic/gin.(*Engine).ServeHTTP
        /home/josephli/go/pkg/mod/github.com/gin-gonic/gin@v1.7.4/gin.go:445
net/http.serverHandler.ServeHTTP
        /usr/local/go/src/net/http/server.go:2887
net/http.(*conn).serve
        /usr/local/go/src/net/http/server.go:1952
[GIN] 2021/08/27 - 20:02:05 | 400 |    5.666175ms |       127.0.0.1 | POST     "/u/v1/user/login"
2021-08-27T20:02:06.007+1000    INFO    middleware/interceptors.go:19   Sending request: /User/GetUserByUsername
2021-08-27T20:02:06.011+1000    INFO    middleware/interceptors.go:19   Sending request: /User/ValidatePassword
2021-08-27T20:02:06.011+1000    ERROR   api/user.go:180 Failed Validating Password      {"msg": "rpc error: code = Unimplemented desc = Method not found!"}
mxshop-api/user-web/api.PasswordLogin
        /home/josephli/go/src/mxshop-api/user-web/api/user.go:180
github.com/gin-gonic/gin.(*Context).Next
        /home/josephli/go/pkg/mod/github.com/gin-gonic/gin@v1.7.4/context.go:165
github.com/gin-gonic/gin.CustomRecoveryWithWriter.func1
        /home/josephli/go/pkg/mod/github.com/gin-gonic/gin@v1.7.4/recovery.go:99
github.com/gin-gonic/gin.(*Context).Next
        /home/josephli/go/pkg/mod/github.com/gin-gonic/gin@v1.7.4/context.go:165
github.com/gin-gonic/gin.LoggerWithConfig.func1
        /home/josephli/go/pkg/mod/github.com/gin-gonic/gin@v1.7.4/logger.go:241
github.com/gin-gonic/gin.(*Context).Next
        /home/josephli/go/pkg/mod/github.com/gin-gonic/gin@v1.7.4/context.go:165
github.com/gin-gonic/gin.(*Engine).handleHTTPRequest
        /home/josephli/go/pkg/mod/github.com/gin-gonic/gin@v1.7.4/gin.go:489
github.com/gin-gonic/gin.(*Engine).ServeHTTP
        /home/josephli/go/pkg/mod/github.com/gin-gonic/gin@v1.7.4/gin.go:445
net/http.serverHandler.ServeHTTP
        /usr/local/go/src/net/http/server.go:2887
net/http.(*conn).serve
        /usr/local/go/src/net/http/server.go:1952
[GIN] 2021/08/27 - 20:02:06 | 400 |    4.593112ms |       127.0.0.1 | POST     "/u/v1/user/login"
2021-08-27T20:02:06.607+1000    INFO    middleware/interceptors.go:19   Sending request: /User/GetUserByUsername
2021-08-27T20:02:06.614+1000    INFO    middleware/interceptors.go:19   Sending request: /User/ValidatePassword
[GIN] 2021/08/27 - 20:02:06 | 200 |   27.850795ms |       127.0.0.1 | POST     "/u/v1/user/login"
2021-08-27T20:02:07.830+1000    INFO    middleware/interceptors.go:19   Sending request: /User/GetUserByUsername
2021-08-27T20:02:07.835+1000    INFO    middleware/interceptors.go:19   Sending request: /User/ValidatePassword
[GIN] 2021/08/27 - 20:02:07 | 200 |   28.932633ms |       127.0.0.1 | POST     "/u/v1/user/login"

两次请求的method name都是一样的,为什么就会not implemented呢;
我还在server端加了interceptor, 400 那次没有收到任何request

server 端的log

2021-08-27 20:01:56.425 | INFO     | __main__:serve:53 - User Service Starting on 127.0.0.1:50051
2021-08-27 20:02:06.609 | INFO     | user_srv.interceptors.interceptors:intercept:18 - received request method: /User/GetUserByUsername;
2021-08-27 20:02:06.615 | INFO     | user_srv.interceptors.interceptors:intercept:18 - received request method: /User/ValidatePassword;
2021-08-27 20:02:07.831 | INFO     | user_srv.interceptors.interceptors:intercept:18 - received request method: /User/GetUserByUsername;
2021-08-27 20:02:07.835 | INFO     | user_srv.interceptors.interceptors:intercept:18 - received request method: /User/ValidatePassword;

proto 也同步重新生成过了, 不然也不可能有200

头大

写回答

1回答

Josephhhhh

提问者

2021-08-27

server 端换了个端口竟然就可以了 也是无语 什么原因呢

0
3
bobby
回复
Josephhhhh
我之前有时候遇到过 端口被占用但是并未报错的情况,为了防止这个问题,你可以设置一个新的未使用过的端口然后测试看看
2021-08-30
共3条回复

Go+Python打造电商系统 自研微服务框架 抓紧高薪机遇

快速转型Go工程师,成为具备双语言后端能力的开发者

508 学习 · 530 问题

查看课程