我健康检查一直失败,找不出什么原因,我把那个127.0.0.1 改成我局域网的ip也失败
package main
import (
"fmt"
"github.com/hashicorp/consul/api"
"net"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
"mxshop_srvs/user_srv/global"
"mxshop_srvs/user_srv/handler"
"mxshop_srvs/user_srv/initialize"
"mxshop_srvs/user_srv/proto"
)
func main() {
//1. 初始化logger
initialize.InitLogger()
//2、初始化配置
initialize.InitConfig()
//3、数据库链接
initialize.InitDb()
IP:=global.ServerConfig.Host
Port:=global.ServerConfig.Port
fmt.Println(global.ServerConfig.MysqlInfo)
g := grpc.NewServer()
proto.RegisterUserServer(g, &handler.UserServer{})
lis, err := net.Listen("tcp", fmt.Sprintf("%s:%d",IP,Port))
if err != nil {
panic("failed to listen:" + err.Error())
}
//注册健康检查
grpc_health_v1.RegisterHealthServer(g,health.NewServer())
//注册到consul
cfg := api.DefaultConfig()
cfg.Address = fmt.Sprintf("%s:%d", global.ServerConfig.ConsulInfo.Host,
global.ServerConfig.ConsulInfo.Port)
fmt.Println(cfg)
client, err := api.NewClient(cfg)
if err != nil {
panic(err)
}
//生成对应的检查对象
check := &api.AgentServiceCheck{
GRPC: fmt.Sprintf("192.168.31.134:%d", Port),
Timeout: "5s",
Interval: "5s",
DeregisterCriticalServiceAfter: "15s",
}
//生成注册对象
registration := new(api.AgentServiceRegistration)
registration.Name = global.ServerConfig.Name
serviceID := global.ServerConfig.Name
registration.ID = serviceID
registration.Port =Port
registration.Tags = []string{"imooc", "bobby", "user", "srv"}
registration.Address = "192.168.31.134"
registration.Check = check
//1. 如何启动两个服务
//2. 即使我能够通过终端启动两个服务,但是注册到consul中的时候也会被覆盖
err = client.Agent().ServiceRegister(registration)
if err != nil {
panic(err)
}
err = g.Serve(lis)
if err != nil {
panic("failed to start grpc:" + err.Error())
}
}
44
收起
正在回答 回答被采纳积分+1
2回答
慕斯卡2397934
2023-03-29 09:19:04
兄弟先把健康检查参数去掉,正常进行课程,暂时不影响后面的,回过头再搞这个
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星