widows环境下的Consul安装
.1 安装consul -
- windows:直接官网下载consul.exe可执行程序
.2 运行consul启动Consul agent的开发模式
PS:此模式是启动一个单节点的consul,且为集群的领袖
下载好文件后解压到目录下:
启动:
D:\gongju> .\consul.exe agent -dev
结果:
D:\gongju> .\consul.exe agent -dev
==> Starting Consul agent...
Version: '1.9.1'
Node ID: 'd65e4c42-8cbf-ac87-6ff1-fc410890182f'
Node name: 'DESKTOP-16CKEN1'
Datacenter: 'dc1' (Segment: '')
Server: true (Bootstrap: false)
Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, gRPC: 8502, DNS: 8600)
Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)
Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false, Auto-Encrypt-TLS: false
.3 查看Consul集群的成员:打开另一个终端执行:
PS D:\gongju> .\consul.exe members
Node Address Status Type Build Protocol DC Segment
DESKTOP-16CKEN1 127.0.0.1:8301 alive server 1.9.1 2 dc1
PS D:\gongju>
修改微服务的启动的代码
.1 注册中心使用默认的MDSN的时候的代码
package main
import (
log "github.com/micro/go-micro/v2/logger"
"github.com/micro/go-micro/v2"
"greeter/handler"
"greeter/subscriber"
greeter "greeter/proto/greeter"
)
func main() {
// New Service
service := micro.NewService(
micro.Name("go.micro.service.greeter"),
micro.Version("latest"),
)
// Initialise service
service.Init()
// Register Handler
_ = greeter.RegisterGreeterHandler(service.Server(), new(handler.Greeter))
// Register Struct as Subscriber
_ =micro.RegisterSubscriber("go.micro.service.greeter", service.Server(), new(subscriber.Greeter))
// Run service
if err := service.Run(); err != nil {
log.Fatal(err)
}
}
.2 修改注册中心启动为cousul:
微服务里面最好是使用启动忽略应用
_ "github.com/micro/go-plugins/registry/consul/v2"
安装依赖,注意是再所在的微服务的目录下进行安装:
D:\code\go\Mi_Onse\greeter>go get github.com/micro/go-plugins
go: github.com/micro/go-plugins upgrade => v1.5.1
1 )命令行的形式启动的服务的时候,指定注册到服务注册中心
命令 1:
D:\code\go\Mi_Onse\greeter>go run main.go --registry=consul
结果:
D:\code\go\Mi_Onse\greeter>go run main.go --registry=consul
go: finding module for package github.com/micro/go-plugins/registry/consul/v2
go: downloading github.com/micro/go-plugins/registry/consul/v2 v2.9.1
go: found github.com/micro/go-plugins/registry/consul/v2 in github.com/micro/go-plugins/registry/consul/v2 v2.9.1
go: downloading github.com/hashicorp/consul/api v1.3.0
2021-01-20 16:16:52 [email protected]/service.go:200 level=info Starting [service] go.micro.service.greeter
2021-01-20 16:16:52 file=grpc/grpc.go:864 level=info Server [grpc] Listening on [::]:58418
2021-01-20 16:16:52 file=grpc/grpc.go:881 level=info Broker [http] Connected to 127.0.0.1:58419
2021-01-20 16:16:52 file=grpc/grpc.go:697 level=info Registry [consul] Registering node: go.micro.service.greeter-73cca071-ce25-46d8-9c86-5becb4928262
2021-01-20 16:16:52 file=grpc/grpc.go:730 level=info Subscribing to topic: go.micro.service.greeter
命令:
D:\code\go\Mi_Onse\greeter>go run main.go --registry=consul
查看注册中心的服务:
命令 2:
D:\code\go\Mi_Onse\greeter>go run main.go --registry=consul --server_address=localhost:8500
结果:
2021-01-20 16:21:26 [email protected]/service.go:200 level=info Starting [service] go.micro.service.greeter
2021-01-20 16:21:26 file=greeter/main.go:40 level=fatal listen tcp 127.0.0.1:8500: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.
exit status 1
D:\code\go\Mi_Onse\greeter>
修改端口号:
D:\code\go\Mi_Onse\greeter>go run main.go --registry=consul --server_address=localhost:8898
2021-01-20 16:22:21 [email protected]/service.go:200 level=info Starting [service] go.micro.service.greeter
2021-01-20 16:22:21 file=grpc/grpc.go:864 level=info Server [grpc] Listening on 127.0.0.1:8898
2021-01-20 16:22:21 file=grpc/grpc.go:881 level=info Broker [http] Connected to 127.0.0.1:58705
2021-01-20 16:22:21 file=grpc/grpc.go:697 level=info Registry [consul] Registering node: go.micro.service.greeter-2b2c0be9-4bb5-4417-8231-2752f9f5000d
2021-01-20 16:22:21 file=grpc/grpc.go:730 level=info Subscribing to topic: go.micro.service.greeter
命令行启动后,我们的使用Mirco 查看服务启动的列表信息(命令行了找不到consul ):
D:\code\go\Mi_Onse>micro --registry mdns --registry_address loaclhost:8500 list services
D:\code\go\Mi_Onse>micro --registry consul --registry_address loaclhost:8500 list services
Registry consul not found
D:\code\go\Mi_Onse>micro --registry mdns --registry_address loaclhost:8500 list services
D:\code\go\Mi_Onse>micro --registry consul --registry_address loaclhost:8500 list services
Registry consul not found
D:\code\go\Mi_Onse>micro --registry etcd --registry_address loaclhost:8500 list services
{"level":"warn","ts":"2021-01-21T12:39:50.651+0800","caller":"clientv3/retry_interceptor.go:61","msg":"retrying of unary invoker failed","target":"endpoint://client-68f0be63-13f6-408b-a7a6-92ce0d8d4bca/loaclhost:8500","attempt":0,"e
rror":"rpc error: code = DeadlineExceeded desc = context deadline exceeded"}
context deadline exceeded
从上面的情况看,应该是无法从注册找到支持consul为中心的查询了!!!
对应的命令里面也没也这个支持写法了!!!
1 )服务启动的时候,再的服务代码里注册
修改之后的代码:
package main
import (
"github.com/micro/go-micro/v2"
log "github.com/micro/go-micro/v2/logger"
"github.com/micro/go-micro/v2/registry"
"github.com/micro/go-plugins/registry/consul/v2"
"greeter/handler"
"greeter/subscriber"
_ "github.com/micro/go-plugins/registry/consul/v2"
greeter "greeter/proto/greeter"
)
func main() {
reg := consul.NewRegistry(func(op *registry.Options) {
op.Addrs = []string{
"127.0.0.1:8500",
}
})
// New Service
service := micro.NewService(
micro.Name("go.micro.service.greeter"),
micro.Version("latest"),
micro.Registry(reg),
)
// Initialise service
service.Init()
// Register Handler
_ = greeter.RegisterGreeterHandler(service.Server(), new(handler.Greeter))
// Register Struct as Subscriber
_ =micro.RegisterSubscriber("go.micro.service.greeter", service.Server(), new(subscriber.Greeter))
// Run service
if err := service.Run(); err != nil {
log.Fatal(err)
}
}
启动服务
D:\code\go\Mi_Onse\greeter>go run main.go
2021-01-20 16:28:56 [email protected]/service.go:200 level=info Starting [service] go.micro.service.greeter
2021-01-20 16:28:56 file=grpc/grpc.go:864 level=info Server [grpc] Listening on [::]:59145
2021-01-20 16:28:56 file=grpc/grpc.go:881 level=info Broker [http] Connected to 127.0.0.1:59146
2021-01-20 16:28:56 file=grpc/grpc.go:697 level=info Registry [consul] Registering node: go.micro.service.greeter-0f739d24-3034-4968-90ee-01fd3a487d4c
2021-01-20 16:28:56 file=grpc/grpc.go:730 level=info Subscribing to topic: go.micro.service.greeter
查看服务: