🎨 完善网关
This commit is contained in:
parent
5ebdb0b747
commit
cd2530afa4
@ -3,4 +3,5 @@ package config
|
|||||||
var (
|
var (
|
||||||
AppInfo appInfo
|
AppInfo appInfo
|
||||||
LokiConfig lokiConfig
|
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
|
package initialization
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"gateway/config"
|
||||||
"gateway/core"
|
"gateway/core"
|
||||||
"gateway/utils"
|
"gateway/utils"
|
||||||
"github.com/nacos-group/nacos-sdk-go/clients"
|
"github.com/nacos-group/nacos-sdk-go/clients"
|
||||||
@ -10,13 +11,14 @@ import (
|
|||||||
|
|
||||||
// 初始化Nacos注册
|
// 初始化Nacos注册
|
||||||
func initNacos() {
|
func initNacos() {
|
||||||
|
config.InitNacosConfig()
|
||||||
|
|
||||||
sc := []constant.ServerConfig{
|
sc := []constant.ServerConfig{
|
||||||
*constant.NewServerConfig("192.168.0.124", 8848),
|
*constant.NewServerConfig(config.NacosConfig.Host, config.NacosConfig.Port),
|
||||||
}
|
}
|
||||||
cc := constant.ClientConfig{
|
cc := constant.ClientConfig{
|
||||||
AppName: "gateway",
|
AppName: config.AppInfo.AppName,
|
||||||
//NamespaceId: "e56f939e-6d22-449c-97d2-39a8ae5afd58", //namespace id
|
NamespaceId: config.NacosConfig.NamespaceId,
|
||||||
NamespaceId: "",
|
|
||||||
TimeoutMs: 5000,
|
TimeoutMs: 5000,
|
||||||
NotLoadCacheAtStart: true,
|
NotLoadCacheAtStart: true,
|
||||||
RotateTime: "1h",
|
RotateTime: "1h",
|
||||||
@ -39,7 +41,7 @@ func initNacos() {
|
|||||||
}
|
}
|
||||||
success, _ := client.RegisterInstance(vo.RegisterInstanceParam{
|
success, _ := client.RegisterInstance(vo.RegisterInstanceParam{
|
||||||
Ip: ip,
|
Ip: ip,
|
||||||
Port: 8889,
|
Port: config.AppInfo.Port,
|
||||||
Weight: 10,
|
Weight: 10,
|
||||||
Enable: true,
|
Enable: true,
|
||||||
Healthy: true,
|
Healthy: true,
|
||||||
@ -51,12 +53,27 @@ func initNacos() {
|
|||||||
core.Log.Panic("服务注册失败,退出程序")
|
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)
|
configClient, err := clients.NewConfigClient(configParam)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
core.Log.Error("创建配置连接失败: %v", err.Error())
|
core.Log.Error("创建配置连接失败: %v", err.Error())
|
||||||
}
|
}
|
||||||
_ = configClient.ListenConfig(vo.ConfigParam{
|
_ = configClient.ListenConfig(vo.ConfigParam{
|
||||||
DataId: "gateway.yml",
|
DataId: config.NacosConfig.CenterConfigName,
|
||||||
Group: "DEFAULT_GROUP",
|
Group: "DEFAULT_GROUP",
|
||||||
OnChange: configChanged,
|
OnChange: configChanged,
|
||||||
})
|
})
|
||||||
|
@ -2,6 +2,7 @@ package middleware
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
gc "gateway/config"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/opentracing/opentracing-go"
|
"github.com/opentracing/opentracing-go"
|
||||||
"github.com/opentracing/opentracing-go/ext"
|
"github.com/opentracing/opentracing-go/ext"
|
||||||
@ -9,16 +10,11 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
AppName = "gateway"
|
|
||||||
JaegerHostPort = "192.168.0.124:6831"
|
|
||||||
)
|
|
||||||
|
|
||||||
func OpenTracing() gin.HandlerFunc {
|
func OpenTracing() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
var parentSpan opentracing.Span
|
var parentSpan opentracing.Span
|
||||||
|
|
||||||
tracer, closer := NewJaegerTracer(AppName)
|
tracer, closer := NewJaegerTracer()
|
||||||
defer closer.Close()
|
defer closer.Close()
|
||||||
|
|
||||||
spCtx, err := opentracing.GlobalTracer().Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(c.Request.Header))
|
spCtx, err := opentracing.GlobalTracer().Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(c.Request.Header))
|
||||||
@ -53,7 +49,7 @@ func OpenTracing() gin.HandlerFunc {
|
|||||||
|
|
||||||
//var Tracer opentracing.Tracer
|
//var Tracer opentracing.Tracer
|
||||||
|
|
||||||
func NewJaegerTracer(serviceName string) (opentracing.Tracer, io.Closer) {
|
func NewJaegerTracer() (opentracing.Tracer, io.Closer) {
|
||||||
cfg := &config.Configuration{
|
cfg := &config.Configuration{
|
||||||
Sampler: &config.SamplerConfig{
|
Sampler: &config.SamplerConfig{
|
||||||
Type: "const", //固定采样
|
Type: "const", //固定采样
|
||||||
@ -62,10 +58,10 @@ func NewJaegerTracer(serviceName string) (opentracing.Tracer, io.Closer) {
|
|||||||
|
|
||||||
Reporter: &config.ReporterConfig{
|
Reporter: &config.ReporterConfig{
|
||||||
LogSpans: true,
|
LogSpans: true,
|
||||||
LocalAgentHostPort: JaegerHostPort,
|
LocalAgentHostPort: gc.AppInfo.JaegerServer,
|
||||||
},
|
},
|
||||||
|
|
||||||
ServiceName: serviceName,
|
ServiceName: gc.AppInfo.AppName,
|
||||||
}
|
}
|
||||||
|
|
||||||
tracer, closer, err := cfg.NewTracer()
|
tracer, closer, err := cfg.NewTracer()
|
||||||
|
Loading…
Reference in New Issue
Block a user