您的位置 首页 golang

go-micro的安装和使用

安装步骤

安装 protobuf

protobuf 谷歌 提供的一种高效的协议数据交换格式工具库,类似于 xml , json ,但是相比他们更高效,体积更小

地址:找到对应的版本下载,解压,并配置环境变量,我在 Ubuntu

操作

方法如下

#下载

#解压 目录/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 -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” }

不迷路操作

收藏+关注

文章来源:智云一二三科技

文章标题:go-micro的安装和使用

文章地址:https://www.zhihuclub.com/101808.shtml

关于作者: 智云科技

热门文章

网站地图