springsession
1.在各服务添加springsession依赖(服务自治)【auth、product、search、member、order、】
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--整合springsession,实现session共享-->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
2.属性配置
server:
servlet:
session:
timeout: 30m
spring:
redis:
host: 101.42.221.186
port: 6379
session:
store-type: redis
3.启动类添加配置
@EnableRedisHttpSession
6.添加以下配置,放大作用域 + 指定redis序列化器【否则使用默认的jdk序列化器】
/**
* springsession配置类
*/
@Configuration
public class GulimallSessionConfig {
@Bean
public CookieSerializer cookieSerializer() {
DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();
cookieSerializer.setDomainName("gulimall.com");// 放大作用域
cookieSerializer.setCookieName("GULISESSION");
cookieSerializer.setCookieMaxAge(60 * 60 * 24 * 7);// 指定cookie有效期7天,会话级关闭浏览器后cookie即失效
return cookieSerializer;
}
@Bean
public RedisSerializer<Object> springSessionDefaultRedisSerializer() {
// 指定session序列化到redis的序列化器
return new GenericJackson2JsonRedisSerializer();
}
}
7.修改product模块gulimall首页,去除session中的loginUser
<li>
<a th:if="${session.loginUser != null}">欢迎, [[${session.loginUser.nickname}]]</a>
<a th:if="${session.loginUser == null}" href="http://auth.gulimall.com/login.html">你好,请登录</a>
</li>
8.测试
=》进入auth.gulimall.com并社交登录
=》进入gulimall.com查看cookie作用域是否修改成功
=》查看redis,session是否存储成功
=》查看gulimall首页nickname是否取到值
线程池:
@EnableConfigurationProperties(MyThreadConfig.ThreadPoolConfigProperties.class)
@Configuration
public class MyThreadConfig {
@Bean
public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties pool) {
return new ThreadPoolExecutor(
pool.getCoreSize(),
pool.getMaxSize(),
pool.getKeepAliveTime(),
TimeUnit.SECONDS,
new LinkedBlockingDeque<>(100000),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.CallerRunsPolicy()
);
}
@ConfigurationProperties(prefix = "gulimall.thread")
@Data
public class ThreadPoolConfigProperties {
private Integer coreSize;
private Integer maxSize;
private Integer keepAliveTime;
}
}
gulimall:
thread:
core-size: 20
max-size: 200
keep-alive-time: 10