Microservice(八)[微服务-micro-consul-protobuf-gRPC-ubuntu]

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
      • 1.修改为grpc创建服务
      • 2.添加consul支持
    • 12.启动srv
      • 1.运行后undefined: grpc.Xxx报错
        • 1.查看本机的proto-gen-go版本
        • 2.查看本机的grpc版本
        • 3.降低版本
        • 4.重新编译
      • 2.再次启动srv
      • 3.报错缘故
    • 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
      • 添加consul支持
    • 8.修改handler/handler.go
      • 修改为grpc创建服务【客户端】
    • 9.启动web
    • 10.go.mod
  • 3.访问
    • 1.consul web访问
    • 2.web service 访问
      • 1.未进行输出报500异常处理
        • 1.打印异常信息
        • 2.添加错误打印并重启web
        • 3.解决
      • 2.异常缘由

系统/工具/插件 版本 说明
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";

Microservice(八)[微服务-micro-consul-protobuf-gRPC-ubuntu]_第1张图片

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)

Microservice(八)[微服务-micro-consul-protobuf-gRPC-ubuntu]_第2张图片

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

Microservice(八)[微服务-micro-consul-protobuf-gRPC-ubuntu]_第3张图片

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"),
)

Microservice(八)[微服务-micro-consul-protobuf-gRPC-ubuntu]_第4张图片

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),
)

Microservice(八)[微服务-micro-consul-protobuf-gRPC-ubuntu]_第5张图片
修改后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

Microservice(八)[微服务-micro-consul-protobuf-gRPC-ubuntu]_第6张图片

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

Microservice(八)[微服务-micro-consul-protobuf-gRPC-ubuntu]_第7张图片

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)

Microservice(八)[微服务-micro-consul-protobuf-gRPC-ubuntu]_第8张图片

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"),
)

Microservice(八)[微服务-micro-consul-protobuf-gRPC-ubuntu]_第9张图片

修改后的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())

Microservice(八)[微服务-micro-consul-protobuf-gRPC-ubuntu]_第10张图片

修改后的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

Microservice(八)[微服务-micro-consul-protobuf-gRPC-ubuntu]_第11张图片

2.web service 访问

http://127.0.0.1:8080

Microservice(八)[微服务-micro-consul-protobuf-gRPC-ubuntu]_第12张图片
Microservice(八)[微服务-micro-consul-protobuf-gRPC-ubuntu]_第13张图片

1.未进行输出报500异常处理

1.打印异常信息

修改$GOPATH/src/com/kokutas/www/web/handler/handler.go
Microservice(八)[微服务-micro-consul-protobuf-gRPC-ubuntu]_第14张图片

2.添加错误打印并重启web

重启web服务后,再次进行交互测试
Microservice(八)[微服务-micro-consul-protobuf-gRPC-ubuntu]_第15张图片
在这里插入图片描述

3.解决

重启web服务,重启的命令进行修改

go run main.go --registry=consul

Microservice(八)[微服务-micro-consul-protobuf-gRPC-ubuntu]_第16张图片
Microservice(八)[微服务-micro-consul-protobuf-gRPC-ubuntu]_第17张图片

2.异常缘由

因为micro已经不再将consul作为默认的服务发现,改而用mdns和etcd,所以不添加此–registry=consul参数运行,并不能发现srv服务,从而无法进行交互。

所以,为了避免异常,在运行srv和web的时候建议都加上【–registry=consul】参数

你可能感兴趣的:(Microservice,microservice)