说明
从前面的流程看,我们似乎已经差不多把一个微服务的开发流程给实施了,一遍,当然真正的微服务开发中还会涉及自身业务处理,上面只是简单的一些请求示例的处理,而且复杂的微服务的开发等更多业务逻辑处理,而且还缺少对微服务的包含微服务组件网关、配置中心、熔断器以及具体服务如何融合起来。
基于上述的说明,我重头开始梳理一次我的开发过程,也方便自己后续回头看看。比较我是一个小白,容易忘事!
参考资料
参考:https://github.com/xbox1994/go-micro-example 此示例是micro是V1版本的,我是 改为了我现在使用的V2版本的。
环境准备
环境依赖
- windows 10 + goland
- docker 搭建我们的etcd注册中心
- golang 1.14
- protoc、protoc-gen-go、protoc-gen-micro
运行方式
因为仅仅只是开发阶段,所以运行我们也运行在Windows平台。直接的go run 即可!
整体架构
从图示流程看,我们的我们服务的流程是:
启动我们的服务,我们的相关的服务都注册到服务注册中心,且从Config Service 获取想的配置信息。启动好后等待浏览器外部请求访问。
1: 外部浏览器请求我们的以 greeter/hello ,进入到我们的Micro Api网关(Api网关启动后)
2:Micro Api网关解析请求,并进行相关的身份的验证,验证通过则把解析出来用户信息写到header中,且转向我们的RPC服务
3:网关通过验证后,在向我们的GreeterService(srv服务)发起请求访问,然后转向我们微服务内部的Greeter.Call方法中,然后处理微服务内部的逻辑,
4:然后我们的GreeterService(srv服务)此时回向我们的UserService(srv服务)进行请求,尝试用户的其他信息,header也转发过去
6:然后我们的UserService(srv服务)中根据header中得到的id查询数据库得到具体的用户信息并返回
根据架构搭建项目
可选 模块创建 命令
$ micro new --type srv --alias auth github.com/hb-go/micro/xxxxxx/srv
$ micro new --type api --alias account github.com/hb-go/micro/xxxxxx/api
$ micro new --type web --alias account github.com/hb-go/micro/xxxxxx/web
实践步骤
1):使用模板命令创建默认两个SRV微服务
D:\code\go\micro-greeter\service>micro new --gopath=false user
D:\code\go\micro-greeter\service>micro new --gopath=false greeter
2):改造我们的user.proto 和 greeter.proto
- 2.1)修改user.proto 源文件:
syntax = "proto3";
package go.micro.service.user;
service User {
rpc Call(Request) returns (Response) {}
rpc Stream(StreamingRequest) returns (stream StreamingResponse) {}
rpc PingPong(stream Ping) returns (stream Pong) {}
}
message Message {
string say = 1;
}
message Request {
string name = 1;
}
message Response {
string msg = 1;
}
message StreamingRequest {
int64 count = 1;
}
message StreamingResponse {
int64 count = 1;
}
message Ping {
int64 stroke = 1;
}
message Pong {
int64 stroke = 1;
}
修改为:
syntax = "proto3";
package go.micro.service.user;
service User {
rpc Login (Request) returns (Response) {}
rpc GetUserInfo (Empty) returns (UserInfo) {}
}
message Empty {
}
message UserInfo {
string id = 1;
string username = 2;
string password = 3;
}
message Token {
string token = 1;
bool valid = 2;
}
message Message {
string say = 1;
}
message Request {
string name = 1;
}
message Response {
string msg = 1;
}
然后生成我们的服务模板
D:\code\go\micro-greeter\service>cd user
D:\code\go\micro-greeter\service\user>protoc --proto_path=. --micro_out=. --go_out=. proto/user/user.proto
2021/01/27 16:28:46 WARNING: Missing 'go_package' option in "proto/user/user.proto",
please specify it with the full Go package path as
a future release of protoc-gen-go will require this be specified.
See https://developers.google.com/protocol-buffers/docs/reference/go-generated#package for more information.
D:\code\go\micro-greeter\service\user>
修改我们的handle下的user,go:
package handler
import (
"context"
log "github.com/micro/go-micro/v2/logger"
user "user/proto/user"
)
type User struct{}
// Call is a single request handler called via client.Call or the generated client code
func (e *User) Call(ctx context.Context, req *user.Request, rsp *user.Response) error {
log.Info("Received User.Call request")
rsp.Msg = "Hello " + req.Name
return nil
}
// Stream is a server side stream handler called via client.Stream or the generated client code
func (e *User) Stream(ctx context.Context, req *user.StreamingRequest, stream user.User_StreamStream) error {
log.Infof("Received User.Stream request with count: %d", req.Count)
for i := 0; i < int(req.Count); i++ {
log.Infof("Responding: %d", i)
if err := stream.Send(&user.StreamingResponse{
Count: int64(i),
}); err != nil {
return err
}
}
return nil
}
// PingPong is a bidirectional stream handler called via client.Stream or the generated client code
func (e *User) PingPong(ctx context.Context, stream user.User_PingPongStream) error {
for {
req, err := stream.Recv()
if err != nil {
return err
}
log.Infof("Got ping %v", req.Stroke)
if err := stream.Send(&user.Pong{Stroke: req.Stroke}); err != nil {
return err
}
}
}
修改为:
PS: 这里可以快速的使用导入实现方法的形式实现我们的相关的对象的方法:
定义好我们的结构体后,选择它,然后右键-
package handler
import (
"context"
"github.com/prometheus/common/log"
user "user/proto/user"
)
type User struct{}
func (u User) Login(ctx context.Context, request *user.Request, response *user.Response) error {
//panic("implement me")
log.Info("Received User.Call request")
response.Msg = "Hello " + request.Name
return nil
}
func (u User) GetUserInfo(ctx context.Context, empty *user.Empty, info *user.UserInfo) error {
panic("implement me")
}
然后气启动一下我们的用户服务:
、D:\code\go\micro-greeter\service\user>go run main.go
2021-01-27 16:46:25 file=v2@v2.9.1/service.go:200 level=info Starting [service] go.micro.service.user
2021-01-27 16:46:25 file=grpc/grpc.go:864 level=info Server [grpc] Listening on [::]:57651
2021-01-27 16:46:25 file=grpc/grpc.go:881 level=info Broker [http] Connected to 127.0.0.1:57652
2021-01-27 16:46:25 file=grpc/grpc.go:697 level=info Registry [mdns] Registering node: go.micro.service.user-1985d4f5-d2c6-4b06-a628-27c62b1f1ef6
2021-01-27 16:46:25 file=grpc/grpc.go:730 level=info Subscribing to topic: go.micro.service.user
然后查看我们的服务信息:
D:\code\go\micro-greeter>micro get service go.micro.service.user
service go.micro.service.user
version latest
ID Address Metadata
go.micro.service.user-1985d4f5-d2c6-4b06-a628-27c62b1f1ef6 192.168.1.213:57651 transport=grpc,broker=http,protocol=grpc,registry=mdns,server=grpc
Endpoint: User.GetUserInfo
Request: {
message_state MessageState {
no_unkeyed_literals NoUnkeyedLiterals
do_not_compare DoNotCompare
do_not_copy DoNotCopy
message_info MessageInfo
}
int32 int32
unknown_fields []uint8
}
Response: {
message_state MessageState {
no_unkeyed_literals NoUnkeyedLiterals
do_not_compare DoNotCompare
do_not_copy DoNotCopy
message_info MessageInfo
}
int32 int32
unknown_fields []uint8
id string
username string
password string
}
Endpoint: User.Login
Request: {
message_state MessageState {
no_unkeyed_literals NoUnkeyedLiterals
do_not_compare DoNotCompare
do_not_copy DoNotCopy
message_info MessageInfo
}
int32 int32
unknown_fields []uint8
name string
}
Response: {
message_state MessageState {
no_unkeyed_literals NoUnkeyedLiterals
do_not_compare DoNotCompare
do_not_copy DoNotCopy
message_info MessageInfo
}
int32 int32
unknown_fields []uint8
msg string
}
Endpoint: User.Handle
Metadata: subscriber=true,topic=go.micro.service.user
Request: {
message_state MessageState {
no_unkeyed_literals NoUnkeyedLiterals
do_not_compare DoNotCompare
do_not_copy DoNotCopy
message_info MessageInfo
}
int32 int32
unknown_fields []uint8
say string
}
Response: {}
D:\code\go\micro-greeter>
同理修改我们的另一个greeter服务:
syntax = "proto3";
package go.micro.service.greeter;
service Greeter {
rpc Call(Request) returns (Response) {}
}
message Request {
string name = 1;
}
message Response {
string msg = 1;
}
修改greeter服务的handler:
package handler
import (
"context"
log "github.com/micro/go-micro/v2/logger"
greeter "greeter/proto/greeter"
)
type Greeter struct{}
func (g Greeter) Call(ctx context.Context, req *greeter.Request, rsp *greeter.Response) error {
log.Info("Received Greeter.Call request")
rsp.Msg = "Hello " + req.Name
return nil
}
func (g Greeter) Stream(ctx context.Context, request *greeter.StreamingRequest, stream greeter.Greeter_StreamStream) error {
panic("implement me")
}
func (g Greeter) PingPong(ctx context.Context, stream greeter.Greeter_PingPongStream) error {
panic("implement me")
}
然后启动我们的服务:
D:\code\go\micro-greeter\service\greeter>go run main.go
2021-01-27 17:01:16 file=v2@v2.9.1/service.go:200 level=info Starting [service] go.micro.service.greeter
2021-01-27 17:01:16 file=grpc/grpc.go:864 level=info Server [grpc] Listening on [::]:60294
2021-01-27 17:01:16 file=grpc/grpc.go:697 level=info Registry [mdns] Registering node: go.micro.service.greeter-7dd028b3-dc0b-4442-a370-627674ca440c
查看启动的服务列表 信息:
D:\code\go\micro-greeter>micro cli
micro> list
go.micro.service.greeter
go.micro.service.user
micro.http.broker
micro>
2):修改我们的注册中心。把服务都注册到我们的ECTD上
D:\code\go\micro-greeter\service\greeter>go run main.go --registry=etcd --registry_address=192.168.219.130:2379
D:\code\go\micro-greeter\service\user>go run main.go --registry=etcd --registry_address=192.168.219.130:2379
查看我们的服务列表:(使用micro cli 进行查看)
D:\code\go\micro-greeter>micro --registry etcd --registry_address 192.168.219.130:2379 cli
micro> list
go.micro.service.greeter
go.micro.service.user
micro>
有疑问加站长微信联系(非本文作者)