环境搭建
注意事项:不要使用go 1.15,不然会一系列依赖问题的引发问题
注意事项:不要使用go 1.15,不然会一系列依赖问题的引发问题
注意事项:不要使用go 1.15,不然会一系列依赖问题的引发问题
开发环境:windos10 + goland + go 1.14 SDK
项目新建
(1) 新建项目(多版本的情况下)
image.png image.png image.png(2)配置代理
image.png(3)查看新建项目MOD
image.png(4)设置相关的环境变量信息
#linux 下
export GO111MODULE=on
export GOPROXY=https://goproxy.io
# windows下设置如下环境变量
setx GO111MODULE on
setx GOPROXY https://goproxy.io
image.png
(5)安装Mirco和GO-MIRCO过程遇到问题示例
可能会出现为问题现象:
1:使用的Mirco版本问题
使用 go get github.com/micro/micro 它会下载的是V1版本的
D:\code\go\Mceshi>go get github.com/micro/micro
go: github.com/micro/micro upgrade => v1.18.0
2:使用go get -u -v github.com/micro/micro 下载micro 的问题
go get: github.com/mholt/certmagic@v0.8.3 updating to
github.com/mholt/certmagic@v0.12.0: parsing go.mod:
module declares its path as: github.com/caddyserver/certmagic
but was required as: github.com/mholt/certmagic
3:使用go get -u -v github.com/micro/go-micr之后引发系列的
# github.com/coreos/etcd/clientv3/balancer/resolver/endpoint
C:\Users\mayn\go\pkg\mod\github.com\coreos\etcd@v3.3.25+incompatible\clientv3\balancer\resolver\endpoint\endpoint.go:114:78: undefined: resolver.BuildOption
C:\Users\mayn\go\pkg\mod\github.com\coreos\etcd@v3.3.25+incompatible\clientv3\balancer\resolver\endpoint\endpoint.go:182:31: undefined: resolver.ResolveNowOption
# github.com/coreos/etcd/clientv3/balancer/picker
C:\Users\mayn\go\pkg\mod\github.com\coreos\etcd@v3.3.25+incompatible\clientv3\balancer\picker\err.go:37:44: undefined: balancer.PickOptions
C:\Users\mayn\go\pkg\mod\github.com\coreos\etcd@v3.3.25+incompatible\clientv3\balancer\picker\roundrobin_balanced.go:55:54: undefined: balancer.PickOptions
# github.com/micro/go-micro/transport/quic
C:\Users\mayn\go\pkg\mod\github.com\micro\go-micro@v1.18.0\transport\quic\quic.go:54:12: q.s.Close undefined (type quic.Session has no field or method Close)
C:\Users\mayn\go\pkg\mod\github.com\micro\go-micro@v1.18.0\transport\quic\quic.go:121:3: unknown field 'IdleTimeout' in struct literal of type quic.Config
解决问题3:
go mod edit -require=google.golang.org/grpc@v1.26.0
或
replace google.golang.org/grpc => google.golang.org/grpc v1.26.0
然后就剩余问题:
# github.com/micro/go-micro/transport/quic
C:\Users\mayn\go\pkg\mod\github.com\micro\go-micro@v1.18.0\transport\quic\quic.go:54:12: q.s.Close undefined (type quic.Session has no field or method Close)
C:\Users\mayn\go\pkg\mod\github.com\micro\go-micro@v1.18.0\transport\quic\quic.go:121:3: unknown field 'IdleTimeout' in struct literal of type quic.Config
然后继续解决问题:
replace github.com/lucas-clemente/quic-go => github.com/lucas-clemente/quic-go v0.14.1
然后新问题:
# github.com/lucas-clemente/quic-go/internal/handshake
C:\Users\mayn\go\pkg\mod\github.com\lucas-clemente\quic-go@v0.14.1\internal\handshake\crypto_setup.go:433:40: not enough arguments in call to h.conn.GetSessionTicket
have ()
want ([]byte)
C:\Users\mayn\go\pkg\mod\github.com\lucas-clemente\quic-go@v0.14.1\internal\handshake\qtls.go:109:3: cannot use c.Certificates (type []tls.Certificate) as type []qtls.Certificate in field value
C:\Users\mayn\go\pkg\mod\github.com\lucas-clemente\quic-go@v0.14.1\internal\handshake\qtls.go:110:3: cannot use c.NameToCertificate (type map[string]*tls.Certificate) as type map[string]*qtls.Certificate in field value
C:\Users\mayn\go\pkg\mod\github.com\lucas-clemente\quic-go@v0.14.1\internal\handshake\qtls.go:111:3: cannot use c.GetCertificate (type func(*tls.ClientHelloInfo) (*tls.Certificate, error)) as type func(*qtls.ClientHelloInfo) (*qtls.
Certificate, error) in field value
C:\Users\mayn\go\pkg\mod\github.com\lucas-clemente\quic-go@v0.14.1\internal\handshake\qtls.go:112:3: cannot use c.GetClientCertificate (type func(*tls.CertificateRequestInfo) (*tls.Certificate, error)) as type func(*qtls.Certificate
RequestInfo) (*qtls.Certificate, error) in field value
C:\Users\mayn\go\pkg\mod\github.com\lucas-clemente\quic-go@v0.14.1\internal\handshake\qtls.go:113:3: cannot use getConfigForClient (type func(*tls.ClientHelloInfo) (*qtls.Config, error)) as type func(*qtls.ClientHelloInfo) (*qtls.Co
nfig, error) in field value
去找好像quic-go@v0.14.1好像这个版本已经没了!!!
切换其他版本!
image.png
会发现就算切换版本也会出现问题:
image.png
(6)基于V2版本的安装(个人能通的流程)
参考大神:go-micro V2 从零开始(一)使用micro工具自动生成项目
- 1)安装V2版本的Mirco
D:\code\go\Mi_Onse>go get github.com/micro/micro/v2
image.png
-
2) 查看安装情况
image.png -
3) 当前目录下生产一个微服务示例
操作:
D:\code\go\Mi_Onse>micro new --gopath=false greeter
结果:
D:\code\go\Mi_Onse>micro new --gopath=false greeter
Creating service go.micro.service.greeter in greeter
.
├── main.go
├── generate.go
├── plugin.go
├── handler
│ └── greeter.go
├── subscriber
│ └── greeter.go
├── proto
│ └── greeter
│ └── greeter.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
download protobuf for micro:
go get -u github.com/golang/protobuf/proto
go get -u github.com/golang/protobuf/protoc-gen-go
go get github.com/micro/micro/v2/cmd/protoc-gen-micro
compile the proto file greeter.proto:
cd greeter
make proto
image.png
PS:1,5版本下执行的话:
会出现:
D:\code\go\M5>micro new --gopath=false greeter
panic: qtls.ConnectionState not compatible with tls.ConnectionState
goroutine 1 [running]:
github.com/lucas-clemente/quic-go/internal/handshake.init.1()
C:/Users/mayn/go/pkg/mod/github.com/lucas-clemente/quic-go@v0.14.1/internal/handshake/unsafe.go:17 +0x139
D:\code\go\M5>
- 4) CD 到我们的微服务下cd greeter,使用protoc然后生成我们的micro的代码模板
操作:
D:\code\go\Mi_Onse\greeter>protoc --proto_path=. --micro_out=. --go_out=. proto/greeter/greeter.proto
结果:
D:\code\go\Mi_Onse\greeter>protoc --proto_path=. --micro_out=. --go_out=. proto/greeter/greeter.proto
2021/01/20 11:59:12 WARNING: Missing 'go_package' option in "proto/greeter/greeter.proto",
please specify it with the full Go package path as
a future release of protoc-gen-go will require this be specified.
See https://developers.google.com/protocol-buffers/docs/reference/go-generated#package for more information.
image.png
查看代码后我们就会报错,是因为上面我们没按生产模板指引去安装相关的依赖:
image.png
这里无妨,可以暂时忽略,我们这时候可以直接启动看看服务:
- 5) 启动我们的刚刚新建的服务示例
D:\code\go\Mi_Onse\greeter>go run main.go
启动结果:
D:\code\go\Mi_Onse\greeter>go run main.go
go: finding module for package github.com/micro/go-micro/v2/logger
go: finding module for package github.com/micro/go-micro/v2
go: finding module for package github.com/micro/go-micro/v2/server
go: finding module for package github.com/golang/protobuf/proto
go: finding module for package github.com/micro/go-micro/v2/client
go: finding module for package google.golang.org/protobuf/reflect/protoreflect
go: finding module for package google.golang.org/protobuf/runtime/protoimpl
go: finding module for package github.com/micro/go-micro/v2/api
go: found github.com/micro/go-micro/v2 in github.com/micro/go-micro/v2 v2.9.1
go: found github.com/golang/protobuf/proto in github.com/golang/protobuf v1.4.3
go: found google.golang.org/protobuf/reflect/protoreflect in google.golang.org/protobuf v1.25.0
2021-01-20 12:02:04 file=v2@v2.9.1/service.go:200 level=info Starting [service] go.micro.service.greeter
2021-01-20 12:02:04 file=grpc/grpc.go:864 level=info Server [grpc] Listening on [::]:59501
2021-01-20 12:02:04 file=grpc/grpc.go:881 level=info Broker [http] Connected to 127.0.0.1:59502
2021-01-20 12:02:04 file=grpc/grpc.go:697 level=info Registry [mdns] Registering node: go.micro.service.greeter-fa6ee1bb-bef8-46ec-8884-e3be9810ec6c
2021-01-20 12:02:04 file=grpc/grpc.go:730 level=info Subscribing to topic: go.micro.service.greeter
-
6) 再去查看我们我们的上面错误异常,发现已经取消了
image.png - 7) 再去查看新建的微服务的下的go.mod文件为:
module greeter
go 1.13
// This can be removed once etcd becomes go gettable, version 3.4 and 3.5 is not,
// see https://github.com/etcd-io/etcd/issues/11154 and https://github.com/etcd-io/etcd/issues/11931.
replace google.golang.org/grpc => google.golang.org/grpc v1.26.0
require (
github.com/golang/protobuf v1.4.3
github.com/micro/go-micro/v2 v2.9.1
google.golang.org/protobuf v1.25.0
)
-
8)使用我们的mirco查看我们的服务列表
image.png
D:\code\go\Mi_Onse>micro list services
结果:
D:\code\go\Mi_Onse>micro list services
go.micro.api
go.micro.service.greeter
go.micro.web
micro.http.broker
- 9)启动Mirco web查看
参考大神:golang微服务框架go-micro 入门笔记2.2 micro工具之微应用利器micro web
Microsoft Windows [版本 10.0.18363.1316]
(c) 2019 Microsoft Corporation。保留所有权利。
D:\code\go\Mi_Onse>micro web
2021-01-20 12:12:40 file=web/web.go:612 level=fatal service=web listen tcp :8082: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.
D:\code\go\Mi_Onse>
image.png
image.png image.png
- 10 )启动Mirco api 网关
注意:如果你使用的是:
D:\code\go\Mi_Onse>micro api --namespace=go.micro.srv
POSTMAN请求的话则会出现:
image.png
修改为:切记不能使用micro api --namespace=go.micro.service 的这种形式
操作:
切记不能使用micro api --namespace=go.micro.service 的这种形式
切记不能使用micro api --namespace=go.micro.service 的这种形式
切记不能使用micro api --namespace=go.micro.service 的这种形式
正确的是下面:
D:\code\go\Mi_Onse> micro api --namespace=go.micro --type=service
结果
D:\code\go\Mi_Onse> micro api --namespace=go.micro --type=service
2021-01-20 12:19:18 file=api/api.go:285 level=info service=api Registering API Default Handler at /
2021-01-20 12:19:18 file=api/api.go:308 level=fatal service=api listen tcp :8080: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.
D:\code\go\Mi_Onse> micro api --namespace=go.micro --type=service
2021-01-20 12:19:19 file=api/api.go:285 level=info service=api Registering API Default Handler at /
2021-01-20 12:19:19 file=api/api.go:308 level=fatal service=api listen tcp :8080: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.
提示已经启动过:需要关闭之前启动的API服务,正常启动显示
D:\code\go\Mi_Onse> micro api --namespace=go.micro --type=service
2021-01-20 12:20:54 file=api/api.go:285 level=info service=api Registering API Default Handler at /
2021-01-20 12:20:54 file=http/http.go:90 level=info service=api HTTP API Listening on [::]:8080
2021-01-20 12:20:54 file=v2@v2.9.1/service.go:200 level=info service=api Starting [service] go.micro.api
2021-01-20 12:20:54 file=grpc/grpc.go:864 level=info service=api Server [grpc] Listening on [::]:60471
2021-01-20 12:20:54 file=grpc/grpc.go:697 level=info service=api Registry [mdns] Registering node: go.micro.api-ca9ff737-1aa4-45a7-8554-c5adcb62d8f9
再次请求:
image.png
最终命令图示:
启动微服务
D:\code\go\Mi_Onse\greeter>go run main.go
启动WEB 服务
D:\code\go\Mi_Onse>micro web
启动API网关服务
D:\code\go\Mi_Onse> micro api --namespace=go.micro --type=service
查看服务列表:
D:\code\go\Mi_Onse>micro list services
image.png
问题场景补充
1.当切换到其他版本或项目使用G1.15之后再切换回来出现了错误:
D:\code\go\Mi_Onse>micro
panic: qtls.ConnectionState not compatible with tls.ConnectionState
goroutine 1 [running]:
github.com/lucas-clemente/quic-go/internal/handshake.init.1()
C:/Users/mayn/go/pkg/mod/github.com/lucas-clemente/quic-go@v0.14.1/internal/handshake/unsafe.go:17 +0x139
解决方法:
从新的执行获取micro工具
D:\code\go\Mi_Onse>go get github.com/micro/micro/v2
D:\code\go\Mi_Onse>go get github.com/micro/micro/v2
创建项目补充说明:
创建服务的时候可以知道服务注册中心的方式
micro new --type "srv" --registry consul micro/rpc/srv
网友评论