micro
最后更新于:2022-04-02 02:49:15
[TOC]
## 概述
## 安装
```
go install go-micro.dev/v4/cmd/micro@master
```
## 命令
```
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
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
signup Signup to the Micro Platform
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
```
## 场景
### micro new 创建微服务
运行
```
micro new service helloworld
cd helloworld
make proto tidy
micro run
```
测试 call 这个 服务
```
micro call helloworld Helloworld.Call '{"name": "John"}'
```
### 添加链路追踪 Jaeger
```
micro new service --jaeger helloworld
```
### micro new client 构建一个 client ``` micro new client helloworld creating client helloworld cd helloworld-client make tidy micro run ``` ### micro generate 生成项目模版 ``` micro generate skaffold ``` ### micro describe 查看服务 ``` micro describe service helloworld { “名称”:“ helloworld ”, “版本”:“最新”, “元数据”:null, “端点”:[ { “名称”:“ Helloworld.Call ”, “请求”:{ “名称“:”呼叫请求“, "类型 " : " CallRequest " , " values " : [ { " name " : " name " , " type " : " string " , " values " : null } ] }, " response " : { " name " : " CallResponse " , “类型” :“ CallResponse ”, “ values ”:[ { “ name ”:“ msg ”, “ type ”:“ string ”, “ values ”:null } ] } } ],“ nodes ”:[ { “ id ”:“helloworld-9660f06a-d608-43d9-9f44-e264ff63c554 " , “地址”:“ 172.26.165.161:45059 ”, “元数据”:{ “代理”:“ http ”, “协议”:“ mucp ”, “注册表”:“ mdns ”, “服务器”:“ mucp ”, "传输“:” http" } } ] } ``` ### micro call 调用服务 ``` micro call helloworld Helloworld.Call ' {"name": "John"} ' ``` 调用流(一次触发,多次返回) ``` micro stream server helloworld Helloworld.ServerStream '{"count": 10}' {"count":0} {"count":1} {"count":2} {"count":3} {"count":4} {"count":5} {"count":6} {"count":7} {"count":8} {"count":9} ```
';
handler/helloworld.go
``` package helloworld import ( "context" log "go-micro.dev/v4/logger" "helloworld/greeter" pb "helloworld/proto" ) type Helloworld struct{} func (e *Helloworld) Call(ctx context.Context, req pb.CallRequest, rsp *pb.CallResponse) error { log.Infof("Received Helloworld.Call request: %v", req) rsp.Msg = greeter.Greet(ctx, req.Name) return nil } ```greeter/greeter.go
``` package greeter import ( "context" "fmt" "go-micro.dev/v4/cmd/micro/debug/trace" ) func Greet(ctx context.Context, name string) string { defer trace.NewSpan(ctx).Finish() return fmt.Sprint("Hello " + name) } ```### micro new client 构建一个 client ``` micro new client helloworld creating client helloworld cd helloworld-client make tidy micro run ``` ### micro generate 生成项目模版 ``` micro generate skaffold ``` ### micro describe 查看服务 ``` micro describe service helloworld { “名称”:“ helloworld ”, “版本”:“最新”, “元数据”:null, “端点”:[ { “名称”:“ Helloworld.Call ”, “请求”:{ “名称“:”呼叫请求“, "类型 " : " CallRequest " , " values " : [ { " name " : " name " , " type " : " string " , " values " : null } ] }, " response " : { " name " : " CallResponse " , “类型” :“ CallResponse ”, “ values ”:[ { “ name ”:“ msg ”, “ type ”:“ string ”, “ values ”:null } ] } } ],“ nodes ”:[ { “ id ”:“helloworld-9660f06a-d608-43d9-9f44-e264ff63c554 " , “地址”:“ 172.26.165.161:45059 ”, “元数据”:{ “代理”:“ http ”, “协议”:“ mucp ”, “注册表”:“ mdns ”, “服务器”:“ mucp ”, "传输“:” http" } } ] } ``` ### micro call 调用服务 ``` micro call helloworld Helloworld.Call ' {"name": "John"} ' ``` 调用流(一次触发,多次返回) ``` micro stream server helloworld Helloworld.ServerStream '{"count": 10}' {"count":0} {"count":1} {"count":2} {"count":3} {"count":4} {"count":5} {"count":6} {"count":7} {"count":8} {"count":9} ```