支持多个服务启动,方便之后的测试

This commit is contained in:
suguo.yao 2021-01-23 15:10:11 +08:00
parent 0ac3f91efa
commit 94370d33fc
7 changed files with 18 additions and 70 deletions

1
.gitignore vendored
View File

@ -15,3 +15,4 @@
# Dependency directories (remove the comment below to include it)
# vendor/
consul-demo
logs/

View File

@ -43,6 +43,7 @@
}()
#### 注册中心注册代码编写
编写在文件consul-service.go中作为源代码组件形式分发
#### 实现客户端代码

View File

@ -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

View File

@ -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,
}

View File

@ -1 +0,0 @@
demo2021012314.log

View File

@ -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

12
main.go
View File

@ -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)
// 服务停止相应