• nacos server 源码运行实现


    下载 nacos 源码

    1、只需保留 nacos console 模块,其他模块均可删除

    2、console 源码结构说明

    1. ├── pom.xml
    2. └── src
    3. ├── main
    4. │ ├── java
    5. │ │ └── com
    6. │ │ └── alibaba
    7. │ │ └── nacos
    8. │ │ ├── Nacos.java # main 启动类
    9. │ │ └── console # 控制台相关源码
    10. │ └── resources
    11. │ ├── application.properties # nacos 配置文件
    12. │ └── static # 静态页面目录
    13. └── test # 单元测试部分

    3、修改application文件

    1. ### Default web context path:
    2. server.servlet.contextPath=/nacos
    3. ### If use MySQL as datasource:
    4. spring.main.allow-circular-references=true
    5. nacos.naming.empty-service.auto-clean=true
    6. nacos.naming.empty-service.clean.initial-delay-ms=50000
    7. nacos.naming.empty-service.clean.period-time-ms=30000
    8. management.metrics.export.elastic.enabled=false
    9. management.metrics.export.influx.enabled=false
    10. spring.datasource.platform=mysql
    11. db.num=1
    12. db.url.0=jdbc:mysql://ip:3306/ry-config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
    13. db.user=root
    14. db.password=root
    15. #*************** Access Control Related Configurations ***************#
    16. ### The ignore urls of auth, is deprecated in 1.2.0:
    17. nacos.security.ignore.urls=/,/error,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-ui/public/**,/v1/auth/**,/v1/console/health/**,/actuator/**,/v1/console/server/**
    18. ### The auth system to use, currently only 'nacos' and 'ldap' is supported:
    19. nacos.core.auth.system.type=nacos
    20. ### If turn on auth system:
    21. nacos.core.auth.enabled=false
    22. ### Turn on/off caching of auth information. By turning on this switch, the update of auth information would have a 15 seconds delay.
    23. nacos.core.auth.caching.enabled=true
    24. ### Since 1.4.1, Turn on/off white auth for user-agent: nacos-server, only for upgrade from old version.
    25. nacos.core.auth.enable.userAgentAuthWhite=false
    26. nacos.core.auth.server.identity.key=serverIdentity
    27. nacos.core.auth.server.identity.value=security
    28. nacos.core.auth.plugin.nacos.token.expire.seconds=18000
    29. nacos.core.auth.plugin.nacos.token.secret.key=SecretKey012345678901234567890123456789012345678901234567890123456789
    30. #*************** Istio Related Configurations ***************#
    31. ### If turn on the MCP server:
    32. nacos.istio.mcp.server.enabled=false

    4、修改 Nacos.java 类

    添加类配置类:ConfigConstants

    1. package com.alibaba.nacos.console.config;
    2. public interface ConfigConstants {
    3. /**
    4. * The System property name of Standalone mode
    5. */
    6. String STANDALONE_MODE = "nacos.standalone";
    7. /**
    8. * 是否开启认证
    9. */
    10. String AUTH_ENABLED = "nacos.core.auth.enabled";
    11. /**
    12. * 日志目录
    13. */
    14. String LOG_BASEDIR = "server.tomcat.basedir";
    15. /**
    16. * access_log日志开关
    17. */
    18. String LOG_ENABLED = "server.tomcat.accesslog.enabled";
    19. }

    主要在 main 方法中增加 两个参数,是否是单机启动 & 是否关闭权限校验

    1. @SpringBootApplication(scanBasePackages = "com.alibaba.nacos")
    2. @ServletComponentScan
    3. @EnableScheduling
    4. public class Nacos {
    5. public static void main(String[] args) {
    6. if (initEnv()) {
    7. SpringApplication.run(Nacos.class, args);
    8. }
    9. }
    10. /**
    11. * 初始化运行环境
    12. */
    13. private static boolean initEnv() {
    14. System.setProperty(ConfigConstants.STANDALONE_MODE, "true");
    15. System.setProperty(ConfigConstants.AUTH_ENABLED, "false");
    16. System.setProperty(ConfigConstants.LOG_BASEDIR, "logs");
    17. System.setProperty(ConfigConstants.LOG_ENABLED, "false");
    18. return true;
    19. }
    20. }

    5、修改 console/pom.xml

    • 由于不在使用 nacos bom 管理,需要给所有依赖坐标增加版本号
    • 由于 nacos-config /nacos-naming 等包没有上传至中央参考 无法下载到,groupId 变更为 com.pig4cloud.nacos 即可下载
    • 变更后参考如下
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <modelVersion>4.0.0</modelVersion>
    6. <parent>
    7. <groupId>com.ruoyi</groupId>
    8. <artifactId>ruoyi</artifactId>
    9. <version>3.5.0</version>
    10. </parent>
    11. <artifactId>nacos-console</artifactId>
    12. <packaging>jar</packaging>
    13. <properties>
    14. <nacos.version>2.1.0</nacos.version>
    15. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    16. </properties>
    17. <dependencies>
    18. <dependency>
    19. <groupId>io.springboot.nacos</groupId>
    20. <artifactId>nacos-config</artifactId>
    21. <version>${nacos.version}</version>
    22. </dependency>
    23. <dependency>
    24. <groupId>org.apache.tomcat.embed</groupId>
    25. <artifactId>tomcat-embed-jasper</artifactId>
    26. </dependency>
    27. <dependency>
    28. <groupId>io.springboot.nacos</groupId>
    29. <artifactId>nacos-naming</artifactId>
    30. <version>${nacos.version}</version>
    31. </dependency>
    32. <dependency>
    33. <groupId>io.springboot.nacos</groupId>
    34. <artifactId>nacos-plugin-default-impl</artifactId>
    35. <version>${nacos.version}</version>
    36. </dependency>
    37. <dependency>
    38. <groupId>io.springboot.nacos</groupId>
    39. <artifactId>nacos-istio</artifactId>
    40. <version>${nacos.version}</version>
    41. </dependency>
    42. <!-- log -->
    43. <!-- log4j通过slf4j来代理 -->
    44. <dependency>
    45. <groupId>org.slf4j</groupId>
    46. <artifactId>log4j-over-slf4j</artifactId>
    47. </dependency>
    48. <!-- apache commons logging通过slf4j来代理 -->
    49. <dependency>
    50. <groupId>org.slf4j</groupId>
    51. <artifactId>jcl-over-slf4j</artifactId>
    52. </dependency>
    53. <!-- java.util.logging 通过slf4j来代理 -->
    54. <dependency>
    55. <groupId>org.slf4j</groupId>
    56. <artifactId>jul-to-slf4j</artifactId>
    57. </dependency>
    58. <dependency>
    59. <groupId>org.springframework.boot</groupId>
    60. <artifactId>spring-boot-starter-security</artifactId>
    61. </dependency>
    62. </dependencies>
    63. <build>
    64. <plugins>
    65. <plugin>
    66. <groupId>org.springframework.boot</groupId>
    67. <artifactId>spring-boot-maven-plugin</artifactId>
    68. </plugin>
    69. </plugins>
    70. <resources>
    71. <resource>
    72. <directory>src/main/resources</directory>
    73. <filtering>true</filtering>
    74. <excludes>
    75. <exclude>**/*.woff</exclude>
    76. <exclude>**/*.woff2</exclude>
    77. <exclude>**/*.ttf</exclude>
    78. </excludes>
    79. </resource>
    80. <resource>
    81. <directory>src/main/resources</directory>
    82. <filtering>false</filtering>
    83. <includes>
    84. <include>**/*.woff</include>
    85. <include>**/*.woff2</include>
    86. <include>**/*.ttf</include>
    87. </includes>
    88. </resource>
    89. </resources>
    90. </build>
    91. </project>

    总结

    • 以上修改后源码参考: https://gitee.com/log4j/pig
    • 是否以源码形式运行,此问题仁者见仁智者见智 根据你们实际情况来
  • 相关阅读:
    云vscode搭建--使用容器化部署
    在Linux中进行GO语言安装
    【zabbix】shell脚本拉取zabbix监控图形
    自由职业者的福音来啦!人工智能带你智能规划,做你所擅长的事情
    重磅!法大大上榜“专精特新”企业
    vue引用拼音组件
    C# 语法分析器(二)LR(0) 语法分析
    uniapp对接支付宝出现的问题
    杰理之内置 FM 没有声音【篇】
    部署前后端为独立的 Docker 节点
  • 原文地址:https://blog.csdn.net/bbj12345678/article/details/125398560