1 Star 5 Fork 0

唐小生 / nacos-vip

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

Nacos 单机/集群的vip地址服务中心

简介

   基于Nacos(官方网站:http://nacos.io )的额外web服务器,针对服务端客户端地址寻址,减少改动(服务端和客户端项目),方便动态扩容和管理。 适用于自建Nacos的单机或集群管理,阿里云的MSE微服务引擎托管则不需要考虑,官方已经处理好。

   在去年公司需要使用配置中心时,通过调研和选型,最终使用Nacos来作为配置中心和注册中心。在使用的过程中,我们 也发现了一些问题,在客户端项目中(spring cloud)和其他客户端(SpringBoot,Go,Node.js,Python...等等)我们在配置服务器地址时 spring.cloud.nacos.config.serverAddr=127.0.0.1:8848,127.0.0.2:8848,127.0.0.3:8848, 如果我们有100+个客户端,地址发生变化,是不是需要对100个客户端都进行更改,这样的方式并不灵活。官方文档和博客上也没有讲的很细,基本只有配置serverAddr的方式,而服务端集群模式下,下线或上线新的服务端机器, 我们需要在cluster.conf进行地址变更,也会出现类似的场景,所以去看源码和调研,产生了这个项目。

   适配客户端和服务端,提供使用方式和部署文档、对不同存储方式的地址列表的统一API管理、对Docker的支持等等,同时也欢迎大家使用、建议、并贡献。如果对大家有所帮助或将来有所帮助,欢迎Star一下哦

存储支持

  • Redis (单机+集群)
  • Cache (单机)
  • 内嵌derby (单机+集群) (开发中)

功能

  • Docker支持
  • 脚本启动 (开发中)

参数

  • nacos-vip系统参数
参数名 含义 可选值 默认值
nacos-vip.accessToken(-D) 操作api的秘钥(从Header头的Access-Token获取) String NULL
nacos-vip.clusterListSize(-D) 操作api的传递ip列表大小的界限值 Integer NULL
nacos-vip.standalone(-D) 是否单机模式 true/false false
  • nacos-vip数据源参数
参数名 含义 可选值 默认值
spring.redis.host(-D) redis地址 String NULL
spring.redis.port(-D) redis端口 int NULL
spring.redis.database(-D) redis库 int NULL
spring.redis.password(-D) redis密码 String NULL

使用

jar包方式使用

  1. 拉取项目到本地编译或下载已经打好的nacos-vip-1.0.2.jar
  2. 启动
    • 注:无论单机或集群启动,都建议使用配置redis方式。
    1. 单机 (-Dnacos-vip.standalone=true)

      1. 配置redis: redis存储数据
      2. 不配置redis: Cache存储数据(数据的生命周期到当前进程结束后)
    2. 集群 (无需配置:默认为false)

      1. 配置redis: redis存储数据
      2. 不配置redis: 内嵌derby存储数据(开发中)
    3. 单机或集群启动时如果已配置redis将会把redis作为存储模式(也可拉取代码在代码中修改 application.properties)

      java -jar -Dspring.redis.database=1 -Dspring.redis.host=127.0.0.1 -Dspring.redis.port=6379 -Dspring.redis.password=123456 nacos-vip-1.0.2.jar > logs/catalina.out 2>&1 & 
    4. 单机启动不配置redis

      java -jar -Dnacos-vip.standalone=true nacos-vip-1.0.2.jar > logs/catalina.out 2>&1 & 

docker方式使用

  1. 参数选项

    • -v 宿主机路径:/home/nacos-vip/logs
    • -e JAVA_OPT="-Dnacos-vip.standalone=true"
    • -p 宿主机端口:8849
    • --name 容器名称
    • -d 后台运行
    • 其余选项请参考docker运行相关命令
  2. 运行(默认会从DockerHub拉取 无需docker pull)

    • 关于集群和单机的模式和相关参数传递请参考 jar包方式使用的描述信息
    docker run -d qm0123/nacos-vip

nacos服务端和客户端配置部署

使用代理(nginx)

server {
    # 监听端口,对应客户端和服务端发起请求所指定的连接点端口 默认值8080,无需更改
    listen                 8080;
    # 自定义域名值 对应客户端和服务端配置的域名
    server_name            nacos-vip.aliyun.com;
    location / {
        # nacos vip 服务的ip:port,此处也可以使用upstream 代理
        proxy_pass         http://127.0.0.1:8849/;
        proxy_set_header   Host             $host:$server_port;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header   Access-Control-Allow-Origin  *;
    }
}

Nacos服务端

  1. 单机
    1. 启动
      sh startup.sh -m standalone
  2. 集群
    1. 启动
      sh startup.sh
    2. 配置
      1. 当前最新版本提供配置集群列表的方式
        1. 解压目录nacos/的conf目录下,有配置文件cluster.conf更改 ip:port 列表
        2. application.properties配置系统参数获取 nacos.member.list=192.168.16.101:8847?raft_port=8807,192.168.16.101?raft_port=8808,192.168.16.101:8849?raft_port=8809
        3. AddressServer vip模式的寻址方式 (推荐使用)
      2. 针对第3种方式的配置
        1. 从环境变量读取
          1. windows 我得电脑->右键属性->高级系统设置->环境变量->新建环境变量 变量名:address_server_domain 变量值: 自定义域名值(例如:nacos-vip.aliyun.com(对应nginx代理的 server_name))
          2. linux 同理
        2. 从Nacos系统参数读取(推荐)
          • 注:(外置/内置)数据源配置方式请参考Nacos(官方网站:http://nacos.io)
           # 需要更改 初始化寻址模式
           nacos.core.member.lookup.type=address-server
           # 需要更改(自己设定的域名即可) 对应nginx代理的 server_name 
           address.server.domain=nacos-vip.aliyun.com 
           # 无需更改 nacos的获取地址请求的端口默认8080
           # address.server.port=8080
           # 需要更改 nacos的获取地址请求的链接是 /nacos/serverlist ,但是客户端和服务端的获取列表协议并不兼容,所以我们需要新开一个接口去兼容服务端
           address.server.url=/nacos/server/serverlist
      3. 我们正常启动,使用下面的Open api方式就可以操作nacos的服务列表了,服务端就可以正常发现了(建议在nacos-vip服务启动后通过Open API操作初始化好服务列表数据)

Nacos客户端

  1. 配置(spring cloud)

    spring:
       cloud:
         nacos:
           config:
             file-extension: yaml
             prefix: ${spring.application.name}
             # 连接Nacos Server指定的连接点 我们将域名放入对应的 endpoint 上
             endpoint: nacos-vip.aliyun.com
           discovery:
             endpoint: nacos-vip.aliyun.com
  2. 域名指定地址的方式

    1. 本地hosts配置
      127.0.0.1 nacos-vip.aliyun.com
    2. 局域网内 使用 DNS 解析域名
    3. 阿里云内网的负载SLB(免费,客户端部署到阿里云服务器时就无需指定域名了,唯一的缺点可能某些时刻在大区下配额不足.)
  3. 类似于其他客户端(SpringBoot,Go,Node.js,Python...等等)接入的方式同理

  4. 针对于客户端,无论服务端是单机还是集群,我们都可以使用这种方式,方便以后的扩展,一次配置永久使用(除非域名更改的情况下) 在也不用为注册中心和服务中心扩容需要更新所有的服务而担心了。

Open API 指南

查询nacos服务地址列表-客户端

  1. 请求示例
    curl http://127.0.0.1:8849/nacos/serverlist
  2. 返回示例
    127.0.0.1
    127.0.0.2
    127.0.0.3
    

查询nacos服务地址列表-服务端

  1. 请求示例
    curl http://127.0.0.1:8849/nacos/server/serverlist
  2. 返回示例
    {
        "code": 200,
        "message": null,
        "data": "127.0.0.1\n127.0.0.2\n127.0.0.3\n"
    }

增加nacos服务端地址

  1. 请求示例
    curl http://127.0.0.1:8849/nacos/serverlist -X POST -H "Content-Type:application/json" -H "Access-Token:" -d '{"clusterIps": ["127.0.0.1","127.0.0.2","127.0.0.3"]}' -v
  2. 返回示例
    {
       "code": 200,
       "message": null,
       "data": null
    }

移除nacos服务端地址

  1. 请求示例

    curl http://127.0.0.1:8849/nacos/serverlist -X DELETE -H "Content-Type:application/json" -H "Access-Token:" -d '{"clusterIps": ["127.0.0.1"]}' -v
  2. 返回示例

    {
       "code": 200,
       "message": null,
       "data": null
    }

清空nacos服务端地址

  1. 请求示例
    curl http://127.0.0.1:8849/nacos/serverlist/all -X DELETE -H "Content-Type:application/json" -H "Access-Token:" -v
  2. 返回示例
    {
       "code": 200,
       "message": null,
       "data": null
    }
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

基于Nacos(官方网站:http://nacos.io )的服务端和客户端地址寻址web服务器 请使用go语言重构的当前项目https://github.com/qm012/nacos-addres 展开 收起
Java
Apache-2.0
取消

发行版 (1)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/qm012/nacos-vip.git
git@gitee.com:qm012/nacos-vip.git
qm012
nacos-vip
nacos-vip
master

搜索帮助