SpringBoot使用模版
SpringBoot使用html模版,Controller需要使用注解:@Controller
pom配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
yml配置
spring:
thymeleaf:
mode: HTML
encoding: UTF-8
prefix: classpath:/templates/
suffix: .html
SpringBoot会话拦截器
会话拦截器,用于在请求一些接口之前,统一去拦截请求,进行验证等操作。
Read on →SpringSession yml配置
使用SpringSession,需要先引入依赖,查看Maven Dependency Spring Session, 目前都是使用分布式的方式部署,因此使用redis会好一点。
Application配置
spring:
session:
store-type: redis
配置启动
在启动类里面配置启动
// 开启使用redis作为Spring Session
@EnableRedisHttpSession
使用方法
public Object setSession(HttpServletRequest request) {
HttpSession session = request.getSession();
session.setAttribute("userInfo", "XXX");
session.setMaxInactiveInterval(3600);
session.getAttribute("userInfo);
session.removeAttribute("userInfo");
}
因为使用Spring Session,需要安装Spring安全框架依赖,因为安装了这个依赖后,程序每次访问都需要登录,很麻烦,因此在启动类将它排除即可。排除方法见下方:
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
Maven Dependency
缓存雪崩预防
当缓存大面积失效,请求会全部打到数据库上,这种情况就是雪崩。
Read on →SpringBoot集成Redis集群
Redis-Cluster集群
集群配置
打开redis配置文件,找到 REDIS CLUSTER 配置块。修改以下配置:
# 开启redis-cluster集群配置
cluster-enabled yes
# 每一个节点在集群中的配置文件, 该文件有redis自己去维护
cluster-config-file nodes-6379.conf
# 超时时间,超时自动切换
cluster-node-timeout 5000
# aof模式
appendonly yes
# aof日志文件名
appendfilename "appendonly.aof"
Read on →构建集群时,需要将.rdb文件和.aof文件删除或者清空,否则会报错。
SpringBoot集成Redis哨兵
Redis哨兵机制与实现
Redis使用源码安装的,在源码包内有一个 sentinel.conf 文件,这个文件就是哨兵机制的配置文件,将该配置文件拷贝到 /usr/local/redis 目录下。然后编辑配置即可。
Read on →