1. 安装

Go Micro 是 Go 中开发微服务的 RPC 框架

1.1.1. 依赖

生成代码需要 protoc-gen-micro

1.1.2. 导入

确保导入 go-micro v2

import "github.com/micro/go-micro/v2"

1.2. 运行

Micro 提供了访问和管理微服务的运行时

1.2.1. 安装

来自源

go get github.com/micro/micro/v2

Docker 映像

docker pull micro/micro

最新版本二进制文件

# MacOS
curl -fsSL https://raw.githubusercontent.com/micro/micro/master/scripts/install.sh | /bin/bash

# Linux
wget -q  https://raw.githubusercontent.com/micro/micro/master/scripts/install.sh -O - | /bin/bash

# Windows
powershell -Command "iwr -useb https://raw.githubusercontent.com/micro/micro/master/scripts/install.ps1 | iex"

1.2.2. 使用

启动服务器

micro server

运行问候器服务

micro run github.com/micro/examples/greeter/srv

列出服务

micro list services

获取服务

micro get service go.micro.srv.greeter

输出

service  go.micro.srv.greeter

version 2019.11.09.10.34

ID      Address Metadata
go.micro.srv.greeter-e25a5edd-0936-4d32-b4d7-e62bf454d5f7       172.17.0.1:33031        broker=http,protocol=mucp,registry=mdns,server=mucp,transport=http

Endpoint: Say.Hello

Request: {
        name string
}

Response: {
        msg string
}

调用服务

micro call go.micro.srv.greeter Say.Hello '{"name": "John"}'

输出

{
    "msg": "Hello John"
}

results matching ""

    No results matching ""