Microservice-微服務-micro-consul-protobuf-gRPC-ubuntu
- consul
- 1.srv
- 1.創建目錄,剝離pb.go文件
- 2.srv創建
- 3.修改.proto文件
- 4.進行編譯.proto
- 5.go mod init [*.pb.go目錄]
- 6.go mod why
- 7.修改go.mod 文件
- 8.go mod tidy
- 9.go mod verify
- 10.go mod graph【選擇性操作】
- 11.修改main.go
- 12.啓動srv
- 13.go.mod
- 2.web
- 1.web創建
- 2.go mod why
- 3.修改go.mod文件
- 4.go mod tidy
- 5.go mod verify
- 6.go mod graph【選擇性操作】
- 7.修改main.go
- 8.修改handler/handler.go
- 9.啓動web
- 10.go.mod
- 3.訪問
系統/工具/插件 | 版本 | 說明 |
---|---|---|
ubuntu | Linux version 5.3.0-46-generic (buildd@lcy01-amd64-013) (gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)) #38~18.04.1-Ubuntu SMP Tue Mar 31 04:17:56 UTC 2020 | cat /proc/version |
consul | Consul v1.7.2 Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents) |
consul version |
micro | micro version 1.18.0 | micro --version |
創建一個service-srv【service】和一個service-web【client】
consul
consul agent -dev
1.srv
1.創建目錄,剝離pb.go文件
mkdir -p $GOPATH/src/com/kokutas/www/pb
2.srv創建
創建service【srv】服務,並自定義命名空間com.kokutas.www【默認是go.micro】
服務名稱的最後一級不要使用下劃線,否則生成的handler/最後一級目錄名.go中的服務還要做修改,【protobuf生成的時候的服務命名規則問題】
micro new --type srv --namespace com.kokutas.www com/kokutas/www/user
go get -u -v google.golang.org/grpc
go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
go get -u github.com/micro/protoc-gen-micro
3.修改.proto文件
修改$GOPATH/src/com/kokutas/www/user/proto/user.proto不然老是編譯的時候提示警告信息
package com.kokutas.www.srv.user;
option go_package = "proto/user;com_kokutas_www_srv_user";
4.進行編譯.proto
cd $GOPATH/src/com/kokutas/www/user
tree $GOPATH/src/com/kokutas/www/
protoc --proto_path=.:$GOPATH/src --go_out=plugins=grpc:$GOPATH/src/com/kokutas/www/pb --micro_out=plugins=grpc:$GOPATH/src/com/kokutas/www/pb proto/user/user.proto
5.go mod init [*.pb.go目錄]
方便後續導包異常的處理【將proto文件從本地導入(pb.go文件所在目錄)】
tree $GOPATH/src/com/kokutas/www
cd $GOPATH/src/com/kokutas/www/pb/proto/user/ && go mod init com/kokutas/www/pb/proto/user && cd $GOPATH/src/com/kokutas/www/user
6.go mod why
explain why packages or modules are needed(解釋爲什麼需要依賴)
cd $GOPATH/src/com/kokutas/www/user
go mod why
com/kokutas/www/user imports
com/kokutas/www/user/proto/user: package com/kokutas/www/user/proto/user is not in GOROOT (/usr/local/go/src/com/kokutas/www/user/proto/user)
7.修改go.mod 文件
修改$GOPATH/src/com/kokutas/www/user下的go.mod 文件
追加【本地只能是相對路徑(=>後面的部分必須是相對路徑),換行符不要動】
cd $GOPATH/src/com/kokutas/www/user
tee -a $GOPATH/src/com/kokutas/www/user/go.mod <<-'EOF'
// 添加是針對proto文件的本地包導入問題處理:只有這個能被go mod why檢測到
require "com/kokutas/www/user/proto/user" v0.0.0
replace "com/kokutas/www/user/proto/user" => "../pb/proto/user"
// 針對的是consul和consul/api版本不一致的問題
replace github.com/hashicorp/consul => github.com/hashicorp/consul latest
replace github.com/hashicorp/consul/api => github.com/hashicorp/consul/api latest
// 針對的是micro 1.8.0的包路徑改變問題:
replace github.com/golang/lint => golang.org/x/lint latest
// 在執行go get -u -v github.com/micor/micro下載的時候已經出現:
// go get: github.com/mholt/[email protected] updating to
// github.com/mholt/[email protected]: parsing go.mod:
// module declares its path as: github.com/caddyserver/certmagic
// but was required as: github.com/mholt/certmagic
replace github.com/testcontainers/testcontainer-go => github.com/testcontainers/testcontainers-go latest
EOF
說明【僅作說明不執行命令】:
// 針對的在執行go get -u -v github.com/micor/micro下載的時候的問題:
replace github.com/testcontainers/testcontainer-go => github.com/testcontainers/testcontainers-go latest
// go get: github.com/mholt/[email protected] updating to
// github.com/mholt/[email protected]: parsing go.mod:
// module declares its path as: github.com/caddyserver/certmagic
// but was required as: github.com/mholt/certmagic
// 針對的是consul和consul/api版本不一致的問題
replace github.com/hashicorp/consul => github.com/hashicorp/consul latest
replace github.com/hashicorp/consul/api => github.com/hashicorp/consul/api latest
go: found github.com/micro/go-plugins/registry/consul in github.com/micro/go-plugins v1.5.1
../../../../../pkg/mod/github.com/micro/[email protected]/registry/consul/consul.go:14:2: ambiguous import: found package github.com/hashicorp/consul/api in multiple modules:
github.com/hashicorp/consul v1.4.2 (/home/wyf/workspace/go/pkg/mod/github.com/hashicorp/[email protected]/api)
github.com/hashicorp/consul/api v1.2.0 (/home/wyf/workspace/go/pkg/mod/github.com/hashicorp/consul/[email protected])
再次執行
go mod why
8.go mod tidy
add missing and remove unused modules(拉取缺少的模塊,移除不用的模塊)
cd $GOPATH/src/com/kokutas/www/user
go mod tidy
9.go mod verify
verify dependencies have expected content (驗證依賴是否正確)
cd $GOPATH/src/com/kokutas/www/user
go mod verify
10.go mod graph【選擇性操作】
print module requirement graph (打印模塊依賴圖)
cd $GOPATH/src/com/kokutas/www/user
go mod graph
11.修改main.go
修改的是$GOPATH/src/com/kokutas/www/user/main.go
原main.go文件:
package main
import (
"com/kokutas/www/user/handler"
"com/kokutas/www/user/subscriber"
"github.com/micro/go-micro"
"github.com/micro/go-micro/util/log"
user "com/kokutas/www/user/proto/user"
)
func main() {
// New Service
service := micro.NewService(
micro.Name("com.kokutas.www.srv.user"),
micro.Version("latest"),
)
// Initialise service
service.Init()
// Register Handler
user.RegisterUserHandler(service.Server(), new(handler.User))
// Register Struct as Subscriber
micro.RegisterSubscriber("com.kokutas.www.srv.user", service.Server(), new(subscriber.User))
// Register Function as Subscriber
micro.RegisterSubscriber("com.kokutas.www.srv.user", service.Server(), subscriber.Handler)
// Run service
if err := service.Run(); err != nil {
log.Fatal(err)
}
}
1.修改爲grpc創建服務
原service的創建:
// New Service
service := micro.NewService(
micro.Name("com.kokutas.www.srv.user"),
micro.Version("latest"),
)
使用grpc的service的創建:
導入的是下面的包:【務必不要導錯】
“github.com/micro/go-micro/service/grpc”
// New Service
// grpc新建服務
service := grpc.NewService(
// 服務名稱
micro.Name("com.kokutas.www.srv.user"),
// 服務版本
micro.Version("latest"),
)
2.添加consul支持
添加完grpc支持沒有做consul支持:
// New Service
// grpc新建服務
service := grpc.NewService(
// 服務名稱
micro.Name("com.kokutas.www.srv.user"),
// 服務版本
micro.Version("latest"),
)
添加完grpc支持做了consul支持:
導入的是下面的包:【務必不要導錯】
“github.com/micro/go-micro/registry”
“github.com/micro/go-plugins/registry/consul”
// 新建consul註冊器
consulReg := consul.NewRegistry(
// 註冊的consul信息
registry.Addrs("127.0.0.1:8500"),
)
// New Service
// grpc新建服務
service := grpc.NewService(
// 服務名稱
micro.Name("com.kokutas.www.srv.user"),
// 服務版本
micro.Version("latest"),
// 服務添加consul支持
micro.Registry(consulReg),
)
修改後main.go文件:
package main
import (
"com/kokutas/www/user/handler"
"com/kokutas/www/user/subscriber"
"github.com/micro/go-micro"
// grpc支持的包,千萬不要導錯
"github.com/micro/go-micro/service/grpc"
"github.com/micro/go-micro/util/log"
// 調用的protobuf生成的包
user "com/kokutas/www/user/proto/user"
// 添加註冊插件
"github.com/micro/go-micro/registry"
// 添加註冊插件的consul支持
"github.com/micro/go-plugins/registry/consul"
)
func main() {
// 新建consul註冊器
consulReg := consul.NewRegistry(
// 註冊的consul信息
registry.Addrs("127.0.0.1:8500"),
)
// New Service
// grpc新建服務
service := grpc.NewService(
// 服務名稱
micro.Name("com.kokutas.www.srv.user"),
// 服務版本
micro.Version("latest"),
// 服務添加consul支持
micro.Registry(consulReg),
)
// Initialise service
// 服務初始化
service.Init()
// Register Handler
// 註冊句柄:服務和User結構體綁定
user.RegisterUserHandler(service.Server(), new(handler.User))
// Register Struct as Subscriber
// 註冊訂閱服務的結構體
micro.RegisterSubscriber("com.kokutas.www.srv.user", service.Server(), new(subscriber.User))
// Register Function as Subscriber
// 註冊訂閱服務的方法
micro.RegisterSubscriber("com.kokutas.www.srv.user", service.Server(), subscriber.Handler)
// Run service
// 啓動服務
if err := service.Run(); err != nil {
log.Fatal(err)
}
}
12.啓動srv
爲了避免使用consul而發生無法發現服務的情況,在運行srv和web的時候建議都加上【–registry=consul】參數
即【go run main.go --registry=consul】
cd $GOPATH/src/com/kokutas/www/user
go run main.go
1.運行後undefined: grpc.Xxx報錯
報錯如下:
../pb/proto/user/user.pb.go:554:7: undefined: grpc.ClientConnInterface
../pb/proto/user/user.pb.go:558:11: undefined: grpc.SupportPackageIsVersion6
../pb/proto/user/user.pb.go:570:5: undefined: grpc.ClientConnInterface
../pb/proto/user/user.pb.go:573:23: undefined: grpc.ClientConnInterface
1.查看本機的proto-gen-go版本
cd $GOPATH/src/com/kokutas/www/user
go list -json github.com/golang/protobuf/protoc-gen-go
或者使用如下命令查看:
cd $GOPATH/src/com/kokutas/www/user
go list -json github.com/golang/protobuf/protoc-gen-go| grep Version
2.查看本機的grpc版本
cd $GOPATH/src/com/kokutas/www/user
go list -json google.golang.org/grpc
或者使用如下命令查看:
cd $GOPATH/src/com/kokutas/www/user
go list -json google.golang.org/grpc | grep Version
3.降低版本
go get -u -v github.com/golang/protobuf/[email protected]
4.重新編譯
cd $GOPATH/src/com/kokutas/www/user
tree $GOPATH/src/com/kokutas/www/
protoc --proto_path=.:$GOPATH/src --go_out=plugins=grpc:$GOPATH/src/com/kokutas/www/pb --micro_out=plugins=grpc:$GOPATH/src/com/kokutas/www/pb proto/user/user.proto
2.再次啓動srv
cd $GOPATH/src/com/kokutas/www/user
go run main.go
3.報錯緣故
設備上的grpc的版本是v1.25.1
設備上的protoc-gen-go的版本是v1.4.0
[email protected]要求grpc的版本是v1.27及以後的,所以將protoc-gen-go進行降低版本到能匹配[email protected]的即可。
13.go.mod
$GOPATH/src/com/kokutas/www/user/go.mod
module com/kokutas/www/user
go 1.14
require github.com/micro/go-micro v1.18.0
require (
// 添加是針對proto文件的本地包導入問題處理:只有這個能被go mod why檢測到
com/kokutas/www/user/proto/user v0.0.0
contrib.go.opencensus.io/exporter/aws v0.0.0-20181029163544-2befc13012d0 // indirect
contrib.go.opencensus.io/integrations/ocsql v0.1.4 // indirect
contrib.go.opencensus.io/resource v0.1.1 // indirect
github.com/Azure/azure-amqp-common-go v1.1.3 // indirect
github.com/Azure/azure-pipeline-go v0.1.9 // indirect
github.com/Azure/azure-sdk-for-go v32.4.0+incompatible // indirect
github.com/Azure/azure-storage-blob-go v0.6.0 // indirect
github.com/Azure/go-autorest v12.0.0+incompatible // indirect
github.com/Azure/go-autorest/autorest/date v0.1.0 // indirect
github.com/Azure/go-autorest/autorest/mocks v0.1.0 // indirect
github.com/Azure/go-autorest/autorest/to v0.2.0 // indirect
github.com/Azure/go-autorest/autorest/validation v0.1.0 // indirect
github.com/Azure/go-autorest/logger v0.1.0 // indirect
github.com/DataDog/datadog-go v3.2.0+incompatible // indirect
github.com/GoogleCloudPlatform/cloudsql-proxy v0.0.0-20190605020000-c4ba1fdf4d36 // indirect
github.com/Microsoft/go-winio v0.4.15-0.20190919025122-fc70bd9a86b5 // indirect
github.com/OpenDNS/vegadns2client v0.0.0-20180418235048-a3fa4a771d87 // indirect
github.com/Shopify/sarama v1.24.1 // indirect
github.com/abbot/go-http-auth v0.4.1-0.20181019201920-860ed7f246ff // indirect
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5 // indirect
github.com/akamai/AkamaiOPEN-edgegrid-golang v0.9.0 // indirect
github.com/alangpierce/go-forceexport v0.0.0-20160317203124-8f1d6941cd75 // indirect
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4 // indirect
github.com/aliyun/alibaba-cloud-sdk-go v0.0.0-20190808125512-07798873deee // indirect
github.com/anacrolix/sync v0.2.0 // indirect
github.com/anacrolix/utp v0.0.0-20180219060659-9e0e1d1d0572 // indirect
github.com/apache/thrift v0.12.0 // indirect
github.com/asim/go-awsxray v0.0.0-20161209120537-0d8a60b6e205 // indirect
github.com/asim/go-bson v0.0.0-20160318195205-84522947cabd // indirect
github.com/beevik/ntp v0.2.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bitly/go-simplejson v0.5.0 // indirect
github.com/blang/semver v3.1.0+incompatible // indirect
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
github.com/boltdb/bolt v1.3.1 // indirect
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b // indirect
github.com/bwmarrin/discordgo v0.20.1 // indirect
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
github.com/cenkalti/backoff/v3 v3.0.0 // indirect
github.com/census-instrumentation/opencensus-proto v0.2.1 // indirect
github.com/cespare/xxhash/v2 v2.1.0 // indirect
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/clbanning/x2j v0.0.0-20191024224557-825249438eec // indirect
github.com/cloudflare/cloudflare-go v0.10.6 // indirect
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd // indirect
github.com/containerd/cgroups v0.0.0-20190919134610-bf292b21730f // indirect
github.com/containerd/console v0.0.0-20180822173158-c12b1e7919c1 // indirect
github.com/containerd/containerd v1.3.0 // indirect
github.com/containerd/continuity v0.0.0-20190426062206-aaeac12a7ffc // indirect
github.com/containerd/fifo v0.0.0-20190226154929-a9fb20d87448 // indirect
github.com/containerd/go-runc v0.0.0-20180907222934-5a6d9f37cfa3 // indirect
github.com/containerd/ttrpc v0.0.0-20190828154514-0e0f228740de // indirect
github.com/containerd/typeurl v0.0.0-20180627222232-a93fcdb778cd // indirect
github.com/coreos/bbolt v1.3.3 // indirect
github.com/coreos/etcd v3.3.17+incompatible // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f // indirect
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
github.com/cpu/goacmedns v0.0.1 // indirect
github.com/decker502/dnspod-go v0.2.0 // indirect
github.com/devigned/tab v0.1.1 // indirect
github.com/dimchansky/utfbom v1.1.0 // indirect
github.com/dnsimple/dnsimple-go v0.30.0 // indirect
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/docker v1.4.2-0.20191101170500-ac7306503d23 // indirect
github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96 // indirect
github.com/eclipse/paho.mqtt.golang v1.2.0 // indirect
github.com/eknkc/basex v1.0.0 // indirect
github.com/elazarl/goproxy v0.0.0-20170405201442-c4fc26588b6e // indirect
github.com/envoyproxy/protoc-gen-validate v0.1.0 // indirect
github.com/evanphx/json-patch v4.2.0+incompatible // indirect
github.com/exoscale/egoscale v0.18.1 // indirect
github.com/forestgiant/sliceutil v0.0.0-20160425183142-94783f95db6c // indirect
github.com/franela/goreq v0.0.0-20171204163338-bcd34c9993f8 // indirect
github.com/go-acme/lego v2.5.0+incompatible // indirect
github.com/go-kit/kit v0.9.0 // indirect
github.com/go-ldap/ldap v3.0.2+incompatible // indirect
github.com/go-log/log v0.1.0 // indirect
github.com/go-logfmt/logfmt v0.4.0 // indirect
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.16.0 // indirect
github.com/go-redsync/redsync v1.3.1 // indirect
github.com/go-sql-driver/mysql v1.4.1 // indirect
github.com/go-stomp/stomp v2.0.3+incompatible // indirect
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible // indirect
github.com/gogo/protobuf v1.2.2-0.20190723190241-65acae22fc9d // indirect
github.com/golang/groupcache v0.0.0-20191002201903-404acd9df4cc // indirect
github.com/golang/mock v1.3.1 // indirect
github.com/google/btree v1.0.0 // indirect
github.com/google/go-cmp v0.4.0 // indirect
github.com/google/go-github v17.0.0+incompatible // indirect
github.com/google/go-replayers/httpreplay v0.1.0 // indirect
github.com/google/martian v2.1.1-0.20190517191504-25dcb96d9e51+incompatible // indirect
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f // indirect
github.com/google/wire v0.3.0 // indirect
github.com/googleapis/gax-go v2.0.2+incompatible // indirect
github.com/googleapis/gax-go/v2 v2.0.5 // indirect
github.com/gophercloud/gophercloud v0.3.0 // indirect
github.com/gorilla/handlers v1.4.2 // indirect
github.com/gorilla/mux v1.7.3 // indirect
github.com/gorilla/websocket v1.4.1 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.9.2 // indirect
github.com/hako/branca v0.0.0-20180808000428-10b799466ada // indirect
github.com/hashicorp/go-plugin v1.0.1 // indirect
github.com/hashicorp/go-retryablehttp v0.5.4 // indirect
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
github.com/hashicorp/go-version v1.1.0 // indirect
github.com/hashicorp/golang-lru v0.5.3 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect
github.com/hudl/fargo v1.3.0 // indirect
github.com/iancoleman/strcase v0.0.0-20190422225806-e506e3ef7365 // indirect
github.com/iij/doapi v0.0.0-20190504054126-0bbf12d6d7df // indirect
github.com/imdario/mergo v0.3.8 // indirect
github.com/jonboulle/clockwork v0.1.0 // indirect
github.com/joncalhoun/qson v0.0.0-20170526102502-8a9cab3a62b1 // indirect
github.com/json-iterator/go v1.1.8 // indirect
github.com/juju/ratelimit v1.0.1 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/labbsr0x/goh v1.0.1 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/lib/pq v1.2.0 // indirect
github.com/linode/linodego v0.10.0 // indirect
github.com/liquidweb/liquidweb-go v1.6.0 // indirect
github.com/lyft/protoc-gen-star v0.4.10 // indirect
github.com/marten-seemann/chacha20 v0.2.0 // indirect
github.com/marten-seemann/qpack v0.1.0 // indirect
github.com/marten-seemann/qtls v0.4.1 // indirect
github.com/micro/cli v0.2.0 // indirect
github.com/micro/go-plugins v1.5.1
github.com/micro/go-rcache v0.3.0 // indirect
github.com/micro/mdns v0.3.0 // indirect
github.com/minio/highwayhash v1.0.0 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/hashstructure v1.0.0 // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/morikuni/aec v0.0.0-20170113033406-39771216ff4c // indirect
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/namedotcom/go v0.0.0-20180403034216-08470befbe04 // indirect
github.com/nats-io/nats.go v1.9.1 // indirect
github.com/nats-io/stan.go v0.5.0 // indirect
github.com/nlopes/slack v0.6.0 // indirect
github.com/nrdcg/auroradns v1.0.0 // indirect
github.com/nrdcg/goinwx v0.6.1 // indirect
github.com/nrdcg/namesilo v0.2.1 // indirect
github.com/nsqio/go-nsq v1.0.7 // indirect
github.com/onsi/ginkgo v1.10.1 // indirect
github.com/onsi/gomega v1.7.0 // indirect
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 // indirect
github.com/opencontainers/runtime-tools v0.0.0-20181011054405-1d69bd0f9c39 // indirect
github.com/opentracing/opentracing-go v1.1.0 // indirect
github.com/openzipkin/zipkin-go v0.1.6 // indirect
github.com/oracle/oci-go-sdk v7.0.0+incompatible // indirect
github.com/ovh/go-ovh v0.0.0-20181109152953-ba5adb4cf014 // indirect
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
github.com/pascaldekloe/goe v0.1.0 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pborman/uuid v1.2.0 // indirect
github.com/pquerna/otp v1.2.0 // indirect
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4 // indirect
github.com/prometheus/procfs v0.0.5 // indirect
github.com/rainycape/memcache v0.0.0-20150622160815-1031fa0ce2f2 // indirect
github.com/rs/cors v1.7.0 // indirect
github.com/ryanuber/go-glob v1.0.0 // indirect
github.com/sacloud/libsacloud v1.26.1 // indirect
github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da // indirect
github.com/serenize/snaker v0.0.0-20171204205717-a683aaf2d516 // indirect
github.com/soheilhy/cmux v0.1.4 // indirect
github.com/sony/gobreaker v0.4.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271 // indirect
github.com/syndtr/gocapability v0.0.0-20170704070218-db04d3cc01c8 // indirect
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
github.com/timewasted/linode v0.0.0-20160829202747-37e84520dcf7 // indirect
github.com/tinylib/msgp v1.1.0 // indirect
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5 // indirect
github.com/transip/gotransip v0.0.0-20190812104329-6d8d9179b66f // indirect
github.com/uber/jaeger-client-go v2.15.0+incompatible // indirect
github.com/uber/jaeger-lib v1.5.0 // indirect
github.com/vultr/govultr v0.1.4 // indirect
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca // indirect
go.etcd.io/bbolt v1.3.3 // indirect
go.uber.org/ratelimit v0.1.0 // indirect
go.uber.org/zap v1.12.0 // indirect
golang.org/x/crypto v0.0.0-20191108234033-bd318be0434a // indirect
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522 // indirect
golang.org/x/net v0.0.0-20191109021931-daa7c04131f5 // indirect
gopkg.in/DataDog/dd-trace-go.v1 v1.19.0 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/bsm/ratelimit.v1 v1.0.0-20160220154919-db14e161995a // indirect
gopkg.in/gcfg.v1 v1.2.3 // indirect
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
gopkg.in/go-playground/validator.v9 v9.30.0 // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ldap.v3 v3.1.0 // indirect
gopkg.in/ns1/ns1-go.v2 v2.0.0-20190730140822-b51389932cbc // indirect
gopkg.in/olivere/elastic.v5 v5.0.82 // indirect
gopkg.in/redis.v3 v3.6.4 // indirect
gopkg.in/square/go-jose.v2 v2.3.1 // indirect
gopkg.in/src-d/go-git.v4 v4.13.1 // indirect
gopkg.in/telegram-bot-api.v4 v4.6.4 // indirect
istio.io/gogo-genproto v0.0.0-20190614210408-e88dc8b0e4db // indirect
k8s.io/client-go v11.0.0+incompatible // indirect
k8s.io/klog v1.0.0 // indirect
k8s.io/kube-openapi v0.0.0-20191107075043-30be4d16710a // indirect
k8s.io/kubernetes v1.13.0 // indirect
k8s.io/utils v0.0.0-20191030222137-2b95a09bc58d // indirect
pack.ag/amqp v0.11.2 // indirect
rsc.io/binaryregexp v0.2.0 // indirect
)
replace com/kokutas/www/user/proto/user => ../pb/proto/user
// 針對的是consul和consul/api版本不一致的問題
replace github.com/hashicorp/consul => github.com/hashicorp/consul v1.7.2
replace github.com/hashicorp/consul/api => github.com/hashicorp/consul/api v1.4.0
// 針對的是micro 1.8.0的包路徑改變問題:
replace github.com/golang/lint => golang.org/x/lint v0.0.0-20200302205851-738671d3881b
// 在執行go get -u -v github.com/micor/micro下載的時候已經出現:
// go get: github.com/mholt/[email protected] updating to
//github.com/mholt/[email protected]: parsing go.mod:
//module declares its path as: github.com/caddyserver/certmagic
// but was required as: github.com/mholt/certmagic
replace github.com/testcontainers/testcontainer-go => github.com/testcontainers/testcontainers-go v0.5.1
2.web
1.web創建
新開一個終端,創建client【web】服務,並自定義命名空間com.kokutas.www【默認是go.micro】
cd $GOPATH/src
micro new --type web --namespace com.kokutas.www com/kokutas/www/web
2.go mod why
explain why packages or modules are needed(解釋爲什麼需要依賴)
cd $GOPATH/src/com/kokutas/www/web
go mod why
go: finding module for package github.com/micro/go-micro/web
go: finding module for package github.com/micro/go-micro/util/log
go: finding module for package github.com/micro/go-micro/client
go: found github.com/micro/go-micro/util/log in github.com/micro/go-micro v1.18.0
com/kokutas/www/web/handler imports
path/to/service/proto/web: package path/to/service/proto/web is not in GOROOT (/usr/local/go/src/path/to/service/proto/web)
3.修改go.mod文件
修改$GOPATH/src/com/kokutas/www/web下的go.mod 文件
追加【本地只能是相對路徑(=>後面的部分必須是相對路徑),換行符不要動】
tee -a $GOPATH/src/com/kokutas/www/web/go.mod <<-'EOF'
// 添加是針對proto文件的本地包導入問題處理:只有這個能被go mod why檢測到
require "path/to/service/proto/web" v0.0.0
replace "path/to/service/proto/web" => "../pb/proto/user"
// 針對的是consul和consul/api版本不一致的問題
replace github.com/hashicorp/consul => github.com/hashicorp/consul latest
replace github.com/hashicorp/consul/api => github.com/hashicorp/consul/api latest
// 針對的是micro 1.8.0的包路徑改變問題:
replace github.com/golang/lint => golang.org/x/lint latest
// 在執行go get -u -v github.com/micor/micro下載的時候已經出現:
// go get: github.com/mholt/[email protected] updating to
// github.com/mholt/[email protected]: parsing go.mod:
// module declares its path as: github.com/caddyserver/certmagic
// but was required as: github.com/mholt/certmagic
replace github.com/testcontainers/testcontainer-go => github.com/testcontainers/testcontainers-go latest
EOF
說明【僅作說明不執行命令】:
// 針對的在執行go get -u -v github.com/micor/micro下載的時候的問題:
replace github.com/testcontainers/testcontainer-go => github.com/testcontainers/testcontainers-go latest
// go get: github.com/mholt/[email protected] updating to
// github.com/mholt/[email protected]: parsing go.mod:
// module declares its path as: github.com/caddyserver/certmagic
// but was required as: github.com/mholt/certmagic
// 針對的是consul和consul/api版本不一致的問題
replace github.com/hashicorp/consul => github.com/hashicorp/consul latest
replace github.com/hashicorp/consul/api => github.com/hashicorp/consul/api latest
go: found github.com/micro/go-plugins/registry/consul in github.com/micro/go-plugins v1.5.1
../../../../../pkg/mod/github.com/micro/[email protected]/registry/consul/consul.go:14:2: ambiguous import: found package github.com/hashicorp/consul/api in multiple modules:
github.com/hashicorp/consul v1.4.2 (/home/wyf/workspace/go/pkg/mod/github.com/hashicorp/[email protected]/api)
github.com/hashicorp/consul/api v1.2.0 (/home/wyf/workspace/go/pkg/mod/github.com/hashicorp/consul/[email protected])
再次執行
go mod why
4.go mod tidy
add missing and remove unused modules(拉取缺少的模塊,移除不用的模塊)
cd $GOPATH/src/com/kokutas/www/web
go mod tidy
5.go mod verify
verify dependencies have expected content (驗證依賴是否正確)
cd $GOPATH/src/com/kokutas/www/web
go mod verify
6.go mod graph【選擇性操作】
print module requirement graph (打印模塊依賴圖)
cd $GOPATH/src/com/kokutas/www/web
go mod graph
7.修改main.go
修改的是$GOPATH/src/com/kokutas/www/web/main.go
原main.go文件:
package main
import (
"net/http"
"github.com/micro/go-micro/util/log"
"com/kokutas/www/web/handler"
"github.com/micro/go-micro/web"
)
func main() {
// create new web service
service := web.NewService(
web.Name("com.kokutas.www.web.web"),
web.Version("latest"),
)
// initialise service
if err := service.Init(); err != nil {
log.Fatal(err)
}
// register html handler
service.Handle("/", http.FileServer(http.Dir("html")))
// register call handler
service.HandleFunc("/web/call", handler.WebCall)
// run service
if err := service.Run(); err != nil {
log.Fatal(err)
}
}
添加consul支持
原web服務【client】未做consul支持:
// create new web service
service := web.NewService(
web.Name("com.kokutas.www.web.web"),
web.Version("latest"),
)
web服務【client】做了consul支持:
導入的是下面的包:【務必不要導錯】
“github.com/micro/go-micro/registry”
“github.com/micro/go-plugins/registry/consul”
// 新建consul註冊器
consulReg := consul.NewRegistry(
// 註冊的consul信息
registry.Addrs("127.0.0.1:8500"),
)
// create new web service
// 新建web服務
service := web.NewService(
// 服務名稱
web.Name("com.kokutas.www.web.web"),
// 服務版本
web.Version("latest"),
// 服務添加consul支持
web.Registry(consulReg),
// 指定web服務端口號
web.Address(":8080"),
)
修改後的main.go
package main
import (
"net/http"
"github.com/micro/go-micro/util/log"
"com/kokutas/www/web/handler"
"github.com/micro/go-micro/web"
// 添加註冊插件
"github.com/micro/go-micro/registry"
// 添加註冊插件的consul支持
"github.com/micro/go-plugins/registry/consul"
)
func main() {
// 新建consul註冊器
consulReg := consul.NewRegistry(
// 註冊的consul信息
registry.Addrs("127.0.0.1:8500"),
)
// create new web service
// 新建web服務
service := web.NewService(
// 服務名稱
web.Name("com.kokutas.www.web.web"),
// 服務版本
web.Version("latest"),
// 服務添加consul支持
web.Registry(consulReg),
// 指定web服務端口號
web.Address(":8080"),
)
// initialise service
// 服務初始化
if err := service.Init(); err != nil {
log.Fatal(err)
}
// register html handler
// 註冊靜態資源處理句柄
service.Handle("/", http.FileServer(http.Dir("html")))
// register call handler
// 註冊/web/call服務請求處理句柄
service.HandleFunc("/web/call", handler.WebCall)
// run service
// 啓動服務
if err := service.Run(); err != nil {
log.Fatal(err)
}
}
8.修改handler/handler.go
修改$GOPATH/src/com/kokutas/www/web/handler/handler.go
原handler/handler.go文件:
package handler
import (
"context"
"encoding/json"
"net/http"
"time"
web "path/to/service/proto/web"
"github.com/micro/go-micro/client"
)
func WebCall(w http.ResponseWriter, r *http.Request) {
// decode the incoming request as json
var request map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
http.Error(w, err.Error(), 500)
return
}
// call the backend service
webClient := web.NewWebService("com.kokutas.www.srv.web", client.DefaultClient)
rsp, err := webClient.Call(context.TODO(), &web.Request{
Name: request["name"].(string),
})
if err != nil {
http.Error(w, err.Error(), 500)
return
}
// we want to augment the response
response := map[string]interface{}{
"msg": rsp.Msg,
"ref": time.Now().UnixNano(),
}
// encode and write the response as json
if err := json.NewEncoder(w).Encode(response); err != nil {
http.Error(w, err.Error(), 500)
return
}
}
修改爲grpc創建服務【客戶端】
原service服務【客戶端】的創建:
// call the backend service
webClient := web.NewWebService("com.kokutas.www.srv.web", client.DefaultClient)
rsp, err := webClient.Call(context.TODO(), &web.Request{
Name: request["name"].(string),
})
if err != nil {
http.Error(w, err.Error(), 500)
return
}
使用grpc的service服務【客戶端】的創建:
導入的是下面的包:【務必不要導錯,且要和srv的 導包一致】
“github.com/micro/go-micro/service/grpc”
// 使用grpc創建客戶端服務
cli := grpc.NewService()
// 客戶端服務初始化
cli.Init()
// 基於後端服務com.kokutas.www.srv.user創建web客戶端
// NewUserService要和pb/protobuf/user/user.pb.micro.go的NewUserService方法名一致
// com.kokutas.www.srv.user也要和pb/protobuf/user/user.pb.micro.go的com.kokutas.www.srv.user的一致
webClient := web.NewUserService("com.kokutas.www.srv.user", cli.Client())
修改後的handler/handler.go文件
package handler
import (
"context"
"encoding/json"
"net/http"
"time"
web "path/to/service/proto/web"
"github.com/micro/go-micro/service/grpc"
)
func WebCall(w http.ResponseWriter, r *http.Request) {
// decode the incoming request as json
var request map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
http.Error(w, err.Error(), 500)
return
}
// 使用grpc創建客戶端服務
cli := grpc.NewService()
// 客戶端服務初始化
cli.Init()
// 基於後端服務com.kokutas.www.srv.user創建web客戶端
// NewUserService要和pb/protobuf/user/user.pb.micro.go的NewUserService方法名一致
// com.kokutas.www.srv.user也要和pb/protobuf/user/user.pb.micro.go的com.kokutas.www.srv.user的一致
webClient := web.NewUserService("com.kokutas.www.srv.user", cli.Client())
// call the backend service
// 客戶端調用後端服務
rsp, err := webClient.Call(context.TODO(), &web.Request{
Name: request["name"].(string),
})
if err != nil {
http.Error(w, err.Error(), 500)
return
}
// we want to augment the response
response := map[string]interface{}{
"msg": rsp.Msg,
"ref": time.Now().UnixNano(),
}
// encode and write the response as json
if err := json.NewEncoder(w).Encode(response); err != nil {
http.Error(w, err.Error(), 500)
return
}
}
9.啓動web
爲了避免使用consul而發生無法發現服務的情況,在運行srv和web的時候建議都加上【–registry=consul】參數
即【go run main.go --registry=consul】
cd $GOPATH/src/com/kokutas/www/web/
go run main.go
10.go.mod
$GOPATH/src/com/kokutas/www/web/go.mod
module com/kokutas/www/web
go 1.14
require github.com/micro/go-micro v1.18.0
require (
github.com/micro/go-plugins v1.5.1 // indirect
// 添加是針對proto文件的本地包導入問題處理:只有這個能被go mod why檢測到
path/to/service/proto/web v0.0.0
)
replace path/to/service/proto/web => ../pb/proto/user
// 針對的是consul和consul/api版本不一致的問題
replace github.com/hashicorp/consul => github.com/hashicorp/consul v1.7.2
replace github.com/hashicorp/consul/api => github.com/hashicorp/consul/api v1.4.0
// 針對的是micro 1.8.0的包路徑改變問題:
replace github.com/golang/lint => golang.org/x/lint v0.0.0-20200302205851-738671d3881b
// 在執行go get -u -v github.com/micor/micro下載的時候已經出現:
// go get: github.com/mholt/[email protected] updating to
//github.com/mholt/[email protected]: parsing go.mod:
//module declares its path as: github.com/caddyserver/certmagic
// but was required as: github.com/mholt/certmagic
replace github.com/testcontainers/testcontainer-go => github.com/testcontainers/testcontainers-go v0.5.1
3.訪問
1.consul web訪問
瀏覽器打開:
http://127.0.0.1:8500
2.web service 訪問
http://127.0.0.1:8080
1.未進行輸出報500異常處理
1.打印異常信息
修改$GOPATH/src/com/kokutas/www/web/handler/handler.go
2.添加錯誤打印並重啓web
重啓web服務後,再次進行交互測試
3.解決
重啓web服務,重啓的命令進行修改
go run main.go --registry=consul
2.異常緣由
因爲micro已經不再將consul作爲默認的服務發現,改而用mdns和etcd,所以不添加此–registry=consul參數運行,並不能發現srv服務,從而無法進行交互。
所以,爲了避免異常,在運行srv和web的時候建議都加上【–registry=consul】參數