go-micro的安装和使用

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 #命令如下authManage authentication, accounts and rulescallCall a service e.g micro call greeter Say.Hello '{"name": "John"}'cliRun the interactive CLIconfigManage configuration valuesenvGet/set micro cli environmentgenGenerate a micro related dependencies e.g protobufgetGet resources from microhealthGet the service healthinitGenerate a profile for micro pluginskillKill a service: micro kill [source]loginInteractive login flow.logoutLogout.logsGet logs for a service e.g. micro logs helloworldnetworkManage the micro service networknewCreate a service templaterunRun a service: micro run [source]serverRun the micro serverserviceRun a micro serviceservicesList services in the registrystatsQuery the stats of specified service(s), e.g micro stats srv1 srv2 srv3statusGet the status of servicesstoreCommands for accessing the storestreamCreate a service stream e.g. micro stream foo Bar.Baz '{"key": "value"}'updateUpdate a service: micro update [source]userPrint the current logged in userhelp, hShows 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"
【go-micro的安装和使用】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",


推荐阅读