您的位置:首页 > 编程语言 > Java开发

Spring boot入门六 服务配置与集成

2017-08-30 18:15 459 查看
前言:
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进行部署,如下图所示:



成功启动!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: