go-micro教程 — 第一章 快速入门
1 参考文档
1、官方仓库:https://github.com/micro/micro的README.md,注意更新记录。
2、博客:https://micro.dev/getting-started【推荐
】
注意
:官方仓库里的文档比博客讲解的要简略,特别是README.md如果有更新,则可能会与博客讲解的有不一致的地方,所以这里推荐参照博客讲解的来操作。
2 所需依赖
go-micro
需要 protoc-gen-micro
来生成代码。所以需要一下依赖:
- protobuf
- protoc-gen-go
- protoc-gen-micro
下载最新版本的依赖
go get https://github.com/protocolbuffers/protobuf/releases
go get github.com/golang/protobuf/protoc-gen-go
go get github.com/micro/micro/v3/cmd/protoc-gen-micro
3 安装 micro
目前最新版本的micro是3.0版本的,所以这里我们安装的也是3.0版本的micro。
3.1 Go Get方式安装:
go install github.com/micro/micro/v3@latest
或者
go install github.com/micro/micro
go install github.com/micro/micro
这个是今天最新发布出来的下载地址【2022年6月30号】,如果下载失败,可以改为第一个地址来下载。
3.2 二进制版本
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"
3.3 Docker 镜像
docker pull micro/micro
4 运行 micro 服务
4.1 启动 micro server 服务
首先,我们必须启动micro server
。执行此操作的命令是:
micro server
启动结果:
F:\Goland\micro-demo>micro server
2022-07-02 20:16:42 file=server/server.go:88 level=info Starting server
2022-07-02 20:16:42 file=server/server.go:116 level=info Registering registry
2022-07-02 20:16:42 file=server/server.go:116 level=info Registering broker
2022-07-02 20:16:42 file=server/server.go:116 level=info Registering network
2022-07-02 20:16:42 file=server/server.go:116 level=info Registering runtime
2022-07-02 20:16:42 file=server/server.go:116 level=info Registering config
2022-07-02 20:16:42 file=server/server.go:116 level=info Registering store
2022-07-02 20:16:43 file=server/server.go:116 level=info Registering events
2022-07-02 20:16:43 file=server/server.go:116 level=info Registering auth
2022-07-02 20:16:43 file=server/server.go:116 level=info Registering proxy
2022-07-02 20:16:43 file=server/server.go:116 level=info Registering api
2022-07-02 20:16:43 file=server/server.go:116 level=info Registering web
2022-07-02 20:16:43 file=server/server.go:206 level=info Starting server runtime
4.2 登录Micro
在使用 micro server
前需要先登录服务,默认的账号密码如下:
账号/密码:admin/micro
F:\Goland\micro-demo>micro login
Enter username: admin
Enter password:
Successfully logged in.
查看 micro server
中正在运行的服务,命令:
F:\Goland\micro-demo>micro services
api
auth
broker
config
events
network
proxy
registry
runtime
store
web
一共11个,所有这些服务都是micro server中的子模块,在浏览器端查看 localhost:8082 。
4.3 运行 helloworld
运行服务的命令是micro run
,注意运行前需要先执行3.2的登陆操作,否则可能会提示:
E:\SoftwareData\Goland\go-micro-demo>micro run github.com/micro/services/helloworld
Not authorized to perform this request
运行 helloworld
的命令:
micro run github.com/micro/services/helloworld
注意
:我电脑上执行完并没有运行成功,而是报错了,报错如下,即拒绝获取,或者报Request timed out,暂未找到解决办法。
F:\Goland\micro-demo>micro run github.com/micro/services/helloworld
Can't create source file C:\WINDOWS\github.com-micro-services-vZ0PIme7g.zip src: open C:\WINDOWS\github.com-micro-services-vZ0PIme7g.zip
: Access is denied.
4.4 查看运行结果
因4.3 helloworld未运行成功,这里暂未验证
4.4.1 查看正在运行的服务的状态
因4.3 helloworld未运行成功,这里暂未验证
F:\Goland\micro-demo>micro status
NAME VERSION SOURCE STATUS BUILD UPDATED METADATA
helloworld latest github.com/micro/services/helloworld running n/a 4s ago owner=admin, group=micro
4.4.2 查看服务的日志
因4.3 helloworld未运行成功,这里暂未验证
我们还可以查看服务的日志以验证它是否正在运行:
F:\Goland\micro-demo>micro logs helloworld
2022-6-06 17:52:21 file=service/service.go:195 level=info Starting [service] helloworld
2022-6-06 17:52:21 file=grpc/grpc.go:902 level=info Server [grpc] Listening on [::]:33975
2022-6-06 17:52:21 file=grpc/grpc.go:732 level=info Registry [service] Registering node: helloworld-67627b23-3336-4b92-a032-09d8d13ecf95
5 调用服务
因4.3 helloworld未运行成功,这里暂未验证
Micro 自动为您的服务生成 CLI 命令,格式为:micro [service] [method]
,默认方法为“调用”。参数可以作为标志传递,因此我们可以使用以下方式调用我们的服务:
$ micro helloworld call --name=Jane
{
"msg": "Hello Jane"
}
5.1 查看服务节点
如果我们想知道服务有哪些节点,我们可以运行以下命令:
F:\Goland\micro-demo>micro helloworld --help
NAME:
micro helloworld
VERSION:
latest
USAGE:
micro helloworld [command]
COMMANDS:
call
5.2 使用 API 方式调用
Micro 在 8080端口上开放了一个 http API,所以可以使用 curl
方式调用。
curl "http://localhost:8080/helloworld?name=John"
5.3 客户端方式调用
编写一个可以用来调用helloworld
服务的客户端。
package main
import (
"context"
"fmt"
"time"
"github.com/micro/micro/v3/service"
proto "github.com/micro/services/helloworld/proto"
)
func main() {
// create and initialise a new service
srv := service.New()
// create the proto client for helloworld
client := proto.NewHelloworldService("helloworld", srv.Client())
// call an endpoint on the service
rsp, err := client.Call(context.Background(), &proto.CallRequest{
Name: "John",
})
if err != nil {
fmt.Println("Error calling helloworld: ", err)
return
}
// print the response
fmt.Println("Response: ", rsp.Message)
// let's delay the process for exiting for reasons you'll see below
time.Sleep(time.Second * 5)
}
运行:
micro run .
运行成功后,不打印任何输出。查看正在运行的内容的一个有用命令是micro status
。此时我们应该运行两个服务:
micro status
NAME VERSION SOURCE STATUS BUILD UPDATED METADATA
example latest example.tar.gz running n/a 2s ago owner=admin, group=micro
helloworld latest github.com/micro/services/helloworld running n/a 5m59s ago owner=admin, group=micro
6 创建服务
要创建新服务,使用micro new
命令。它会输出类似于以下的内容:
F:\Goland\micro-demo>micro new helloworld
Creating service helloworld
.
├── micro.mu
├── main.go
├── generate.go
├── handler
│ └── helloworld.go
├── proto
│ └── helloworld.proto
├── Dockerfile
├── Makefile
├── README.md
├── .gitignore
└── go.mod
download protoc zip packages (protoc-$VERSION-$PLATFORM.zip) and install:
visit https://github.com/protocolbuffers/protobuf/releases
compile the proto file helloworld.proto:
cd helloworld
make init
go mod vendor
make proto
按照说明编译 helloworld.proto
即可。
7 总结
因有些服务启动失败,本文的 五、六步骤并未真正验证实际情况。