transfer_middleware/until/until_nacos/nacos.go

126 lines
2.8 KiB
Go

package until_nacos
import (
"fmt"
"github.com/go-kratos/kratos/contrib/registry/nacos/v2"
"github.com/nacos-group/nacos-sdk-go/clients"
"github.com/nacos-group/nacos-sdk-go/clients/config_client"
"github.com/nacos-group/nacos-sdk-go/common/constant"
"github.com/nacos-group/nacos-sdk-go/common/logger"
"github.com/nacos-group/nacos-sdk-go/vo"
"time"
)
type Logger struct {
}
func (l Logger) Info(args ...interface{}) {
}
func (l Logger) Warn(args ...interface{}) {
}
func (l Logger) Error(args ...interface{}) {
fmt.Println("hello")
}
func (l Logger) Debug(args ...interface{}) {
}
func (l Logger) Infof(fmt string, args ...interface{}) {
}
func (l Logger) Warnf(fmt string, args ...interface{}) {
}
func (l Logger) Errorf(fmt string, args ...interface{}) {
}
func (l Logger) Debugf(fmt string, args ...interface{}) {
}
func NewDiscovery(ip string, port uint64, space string, user string, password string, log string) *nacos.Registry {
defer func() {
if err := recover(); err != nil {
fmt.Println("panic:", err)
}
}()
sc := []constant.ServerConfig{
*constant.NewServerConfig(ip, port),
}
cc := constant.ClientConfig{
NamespaceId: space,
TimeoutMs: 5000,
LogDir: "./log/" + time.Now().Format(time.DateOnly) + "/" + log,
LogLevel: "error",
CustomLogger: Logger{},
Username: user,
Password: password,
}
client, err := clients.NewNamingClient(
vo.NacosClientParam{
ServerConfigs: sc,
ClientConfig: &cc,
},
)
if err != nil {
logger.Error("nacos client err", err)
}
return nacos.New(client)
}
func NewRegistrarxxx(ip string, port uint64, opts ...constant.ClientOption) *nacos.Registry {
sc := []constant.ServerConfig{
*constant.NewServerConfig(ip, port),
}
clientOpts := append([]constant.ClientOption{
constant.WithNamespaceId("transfer"),
constant.WithTimeoutMs(5000),
constant.WithCustomLogger(Logger{}),
}, opts...)
cc := constant.NewClientConfig(clientOpts...)
client, err := clients.NewNamingClient(
vo.NacosClientParam{
ServerConfigs: sc,
ClientConfig: cc,
},
)
if err != nil {
panic(err)
}
return nacos.New(client)
}
func NewNacosConfig(ip string, port uint64, opts ...constant.ClientOption) config_client.IConfigClient {
//nacos配置中心
sc := []constant.ServerConfig{
*constant.NewServerConfig(ip, port),
}
clientOpts := append([]constant.ClientOption{
constant.WithTimeoutMs(5000),
constant.WithLogDir("./log/nacos/log/" + time.Now().Format(time.DateOnly)),
constant.WithCacheDir("./log/nacos/cache"),
constant.WithLogLevel("error"),
constant.WithCustomLogger(Logger{}),
}, opts...)
cc := constant.NewClientConfig(clientOpts...)
client, err := clients.NewConfigClient(
vo.NacosClientParam{
ClientConfig: cc,
ServerConfigs: sc,
},
)
if err != nil {
panic(err)
}
return client
}