🎨 完善网关
This commit is contained in:
parent
5ebdb0b747
commit
cd2530afa4
@ -3,4 +3,5 @@ package config
|
||||
var (
|
||||
AppInfo appInfo
|
||||
LokiConfig lokiConfig
|
||||
NacosConfig nacosConfig
|
||||
)
|
||||
|
28
config/nacos.go
Normal file
28
config/nacos.go
Normal file
@ -0,0 +1,28 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"gateway/utils"
|
||||
)
|
||||
|
||||
// Nacos配置
|
||||
type nacosConfig struct {
|
||||
Host string // 主机
|
||||
Port uint64 // 端口
|
||||
NamespaceId string // 命名空间
|
||||
CenterConfigName string // 外部配置文件名,多个以逗号隔开
|
||||
}
|
||||
|
||||
// InitNacosConfig 初始化OSS配置
|
||||
func InitNacosConfig() {
|
||||
host := utils.GetEnvVal("NACOS_HOST", "nacos-headless")
|
||||
port := utils.GetEnvIntVal("NACOS_PORT", 8848)
|
||||
namespaceId := utils.GetEnvVal("NACOS_NAMESPACE", "")
|
||||
centerConfigName := utils.GetEnvVal("NACOS_CONFIG_NAME", "application.yml")
|
||||
|
||||
NacosConfig = nacosConfig{
|
||||
Host: host,
|
||||
Port: uint64(port),
|
||||
NamespaceId: namespaceId,
|
||||
CenterConfigName: centerConfigName,
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package initialization
|
||||
|
||||
import (
|
||||
"gateway/config"
|
||||
"gateway/core"
|
||||
"gateway/utils"
|
||||
"github.com/nacos-group/nacos-sdk-go/clients"
|
||||
@ -10,13 +11,14 @@ import (
|
||||
|
||||
// 初始化Nacos注册
|
||||
func initNacos() {
|
||||
config.InitNacosConfig()
|
||||
|
||||
sc := []constant.ServerConfig{
|
||||
*constant.NewServerConfig("192.168.0.124", 8848),
|
||||
*constant.NewServerConfig(config.NacosConfig.Host, config.NacosConfig.Port),
|
||||
}
|
||||
cc := constant.ClientConfig{
|
||||
AppName: "gateway",
|
||||
//NamespaceId: "e56f939e-6d22-449c-97d2-39a8ae5afd58", //namespace id
|
||||
NamespaceId: "",
|
||||
AppName: config.AppInfo.AppName,
|
||||
NamespaceId: config.NacosConfig.NamespaceId,
|
||||
TimeoutMs: 5000,
|
||||
NotLoadCacheAtStart: true,
|
||||
RotateTime: "1h",
|
||||
@ -39,7 +41,7 @@ func initNacos() {
|
||||
}
|
||||
success, _ := client.RegisterInstance(vo.RegisterInstanceParam{
|
||||
Ip: ip,
|
||||
Port: 8889,
|
||||
Port: config.AppInfo.Port,
|
||||
Weight: 10,
|
||||
Enable: true,
|
||||
Healthy: true,
|
||||
@ -51,12 +53,27 @@ func initNacos() {
|
||||
core.Log.Panic("服务注册失败,退出程序")
|
||||
}
|
||||
|
||||
// 打印所有服务
|
||||
go func() {
|
||||
instances, err := client.SelectAllInstances(vo.SelectAllInstancesParam{
|
||||
ServiceName: config.AppInfo.AppName,
|
||||
})
|
||||
if err != nil {
|
||||
core.Log.Error("获取所有服务失败")
|
||||
return
|
||||
}
|
||||
core.Log.Debug("获取到服务总数: %v", len(instances))
|
||||
for _, instance := range instances {
|
||||
core.Log.Debug("服务ID: %v --> %v:%v", instance.InstanceId, instance.Ip, instance.Port)
|
||||
}
|
||||
}()
|
||||
|
||||
configClient, err := clients.NewConfigClient(configParam)
|
||||
if err != nil {
|
||||
core.Log.Error("创建配置连接失败: %v", err.Error())
|
||||
}
|
||||
_ = configClient.ListenConfig(vo.ConfigParam{
|
||||
DataId: "gateway.yml",
|
||||
DataId: config.NacosConfig.CenterConfigName,
|
||||
Group: "DEFAULT_GROUP",
|
||||
OnChange: configChanged,
|
||||
})
|
||||
|
@ -2,6 +2,7 @@ package middleware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
gc "gateway/config"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/opentracing/opentracing-go"
|
||||
"github.com/opentracing/opentracing-go/ext"
|
||||
@ -9,16 +10,11 @@ import (
|
||||
"io"
|
||||
)
|
||||
|
||||
const (
|
||||
AppName = "gateway"
|
||||
JaegerHostPort = "192.168.0.124:6831"
|
||||
)
|
||||
|
||||
func OpenTracing() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
var parentSpan opentracing.Span
|
||||
|
||||
tracer, closer := NewJaegerTracer(AppName)
|
||||
tracer, closer := NewJaegerTracer()
|
||||
defer closer.Close()
|
||||
|
||||
spCtx, err := opentracing.GlobalTracer().Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(c.Request.Header))
|
||||
@ -53,7 +49,7 @@ func OpenTracing() gin.HandlerFunc {
|
||||
|
||||
//var Tracer opentracing.Tracer
|
||||
|
||||
func NewJaegerTracer(serviceName string) (opentracing.Tracer, io.Closer) {
|
||||
func NewJaegerTracer() (opentracing.Tracer, io.Closer) {
|
||||
cfg := &config.Configuration{
|
||||
Sampler: &config.SamplerConfig{
|
||||
Type: "const", //固定采样
|
||||
@ -62,10 +58,10 @@ func NewJaegerTracer(serviceName string) (opentracing.Tracer, io.Closer) {
|
||||
|
||||
Reporter: &config.ReporterConfig{
|
||||
LogSpans: true,
|
||||
LocalAgentHostPort: JaegerHostPort,
|
||||
LocalAgentHostPort: gc.AppInfo.JaegerServer,
|
||||
},
|
||||
|
||||
ServiceName: serviceName,
|
||||
ServiceName: gc.AppInfo.AppName,
|
||||
}
|
||||
|
||||
tracer, closer, err := cfg.NewTracer()
|
||||
|
Loading…
Reference in New Issue
Block a user