最近剛剛接觸spring boot 和spring cloud,只知道能夠直接經過main方法啓動服務,一直不會將項目部署到tomcat中,今天學了一下,作個記錄備忘.java
在pom文件中引入spring-boot-starter-web,而後排除掉內置的tomcat,最後引入javax.servlet-api,修改package爲war,詳細以下web
<?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> <groupId>SpringCloudServer</groupId> <artifactId>Spring_Cloud_Server</artifactId> <version>1.0-SNAPSHOT</version> <name>springcloudserver</name> <packaging>war</packaging> <!--spring cloud server 使用時須要注意parent的版本號和dependencies的版本號--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> <relativePath/> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <!--<version>Brixton.SR5</version>--> <version>Dalston.SR2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!--引入spring boot的web--> <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> <!--引入 servlet api--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies></project>
主函數啓動類須要繼承SpringBootServletInitializer,重寫config方法spring
import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.boot.web.support.SpringBootServletInitializer;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication@EnableEurekaServerpublic class ServerApplication_First extends SpringBootServletInitializer{ @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(ServerApplication_First.class); } public static void main(String[] args) { new SpringApplicationBuilder(ServerApplication_First.class).web(true).run(args); } }
注意:由於把spring boot自帶的tomcat排除了,因此沒法再使用main主函數啓動程序apache