这里简单的实现springboot (2.2版本) 通过redis(3.4版本)实现 session的分布式共享,比较简单,代码如下
首先pom文件的代码如下:
<!--打war包使用外部的tomcat-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!--下面这一行去掉,运行和调试都可以用,要是不去调试的时候报错-->
<!--<scope>provided</scope>-->
</dependency>
<!--通redis实现session共享 不用配置开箱即用-->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<!--实现redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--通过jedis客户端实现redis-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.1.0</version>
</dependency>
<!--实现热部署 idea需要额外的配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
需要说明的是这里通过大war包通过外部的tomcat实现部署,没有使用springboot内置的tomcat。
application的代码很简单就写了一个端口,如下:
server:
port: 9011
另外单独写了一个redis的属性配置 redis.porperties文件 代码如下:
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
#spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=1024
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=100000
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=10
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=10000
#redis配置结束
spring.redis.block-when-exhausted=true
配置文件的代码如下
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPool;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.JedisPoolConfig;
@Slf4j
@Configuration
@PropertySource("classpath:redis.properties")
@EnableCaching
public class CachingConfigurer {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.timeout}")
private int timeout;
@Value("${spring.redis.jedis.pool.max-active}")
private int maxActive;
@Value("${spring.redis.jedis.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.jedis.pool.min-idle}")
private int minIdle;
@Value("${spring.redis.jedis.pool.max-wait}")
private long maxWaitMillis;
@Bean
public JedisPool generateJedisPoolFactory() {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(maxActive);
poolConfig.setMaxIdle(maxIdle);
poolConfig.setMinIdle(minIdle);
poolConfig.setMaxWaitMillis(maxWaitMillis);
JedisPool jedisPool = new JedisPool(poolConfig, host, port, timeout);
return jedisPool;
}
}
测试的controller层的代码如下:
import com.ext.demotwo.common.RedisUtil;
import com.ext.demotwo.common.RedisUtil22;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@RestController
public class redisController {
@RequestMapping("/get3")
public String get1(HttpSession session){
log.info(session.getId());
return "success";
}
}
因为这里是打war包,所以要在启动类做一些修改代码如下:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class DemotwoApplication extends SpringBootServletInitializer { //打war包 要继承SpringBootServletInitializer 这个类 同时端口号要和tomcat的端口号保持一致
public static void main(String[] args) {
SpringApplication.run(DemotwoApplication.class, args);
}
/*打war包要加的下面的方法*/
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application){
System.out.println("外部tomcat,chapter启动!");
return application.sources(DemotwoApplication.class);
}
}
可是设置不同的端口,打两个包放到两个tomcat运行,比如我用的 端口分别是 9010,9011,放在不同的tomcat上面运行,特别要注意,这个时候要把tomcat的端口号要修改 9010,9011和这里的
端口号对应,否则会出错。
实验的结果,sessionid 都是一样的。
欢迎批评指正,2020,11,11 河北邯郸
版权声明:本文为aaaa00原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。