diff --git a/.gitignore b/.gitignore index fc7ef0a..ded2c72 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ # Dependency directories (remove the comment below to include it) # vendor/ consul-demo +logs/ diff --git a/README.md b/README.md index 6f71faf..533b152 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ }() #### 注册中心注册代码编写 + 编写在文件consul-service.go中,作为源代码组件形式分发 #### 实现客户端代码 diff --git a/consul/consul-service.go b/consul/consul-service.go index a35e6d0..eb85814 100644 --- a/consul/consul-service.go +++ b/consul/consul-service.go @@ -12,7 +12,7 @@ const ( ) //Register 注册服务到consul -func Register(id string, name string, port int, tags ...string) { +func Register(id string, name string, addr string, port int, tags ...string) { // 创建连接consul服务配置 config := consulapi.DefaultConfig() config.Address = consulAddr @@ -30,7 +30,7 @@ func Register(id string, name string, port int, tags ...string) { registration.Name = name registration.Port = port registration.Tags = tags - registration.Address = "192.168.8.146" + registration.Address = addr // 增加consul健康检查回调函数 check := new(consulapi.AgentServiceCheck) @@ -67,10 +67,12 @@ func DeRegister(name string) { func FindServer() { // 创建连接consul服务配置 config := consulapi.DefaultConfig() - config.Address = "127.0.0.1:8500" + config.Address = consulAddr client, err := consulapi.NewClient(config) if err != nil { - log.Fatal("consul client error : ", err) + log.WithFields(log.Fields{ + "func": "DeRegister", + }).Errorf("%v", err) } // 获取所有service diff --git a/health/health-service.go b/health/health-service.go index 237de73..6a48ece 100644 --- a/health/health-service.go +++ b/health/health-service.go @@ -15,7 +15,7 @@ type Server struct { func (s *Server) Check(c context.Context, req *pb.HealthCheckRequest) (*pb.HealthCheckResponse, error) { log.WithFields(log.Fields{ "func": "Check", - }).Infof("%s %s", req.Service, req.GetService()) + }).Infof("%s %s %s", req.String(), req.Service, req.GetService()) resp := &pb.HealthCheckResponse{ Status: pb.HealthCheckResponse_SERVING, } diff --git a/logs/demo b/logs/demo deleted file mode 120000 index cae2bb0..0000000 --- a/logs/demo +++ /dev/null @@ -1 +0,0 @@ -demo2021012314.log \ No newline at end of file diff --git a/logs/demo2021012314.log b/logs/demo2021012314.log deleted file mode 100644 index cf94d35..0000000 --- a/logs/demo2021012314.log +++ /dev/null @@ -1,61 +0,0 @@ -time="2021-01-23T14:25:44+08:00" level=error msg="ServiceRegister: Unexpected response code: 400 (Invalid service address)" func=Register -time="2021-01-23T14:27:16+08:00" level=info func=Check -time="2021-01-23T14:27:21+08:00" level=info func=Check -time="2021-01-23T14:27:26+08:00" level=info func=Check -time="2021-01-23T14:27:31+08:00" level=info func=Check -time="2021-01-23T14:27:36+08:00" level=info func=Check -time="2021-01-23T14:27:41+08:00" level=info func=Check -time="2021-01-23T14:27:57+08:00" level=info func=Check -time="2021-01-23T14:28:02+08:00" level=info func=Check -time="2021-01-23T14:28:07+08:00" level=info func=Check -time="2021-01-23T14:28:12+08:00" level=info func=Check -time="2021-01-23T14:28:17+08:00" level=info func=Check -time="2021-01-23T14:28:22+08:00" level=info func=Check -time="2021-01-23T14:28:27+08:00" level=info func=Check -time="2021-01-23T14:28:32+08:00" level=info func=Check -time="2021-01-23T14:28:37+08:00" level=info func=Check -time="2021-01-23T14:28:42+08:00" level=info func=Check -time="2021-01-23T14:28:47+08:00" level=info func=Check -time="2021-01-23T14:28:52+08:00" level=info func=Check -time="2021-01-23T14:28:57+08:00" level=info func=Check -time="2021-01-23T14:29:02+08:00" level=info func=Check -time="2021-01-23T14:29:07+08:00" level=info func=Check -time="2021-01-23T14:29:12+08:00" level=info func=Check -time="2021-01-23T14:29:17+08:00" level=info func=Check -time="2021-01-23T14:29:22+08:00" level=info func=Check -time="2021-01-23T14:29:27+08:00" level=info func=Check -time="2021-01-23T14:29:32+08:00" level=info func=Check -time="2021-01-23T14:29:37+08:00" level=info func=Check -time="2021-01-23T14:29:56+08:00" level=info func=Check -time="2021-01-23T14:30:01+08:00" level=info func=Check -time="2021-01-23T14:30:06+08:00" level=info func=Check -time="2021-01-23T14:30:11+08:00" level=info func=Check -time="2021-01-23T14:30:16+08:00" level=info func=Check -time="2021-01-23T14:30:21+08:00" level=info func=Check -time="2021-01-23T14:30:26+08:00" level=info func=Check -time="2021-01-23T14:30:31+08:00" level=info func=Check -time="2021-01-23T14:30:36+08:00" level=info func=Check -time="2021-01-23T14:30:41+08:00" level=info func=Check -time="2021-01-23T14:30:46+08:00" level=info func=Check -time="2021-01-23T14:30:51+08:00" level=info func=Check -time="2021-01-23T14:31:41+08:00" level=info msg=" " func=Check -time="2021-01-23T14:31:46+08:00" level=info msg=" " func=Check -time="2021-01-23T14:31:51+08:00" level=info msg=" " func=Check -time="2021-01-23T14:31:56+08:00" level=info msg=" " func=Check -time="2021-01-23T14:32:02+08:00" level=info msg=" " func=Check -time="2021-01-23T14:32:07+08:00" level=info msg=" " func=Check -time="2021-01-23T14:32:12+08:00" level=info msg=" " func=Check -time="2021-01-23T14:32:17+08:00" level=info msg=" " func=Check -time="2021-01-23T14:32:22+08:00" level=info msg=" " func=Check -time="2021-01-23T14:32:27+08:00" level=info msg=" " func=Check -time="2021-01-23T14:32:32+08:00" level=info msg=" " func=Check -time="2021-01-23T14:32:37+08:00" level=info msg=" " func=Check -time="2021-01-23T14:32:42+08:00" level=info msg=" " func=Check -time="2021-01-23T14:32:47+08:00" level=info msg=" " func=Check -time="2021-01-23T14:32:52+08:00" level=info msg=" " func=Check -time="2021-01-23T14:32:57+08:00" level=info msg=" " func=Check -time="2021-01-23T14:33:02+08:00" level=info msg=" " func=Check -time="2021-01-23T14:33:07+08:00" level=info msg=" " func=Check -time="2021-01-23T14:33:12+08:00" level=info msg=" " func=Check -time="2021-01-23T14:33:17+08:00" level=info msg=" " func=Check -time="2021-01-23T14:33:22+08:00" level=info msg=" " func=Check diff --git a/main.go b/main.go index 9de4877..f597dd4 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,8 @@ package main import ( "context" + "flag" + "fmt" "log" "net" "os" @@ -21,6 +23,10 @@ const ( ) func main() { + addr := flag.String("tcp", "0.0.0.0", "service address") + port := flag.Int("port", 9001, "service port") + flag.Parse() + ctx := context.Background() ctx, cancel := context.WithCancel(ctx) defer cancel() @@ -32,8 +38,8 @@ func main() { // grpc服务启动 go func() { - log.Printf("starting grpc service on %s\n", ":9001") - lis, err := net.Listen("tcp", ":9001") + log.Printf("starting grpc service on %s\n", *addr) + lis, err := net.Listen("tcp", fmt.Sprintf("%s:%d", *addr, *port)) if err != nil { log.Fatal("fail to open port: ", err) } @@ -44,7 +50,7 @@ func main() { }() //注册至consul - consul.Register(APPNAME, APPNAME, 9001, "demo") + consul.Register(APPNAME, APPNAME, *addr, *port, "demo") defer consul.DeRegister(APPNAME) // 服务停止相应