前言:

Spring Boot是什么,解决哪些问题

     1) Spring Boot使编码变简单

     2) Spring Boot使配置变简单

     3) Spring Boot使部署变简单

     4) Spring Boot使监控变简单

   

由于Spring Boot的定位是用来解决微服务的,因此它的服务容器是集成在内部的,因此不需要额外的进行部署,只需要启动SpringApplication就可以了,

不过有时候是需要将工程项目代码与服务进行分离,代码打包成war,然后再部署到tomcat或者jetty,weblogic中。而spring boot除了内置服务容器之外,也兼容与服务分离的方式,具体配置如下:


1、配置pom.xml依赖,

<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sam.project.service</groupId>
	<artifactId>spring_boot_service</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring_boot_service Maven Webapp</name>
	<url>http://maven.apache.org</url>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.1.RELEASE</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>


	</dependencies>
	<build>
		<finalName>spring_boot_service</finalName>
	</build>
</project>



2、配置启动入口:

package com.sam.project.service;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;

/**
 * @ClassName: Application 
 * @Description: springboot启动器
 */
@EnableCaching
@SpringBootApplication
public class Application extends SpringBootServletInitializer {

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(Application.class);
	}

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}



2、工程名称配置:

在application.properties文件中加入:


server.contextPath=/spring_boot_service



总结:将内置服务分离只需要配置一个地方,即tomcat引用:

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>





配置完成后即可添加到容器中进行部署,或者打包war进行部署,如下图所示:

spring boot集成docker spring boot 集成_spring


成功启动!