一开始是想部署到tomcat,发现gateway不能用tomcat部署,spring cloud gateway是基于netty的webflux响应式框架,必须要使用内置的tomcat,无法部署到外置的tomcat..
前端暂时还是放到tomcat..
@Component
public class NacosConfig implements ApplicationRunner {
@Autowired(required = false)
private NacosAutoServiceRegistration registration;
@Value("${server.port}")
Integer port;
@Override
public void run(ApplicationArguments args) {
if (registration != null && port != null) {
Integer tomcatPort = port;
try {
tomcatPort = new Integer(getTomcatPort());
} catch (Exception e) {
e.printStackTrace();
}
registration.setPort(tomcatPort);
registration.start();
}
}
/**
* 获取外部tomcat端口
*/
public String getTomcatPort() throws Exception {
MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
Set objectNames = beanServer.queryNames(new ObjectName("*:type=Connector,*"), Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
String port = objectNames.iterator().next().getKeyProperty("port");
return port;
}
}
打包,直接启动,将前端包打好放到tomcat,浏览器访问正常。
特别注意: nacos控制台--配置列表中的配置内容不要出现中文,不要出现中文,不要出现中文
@echo 1、aaa--------------------
start java -jar D:\nacos-server\aaa.jar
@echo 2、bbb--------------------
start java -jar D:\nacos-server\bbb.jar
@echo 3、gateway--------------------
start java -jar D:\nacos-server\gateway-service.jar
但是日志却打不出来(后面再来解决)
@echo off
start javaw -jar -Dfile.encoding=utf-8 -Xms500m -Xmx512m D:\nacos-server\aaa.jar > D:\nacos-server\aaa-%date:~0,4%%date:~5,2%%date:~8,2%.log 2>&1 &
start javaw -jar -Dfile.encoding=utf-8 -Xms500m -Xmx512m D:\nacos-server\bbb.jar > D:\nacos-server\bbb-%date:~0,4%%date:~5,2%%date:~8,2%.log 2>&1 &
start javaw -jar -Dfile.encoding=utf-8 -Xms500m -Xmx512m D:\nacos-server\gateway.jar > D:\nacos-server\gateway-%date:~0,4%%date:~5,2%%date:~8,2%.log 2>&1 &
exit
## 记录下..
timeout /t 2 2秒之后自动关闭
pause 按任意键退出
exit 自动退出
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp
netstat -aon|findstr 8081 # 根据端口号查看pid
taskkill /f /pid 7848 # 强制杀死进程
关闭多个,写一个shutdown.bat
参考博客 https://developer.aliyun.com/article/887451
@echo off
set port=8081 8082 8083
for /f "tokens=1-5" %%a in ('netstat -ano^|findstr ":%port%"') do (
echo PID:%%e is running,kill the process [%%e] who use the port [%%b]
taskkill /F /pid %%e
)
pause
spring.datasource.platform=mysql;
db.num=1;
db-url、user、password
create database IF NOT EXISTS `db_nacos` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
USE `db_nacos`;
mysql - u root -p
输入mysql密码
source d:\nacos-mysql.sql;
参考博客 https://blog.csdn.net/GBK_8/article/details/123631533
设置windows开机自启需要使用到WinSW.NET4.exe工具
## 最近git总是访问不上,在阿里云服务器下载
wget https://github.com/winsw/winsw/releases/download/v3.0.0-alpha.10/WinSW-x64.exe
将WinSW-x64.exe重命名为nacos-service.exe,放到D:\nacos-1.2.0\nacos\bin目录下,并新建一个nacos-service.xml
nacos
Nacos Service
Nacos服务
D:\nacos-1.2.0\nacos\logs\
roll
D:\nacos-1.2.0\nacos\bin\startup.cmd
D:\nacos-1.2.0\nacos\bin\shutdown.cmd
输入nacos-service.exe install即可 出现下图及说明成功,卸载命令 :nacos-service.exe uninstall
重启电脑,java服务正常运行,nacos自动启动,java服务注册到nacos,浏览器访问正常。