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

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


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

相关推荐
川川菜鸟11 小时前
Claude Code 安装与配置完整指南(Windows)
windows
xxxmine12 小时前
redis学习
数据库·redis·学习
qq_54702617912 小时前
Redis 常见问题
数据库·redis·mybatis
PfCoder13 小时前
WinForm真入门(23)---PictureBox 控件详细用法
开发语言·windows·c#·winform
知识即是力量ol13 小时前
基于 Redis 实现白名单,黑名单机制详解及应用场景
数据库·redis·缓存
CoLiuRs13 小时前
语义搜索系统原理与实现
redis·python·向量·es
Mr_Xuhhh13 小时前
MySQL表的增删改查(CRUD)操作详解
数据库·windows
老姚---老姚13 小时前
在windows下编译go语言编写的dll库
开发语言·windows·golang
love530love14 小时前
技术复盘:llama-cpp-python CUDA 编译实战 (Windows)
人工智能·windows·python·llama·aitechlab·cpp-python·cuda版本
fengxin_rou15 小时前
Redis 从零到精通:第一篇 初识redis
数据库·redis·缓存