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 rulescall Call a service e。g micro call greeter Say。Hello ‘{“name”: “John”}’cli Run the interactive CLIconfig Manage configuration valuesenv Get/set micro cli environmentgen Generate a micro related dependencies e。g protobufget Get resources from microhealth Get the service healthinit Generate a profile for micro pluginskill Kill a service: micro kill [source]login Interactive login flow。logout Logout。logs Get logs for a service e。g。 micro logs helloworldnetwork Manage the micro service networknew Create a service templaterun Run a service: micro run [source]server Run the micro serverservice Run a micro serviceservices List services in the registrystats Query the stats of specified service(s), e。g micro stats srv1 srv2 srv3status Get the status of servicesstore Commands for accessing the storestream 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 userhelp, 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。proto
syntax = “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” }
不迷路操作
收藏+關注