前言
最近刚刚接触spring boot 和spring cloud,只知道可以直接通过main方法启动服务,一直不会将项目部署到tomcat中,今天学了一下,做个记录备忘.
步骤
pom文件
在pom文件中引入spring-boot-starter-web,然后排除掉内置的tomcat,最后引入javax.servlet-api,修改package为war,详细如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
<? 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方法
1
2
3
4
5
6
7
|
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主函数启动程序
注意事项
本文转自 baishuchao 51CTO博客,原文链接:http://blog.51cto.com/baishuchao/1981984