mirror of
https://gitee.com/dromara/sa-token.git
synced 2026-02-27 16:50:24 +08:00
优化文档
This commit is contained in:
+71
-64
@@ -43,71 +43,78 @@ public class SaAloneRedisInject implements EnvironmentAware{
|
||||
*/
|
||||
@Override
|
||||
public void setEnvironment(Environment environment) {
|
||||
|
||||
// 如果为空或者默认实现,则不进行任何操作
|
||||
if(saTokenDao == null || saTokenDao instanceof SaTokenDaoDefaultImpl) {
|
||||
return;
|
||||
}
|
||||
|
||||
// ------------------- 开始注入
|
||||
|
||||
// 获取cfg对象
|
||||
RedisProperties cfg = Binder.get(environment).bind(ALONE_PREFIX, RedisProperties.class).get();
|
||||
|
||||
// 1. Redis配置
|
||||
RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration();
|
||||
redisConfig.setHostName(cfg.getHost());
|
||||
redisConfig.setPort(cfg.getPort());
|
||||
redisConfig.setDatabase(cfg.getDatabase());
|
||||
redisConfig.setPassword(RedisPassword.of(cfg.getPassword()));
|
||||
|
||||
// 2. 连接池配置
|
||||
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
|
||||
// pool配置
|
||||
Lettuce lettuce = cfg.getLettuce();
|
||||
if(lettuce.getPool() != null) {
|
||||
RedisProperties.Pool pool = cfg.getLettuce().getPool();
|
||||
// 连接池最大连接数
|
||||
poolConfig.setMaxTotal(pool.getMaxActive());
|
||||
// 连接池中的最大空闲连接
|
||||
poolConfig.setMaxIdle(pool.getMaxIdle());
|
||||
// 连接池中的最小空闲连接
|
||||
poolConfig.setMinIdle(pool.getMinIdle());
|
||||
// 连接池最大阻塞等待时间(使用负值表示没有限制)
|
||||
poolConfig.setMaxWaitMillis(pool.getMaxWait().toMillis());
|
||||
}
|
||||
LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder builder = LettucePoolingClientConfiguration.builder();
|
||||
// timeout
|
||||
if(cfg.getTimeout() != null) {
|
||||
builder.commandTimeout(cfg.getTimeout());
|
||||
}
|
||||
// shutdownTimeout
|
||||
if(lettuce.getShutdownTimeout() != null) {
|
||||
builder.shutdownTimeout(lettuce.getShutdownTimeout());
|
||||
}
|
||||
// 创建Factory对象
|
||||
LettuceClientConfiguration clientConfig = builder.poolConfig(poolConfig).build();
|
||||
LettuceConnectionFactory factory = new LettuceConnectionFactory(redisConfig, clientConfig);
|
||||
factory.afterPropertiesSet();
|
||||
|
||||
// 3. 开始初始化 SaTokenDao
|
||||
// 如果是SaTokenDaoRedis
|
||||
try {
|
||||
Class.forName("cn.dev33.satoken.dao.SaTokenDaoRedis");
|
||||
SaTokenDaoRedis dao = (SaTokenDaoRedis)saTokenDao;
|
||||
dao.isInit = false;
|
||||
dao.init(factory);
|
||||
return;
|
||||
} catch (ClassNotFoundException e) {
|
||||
}
|
||||
// 如果是SaTokenDaoRedisJackson
|
||||
try {
|
||||
Class.forName("cn.dev33.satoken.dao.SaTokenDaoRedisJackson");
|
||||
SaTokenDaoRedisJackson dao = (SaTokenDaoRedisJackson)saTokenDao;
|
||||
dao.isInit = false;
|
||||
dao.init(factory);
|
||||
return;
|
||||
} catch (ClassNotFoundException e) {
|
||||
// 如果为空或者默认实现,则不进行任何操作
|
||||
if(saTokenDao == null || saTokenDao instanceof SaTokenDaoDefaultImpl) {
|
||||
return;
|
||||
}
|
||||
// 如果配置文件不包含相关配置,则不进行任何操作
|
||||
if(environment.getProperty(ALONE_PREFIX + ".host") == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// ------------------- 开始注入
|
||||
|
||||
// 获取cfg对象
|
||||
RedisProperties cfg = Binder.get(environment).bind(ALONE_PREFIX, RedisProperties.class).get();
|
||||
|
||||
// 1. Redis配置
|
||||
RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration();
|
||||
redisConfig.setHostName(cfg.getHost());
|
||||
redisConfig.setPort(cfg.getPort());
|
||||
redisConfig.setDatabase(cfg.getDatabase());
|
||||
redisConfig.setPassword(RedisPassword.of(cfg.getPassword()));
|
||||
|
||||
// 2. 连接池配置
|
||||
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
|
||||
// pool配置
|
||||
Lettuce lettuce = cfg.getLettuce();
|
||||
if(lettuce.getPool() != null) {
|
||||
RedisProperties.Pool pool = cfg.getLettuce().getPool();
|
||||
// 连接池最大连接数
|
||||
poolConfig.setMaxTotal(pool.getMaxActive());
|
||||
// 连接池中的最大空闲连接
|
||||
poolConfig.setMaxIdle(pool.getMaxIdle());
|
||||
// 连接池中的最小空闲连接
|
||||
poolConfig.setMinIdle(pool.getMinIdle());
|
||||
// 连接池最大阻塞等待时间(使用负值表示没有限制)
|
||||
poolConfig.setMaxWaitMillis(pool.getMaxWait().toMillis());
|
||||
}
|
||||
LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder builder = LettucePoolingClientConfiguration.builder();
|
||||
// timeout
|
||||
if(cfg.getTimeout() != null) {
|
||||
builder.commandTimeout(cfg.getTimeout());
|
||||
}
|
||||
// shutdownTimeout
|
||||
if(lettuce.getShutdownTimeout() != null) {
|
||||
builder.shutdownTimeout(lettuce.getShutdownTimeout());
|
||||
}
|
||||
// 创建Factory对象
|
||||
LettuceClientConfiguration clientConfig = builder.poolConfig(poolConfig).build();
|
||||
LettuceConnectionFactory factory = new LettuceConnectionFactory(redisConfig, clientConfig);
|
||||
factory.afterPropertiesSet();
|
||||
|
||||
// 3. 开始初始化 SaTokenDao
|
||||
// 如果是SaTokenDaoRedis
|
||||
try {
|
||||
Class.forName("cn.dev33.satoken.dao.SaTokenDaoRedis");
|
||||
SaTokenDaoRedis dao = (SaTokenDaoRedis)saTokenDao;
|
||||
dao.isInit = false;
|
||||
dao.init(factory);
|
||||
return;
|
||||
} catch (ClassNotFoundException e) {
|
||||
}
|
||||
// 如果是SaTokenDaoRedisJackson
|
||||
try {
|
||||
Class.forName("cn.dev33.satoken.dao.SaTokenDaoRedisJackson");
|
||||
SaTokenDaoRedisJackson dao = (SaTokenDaoRedisJackson)saTokenDao;
|
||||
dao.isInit = false;
|
||||
dao.init(factory);
|
||||
return;
|
||||
} catch (ClassNotFoundException e) {
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user