1 Star 0 Fork 0

xlizy/common-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
dubbo.go 5.74 KB
一键复制 编辑 原始数据 按行查看 历史
xlizy 提交于 2024-10-24 10:20 . 重构
package dubbo
import (
"context"
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/config"
"dubbo.apache.org/dubbo-go/v3/filter"
"dubbo.apache.org/dubbo-go/v3/protocol"
_ "dubbo.apache.org/dubbo-go/v3/registry/directory"
"gitee.com/xlizy/common-go/base/common_config"
"gitee.com/xlizy/common-go/base/common_const/threadlocal"
"gitee.com/xlizy/common-go/utils/common_utils"
"gitee.com/xlizy/common-go/utils/json"
"gitee.com/xlizy/common-go/utils/zlog"
"github.com/google/uuid"
"math/rand/v2"
"net"
"strconv"
"time"
_ "dubbo.apache.org/dubbo-go/v3/cluster/cluster/failover"
_ "dubbo.apache.org/dubbo-go/v3/cluster/loadbalance/random"
_ "dubbo.apache.org/dubbo-go/v3/config_center/nacos"
//_ "dubbo.apache.org/dubbo-go/v3/imports"
_ "dubbo.apache.org/dubbo-go/v3/filter/accesslog"
_ "dubbo.apache.org/dubbo-go/v3/filter/active"
_ "dubbo.apache.org/dubbo-go/v3/filter/adaptivesvc"
_ "dubbo.apache.org/dubbo-go/v3/filter/auth"
_ "dubbo.apache.org/dubbo-go/v3/filter/echo"
_ "dubbo.apache.org/dubbo-go/v3/filter/exec_limit"
_ "dubbo.apache.org/dubbo-go/v3/filter/generic"
_ "dubbo.apache.org/dubbo-go/v3/filter/graceful_shutdown"
_ "dubbo.apache.org/dubbo-go/v3/filter/hystrix"
_ "dubbo.apache.org/dubbo-go/v3/filter/metrics"
_ "dubbo.apache.org/dubbo-go/v3/filter/otel/trace"
_ "dubbo.apache.org/dubbo-go/v3/filter/polaris/limit"
_ "dubbo.apache.org/dubbo-go/v3/filter/seata"
_ "dubbo.apache.org/dubbo-go/v3/filter/sentinel"
_ "dubbo.apache.org/dubbo-go/v3/filter/token"
_ "dubbo.apache.org/dubbo-go/v3/filter/tps"
_ "dubbo.apache.org/dubbo-go/v3/filter/tps/limiter"
_ "dubbo.apache.org/dubbo-go/v3/filter/tps/strategy"
_ "dubbo.apache.org/dubbo-go/v3/filter/tracing"
_ "dubbo.apache.org/dubbo-go/v3/metadata/report/nacos"
_ "dubbo.apache.org/dubbo-go/v3/metadata/service/exporter/configurable"
_ "dubbo.apache.org/dubbo-go/v3/metadata/service/local"
_ "dubbo.apache.org/dubbo-go/v3/protocol/dubbo3"
_ "dubbo.apache.org/dubbo-go/v3/protocol/dubbo3/health"
_ "dubbo.apache.org/dubbo-go/v3/protocol/dubbo3/reflection"
_ "dubbo.apache.org/dubbo-go/v3/proxy/proxy_factory"
_ "dubbo.apache.org/dubbo-go/v3/registry/nacos"
_ "dubbo.apache.org/dubbo-go/v3/registry/protocol"
)
type Service struct {
Name string
InterfaceName string
Interface interface{}
Weight int
}
type Services struct {
Consumer []Service
Provider []Service
}
func InitDubbo2(services Services) {
}
func InitDubbo(services Services) {
nacosCfg := common_config.GetNacosCfg()
extension.SetFilter("TraceIdFilter", NewTraceIdFilter)
rc := config.GetRootConfig()
rc.Logger = config.NewLoggerConfigBuilder().Build()
rc.Logger.Level = "error"
rc.Logger.File = &config.File{
//Name: "./logs/dubbo.log",
MaxAge: 30,
}
rc.Application.Name = common_config.GetAppName()
rc.Registries["nacos"] = &config.RegistryConfig{
Protocol: "nacos",
Address: nacosCfg.Addr,
Namespace: nacosCfg.Namespace,
Username: nacosCfg.Username,
Password: nacosCfg.Password,
TTL: "0",
RegistryType: "interface",
}
port := ""
for i := 0; i < 10; i++ {
pi := rand.IntN(16383) + 49152
port = strconv.Itoa(pi)
zlog.Info("端口:{}", port)
if checkPort(port) {
break
}
}
rc.Protocols["tri"] = &config.ProtocolConfig{
Name: "tri",
Ip: common_utils.GetLocalPriorityIp(common_config.PriorityNetwork.Networks),
Port: port,
}
check := false
for _, service := range services.Consumer {
config.SetConsumerService(service.Interface)
rc.Consumer.References[service.Name] = &config.ReferenceConfig{
InterfaceName: service.InterfaceName,
Protocol: "tri",
Check: &check,
Retries: "0",
Group: common_config.GetNacosCfg().Cluster,
Version: "1.0.0",
Filter: "TraceIdFilter",
Loadbalance: "clusterWeightedRandomRobinLoadBalance",
}
}
for _, service := range services.Provider {
config.SetProviderService(service.Interface)
servicesParams := make(map[string]string)
if service.Weight <= 0 {
servicesParams["appWeight"] = "1"
} else {
servicesParams["appWeight"] = strconv.Itoa(service.Weight)
}
rc.Provider.Services[service.Name] = &config.ServiceConfig{
Interface: service.InterfaceName,
Group: common_config.GetNacosCfg().Cluster,
Params: servicesParams,
Version: "1.0.0",
Filter: "TraceIdFilter",
Loadbalance: "clusterWeightedRandomRobinLoadBalance",
}
}
if err := config.Load(config.WithRootConfig(rc)); err != nil {
zlog.Error("初始化Dubbo异常:{}", err.Error())
panic(err)
}
}
type traceIdFilter struct {
}
func (t traceIdFilter) Invoke(ctx context.Context, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
zlog.Info("调用节点:{}", invoker.GetURL())
traceId := "<nil>"
traceIdKey := "_dubbo_trace_id"
filterVal, ok := invocation.GetAttachment(traceIdKey)
if ok {
traceId = filterVal
} else {
traceId = threadlocal.GetTraceId()
}
if traceId == "<nil>" {
traceId = uuid.New().String()
}
invocation.SetAttachment(traceIdKey, traceId)
threadlocal.SetTraceId(traceId)
zlog.Info("dubbo-call-start,methodName:{},invocation:{}", invocation.MethodName(), json.ToJsonStr(invocation))
return invoker.Invoke(ctx, invocation)
}
func (t traceIdFilter) OnResponse(ctx context.Context, result protocol.Result, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
zlog.Info("dubbo-call-end,response:{}", json.ToJsonStr(result))
return result
}
func NewTraceIdFilter() filter.Filter {
return &traceIdFilter{}
}
// 检测端口
func checkPort(port string) bool {
conn, err := net.DialTimeout("tcp", "127.0.0.1:"+port, 3*time.Second)
if err != nil {
return true
} else {
if conn != nil {
conn.Close()
return false
} else {
return true
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/xlizy/common-go.git
git@gitee.com:xlizy/common-go.git
xlizy
common-go
common-go
v0.3.1

搜索帮助