首页 > 其他分享 >gin-consul配置到全局

gin-consul配置到全局

时间:2022-11-09 16:14:42浏览次数:41  
标签:err ut consul global ServerConfig api initialize gin 全局

一.配置全局变量

global/golbal.go中添加: UserSrvClient proto.UserClient

package global

import (
	ut "github.com/go-playground/universal-translator"
	"mxshop-api/user-web/config"
	"mxshop-api/user-web/proto"
)

var (
	Trans        ut.Translator
	ServerConfig *config.ServerConfig = &config.ServerConfig{}
	UserSrvClient proto.UserClient
)

  

二. 配置初始化文件

在initialize中创建srv_conn.go初始化的文件

package initialize

import (
	"fmt"
	"github.com/hashicorp/consul/api"
	"go.uber.org/zap"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"mxshop-api/user-web/global"
	"mxshop-api/user-web/proto"
)

func InitSrvConn() {
	//从注册中心获取到用户服务的信息
	cfg := api.DefaultConfig()
	cfg.Address = "127.0.0.1:8500"

	//zap.S().Infof(fmt.Sprintf("配置:%s", global.ServerConfig.ConsulInfo.Host))
	cfg.Address = fmt.Sprintf("%s:%d", global.ServerConfig.ConsulInfo.Host, global.ServerConfig.ConsulInfo.Port)

	userSrvHost := ""
	userSrvPost := 0

	client, err := api.NewClient(cfg)
	if err != nil {
		panic(err)
	}

	data, err := client.Agent().ServicesWithFilter(fmt.Sprintf(`Service == "%s"`, global.ServerConfig.UserSrvInfo.Name))
	//data, err := client.Agent().ServicesWithFilter(fmt.Sprintf("Service == \"%s\"", global.ServerConfig.UserSrvInfo.Name))

	if err != nil {
		panic(err)
	}
	for _, value := range data {
		userSrvHost = value.Address
		userSrvPost = value.Port
		break
	}

	if userSrvHost == "" {
		zap.S().Fatal("【InitSrvConn】链接【用户失败】")
	}
	//拨号连接用户RPC服务
	userConn, err := grpc.Dial(fmt.Sprintf("%s:%d", userSrvHost, userSrvPost), grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		zap.S().Errorw("[GetUserList]连接失败【用户服务失败】", "msg", err.Error())
	}
	userSrvClient := proto.NewUserClient(userConn)

	global.UserSrvClient = userSrvClient
}

 三. main中初始化

package main

import (
	"fmt"
	"github.com/gin-gonic/gin/binding"
	ut "github.com/go-playground/universal-translator"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
	"mxshop-api/user-web/global"

	"mxshop-api/user-web/initialize"
	myvalidator "mxshop-api/user-web/validator"
)

func main() {

	//初始化logger
	initialize.InitLogger()

	//初始化配置文件
	initialize.InitConfig()
	//初始化routes
	Router := initialize.Routers()

	//初始化翻译
	if err := initialize.InitTrans("zh"); err != nil {
		panic(err)
	}

	//初始化srv的链接
	initialize.InitSrvConn()

	//注册验证器
	if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
		_ = v.RegisterValidation("mobile", myvalidator.ValidateMobile)
		_ = v.RegisterTranslation("mobile", global.Trans, func(ut ut.Translator) error {
			return ut.Add("mobile", "{0} 非法的手机号码!", true) // see universal-translator for details
		}, func(ut ut.Translator, fe validator.FieldError) string {
			t, _ := ut.T("mobile", fe.Field())
			return t
		})
	}
	/*
		1. S()可以获取一个全局的sugar,可以让我们自己设置一个全局的logger
		2. 日志是分级别的,debug, info , warn, error, fetal
		3. S函数和L函数很有用, 提供了一个全局的安全访问logger的途径
	*/

	zap.S().Infof("启动服务")
	//port := 8881
	if err := Router.Run(fmt.Sprintf(":%d", global.ServerConfig.Port)); err != nil {
		zap.S().Panic("启动失败:", err.Error())
	}

}

  

 

标签:err,ut,consul,global,ServerConfig,api,initialize,gin,全局
From: https://www.cnblogs.com/wlike/p/16874114.html

相关文章