红茶的个人站点

  • 首页
  • 专栏
  • 开发工具
  • 其它
  • 隐私政策
Awalon
Talk is cheap,show me the code.
  1. 首页
  2. 开发工具
  3. 正文

NACOS 二次开发指南

2026年2月3日 12点热度 0人点赞 0条评论

下载代码

通过官方仓库下载代码:

git clone git@github.com:alibaba/nacos.git

基于特定版本进行二次开发,比如这里基于2.2.2进行开发,NACOS 使用 tag 标记不同版本的代码,所以可以切换到特定版本的代码:

git checkout 2.2.2

基于这个版本创建本地分支进行开发:

git switch -c my-2.2.2

项目前端

NACOS 的前端项目使用 REACT 构建。项目启动命令位于console-ui/package.json。

启动项目:

npm start

此时使用的是console-ui/build/webpack.dev.conf.js中的配置信息运行, 如果需要修改对应的服务端配置,可以修改其中的代理配置:

proxy: [{
    context: ['/'],
    changeOrigin: true,
    secure: false,
    target: 'http://localhost:8848',
    pathRewrite: {'^/v1' : '/nacos/v1', '^/v2' : '/nacos/v2'}
}]

项目编译:

npm run build

编译脚本会在编译后将生成的 html 等文件复制到 console 的静态资源目录下, 这样运行 console 模块时,会加载修改后的前端页面。

build 命令使用的配置文件为console-ui/build/webpack.prod.conf.js。

项目后端

项目后端基于 SpringBoot 构建。项目启动的入口类位于 console/src/main/java/com/alibaba/nacos/Nacos.java。

项目后端的静态资源中包含前端页面文件,可以单独运行。除非你需要调试修改前端页面。

代码风格

项目打包插件中包含代码风格检查,因此后端代码需要遵守一些规则。

不能使用*进行批量引用,错误的示范:

import com.alibaba.nacos.common.utils.*;

必须使用单行引用,正确的示范:

import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.common.utils.UrlUtils;
import com.alibaba.nacos.common.utils.ConcurrentHashSet;

在 idea 中可以右键,选择修改为单行引用。

JavaDoc 注释首行必须有摘要,且必须以英文句号结尾,错误的示范:

 /**
 * This is a test class
 */
 class  Test {}

正确的示范:

 /**
 * This is a test class.
 */
 class  Test {}

方法前的 JavaDoc 注释同样要遵循这个规则。

禁止在方法前使用//注释,错误的示范:

// 测试用例(可删除)
public static void main(String[] args) {
    // ...
}

正确的示范:

/**
* Test case.
*/
public static void main(String[] args) {
    // ...
}

类/对象属性前要有空行,错误的示范:

public class ModifiableRequestWrapper extends HttpServletRequestWrapper {
    // 维护可修改的参数Map
    private final Map<String, String[]> parameterMap;
}

正确的示范:

public class ModifiableRequestWrapper extends HttpServletRequestWrapper {
    
    // 维护可修改的参数Map
    private final Map<String, String[]> parameterMap;
}

不能使用行尾注释,错误的示范:

public class CryptoUtil {
    private static final String ALGORITHM = "AES/CBC/PKCS5Padding"; // 填充模式和前端Pkcs7一致(Java中是Pkcs5)
}

正确的示范:

public class CryptoUtil {
    // 填充模式和前端Pkcs7一致(Java中是Pkcs5)
    private static final String ALGORITHM = "AES/CBC/PKCS5Padding";
}

字段注释只能使用 JavaDoc,不能使用//,错误的示范:

public class CryptoUtil {
    // 填充模式和前端Pkcs7一致(Java中是Pkcs5)
    private static final String ALGORITHM = "AES/CBC/PKCS5Padding";
}

正确的示范:

public class CryptoUtil {
    
    /**
     * The fill pattern is the same as the frontend Pkcs7 (Pkcs5 in Java)
     */
    private static final String ALGORITHM = "AES/CBC/PKCS5Padding";
}

代码头部必须添加版权信息,正确的示范:

/*
 * Copyright icexmoon.
 *
 * 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.
 */
​
package com.alibaba.nacos.console;
//...

方法名不能有连续的大写字母出现,错误的示范:

public static String decryptAES(String cipherText) {
    // ...
}

正确的示范:

public static String decryptAes(String cipherText) {
    // ...
}

打包

打包时需要勾选 maven 的release-nacos配置选项。 需要为生命周期的 install 选项添加虚拟机选项:

-Dmaven.test.skip=true -Dcheckstyle.skip=true -Drat.skip=true -Denforcer.skip=true -Dfile.encoding=UTF-8

注意:nacos 打包时需要执行 maven install,而非通常的 maven package。

打包成功后,会在distribution/target目录下生成打包结果, 包含可以直接发行的压缩包*.tar.gz和*.zip。

也可以直接使用distribution/target/nacos-server-2.2.2。

本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: nacos
最后更新:2026年2月3日

魔芋红茶

加一点PHP,加一点Go,加一点Python......

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2021 icexmoon.cn. ALL RIGHTS RESERVED.
本网站由提供CDN加速/云存储服务

Theme Kratos Made By Seaton Jiang

宁ICP备2021001508号

宁公网安备64040202000141号