SpringCloud项目打war包部署

最近项目需要用到springcloud做微服务的形式来开发。springcloud基于springboot在开发上有很大的优势,在开发时可以直接通过jar包的形式来启动运行,也就是内嵌tomcat,但在实际项目部署当中,还是使用tomcat来部署比较方便,而且也有现成的监控。以下就是打war的流程:

1修改pom文件的packaging

<packaging>war</packaging>

2 修改Pom文件移除内嵌的tomcat

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

3添加对servlet包的依赖

<dependency>
        <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>

4 修改启动类。启动类要继承SpringBootServletInitializer类,并且重载configure方法。代码如下:

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ClientApplication extends SpringBootServletInitializer{

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // 注意这里要指向原先用main方法执行的Application启动类
        return builder.sources(ClientApplication.class, WebConfig.class);
    }

    public static void main(String[] args)
    {
        new SpringApplicationBuilder().sources(ClientApplication.class, WebConfig.class).run(args);
    }
}

按以上方法修改后,maven->install,则可以打包成war包

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐