一、后端部署(tomcat)
1、修改打包方式为war包
<packaging>war</packaging>
2、移除tomcat依赖或者将tomcat依赖scope改为provide
移除Tomcat依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 移除嵌入式tomcat插件 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
将scope改为provide
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
3、继承SpringBootServletInitializer
正常springboot项目在创建的时候是会生成启动类的,这个类启动的是内置的tomcat容器。
但是我们是用的外部的容器,这个时候我们发现有一个问题就是,我们配置的时候好像少了一个web.xml文件,这个是在spring+springmvc时代的配置文件,但是在springboot中是没有这个文件的,但是功能上来说我们不能丢弃这,所以才需要继承SpringBootServletInitializer这个类来替代web.xml的功能。
启动类代码:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
方式一:启动类继承
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
}
方式二:新增类
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
//此处的Application.class为带有@SpringBootApplication注解的启动类
return builder.sources(Application.class);
}
}
4、注意事项
使用外部Tomcat部署访问的时候,application.yml中配置的
server.port=
server.servlet.context-path=
将失效,请使用tomcat的端口。
建议pom.xml文件中<build></build>标签下添加<finalName></finalName>标签:
<build>
<finalName>war包名称</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
5、配置Tomcat
打开conf文件夹下的server.xml,在其HOST节点下添加
<Context docBase="此处填写war包名称" path="/" reloadable="true" source=""/>
端口号配置:
<Connect port"端口号" protocol="HTTP/1.1" connectionTimeout"8443" />
配置完成后重启tomcat
二、前端部署(ngnix)
1.前端打包
运行npm run build:prod命令(此处以若依为例)
成功后根目录生成dist文件夹,里面有js、css、html等静态文件
2、部署静态文件
将static文件夹、favicon以及index.html复制到nginx的html文件夹下
3、配置nginx
打开conf文件夹下的nginx.conf,加入以下配置:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 端口号; //此处为端口号配置
server_name localhost;
charset utf-8;
location / {
root 此处为静态文件存放的路径,即html文件夹存放位置,建议填写绝对路径;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
location /prod-api/ { //此处是做路径跳转,具体根据项目来定,此外为若依配置;建议/prod-api/和/dev-api/两个都加上
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8080/; //跳转路径
}
location /dev-api/ {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8080/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
重要的配置:
- server:listen:端口号
- location/:root:静态文件路径
- location /prod-api/:前后端映射路径跳转
三、netty配置
使用netty时,如果不做修改,会导致项目无法启动。
netty端口号在程序中yml中配置,本项目为:9090
netty:
#端口
port: 8088
重要:
- netty服务端绑定端口异步监听,等待客户端连接,这个过程会导致线程变为wait状态
- 解决:开辟一个新的线程专门监听客户端,程序如下:
@Component
public class NettyServerListener implements ApplicationListener<ContextRefreshedEvent> {
@Autowired
private TCPServer tcpServer;
private static final Logger LOGGER = LoggerFactory.getLogger(NettyServerListener.class);
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
if (event.getApplicationContext().getParent() == null){
LOGGER.info("NettyServer Start Success");
new Thread(tcpServer).start();
}
}
}
TCPServer需要继承Runnable,并重写run方法。run方法中书写netty服务端逻辑