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进行部署,如下图所示:
成功启动!
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进行部署,如下图所示:
成功启动!
相关文章推荐
- SpringBoot入门-21(springboot集成mybatis注解形式增删查改properties配置,利用@Provider实现动态SQL)
- 微服务开发的入门级框架Spring Boot详解:属性配置
- SpringBoot入门-19(springboot集成mybatis注解形式增删查改properties配置,利用《script》实现动态SQL)
- SpringBoot入门-20(springboot集成mybatis注解形式properties配置,利用@Provider实现动态SQL)
- SpringBoot入门-17(springboot集成mybatis注解形式增删查改properties配置)
- 微服务开发的入门级框架Spring Boot详解:集成Mybatis
- Spring Boot 部署与服务配置
- 【springboot 入门篇】第2篇 springboot的各种配置
- SpringBoot入门之内嵌Tomcat配置
- springboot 集成elasticsearh的简单配置
- Spring Boot 集成RabbitMQ入门Demo
- 微服务开发的入门级框架Spring Boot详解:注解
- 微服务架构之Spring Boot入门
- Spring boot 集成工作流flowable去掉xml配置
- 4 springboot项目集成使用disconf,配置文件更新及回调
- SpringBoot入门-12-1(springboot配置servlet,利用注解实现)
- 微服务开发的入门级框架Spring Boot详解(四)
- 1 Springboot SpringCloud集成OAuth2入门详细教程
- 【springboot 入门篇】第2篇 springboot的各种配置
- Spring Cloud Spring Boot mybatis分布式微服务云架构(三十三)注解配置与EhCache使用(1)