因Spring Boot自带服务器,所以只需打成jar即可。
步骤如下:
1. 修改*Application.java文件,使其继承[SpringBootServletInitializer]类,并重写[configure]方法
package com.chyer.apigateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableEurekaClient
@RestController
public class ApiGatewayApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(ApiGatewayApplication.class, args);
}
//为了打包springboot项目
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(this.getClass());
}
}
2. 修改pom.xml文件,追加2个插件(如报缺少servlet包,可自行引入)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.chyer</groupId>
<artifactId>chyer-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.chyer</groupId>
<artifactId>api-gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>api-gateway</name>
<description>Demo project of gateway</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.chyer.apigateway.ApiGatewayApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
3. 打包命令
mvn clean
mvn install -Dmaven.test.skip=true
ls -l ./target/
结果:

4. 运行
java -jar api-gateway-0.0.1-SNAPSHOT.jar
结果:
![]()
本文详细介绍如何将SpringBoot项目打成可独立运行的jar包,包括修改Application类以继承SpringBootServletInitializer,以及在pom.xml中配置必要的插件和参数。通过遵循这些步骤,可以简化部署流程。
1034

被折叠的 条评论
为什么被折叠?



