<返回更多

go-micro的安装和使用

2021-09-06  石老师小跟班  
加入收藏

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.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" }

声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多资讯 >>>