nacos coredns plugin 修改代码简单说明
因为历史版本比较老以及兼容问题,调整支持coredns 新版本,以下简单说明下代码修改
支持go mod
因为构建是推荐基于了go mod,所以支持了go mod
go.mod
module github.com/rongfengliang/coredns-nacos
支持用户密码认证
主要是在配置解决以及client 中添加了密码认证的支持
- 解析部分
for c.Next() {
nacosImpl.Zones = c.RemainingArgs()
if c.NextBlock() {
for {
switch v := c.Val(); v {
case "nacos_namespaceId":
namespaceId = c.RemainingArgs()[0]
case "nacos_server_host":
serverHosts = strings.Split(c.RemainingArgs()[0], ",")
case "nacos_username":
userName = c.RemainingArgs()[0]
case "nacos_password":
password = c.RemainingArgs()[0]
- client
func NewNacosClient(namespaceId string, serverHosts []string, userName, password string) *NacosClient {
fmt.Println("init nacos client.")
initLog()
vc := NacosClient{NewConcurrentMap(), UDPServer{}}
vc.loadCache()
vc.udpServer.vipClient = &vc
//init grpcClient
var err error
GrpcClient, err = NewNacosGrpcClient(namespaceId, serverHosts, userName, password, &vc)
if err != nil {
NacosClientLogger.Error("init nacos-grpc-client failed.", err)
}
if EnableReceivePush {
go vc.udpServer.StartServer()
}
AllDoms = AllDomsMap{}
AllDoms.Data = make(map[string]bool)
AllDoms.DLock = sync.RWMutex{}
AllDoms.CacheSeconds = 20
vc.getAllServiceNames()
go vc.asyncGetAllServiceNames()
//go vc.asyncUpdateDomain()
NacosClientLogger.Info("cache-path: " + CachePath)
return &vc
}
- grpc client
func NewNacosGrpcClient(namespaceId string, serverHosts []string, userName, password string, vc *NacosClient) (*NacosGrpcClient, error) {
var nacosGrpcClient NacosGrpcClient
nacosGrpcClient.nacosClient = vc
if namespaceId == "public" {
namespaceId = ""
}
nacosGrpcClient.namespaceId = namespaceId //When namespace is public, fill in the blank string here.
serverConfigs := make([]constant.ServerConfig, len(serverHosts))
for i, serverHost := range serverHosts {
serverIp := strings.Split(serverHost, ":")[0]
serverPort, err := strconv.Atoi(strings.Split(serverHost, ":")[1])
if err != nil {
NacosClientLogger.Error("nacos server host config error!", err)
}
serverConfigs[i] = *constant.NewServerConfig(
serverIp,
uint64(serverPort),
constant.WithScheme("http"),
constant.WithContextPath("/nacos"),
)
}
nacosGrpcClient.serverConfigs = serverConfigs
nacosGrpcClient.clientConfig = *constant.NewClientConfig(
constant.WithNamespaceId(namespaceId),
constant.WithTimeoutMs(5000),
constant.WithNotLoadCacheAtStart(true),
constant.WithUpdateCacheWhenEmpty(true),
constant.WithUsername(userName),
constant.WithPassword(password),
constant.WithLogDir(LogPath),
constant.WithCacheDir(CachePath),
constant.WithLogLevel("debug"),
)
var err error
nacosGrpcClient.grpcClient, err = clients.NewNamingClient(
vo.NacosClientParam{
ClientConfig: &nacosGrpcClient.clientConfig,
ServerConfigs: nacosGrpcClient.serverConfigs,
},
)
if err != nil {
fmt.Println("init nacos-client error")
}
nacosGrpcClient.SubscribeMap = AllDomsMap{}
nacosGrpcClient.SubscribeMap.Data = make(map[string]bool)
nacosGrpcClient.SubscribeMap.DLock = sync.RWMutex{}
return &nacosGrpcClient, err
}
说明
注意corends plugin 的顺序是基于plugin.cfg 静态配置的,对于使用此plugin 推荐在kubernetes,等后续支持nacos 的goup 解析
浙公网安备 33010602011771号