consul的安装
https://blog.csdn.net/qq904748592/article/details/127650268
1,在项目中定义服务名称,以及版本号,以及名称
"flag"
"github.com/go-kratos/kratos/contrib/registry/consul/v2"
"github.com/google/uuid"
"github.com/hashicorp/consul/api"
"os"
"valuation/internal/conf"
"github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/config"
"github.com/go-kratos/kratos/v2/config/file"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware/tracing"
"github.com/go-kratos/kratos/v2/transport/grpc"
"github.com/go-kratos/kratos/v2/transport/http"
_ "go.uber.org/automaxprocs"
// Name is the name of the compiled software.
Name string = "Valuation"
// Version is the version of the compiled software.
Version string = "1.0.0"
// flagconf is the config flag.
flagconf string
//id, _ = os.Hostname()
id = Name + "-" + uuid.NewString()
2,将服务注册到注册中心
//获取consul 客户端
consulConfig := api.DefaultConfig()
consulConfig.Address = "localhost:8500"
consulClient, err := api.NewClient(consulConfig)
if err != nil {
log.Fatal(err)
}
//二,获取consul注册管理器
reg := consul.New(consulClient)
return kratos.New(
kratos.ID(id),
kratos.Name(Name),
kratos.Version(Version),
kratos.Metadata(map[string]string{}),
kratos.Logger(logger),
kratos.Server(
gs,
hs,
),
//三,创建服务时,指定服务器注册
kratos.Registrar(reg),
)
3,拉取依赖并启动
go get github.com/google/wire/cmd/wire
go generate ./...
kratos run