编译micro一波三折

背景

由于本机上micro版本较低,其实本机版本是0.13.0,然后用这个版本命令行调用远程rpc一直报:

1
2
3
4
5
6
{
"id": "go.micro.client.codec",
"code": 500,
"detail": "json: cannot unmarshal string into Go struct field clientResponse.id of type uint64",
"status": "Internal Server Error"
}

故需要升级本机micro版本。

升级版本: 1.5.0

升级过程可谓一波三折,坑中有坑啊。以下内容可能会让你有砸电脑的冲动,请三思而后行。

下面简单介绍升级过程

1
2
3
4
5
6
7
8
// 拉取micro源码到本地
git clone https://github.com/micro/micro.git
// 切换到v1.6.0版本
git checkout v1.6.0
// 本地基于v1.6.0版本创建一个新的v1.6.0版本分支
git checkout -b v1.6.0

以上步骤操作完成之后已在本地创建了micro的v1.6.0版本分支,下面我们会基于这个分支来编译micro。


由于本地项目中使用到了micro的第三方plugin,具体包含:

registryetcdv3
transporttcp

所以在编译micro的时候需要将这两个插件包含进来。

我们需要在源码目录中创建 plugins.go 文件用来引用这些plugin,文件目录为micro源码根目录,如下:

1
2
Dockerfile README.md bot cmd go.sum main.go new plugins.go scripts web
LICENSE api cli go.mod internal network plugin proxy service

在源码目录中新增了一个 plugins.go 文件, 文件内容如下:

1
2
3
4
5
6
package main
import (
_ "github.com/micro/go-plugins/registry/etcdv3"
_ "github.com/micro/go-plugins/transport/tcp"
)

是的,你没看错,就是这么简单,只要将依赖的插件包引用进来即可,666………………..


好了,上面文件已经创建好了,此时我们可以尝试编译micro了,话不多说,行动起来吧

1
2
// 在源码根目录运行如下命令
GO111MODULE=on go build -i -o micro main.go plugins.go

但是,现实总是残酷的,我们看到报了如下错误:

1
2
3
4
5
6
7
go: finding github.com/micro/go-plugins/registry/etcdv3 latest
go: finding github.com/micro/go-plugins/transport/tcp latest
go: finding github.com/micro/go-plugins/registry latest
go: finding github.com/micro/go-plugins/transport latest
go: finding github.com/nats-io/nats.go v1.8.2-0.20190607221125-9f4d16fe7c2d
go: github.com/nats-io/nats.go@v1.8.2-0.20190607221125-9f4d16fe7c2d: unknown revision 9f4d16fe7c2d
go: error loading module requirements

发现无法识别 github.com/nats-io/nats.go@v1.8.2-0.20190607221125-9f4d16fe7c2d

从github中查看github.com/nats-io/nats.go仓库,发现目前最新的release版本为v1.8.1,压根儿就没有v1.8.2版本。

然后开始从源码中查找哪里依赖了这个版本的仓库,发现 go-plugins@v1.1.1 依赖了。

尝试如何解决,首先查看micro源码中的go.mod文件中是否有依赖 go-plugins@v1.1.1, 内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
module github.com/micro/micro
go 1.12
require (
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e
github.com/golang/protobuf v1.3.1
github.com/google/uuid v1.1.1
github.com/gorilla/mux v1.7.2
github.com/micro/cli v0.2.0
github.com/micro/go-micro v1.6.0
github.com/serenize/snaker v0.0.0-20171204205717-a683aaf2d516
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca
golang.org/x/net v0.0.0-20190606173856-1492cefac77f
google.golang.org/genproto v0.0.0-20180831171423-11092d34479b // indirect
google.golang.org/grpc v1.21.1
)

发现并没有依赖,所以之前编译的时候使用了最新的版本。我们修改一下go-plugins版本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
module github.com/micro/micro
go 1.12
require (
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e
github.com/golang/protobuf v1.3.1
github.com/google/uuid v1.1.1
github.com/gorilla/mux v1.7.2
github.com/micro/cli v0.2.0
github.com/micro/go-micro v1.6.0
github.com/serenize/snaker v0.0.0-20171204205717-a683aaf2d516
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca
golang.org/x/net v0.0.0-20190606173856-1492cefac77f
google.golang.org/genproto v0.0.0-20180831171423-11092d34479b // indirect
google.golang.org/grpc v1.21.1
github.com/micro/go-plugins v1.1.0
)

再次编译……………………………..

哇呀呀呀呀呀呀…………………………….

又报错了/(ㄒoㄒ)/~

1
2
3
go: github.com/golang/lint@v0.0.0-20190313153728-d0100b6bd8b3: parsing go.mod: unexpected module path "golang.org/x/lint"
go: github.com/testcontainers/testcontainer-go@v0.0.2: parsing go.mod: unexpected module path "github.com/testcontainers/testcontainers-go"
go: error loading module requirements

好在这个问题已经有人踩过坑了,参考链接:https://github.com/golang/lint/issues/446#issuecomment-483638233

这个只是lint的解决方案,testcontainers同理,修改之后的go.mod如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module github.com/micro/micro
go 1.12
require (
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e
github.com/golang/protobuf v1.3.1
github.com/google/uuid v1.1.1
github.com/gorilla/mux v1.7.2
github.com/micro/cli v0.2.0
github.com/micro/go-micro v1.6.0
github.com/serenize/snaker v0.0.0-20171204205717-a683aaf2d516
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca
golang.org/x/net v0.0.0-20190606173856-1492cefac77f
google.golang.org/genproto v0.0.0-20180831171423-11092d34479b // indirect
google.golang.org/grpc v1.21.1
github.com/micro/go-plugins v1.1.0
)
replace github.com/golang/lint => golang.org/x/lint v0.0.0-20190409202823-959b441ac422
replace github.com/testcontainers/testcontainer-go => github.com/testcontainers/testcontainers-go v0.0.0-20181115231424-8e868ca12c0f

心想这回应该可以大功告成了吧,我们再来一遍。

🤮🤮🤮🤮🤮🤮🤮,”中毒身亡”

1
2
3
build command-line-arguments: cannot load github.com/hashicorp/consul/api: ambiguous import: found github.com/hashicorp/consul/api in multiple modules:
github.com/hashicorp/consul v1.4.4 (/Users/doublesouth/Documents/mycode/golang/lib/pkg/mod/github.com/hashicorp/consul@v1.4.4/api)
github.com/hashicorp/consul/api v1.1.0 (/Users/doublesouth/Documents/mycode/golang/lib/pkg/mod/github.com/hashicorp/consul/api@v1.1.0)

还有最后一口气,我们再坚持一下…

又是各种网上查找

最后功夫不负有心人,我们找到了问题的解决方案:https://github.com/hashicorp/consul/issues/6019#issuecomment-505782870

github真的是个很神奇的网站😝,再次修改go.mod:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
module github.com/micro/micro
go 1.12
require (
github.com/SAP/go-hdb v0.14.1 // indirect
github.com/StackExchange/wmi v0.0.0-20181212234831-e0a55b97c705 // indirect
github.com/armon/circbuf v0.0.0-20190214190532-5111143e8da2 // indirect
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e
github.com/coredns/coredns v1.4.0 // indirect
github.com/envoyproxy/go-control-plane v0.6.9 // indirect
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/golang/protobuf v1.3.1
github.com/google/btree v1.0.0 // indirect
github.com/google/uuid v1.1.1
github.com/gorilla/mux v1.7.2
github.com/hashicorp/consul v1.4.4 // indirect
github.com/hashicorp/go-gcp-common v0.5.0 // indirect
github.com/hashicorp/go-memdb v1.0.0 // indirect
github.com/hashicorp/go-plugin v1.0.0 // indirect
github.com/hashicorp/go-retryablehttp v0.5.3 // indirect
github.com/hashicorp/hil v0.0.0-20190212132231-97b3a9cdfa93 // indirect
github.com/hashicorp/raft-boltdb v0.0.0-20171010151810-6e5ba93211ea // indirect
github.com/hashicorp/vault v1.1.0 // indirect
github.com/hashicorp/vault-plugin-auth-alicloud v0.0.0-20190320211238-36e70c54375f // indirect
github.com/hashicorp/vault-plugin-auth-azure v0.0.0-20190320211138-f34b96803f04 // indirect
github.com/hashicorp/vault-plugin-auth-centrify v0.0.0-20190320211357-44eb061bdfd8 // indirect
github.com/hashicorp/vault-plugin-auth-kubernetes v0.0.0-20190328163920-79931ee7aad5 // indirect
github.com/hashicorp/vault-plugin-secrets-ad v0.0.0-20190327182327-ed2c3d4c3d95 // indirect
github.com/hashicorp/vault-plugin-secrets-alicloud v0.0.0-20190320213517-3307bdf683cb // indirect
github.com/hashicorp/vault-plugin-secrets-azure v0.0.0-20190320211922-2dc8a8a5e490 // indirect
github.com/hashicorp/vault-plugin-secrets-gcp v0.0.0-20190320211452-71903323ecb4 // indirect
github.com/hashicorp/vault-plugin-secrets-gcpkms v0.0.0-20190320213325-9e326a9e802d // indirect
github.com/influxdata/influxdb v1.7.5 // indirect
github.com/lyft/protoc-gen-validate v0.0.14 // indirect
github.com/mattn/go-colorable v0.1.1 // indirect
github.com/micro/cli v0.2.0
github.com/micro/go-micro v1.6.0
github.com/micro/go-plugins v1.1.0
github.com/pascaldekloe/goe v0.1.0 // indirect
github.com/serenize/snaker v0.0.0-20171204205717-a683aaf2d516
github.com/shirou/gopsutil v2.18.12+incompatible // indirect
github.com/ugorji/go/codec v0.0.0-20190320090025-2dc34c0b8780 // indirect
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca
golang.org/x/net v0.0.0-20190606173856-1492cefac77f
google.golang.org/grpc v1.21.1
layeh.com/radius v0.0.0-20190322222518-890bc1058917 // indirect
)
replace github.com/golang/lint => golang.org/x/lint v0.0.0-20190409202823-959b441ac422
replace github.com/testcontainers/testcontainer-go => github.com/testcontainers/testcontainers-go v0.0.0-20181115231424-8e868ca12c0f
replace github.com/hashicorp/consul => github.com/hashicorp/consul v1.5.1

好啦,我们在编译一下,终于大功告成了。

小伙伴,如果您觉得文章还不错,欢迎您的支持,我会继续努力创作!