springboot的打包方式有很多种。有打成war的,有打成jar的,也有直接提交到github,通过jekins进行打包部署的。
这里主要介绍如何打成jar进行部署。不推荐用war,因为springboot适合前后端分离,打成jar进行部署更合适。
首先需要在application.yml当中配置端口
server:
port: 6868 # 服务端口
marven的配置文件
<?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">
<parent>
<artifactId>tensquare_parent</artifactId>
<groupId>com.wch</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>tensquare-eureke</artifactId>
<dependencies>
<!-- 引入eureka-server 依赖 add by WanChengHe 20190507 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.wch.eureka.EurekaServer</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
注意最下面的build这块一定要配置否则打jar的时候会说找不 到主类
在启动类当中加上extends SpringBootServletInitializer并重写configure方法,这是为了打包springboot项目用的。
package com.wch.eureka;
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.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer extends SpringBootServletInitializer {
public static void main(String[] args){
SpringApplication.run(EurekaServer.class,args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(this.getClass());
}
}
然后按照顺序运行mvn clean再mvn install,我是用idea执行的
然后就会出来我们需要的jar
然后到这个jar的根目录下执行java -jar smallsystem-0.0.1-SNAPSHOT.jar
这个执行方式windows和linux上都一样