Skip to content

Commit

Permalink
update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
SoarYu committed Sep 23, 2022
1 parent 1e35dc6 commit 850a727
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# nacos-coredns-plugin [中文](./README_CN.md) #
This project provides a **DNS-F** client based on [CoreDNS](https://coredns.io/), which can help export those registed services on Nacos as DNS domain. **DNS-F** client is a dedicated agent process(side car) beside the application's process to foward the service discovery DNS domain query request to Nacos.

## Quic Start
To build and run nacos coredns plugin, the OS must be Linux or Mac. And also, make sure your golang version is 1.17 or higher as go mod support and other api is needed, and golang environments(GOPATH,GOROOT,GOHOME) must be configured correctly.
To build and run nacos coredns plugin, the OS must be Linux or Mac. And also, make sure your nacos version is 2.0 or higher and golang version is 1.17 or higher. And golang environments(GOPATH,GOROOT,GOHOME) must be configured correctly. Because it needs to support the gRPC connection feature of the nacos2.x version and the go mod function.

### Build
```
Expand All @@ -17,7 +18,7 @@ To run nacos coredns plugin, you need a configuration file. A possible file may
log
nacos {
nacos_namespaceId public
nacos_server_host console.nacos.io:8848
nacos_server_host 127.0.0.1:8848
}
forward . /etc/resolv.conf
}
Expand All @@ -30,9 +31,9 @@ To run nacos coredns plugin, you need a configuration file. A possible file may
* Firstly, you need to deploy nacos server. [Here](https://github.com/alibaba/nacos)
* Secondly, register service on nacos.
* Then run ```$GOPATH/src/coredns/coredns -conf $path_to_corefile -dns.port $dns_port```
![image](https://cdn.nlark.com/lark/0/2018/png/7601/1542623914418-f529409b-c229-4ef9-aec3-b9c5df23c906.png)
![image](https://cdn.nlark.com/yuque/0/2022/png/29425667/1663504581023-95437fee-0e3d-4b6a-851c-44a352dedd81.png)

### Test
dig $nacos_service_name @127.0.0.1 -p $dns_port

![image](https://cdn.nlark.com/lark/0/2018/png/7601/1542624023214-29cd9f71-0183-4231-b092-57535e8cfcfe.png)
![image](https://cdn.nlark.com/yuque/0/2022/png/29425667/1663504588231-341b38fe-da55-41eb-a24b-e3752b86faa4.png)
41 changes: 41 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# nacos-coredns-plugin [English](./README.md) #
本项目提供了一个基于CoreDNS的DNS-F客户端,可以将Nacos上注册的服务导出为DNS域名。 本DNS-F客户端是应用程序进程旁边的一个专用代理进程(side car),可以将服务名作为DNS域名查询请求转发到本客户端,提供服务发现的功能。
## 快速开始
要构建和运行本 nacos-coredns 插件,操作系统必须是 Linux 或 Mac。 另外,请确保您的 nacos 服务端版本为2.0或更高级版本,以及 golang 版本为 1.17 或更高级版本, 并且必须正确配置 golang 环境(GOPATH、GOROOT)。因为需要支持 nacos2.x 版本的gRPC连接功能和 go mod 功能。
### 构建
```
git clone https://github.com/nacos-group/nacos-coredns-plugin.git
cp nacos-coredns-plugin/bin/build.sh ~/
cd ~/
sh build.sh
```
### 配置
运行本 nacos-coredns 插件,您需要一个配置文件。 一个标准的配置文件如下:
```
. {
log
nacos {
nacos_namespaceId public
nacos_server_host console.nacos.io:8848
}
forward . /etc/resolv.conf
}
```
* forward:未在 nacos 注册的域名将被转发到upstream。
* nacos_namespaceId:nacos namespaceId,默认为public。
* nacos_server_host:nacos 服务端的IP地址和端口,如果有两个或多个 nacos 服务端,用逗号分隔

### 运行
* 首先需要部署一个nacos服务端。 [部署参考](https://github.com/alibaba/nacos)
* 其次,在nacos上注册服务。
* 然后输入配置文件 $path_to_corefile 和指定端口 $dns_port ,运行本项目

```$GOPATH/src/coredns/coredns -conf $path_to_corefile -dns.port $dns_port```

![image](https://cdn.nlark.com/yuque/0/2022/png/29425667/1663504581023-95437fee-0e3d-4b6a-851c-44a352dedd81.png)

### 服务发现例子
输入服务名 $nacos_service_name ,本项目部署的IP地址 $dns_ip 和端口 $dns_port

```dig $nacos_service_name @$dns_ip -p $dns_port ```
![image](https://cdn.nlark.com/yuque/0/2022/png/29425667/1663504588231-341b38fe-da55-41eb-a24b-e3752b86faa4.png)
6 changes: 2 additions & 4 deletions nacos/nacos_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ func init() {
type NacosClient struct {
serviceMap ConcurrentMap
udpServer UDPServer
//serverManager ServerManager
//serverPort int
}

type NacosClientError struct {
Expand Down Expand Up @@ -162,7 +160,7 @@ func (nacosClient *NacosClient) getAllServiceNames() {
allDoms[service] = true
}
AllDoms.Data = allDoms
AllDoms.CacheSeconds = 10 //刷新间隔
AllDoms.CacheSeconds = 20 //刷新间隔
} else {
for _, service := range services {
if !AllDoms.Data[service] {
Expand Down Expand Up @@ -251,7 +249,7 @@ func NewNacosClient(namespaceId string, serverHosts []string) *NacosClient {
AllDoms = AllDomsMap{}
AllDoms.Data = make(map[string]bool)
AllDoms.DLock = sync.RWMutex{}
AllDoms.CacheSeconds = 10
AllDoms.CacheSeconds = 20

vc.getAllServiceNames()

Expand Down
4 changes: 2 additions & 2 deletions nacos/nacos_grpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ func (ngc *NacosGrpcClient) Subscribe(serviceName string) error {
return err
}

defer ngc.SubscribeMap.DLock.Unlock()
ngc.SubscribeMap.DLock.Lock()
ngc.SubscribeMap.Data[serviceName] = true
ngc.SubscribeMap.DLock.Unlock()

return nil
}
Expand All @@ -146,9 +146,9 @@ func (ngc *NacosGrpcClient) Unsubsrcibe(serviceName string) error {
return err
}

defer ngc.SubscribeMap.DLock.Unlock()
ngc.SubscribeMap.DLock.Lock()
ngc.SubscribeMap.Data[serviceName] = false
ngc.SubscribeMap.DLock.Unlock()

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion nacos/nacos_grpc_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
var grpcClientTest = NewNacosGrpcClientTest()

func NewNacosGrpcClientTest() *NacosGrpcClient {
grpcClient, err := NewNacosGrpcClient("", []string{"106.52.77.111:8848"}, nacosClientTest)
grpcClient, err := NewNacosGrpcClient("", []string{"console.nacos.io:8848"}, nacosClientTest)
if err != nil {
fmt.Println("init grpc client failed")
}
Expand Down

0 comments on commit 850a727

Please sign in to comment.