伪前后端分离
图
随着Vue等前端框架的兴起,前后端分离也成为了开发的趋势,开发的便捷和高效是无法拒绝的优势,而前端的部署离不开Nginx的参与,而在SpringMVC的项目中,如果不想部署Nginx可以使用伪前后端分离

背景

正常情况下前后端分离后都是分开部署的,前端使用Nginx作为服务器请求资源,如果在有些情况下不想安装Nginx或者减少部署复杂度,会将前端项目的静态资源放入后端SpringBoot的姿态资源下resources/static,但是前端的更改部署会重新打Jar包,并不能将前后端的部署分离开来

解决方案:将相关静态资源的请求转发到Jar包所在目录下,实现前端项目独立运行环境,把使用Tomcat来转发静态资源

图

配置静态位置

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

/**
 * 静态资源相关配置
 */
@Slf4j
@Configuration
public class WebConfig implements WebMvcConfigurer {

    /**
     * 配置静态资源位置
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String homeDir = getBinPath();
        String frontRoot = homeDir + "/dist";
        log.info("前端资源目录:{}", frontRoot);
        String frontLocation = "file:" + frontRoot;
        registry.addResourceHandler("/index.html").addResourceLocations(frontLocation + "/index.html");
        registry.addResourceHandler("/favicon.ico").addResourceLocations(frontLocation + "/favicon.ico");
        registry.addResourceHandler("/static/**").addResourceLocations(frontLocation + "/static/");
        registry.addResourceHandler("/assets/**").addResourceLocations(frontLocation + "/assets/");
    }

    /**
     * 获取程序执行目录,即Jar包所在的目录(开发环境为项目所在目录)
     */
    public static String getBinPath() {
        ApplicationHome applicationHome = new ApplicationHome(WebConfig.class);
        File file = applicationHome.getSource();
        if (file == null) {
            return System.getProperty("user.dir");
        }
        return file.getParentFile().toString().replace("\\target", "");
    }

}

Maven指定打包名称

<finalName>name</finalName>

示例

<build>
    <finalName>convert-def</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

win启动脚本

startup.bat

@echo off

set app_name=springboot

java -Duser.timezone=Asia/Shanghai -jar -Xms512m -Xmx512m %app_name%.jar

linux脚本

关闭

shutdown.sh

#!/bin/sh

# 执行文件名称
app_name="springboot"

pid=$(ps -ef | grep $app_name.jar | grep -v grep | awk '{print $2}')
if [ -n "$pid" ]; then
  echo "stop $app_name.jar, pid:$pid"
  kill -9 "$pid"
fi

启动

startup.sh

@echo off

set app_name=springboot

java -Duser.timezone=Asia/Shanghai -jar -Xms512m -Xmx512m %app_name%.jar

后端接口命名问题

如果前端路由没有使用Hash模式,地址最好不要和后端接口地址重复,在页面刷新时响应的数据是接口数据,没有页面

同时想做到前端不配置固定请求地址,就使用相对路径,不要写绝对请求地址

SpringBoot转发/index.html

/请求转发到/index.html,地址栏不变

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 将首页的访问请求转发到静态页面
 */
@Controller
public class IndexController {

    @RequestMapping("/")
    public void indexRedirect(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/index.html");
        requestDispatcher.forward(request,response);
    }

}

新的静态文件配置调整

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

/**
 * 静态资源相关配置
 */
@Slf4j
@Configuration
public class WebConfig implements WebMvcConfigurer {

    /**
     * 配置静态资源位置
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String homeDir = getBinPath();
        String frontRoot = homeDir + "/dist";
        log.info("前端资源目录:{}", frontRoot);
        String frontLocation = "file:" + frontRoot;
        registry.addResourceHandler("/index.html").addResourceLocations(frontLocation);
        registry.addResourceHandler("/favicon.ico").addResourceLocations(frontLocation);
        registry.addResourceHandler("/static/**").addResourceLocations(frontLocation + "/static/");
        registry.addResourceHandler("/assets/**").addResourceLocations(frontLocation + "/assets/");
    }

    /**
     * 获取程序执行目录,即Jar包所在的目录(开发环境为项目所在目录)
     */
    public static String getBinPath() {
        ApplicationHome applicationHome = new ApplicationHome(WebConfig.class);
        File file = applicationHome.getSource();
        if (file == null) {
            return System.getProperty("user.dir");
        }
        return file.getParentFile().toString().replace("\\target", "");
    }

}

部署后非首页刷新没页面问题

import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;

/**
 * 将首页的访问请求转发到静态页面
 */
@Controller
public class IndexController {

    @RequestMapping({"/", "/article/*", "/login", "/category/*", "/manage/**"})
    public void indexRedirect(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/index.html");
        requestDispatcher.forward(request, response);
    }

}