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

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


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

相关推荐
陈老师还在写代码4 小时前
springboot 打包出来的 jar 包的名字是在哪儿决定的
spring boot·后端·jar
非凡ghost5 小时前
批量转双层PDF(可识别各种语言) 中文绿色版
前端·windows·pdf·计算机外设·软件需求
m0_748240258 小时前
Windows编程+使用C++编写EXE加壳程序
开发语言·c++·windows
ttghgfhhjxkl11 小时前
Windows 系统下 RabbitMQ 延迟插件的安装步骤与常见问题修复
windows·rabbitmq·ruby
非凡ghost13 小时前
Adobe Lightroom安卓版(手机调色软件)绿色版
前端·windows·adobe·智能手机·软件需求
准时准点睡觉14 小时前
window安装MYSQL5.5出错:a windows service with the name MYSQL alreadyexists....
数据库·windows·mysql
喆星时瑜16 小时前
Windows图标修复--缓存重建教程
windows·缓存
码界奇点19 小时前
Java 开发日记MySQL 与 Redis 双写一致性策略挑战与实战解析
java·redis·sql·mysql·java-ee
DemonAvenger20 小时前
Redis分布式锁:实现原理深度解析与实战案例分析
数据库·redis·性能优化