Skip to content

Commit

Permalink
<feat> 完善grpc插件
Browse files Browse the repository at this point in the history
  • Loading branch information
ChansonWong committed Dec 25, 2019
1 parent 0d9f9d4 commit 431574c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand Down
23 changes: 17 additions & 6 deletions grpc-server-plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}{}

Expand All @@ -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))
}

0 comments on commit 431574c

Please sign in to comment.