一个Window的服务器老是重启,每次服务器重启都得登录服务器重启jar很麻烦,所以百度一下弄了一个重启配置,这里记录一下
下面是简单的springboot项目
application.yml
yaml
server:
port: 8888
name: ceshi
spring:
profiles:
active: dev
application-dev.yml
yaml
ceshi:
name: ceshi-dev
application-prod.yml
yaml
ceshi:
name: ceshi-prod
controller
java
@RestController
@RequestMapping("/ceshi")
public class CeshiController {
@Value("${ceshi.name}")
private String ceshi;
@GetMapping("/test")
public String test() {
boolean b = RedisConnectionTest.testRedisConnection("127.0.01", 6379);
return ceshi + " 6379:" + b;
}
}
java
import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisConnectionException;
public class RedisConnectionTest {
public static boolean testRedisConnection(String host, int port) {
try (Jedis jedis = new Jedis(host, port)) {
// 测试连接
String result = jedis.ping();
return "PONG".equals(result);
} catch (JedisConnectionException e) {
System.err.println("Redis连接失败: " + e.getMessage());
return false;
} catch (Exception e) {
System.err.println("测试Redis时发生错误: " + e.getMessage());
return false;
}
}
public static boolean testRedisConnectionWithAuth(String host, int port, String password) {
try (Jedis jedis = new Jedis(host, port)) {
if (password != null && !password.isEmpty()) {
jedis.auth(password);
}
return "PONG".equals(jedis.ping());
} catch (Exception e) {
System.err.println("Redis连接失败: " + e.getMessage());
return false;
}
}
public static void main(String[] args) {
String host = "localhost";
int port = 6379;
boolean isConnected = testRedisConnection(host, port);
if (isConnected) {
System.out.println("✅ Redis服务运行正常");
} else {
System.out.println("❌ Redis服务未启动或连接失败");
}
}
}
只引入了redis,来测试redis是否启动成功
xml
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.4.3</version>
</dependency>
这是我虚拟机的目录,以这个举例,jar包就是上边简单的springboot打包之后的名字

下面是startup.bat的内容(把文件路径改一改)
bash
@echo off
chcp 65001 > nul
title Application Stack Manager
echo ========================================
echo Starting Services...
echo ========================================
:: 函数:启动服务
call :start_redis
call :start_nginx
call :start_java
echo.
echo ========================================
echo All services started successfully!
echo ========================================
echo Redis: Running on port 6379
echo Nginx: Running on port 8080
echo Java: Application started
echo ========================================
pause
exit /b
:start_redis
echo [1/3] Starting Redis...
if exist "C:\test\Redis-x64-5.0.14.1\redis-server.exe" (
start "Redis Service" /min "C:\test\Redis-x64-5.0.14.1\redis-server.exe" "C:\test\Redis-x64-5.0.14.1\redis.windows.conf"
timeout /t 3 /nobreak > nul
echo ✓ Redis started successfully
) else (
echo ✗ Redis not found
)
exit /b
:start_nginx
echo [2/3] Starting Nginx...
if exist "C:\test\nginx-1.24.0\nginx.exe" (
:: 停止现有Nginx
taskkill /f /im nginx.exe > nul 2>&1
timeout /t 2 /nobreak > nul
:: 在Nginx目录中后台启动
pushd "C:\test\nginx-1.24.0"
start "Nginx Service" /B nginx.exe
popd
:: 等待并验证
timeout /t 3 /nobreak > nul
tasklist /fi "imagename eq nginx.exe" | find /i "nginx.exe" > nul
if errorlevel 1 (
echo ✗ Nginx failed to start
) else (
echo ✓ Nginx started successfully
)
) else (
echo ✗ Nginx not found
)
exit /b
:start_java
echo [3/3] Starting Java Application...
cd /d "C:\test"
if exist "java-component.jar" (
echo ✓ Starting Java application...
java -jar java-component.jar --spring.profiles.active=prod
) else (
echo ✗ java-component.jar not found
)
exit /b
下面是配置Window配置步骤:
1、win+r打开,输入taskschd.msc,打开任务计划程序

2、点击操作,创建任务或者创建基本任务

3、名称:MyJavaAppAutoStart(随便起名)

4、触发器:"计算机启动时"

5、操作:"启动程序"

6、程序或脚本:C:\test\startup.bat

7、完成创建

如果没有达到效果就找到你的任务,配置一下不管用户是否登录都要运行,给它个最高权限,之后重启一下虚拟机试试


重启之后应该直接访问接口就能访问到东西
