Windows配置jar、redis、nginx开机自启

一个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、完成创建

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


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

相关推荐
雨中飘荡的记忆20 小时前
大流量下库存扣减的数据库瓶颈:Redis分片缓存解决方案
java·redis·后端
阿白的白日梦1 天前
winget基础管理---更新/修改源为国内源
windows
曲幽1 天前
FastAPI分布式系统实战:拆解分布式系统中常见问题及解决方案
redis·python·fastapi·web·httpx·lock·asyncio
埃博拉酱5 天前
VS Code Remote SSH 连接 Windows 服务器卡在"下载 VS Code 服务器":prcdn DNS 解析失败的诊断与 BITS 断点续传
windows·ssh·visual studio code
唐宋元明清21886 天前
.NET 本地Db数据库-技术方案选型
windows·c#
加号36 天前
windows系统下mysql多源数据库同步部署
数据库·windows·mysql
tryCbest6 天前
Windows环境下配置pip镜像源
windows·pip
呉師傅6 天前
火狐浏览器报错配置文件缺失如何解决#操作技巧#
运维·网络·windows·电脑
百事牛科技6 天前
保护文档安全:PDF限制功能详解与实操
windows·pdf
一个人旅程~6 天前
如何用命令行把win10/win11设置为长期暂停更新?
linux·windows·经验分享·电脑