GO 微服務GO-Micro(6)個人學習筆記記錄- 萬惡的環境搭建和Micro創建項目greeter微服務示例

環境搭建

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

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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,663評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,125評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,506評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,614評論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,402評論 6 404
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,934評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,021評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,168評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,690評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,596評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,784評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,288評論 5 357
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,027評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,404評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,662評論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,398評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,743評論 2 370

推薦閱讀更多精彩內容