go-micro是基于 Go 語言用于開發的微服務的 RPC 框架,主要功能如下:
服務發現,負載均衡 ,消息編碼,請求/響應,Async Messaging,可插拔接口,最后這個功能牛p
安裝步驟
安裝protobuf
protobuf是谷歌提供的一種高效的協議數據交換格式工具庫,類似于xml,json,但是相比他們更高效,體積更小
地址:https://github.com/protocolbuffers/protobuf/releases,找到對應的版本下載,解壓,并配置環境變量,我在Ubuntu下
操作
方法如下
#下載
https://github.com/protocolbuffers/protobuf/releases/download/v3.17.3/protoc-3.17.3-linux-x86_64.zip
#解壓 目錄/usr/local
unzip protoc-3.17.3-linux-x86_64.zip
#設置環境變量 ~/.bashrc文件下
export PATH=$PATH:/usr/local/protoc/bin
#生效
source ~/.bashrc
下載相關的依賴
go get -u github.com/golang/protobuf/proto
go get -u github.com/golang/protobuf/protoc-gen-go
//go get github.com/micro/micro/v3/cmd/protoc-gen-micro
go get github.com/asim/go-micro/cmd/protoc-gen-micro/v3
安裝v3版本的micro
go get github.com/micro/micro/v3
安裝二進制文件
#linuxr操作
wget -q https://raw.githubusercontent.com/micro/micro/master/scripts/install.sh -O - | /bin/bash
基本操作
幫助命令
micro -h
#命令如下
auth Manage authentication, accounts and rules
call Call a service e.g micro call greeter Say.Hello '{"name": "John"}'
cli Run the interactive CLI
config Manage configuration values
env Get/set micro cli environment
gen Generate a micro related dependencies e.g protobuf
get Get resources from micro
health Get the service health
init Generate a profile for micro plugins
kill Kill a service: micro kill [source]
login Interactive login flow.
logout Logout.
logs Get logs for a service e.g. micro logs helloworld
network Manage the micro service network
new Create a service template
run Run a service: micro run [source]
server Run the micro server
service Run a micro service
services List services in the registry
stats Query the stats of specified service(s), e.g micro stats srv1 srv2 srv3
status Get the status of services
store Commands for accessing the store
stream Create a service stream e.g. micro stream foo Bar.Baz '{"key": "value"}'
update Update a service: micro update [source]
user Print the current logged in user
help, h Shows a list of commands or help for one command
運行服務
micro server
登錄: 用戶名: admin 密碼:micro
micro login
登錄成功后可以查看服務
micro servers
#顯示如下
api auth broker config events network proxy registry runtime server store
查看運行狀態
micro status
查看日志
micro logs 服務名
創建服務
micro new 服務名
創建服務案例
編寫hello.proto文件
//目錄路徑:
/mnt/d/go/go-micro/proto/hello.protosyntax = "proto3";
//如果不加會報錯
//說明:option go_package = "path;name";
//path 表示生成的go文件的存放地址,會自動生成目錄的。
//name 表示生成的go文件所屬的包名
option go_package="./;hello";
//結構體
message InfoRequest{
string username=1;
}
message InfoResponse{
string msg=2;
}
//定論接口
service Hello{
rpc Info(InfoRequest)returns (InfoResponse);
}
在proto文件所在的目錄生成對應的go文件
命令如下:會在當前目錄下生成hell.pb.go文件
protoc --proto_path= . --micro_out=. --go_out=. ./hello.proto
編寫一 個server文件測試
//文件路徑
:/mnt/d/go/go-micro/proto/server.go//代碼如下
package main
import (
"fmt"
"github.com/asim/go-micro/v3"
"context"
proto "test/go-micro/proto"
)
/*
Example usage of top level service initialisation
*/
type Greeter struct{}
func (g *Greeter) Hello(ctx context.Context, req *proto.Request, rsp *proto.Response) error {
rsp.Greeting = "Hello " + req.Name
return nil
}
func main() {
// 創建一個服務
service := micro.NewService(
micro.Name("greeter"),
micro.Address(":8081"),
micro.Version("latest"),
micro.Metadata(map[string]string{
"type": "helloworld",
"content-type":"Application/json",
}),
)
//初始化
service.Init(
)
// 注冊服務
proto.RegisterGreeterHandler(service.Server(), new(Greeter))
// 啟動服務
if err := service.Run(); err != nil {
fmt.Println(err)
}
}
客戶端調用
命令如下:
micro call greeter Greeter.Hello '{"name": "John"}'
返回值如下:
{ "greeting": "Hello John" }