From ceb0903819fbe268f1153486559a464c497e99e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=87=8C=E6=B5=B7=E4=BB=B2=E5=AD=90?= Date: Sat, 23 Jan 2021 21:58:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E5=AE=9E=E7=8E=B0=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E6=9F=A5=E6=89=BE=E4=B8=8E=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/main.go | 14 +++++++++++--- consul/consul-service.go | 21 +++++++++++++++++++++ sample/sample-service.go | 9 ++++++++- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/client/main.go b/client/main.go index 3b9312d..cf140b3 100644 --- a/client/main.go +++ b/client/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "fmt" "time" log "github.com/sirupsen/logrus" @@ -11,10 +12,17 @@ import ( ) func main() { - consul.FindServer() + node, err := consul.FindNode("demo") + if err != nil { + log.WithFields(log.Fields{ + "func": "main", + }).Errorf("%v", err) + return + } + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - conn, err := grpc.Dial("127.0.0.1:9001", grpc.WithBlock(), grpc.WithInsecure()) - // conn, err := grpc.DialContext(ctx, "consul://127.0.0.1:8500/demo", grpc.WithBlock(), grpc.WithInsecure(), grpc.WithBalancerName("round_robin")) + // conn, err := grpc.Dial("127.0.0.1:9001", grpc.WithBlock(), grpc.WithInsecure()) + conn, err := grpc.DialContext(ctx, fmt.Sprintf("%s:%d", node.Address, node.Port), grpc.WithBlock(), grpc.WithInsecure(), grpc.WithBalancerName("round_robin")) if err != nil { cancel() } diff --git a/consul/consul-service.go b/consul/consul-service.go index b418e78..f218232 100644 --- a/consul/consul-service.go +++ b/consul/consul-service.go @@ -63,6 +63,27 @@ func DeRegister(name string, addr string, port int) { client.Agent().ServiceDeregister(fmt.Sprintf("%s-%s:%d", name, addr, port)) } +//FindNode 查找节点 +func FindNode(servicename string) (*consulapi.AgentService, error) { + // 创建连接consul服务配置 + config := consulapi.DefaultConfig() + config.Address = consulAddr + client, err := consulapi.NewClient(config) + if err != nil { + log.WithFields(log.Fields{ + "func": "FindNode", + }).Errorf("NewClient: %v", err) + } + services, _, err := client.Health().Service(servicename, "", true, nil) + if err != nil { + return nil, err + } + for _, service := range services { + return service.Service, nil + } + return nil, nil +} + //FindServer 从consul中发现服务 func FindServer() { // 创建连接consul服务配置 diff --git a/sample/sample-service.go b/sample/sample-service.go index c9f18b3..1dcfcc2 100644 --- a/sample/sample-service.go +++ b/sample/sample-service.go @@ -2,6 +2,8 @@ package sample import ( "context" + "strconv" + "time" pb "myschools.me/suguo/consul-demo/proto" ) @@ -10,10 +12,15 @@ import ( type Server struct { } +var r int + //Say 实现微服务接口 func (s *Server) Say(c context.Context, req *pb.SayRequest) (*pb.SayResponse, error) { + if r == 0 { + r = int(time.Now().Unix()) + } resp := &pb.SayResponse{ - Reply: req.Name + req.Day, + Reply: req.Name + req.Day + " " + strconv.Itoa(r), } return resp, nil }