環境搭建
注意事項:不要使用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