1. 程式人生 > >Spring Cloud 註冊中心在tomcat中部署

Spring Cloud 註冊中心在tomcat中部署

spring cloud 註冊中心在tomcat中部署

前言

最近剛剛接觸spring boot 和spring cloud,只知道可以直接通過main方法啟動服務,一直不會將項目部署到tomcat中,今天學了一下,做個記錄備忘.

步驟

pom文件

在pom文件中引入spring-boot-starter-web,然後排除掉內置的tomcat,最後引入javax.servlet-api,修改package為war,詳細如下

<?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方法

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主函數啟動程序

註意事項

技術分享

技術分享




本文出自 “小菜鳥” 博客,請務必保留此出處http://baishuchao.blog.51cto.com/12918589/1981984

Spring Cloud 註冊中心在tomcat中部署