5

go-micro微服务教程三:实现用户信息微服务

 2 years ago
source link: http://entere.github.io/2019/11/11/go-micro%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%95%99%E7%A8%8B%E4%B8%89%EF%BC%9A%E5%AE%9E%E7%8E%B0%E7%94%A8%E6%88%B7%E4%BF%A1%E6%81%AF%E5%BE%AE%E6%9C%8D%E5%8A%A1/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

本示例的目的是演示一个3层的用户服务架构,获取本节代码

3层服务架构:micro api->api service->backend service

  • 网关 micro api: (localhost:8080) - http访问入口网关
  • API层 api service: (io.github.entere.api.user) - 对外暴露的API服务
  • 服务层 backend service: (io.github.entere.srv.user) - 内网的后台服务
    本节,我们先实现backend service 以下简称 srv

在 定义User原型

生成微服务骨架代码

cd micro-examples/part2
micro new user/srv --type=srv --alias=user --namespace=io.github.entere --gopath=false

修改vi user/srv/proto/user/user.proto中的内容,定义User原型

syntax = "proto3";

package io.github.entere.srv.user;
// import "google/protobuf/timestamp.proto";
service User {
rpc QueryUserByID(QueryRequest) returns (QueryResponse) {}
}

<!--more-->

message UserInfo {
string user_id = 1;
string nickname = 2;
string mobile = 3;
string avatar_url = 4;
int32 gender = 5;
uint32 created_at = 6;
uint32 updated_at = 7;
// google.protobuf.Timestamp created_at = 6;
// google.protobuf.Timestamp updated_at = 7;
}

message Error {
string msg = 1;
string info = 2;
}

message QueryRequest {
string user_id = 1;

}

message QueryResponse {
uint32 code = 1;
Error error = 2;
UserInfo data = 3;
}

上面我们定义了User服务的基本原型结构,包含用户信息UserInfo,请求QueryRequest与响应结构QueryResponse,还定义了查询用户的方法QueryUserByID。

下面我们生成类型与服务方法:

cd user/srv
protoc --proto_path=${GOPATH}/src:. --micro_out=. --go_out=. proto/user/user.proto

编写用户模型服务

vi handler/user.go

调整QueryUserByID方法:


package handler

import (
"context"

user "github.com/entere/micro-examples/part2/user/srv/proto/user"
)

type User struct{}

// Call is a single request handler called via client.Call or the generated client code
func (e *User) QueryUserByID(ctx context.Context, req *user.QueryRequest, rsp *user.QueryResponse) error {

userID := req.UserId
if userID == "" {
userID = "xxxxxx"
}
userInfo := &user.UserInfo{
UserId: userID,
Nickname: "匿名",
Mobile: "138********",
AvatarUrl: "avatar.jpg",
Gender: 1,
}
rsp.Code = 200
rsp.Error = nil
rsp.Data = userInfo

return nil
}

修改main.go 注册微服务


vi main.go

package main

import (
"github.com/entere/micro-examples/part2/user/srv/handler"
"github.com/micro/go-micro"
"github.com/micro/go-micro/util/log"

user "github.com/entere/micro-examples/part2/user/srv/proto/user"
)

func main() {
// 新建服务
service := micro.NewService(
micro.Name("io.github.entere.srv.user"),
micro.Version("latest"),
)

// 初始化服务

service.Init()

// 注册服务
user.RegisterUserHandler(service.Server(), new(handler.User))

// 启动服务
if err := service.Run(); err != nil {
log.Fatal(err)
}
}
make build
./user-srv

micro call io.github.entere.srv.user User.QueryUserByID '{"user_id":"543987654"}'
{
"code": 200,
"data": {
"user_id": "543987654",
"nickname": "匿名",
"mobile": "138********",
"avatar_url": "avatar.jpg",
"gender": 1
}
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK