mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-08 20:49:00 +08:00
refactor(pigx-register): 简化Nacos启动配置并移除冗余代码
- 删除ConfigConstants.java文件,其配置已迁移至application.properties - 移除Spring Boot Admin相关配置,简化监控设置 - 重构PigxNacosApplication启动类,精简初始化逻辑 - 将standalone模式配置移至main方法直接设置 - 优化部署类型处理流程,保持核心功能不变
This commit is contained in:
1 parent
1b560e486b
commit
bcd0f82bcc
3 files changed
+64
-206
No files matched your search
+21
-105
@@ -1,137 +1,53 @@
|
||||
/*
|
||||
* Copyright 1999-2023 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* 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.bootstrap;
|
||||
|
||||
import com.alibaba.nacos.NacosServerBasicApplication;
|
||||
import com.alibaba.nacos.NacosServerWebApplication;
|
||||
import com.alibaba.nacos.bootstrap.config.ConfigConstants;
|
||||
import com.alibaba.nacos.console.NacosConsole;
|
||||
import com.alibaba.nacos.core.listener.startup.NacosStartUp;
|
||||
import com.alibaba.nacos.core.listener.startup.NacosStartUpManager;
|
||||
import com.alibaba.nacos.sys.env.Constants;
|
||||
import com.alibaba.nacos.sys.env.DeploymentType;
|
||||
import com.alibaba.nacos.sys.env.EnvUtil;
|
||||
import org.springframework.boot.Banner;
|
||||
import org.springframework.boot.ResourceBanner;
|
||||
import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.jmx.export.MBeanExporter;
|
||||
import org.springframework.jmx.support.RegistrationPolicy;
|
||||
|
||||
/**
|
||||
* Nacos启动类。
|
||||
* @author nacos
|
||||
* <p>
|
||||
* 针对于开发环境的启动类,主要是为了方便开发者开箱即用 PIGX ,而不需要考虑 nacos 的各版本兼容性问题。
|
||||
*
|
||||
* @author lengleng
|
||||
* @date 2025/04/30
|
||||
* nacos console 源码运行,方便开发 生产从官网下载zip最新版集群配置运行
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class PigxNacosApplication {
|
||||
|
||||
private static final String SPRING_JXM_ENABLED = "spring.jmx.enabled";
|
||||
/**
|
||||
* 独立模式系统属性名称
|
||||
*/
|
||||
private static String STANDALONE_MODE = "nacos.standalone";
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 修改这里的配置项,来指定Nacos Web Console的端口
|
||||
System.setProperty(ConfigConstants.NACOS_CONSOLE_PORT, "18080");
|
||||
initEnv();
|
||||
System.setProperty(STANDALONE_MODE, "true");
|
||||
|
||||
String type = System.getProperty(Constants.NACOS_DEPLOYMENT_TYPE, Constants.NACOS_DEPLOYMENT_TYPE_MERGED);
|
||||
DeploymentType deploymentType = DeploymentType.getType(type);
|
||||
EnvUtil.setDeploymentType(deploymentType);
|
||||
switch (deploymentType) {
|
||||
case MERGED:
|
||||
startWithConsole(args);
|
||||
break;
|
||||
case SERVER:
|
||||
startWithoutConsole(args);
|
||||
break;
|
||||
case CONSOLE:
|
||||
startOnlyConsole(args);
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Unsupported nacos deployment type " + type);
|
||||
}
|
||||
}
|
||||
|
||||
private static void prepareCoreContext(ConfigurableApplicationContext coreContext) {
|
||||
if (coreContext.getEnvironment().getProperty(SPRING_JXM_ENABLED, Boolean.class, false)) {
|
||||
// Avoid duplicate registration MBean to exporter.
|
||||
coreContext.getBean(MBeanExporter.class).setRegistrationPolicy(RegistrationPolicy.IGNORE_EXISTING);
|
||||
}
|
||||
}
|
||||
|
||||
private static void startWithoutConsole(String[] args) {
|
||||
ConfigurableApplicationContext coreContext = startCoreContext(args);
|
||||
prepareCoreContext(coreContext);
|
||||
ConfigurableApplicationContext webContext = startServerWebContext(args, coreContext);
|
||||
}
|
||||
|
||||
private static void startWithConsole(String[] args) {
|
||||
ConfigurableApplicationContext coreContext = startCoreContext(args);
|
||||
prepareCoreContext(coreContext);
|
||||
ConfigurableApplicationContext serverWebContext = startServerWebContext(args, coreContext);
|
||||
ConfigurableApplicationContext consoleContext = startConsoleContext(args, coreContext);
|
||||
}
|
||||
|
||||
private static ConfigurableApplicationContext startCoreContext(String[] args) {
|
||||
// Start Core Context
|
||||
NacosStartUpManager.start(NacosStartUp.CORE_START_UP_PHASE);
|
||||
return new SpringApplicationBuilder(NacosServerBasicApplication.class).web(WebApplicationType.NONE)
|
||||
.banner(getBanner("core-banner.txt")).run(args);
|
||||
}
|
||||
ConfigurableApplicationContext coreContext = new SpringApplicationBuilder(NacosServerBasicApplication.class)
|
||||
.web(WebApplicationType.NONE)
|
||||
.run(args);
|
||||
|
||||
private static ConfigurableApplicationContext startServerWebContext(String[] args,
|
||||
ConfigurableApplicationContext coreContext) {
|
||||
// Start Server Web Context
|
||||
NacosStartUpManager.start(NacosStartUp.WEB_START_UP_PHASE);
|
||||
return new SpringApplicationBuilder(NacosServerWebApplication.class).parent(coreContext)
|
||||
.banner(getBanner("nacos-server-web-banner.txt")).run(args);
|
||||
}
|
||||
ConfigurableApplicationContext serverWebContext = new SpringApplicationBuilder(NacosServerWebApplication.class)
|
||||
.parent(coreContext)
|
||||
.run(args);
|
||||
|
||||
private static ConfigurableApplicationContext startConsoleContext(String[] args,
|
||||
ConfigurableApplicationContext coreContext) {
|
||||
// Start Console Context
|
||||
NacosStartUpManager.start(NacosStartUp.CONSOLE_START_UP_PHASE);
|
||||
return new SpringApplicationBuilder(NacosConsole.class).parent(coreContext)
|
||||
.banner(getBanner("nacos-console-banner.txt")).run(args);
|
||||
ConfigurableApplicationContext consoleContext = new SpringApplicationBuilder(NacosConsole.class)
|
||||
.parent(coreContext)
|
||||
.run(args);
|
||||
}
|
||||
|
||||
private static void startOnlyConsole(String[] args) {
|
||||
NacosStartUpManager.start(NacosStartUp.CONSOLE_START_UP_PHASE);
|
||||
ConfigurableApplicationContext consoleContext = new SpringApplicationBuilder(NacosConsole.class).banner(
|
||||
getBanner("nacos-console-banner.txt")).run(args);
|
||||
}
|
||||
|
||||
private static Banner getBanner(String bannerFileName) {
|
||||
return new ResourceBanner(new ClassPathResource(bannerFileName));
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化运行环境方法。
|
||||
* <p>
|
||||
* 设置系统属性以配置运行环境,如独立模式、认证启用、日志基础目录和日志启用状态。
|
||||
*
|
||||
* @return 返回初始化是否成功,成功返回true。
|
||||
*/
|
||||
private static boolean initEnv() {
|
||||
System.setProperty(ConfigConstants.STANDALONE_MODE, "true");
|
||||
System.setProperty(ConfigConstants.AUTH_ENABLED, "true");
|
||||
System.setProperty(ConfigConstants.LOG_BASEDIR, "logs");
|
||||
System.setProperty(ConfigConstants.LOG_ENABLED, "false");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the pig4cloud.com developer nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.alibaba.nacos.bootstrap.config;
|
||||
|
||||
/**
|
||||
* 配置常量接口:用于覆盖Nacos默认配置
|
||||
*
|
||||
* @author lengleng
|
||||
* @date 2025/05/06
|
||||
*/
|
||||
public interface ConfigConstants {
|
||||
|
||||
/**
|
||||
* 独立模式系统属性名称
|
||||
*/
|
||||
String STANDALONE_MODE = "nacos.standalone";
|
||||
|
||||
/**
|
||||
* Nacos控制台端口号配置键
|
||||
*/
|
||||
String NACOS_CONSOLE_PORT = "nacos.console.port";
|
||||
|
||||
/**
|
||||
* 是否开启认证
|
||||
*/
|
||||
String AUTH_ENABLED = "nacos.core.auth.enabled";
|
||||
|
||||
/**
|
||||
* 日志目录
|
||||
*/
|
||||
String LOG_BASEDIR = "server.tomcat.basedir";
|
||||
|
||||
/**
|
||||
* access_log日志开关
|
||||
*/
|
||||
String LOG_ENABLED = "server.tomcat.accesslog.enabled";
|
||||
}
|
||||
@@ -1,90 +1,84 @@
|
||||
spring.application.name=@project.artifactId@
|
||||
# Nacos 控制台端口,修改请在 PigxNacosApplication.java 启动类中才有效
|
||||
# Nacos \u63A7\u5236\u53F0\u7AEF\u53E3
|
||||
nacos.console.port=18080
|
||||
# Nacos 服务端主端口
|
||||
# Nacos \u670D\u52A1\u7AEF\u4E3B\u7AEF\u53E3
|
||||
nacos.server.main.port=8848
|
||||
# 数据库设置
|
||||
# \u6570\u636E\u5E93\u8BBE\u7F6E
|
||||
spring.sql.init.platform=mysql
|
||||
db.num=1
|
||||
db.url.0=jdbc:mysql://${MYSQL_HOST:pigx-mysql}:${MYSQL_PORT:3306}/${MYSQL_DB:pigxx_config}?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true
|
||||
db.user=root
|
||||
db.password=root
|
||||
# nacos 会自动装配数据源,所以排除 spring.datasource 自动配置
|
||||
# nacos \u4F1A\u81EA\u52A8\u88C5\u914D\u6570\u636E\u6E90\uFF0C\u6240\u4EE5\u6392\u9664 spring.datasource \u81EA\u52A8\u914D\u7F6E
|
||||
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
|
||||
# 配置 spring boot admin 客户端
|
||||
spring.boot.admin.client.url=http://pigx-monitor:5001
|
||||
spring.boot.admin.client.username=pig
|
||||
spring.boot.admin.client.password=pig
|
||||
spring.boot.admin.client.instance.service-host-type=ip
|
||||
#*************** 监控相关配置 ***************#
|
||||
# Prometheus 监控配置
|
||||
#*************** \u76D1\u63A7\u76F8\u5173\u914D\u7F6E ***************#
|
||||
# Prometheus \u76D1\u63A7\u914D\u7F6E
|
||||
#management.endpoints.web.exposure.include=prometheus
|
||||
# ElasticSearch 监控配置 - 已禁用
|
||||
# ElasticSearch \u76D1\u63A7\u914D\u7F6E - \u5DF2\u7981\u7528
|
||||
management.elastic.metrics.export.enabled=false
|
||||
# InfluxDB 监控配置 - 已禁用
|
||||
# InfluxDB \u76D1\u63A7\u914D\u7F6E - \u5DF2\u7981\u7528
|
||||
management.influx.metrics.export.enabled=false
|
||||
#*************** 配置中心相关 ***************#
|
||||
# Nacos 配置推送最大重试次数
|
||||
#*************** \u914D\u7F6E\u4E2D\u5FC3\u76F8\u5173 ***************#
|
||||
# Nacos \u914D\u7F6E\u63A8\u9001\u6700\u5927\u91CD\u8BD5\u6B21\u6570
|
||||
nacos.config.push.maxRetryTime=50
|
||||
#*************** 命名服务相关 ***************#
|
||||
# 空服务自动清理
|
||||
#*************** \u547D\u540D\u670D\u52A1\u76F8\u5173 ***************#
|
||||
# \u7A7A\u670D\u52A1\u81EA\u52A8\u6E05\u7406
|
||||
nacos.naming.empty-service.auto-clean=true
|
||||
nacos.naming.empty-service.clean.initial-delay-ms=50000
|
||||
nacos.naming.empty-service.clean.period-time-ms=30000
|
||||
#*************** Nacos Web 相关配置 ***************#
|
||||
# Nacos 服务端上下文路径
|
||||
#*************** Nacos Web \u76F8\u5173\u914D\u7F6E ***************#
|
||||
# Nacos \u670D\u52A1\u7AEF\u4E0A\u4E0B\u6587\u8DEF\u5F84
|
||||
nacos.server.contextPath=/nacos
|
||||
#*************** 日志相关配置 ***************#
|
||||
# 开启访问日志
|
||||
#*************** \u65E5\u5FD7\u76F8\u5173\u914D\u7F6E ***************#
|
||||
# \u5F00\u542F\u8BBF\u95EE\u65E5\u5FD7
|
||||
server.tomcat.accesslog.enabled=true
|
||||
# 访问日志保留天数配置
|
||||
# \u8BBF\u95EE\u65E5\u5FD7\u4FDD\u7559\u5929\u6570\u914D\u7F6E
|
||||
server.tomcat.accesslog.max-days=30
|
||||
# 访问日志格式
|
||||
# \u8BBF\u95EE\u65E5\u5FD7\u683C\u5F0F
|
||||
server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D %{User-Agent}i %{Request-Source}i
|
||||
# 日志基础目录
|
||||
# \u65E5\u5FD7\u57FA\u7840\u76EE\u5F55
|
||||
server.tomcat.basedir=file:.
|
||||
#*************** API 错误处理 ***************#
|
||||
# 错误信息显示设置
|
||||
#*************** API \u9519\u8BEF\u5904\u7406 ***************#
|
||||
# \u9519\u8BEF\u4FE1\u606F\u663E\u793A\u8BBE\u7F6E
|
||||
server.error.include-message=ALWAYS
|
||||
#*************** Nacos 控制台配置 ***************#
|
||||
# Nacos 控制台上下文路径
|
||||
#*************** Nacos \u63A7\u5236\u53F0\u914D\u7F6E ***************#
|
||||
# Nacos \u63A7\u5236\u53F0\u4E0A\u4E0B\u6587\u8DEF\u5F84
|
||||
nacos.console.contextPath=
|
||||
# Nacos 控制台远程服务上下文路径
|
||||
# Nacos \u63A7\u5236\u53F0\u8FDC\u7A0B\u670D\u52A1\u4E0A\u4E0B\u6587\u8DEF\u5F84
|
||||
nacos.console.remote.server.context-path=/nacos
|
||||
#*************** 安全相关配置 ***************#
|
||||
# 安全忽略 URL
|
||||
#*************** \u5B89\u5168\u76F8\u5173\u914D\u7F6E ***************#
|
||||
# \u5B89\u5168\u5FFD\u7565 URL
|
||||
nacos.security.ignore.urls=/,/error,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-ui/public/**,/v1/auth/**,/v1/console/health/**,/actuator/**,/v1/console/server/**
|
||||
# 认证系统类型
|
||||
# \u8BA4\u8BC1\u7CFB\u7EDF\u7C7B\u578B
|
||||
nacos.core.auth.system.type=nacos
|
||||
# 开启认证
|
||||
# \u5F00\u542F\u8BA4\u8BC1
|
||||
nacos.core.auth.enabled=false
|
||||
# 启用 API 访问权限
|
||||
# \u542F\u7528 API \u8BBF\u95EE\u6743\u9650
|
||||
nacos.core.auth.admin.enabled=true
|
||||
# 启用控制台 API 认证
|
||||
# \u542F\u7528\u63A7\u5236\u53F0 API \u8BA4\u8BC1
|
||||
nacos.core.auth.console.enabled=true
|
||||
# 开启认证缓存
|
||||
# \u5F00\u542F\u8BA4\u8BC1\u7F13\u5B58
|
||||
nacos.core.auth.caching.enabled=true
|
||||
# 服务器身份认证 (当 nacos.core.auth.enabled=true 时生效)
|
||||
# \u670D\u52A1\u5668\u8EAB\u4EFD\u8BA4\u8BC1 (\u5F53 nacos.core.auth.enabled=true \u65F6\u751F\u6548)
|
||||
nacos.core.auth.server.identity.key=VGhpc0lzTXlDdXN0b21TZWNyZXRLZXkwMTIzNDU2Nzg=
|
||||
nacos.core.auth.server.identity.value=VGhpc0lzTXlDdXN0b21TZWNyZXRLZXkwMTIzNDU2Nzg=
|
||||
# Nacos 令牌相关配置
|
||||
# Nacos \u4EE4\u724C\u76F8\u5173\u914D\u7F6E
|
||||
nacos.core.auth.plugin.nacos.token.cache.enable=false
|
||||
nacos.core.auth.plugin.nacos.token.expire.seconds=18000
|
||||
# 认证密钥 (Base64 编码)
|
||||
# \u8BA4\u8BC1\u5BC6\u94A5 (Base64 \u7F16\u7801)
|
||||
nacos.core.auth.plugin.nacos.token.secret.key=VGhpc0lzTXlDdXN0b21TZWNyZXRLZXkwMTIzNDU2Nzg=
|
||||
#*************** Istio 服务网格配置 ***************#
|
||||
# MCP 服务支持 - 已禁用
|
||||
#*************** Istio \u670D\u52A1\u7F51\u683C\u914D\u7F6E ***************#
|
||||
# MCP \u670D\u52A1\u652F\u6301 - \u5DF2\u7981\u7528
|
||||
nacos.istio.mcp.server.enabled=false
|
||||
#*************** K8s 相关配置 ***************#
|
||||
# K8s 同步支持 - 已禁用
|
||||
#*************** K8s \u76F8\u5173\u914D\u7F6E ***************#
|
||||
# K8s \u540C\u6B65\u652F\u6301 - \u5DF2\u7981\u7528
|
||||
nacos.k8s.sync.enabled=false
|
||||
#*************** 部署模式配置 ***************#
|
||||
# 部署模式: 'merged' 混合模式, 'server' 服务端模式, 'console' 控制台模式
|
||||
#*************** \u90E8\u7F72\u6A21\u5F0F\u914D\u7F6E ***************#
|
||||
# \u90E8\u7F72\u6A21\u5F0F: 'merged' \u6DF7\u5408\u6A21\u5F0F, 'server' \u670D\u52A1\u7AEF\u6A21\u5F0F, 'console' \u63A7\u5236\u53F0\u6A21\u5F0F
|
||||
nacos.deployment.type=merged
|
||||
#*************** 模糊匹配配置 ***************#
|
||||
# 配置中心模糊匹配最大值
|
||||
#*************** \u6A21\u7CCA\u5339\u914D\u914D\u7F6E ***************#
|
||||
# \u914D\u7F6E\u4E2D\u5FC3\u6A21\u7CCA\u5339\u914D\u6700\u5927\u503C
|
||||
nacos.config.fuzzy.watch.max.pattern.count=20
|
||||
nacos.config.fuzzy.watch.max.pattern.match.config.count=500
|
||||
# 服务发现模糊匹配最大值
|
||||
# \u670D\u52A1\u53D1\u73B0\u6A21\u7CCA\u5339\u914D\u6700\u5927\u503C
|
||||
nacos.naming.fuzzy.watch.max.pattern.count=20
|
||||
nacos.naming.fuzzy.watch.max.pattern.match.service.count=500
|
||||
Reference in new issue
Block a user