From 431574c44f5d1b5e70faf640cb554569447f5804 Mon Sep 17 00:00:00 2001 From: huangguochao <917929050@qq.com> Date: Wed, 25 Dec 2019 11:29:26 +0800 Subject: [PATCH] =?UTF-8?q?=20=E5=AE=8C=E5=96=84grpc=E6=8F=92?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 3 ++- grpc-server-plugin.go | 23 +++++++++++++++++------ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index de87639..4ca1433 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,9 @@ module github.com/pku-hit/grpc-server-plugin -go 1.12 +go 1.13 require ( + github.com/lucas-clemente/quic-go v0.13.0 github.com/micro/go-micro v1.9.1 github.com/nats-io/nats-server/v2 v2.0.4 // indirect github.com/pku-hit/consul-plugin v1.3.0 diff --git a/grpc-server-plugin.go b/grpc-server-plugin.go index d0c73cb..3e78f59 100644 --- a/grpc-server-plugin.go +++ b/grpc-server-plugin.go @@ -3,16 +3,20 @@ package grpc_server_plugin import ( "fmt" "github.com/micro/go-micro" + "github.com/micro/go-micro/registry" + "github.com/micro/go-micro/registry/consul" "github.com/micro/go-micro/server" "github.com/micro/go-micro/service/grpc" - register "github.com/pku-hit/consul-plugin" "gopkg.in/yaml.v2" "io/ioutil" + "time" ) var Server = struct { RunMode string `yaml:"runmode"` GRPCPort string `yaml:"grpcport"` + ConsulHost string `yaml:"ConsulHost"` + ConsulPort string `yaml:"ConsulPort"` ServerName string `yaml:"servername"` }{} @@ -25,11 +29,18 @@ func init() { } yaml.Unmarshal(config, &Server) + consulAddress := Server.ConsulHost + ":" + Server.ConsulPort + reg := consul.NewRegistry(func(op *registry.Options) { + op.Addrs = []string{ + consulAddress, + } + }) + Service = grpc.NewService( - micro.Name(Server.ServerName), - micro.Registry(register.Reg), + micro.Name("grpc-"+Server.ServerName), + micro.Registry(reg), + micro.RegisterTTL(time.Second*30), + micro.RegisterInterval(time.Second*15), ) - metadata := make(map[string]string) - metadata["gRPC.port"] = Server.GRPCPort - Service.Server().Init(server.Address(":"+Server.GRPCPort), server.Metadata(metadata)) + Service.Server().Init(server.Address(":" + Server.GRPCPort)) }