课时六十、spring boot 服务配置和部署
理论
Spring Boot 其默认是集成web容器的,启动方式像普通Java程序一样,main函数入口启动。由配置来决定是使用Tomcat容器或Jetty容器,默认是Tomcat。当然你也可以将项目打包成war包,放到独立的web容器中(如:Tomcat、weblogic等等
一、内嵌Server配置
Server的几个常用的配置做个简单说明:
#项目contextPath,一般在正式发布版本中,我们不配置
server.context-path=/springboot# 错误页:指定发生错误时,跳转的URL。请查看BasicErrorController。
server.error.path=/error# 服务端口,默认为8080
server.port=8080# session最大超时时间(分钟),默认为30
server.session-timeout=60# 该服务绑定IP地址,启动服务器时如本机不是该IP地址则抛出异常启动失败,只有特殊需求的情况下才配置
# server.address=192.168.16.11
二、Tomcat 配置项
Server的几个常用的配置做个简单说明:
#项目contextPath,一般在正式发布版本中,我们不配置
server.context-path=/springboot# 错误页:指定发生错误时,跳转的URL。请查看BasicErrorController。
server.error.path=/error# 服务端口,默认为8080
server.port=8080# session最大超时时间(分钟),默认为30
server.session-timeout=60# 该服务绑定IP地址,启动服务器时如本机不是该IP地址则抛出异常启动失败,只有特殊需求的情况下才配置
# server.address=192.168.16.11
三、Jetty 选择
如果你要选择Jetty,也非常简单,只需要把pom.xml中的tomcat依赖排除,并加入Jetty容器的依赖接口,如下配置:
排除tomcat依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 排除默认的tomcat,引入jetty容器. -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
引入jetty的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
参考:spring-boot-handlerInterceptor
课时六十一、 Spring Boot 定制URL匹配规则
事情的起源
事情的起源:有人问我,说编写了一个/hello访问路径,但是吧,不管是输入/hello还是/hello.html,还是/hello.xxx都能进行访问。当时我还以为他对代码进行处理了,后来发现不是,后来发现这是Spring Boot路由规则。好了,有废话了下,那么看看我们解决上面这个导致的问题。
核心的开发步骤就是两步
(1)启动类 extends WebMvcConfigurationSupport
public class App extends WebMvcConfigurerAdapter
(2)重写configurePathMatch方法
@Configuration
public class MyWebMvcConfigurationSupport extends WebMvcConfigurationSupport{
@Override
protected void configurePathMatch(PathMatchConfigurer configurer) {
super.configurePathMatch(configurer);
configurer.setUseSuffixPatternMatch(false)
.setUseTrailingSlashMatch(true);}
}
setUseSuffixPatternMatch
setUseSuffixPatternMatch(boolean useSuffixPatternMatch):
设置是否是后缀模式匹配,如“/user”是否匹配/user.*,默认真即匹配;
当此参数设置为true的时候,那么/user.html,/user.aa,/user.*都能是正常访问的。
当此参数设置为false的时候,那么只能访问/user或者/user/( 这个前提是setUseTrailingSlashMatch 设置为true了)。
setUseTrailingSlashMatch (boolean useSuffixPatternMatch):
设置是否自动后缀路径模式匹配,如“/user”是否匹配“/user/”,默认真即匹配;
当此参数设置为true后,那么地址/user,/user/都能正常访问。
当此参数设置为false的时候,那么就只能访问/user。
当以上两个参数都设置为true的时候,那么路径/user或者/user.aa,/user.*,/user/都是能正常访问的,但是类似/user.html/ 是无法访问的。
当都设置为false的时候,那么就只能访问/user路径了。
参考:spring-boot-handlerInterceptor
课时六十二、 Spring Boot – 修改启动banner
我们都知道Spring Boot启动的时候,是如下的图标的:我们都知道Spring Boot启动的时候,是如下的图标的:
___________ ____
/ ___/\__ \ / ___\
\___ \ / __ \_/ /_/ >
/____ >(____ /\___ /
\/ \//_____/
Spring Boot – 启动彩蛋
那么我想要自定义的更漂亮,那么应该怎么做呢?
最简单的方式就是在resources目录下面放入一个banner.txt文件,Spring Boot启动的时候就会优先启动这个文件中的内容。
这里推荐两个字符画的网站,我们可以将生成的的字符串放入这个banner.txt文件中。
http://www.network-science.de/ascii/
http://patorjk.com/software/taag/
参考:spring-boot-handlerInterceptor