Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
K
kwell-mes
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
gavelinfo
kwell-mes
Commits
df89ba77
Commit
df89ba77
authored
Jun 20, 2022
by
yff
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of gitlab.gavelinfo.com:gavelinfo/kwell-mes
parents
615d3baa
985b4e6d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
94 additions
and
78 deletions
+94
-78
RedisConfig.java
gavel/src/main/java/com/gavel/kwell/config/RedisConfig.java
+94
-78
No files found.
gavel/src/main/java/com/gavel/kwell/config/RedisConfig.java
View file @
df89ba77
package
com
.
gavel
.
kwell
.
config
;
import
java.time.Duration
;
import
org.apache.commons.pool2.impl.GenericObjectPoolConfig
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.boot.autoconfigure.data.redis.RedisProperties
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Primary
;
import
org.springframework.context.annotation.PropertySource
;
import
org.springframework.data.redis.connection.RedisConnectionFactory
;
import
org.springframework.data.redis.connection.RedisPassword
;
import
org.springframework.data.redis.connection.RedisStandaloneConfiguration
;
import
org.springframework.data.redis.connection.jedis.JedisClientConfiguration
;
import
org.springframework.data.redis.connection.jedis.JedisConnectionFactory
;
import
org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration
;
import
org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory
;
import
org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer
;
import
org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer
;
import
org.springframework.data.redis.serializer.StringRedisSerializer
;
...
...
@@ -17,12 +24,13 @@ import com.fasterxml.jackson.annotation.JsonAutoDetect;
import
com.fasterxml.jackson.annotation.PropertyAccessor
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
redis.clients.jedis.JedisPoolConfig
;
@Configuration
@PropertySource
(
value
=
{
"classpath:config.properties"
})
public
class
RedisConfig
{
@Autowired
private
RedisProperties
redisProperties
;
@Value
(
"${redis.username:}"
)
private
String
username
;
@Value
(
"${redis.password:}"
)
...
...
@@ -61,79 +69,8 @@ public class RedisConfig {
@Value
(
"${spring.redis.testWhileIdle:false}"
)
private
boolean
testWhileIdle
;
/**
* JedisPoolConfig 连接池
* @return
*/
@Bean
public
JedisPoolConfig
jedisPoolConfig
()
{
JedisPoolConfig
jedisPoolConfig
=
new
JedisPoolConfig
();
// 最大空闲数
jedisPoolConfig
.
setMaxIdle
(
maxIdle
);
// 连接池的最大数据库连接数
jedisPoolConfig
.
setMaxTotal
(
maxTotal
);
// 最大建立连接等待时间
jedisPoolConfig
.
setMaxWaitMillis
(
maxWaitMillis
);
// 逐出连接的最小空闲时间 默认1800000毫秒(30分钟)
jedisPoolConfig
.
setMinEvictableIdleTimeMillis
(
minEvictableIdleTimeMillis
);
// 每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3
jedisPoolConfig
.
setNumTestsPerEvictionRun
(
numTestsPerEvictionRun
);
// 逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
jedisPoolConfig
.
setTimeBetweenEvictionRunsMillis
(
timeBetweenEvictionRunsMillis
);
// 是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
jedisPoolConfig
.
setTestOnBorrow
(
testOnBorrow
);
// 在空闲时检查有效性, 默认false
jedisPoolConfig
.
setTestWhileIdle
(
testWhileIdle
);
return
jedisPoolConfig
;
}
/**
* jedis连接工厂
* @param jedisPoolConfig
* @return
*/
@Bean
public
RedisConnectionFactory
redisConnectionFactory
(
JedisPoolConfig
jedisPoolConfig
)
{
RedisStandaloneConfiguration
redisStandaloneConfiguration
=
new
RedisStandaloneConfiguration
();
//设置redis服务器的host或者ip地址
redisStandaloneConfiguration
.
setHostName
(
host
);
redisStandaloneConfiguration
.
setPort
(
port
);
redisStandaloneConfiguration
.
setPassword
(
RedisPassword
.
of
(
password
));
redisStandaloneConfiguration
.
setDatabase
(
database
);
//获得默认的连接池构造
//这里需要注意的是,edisConnectionFactoryJ对于Standalone模式的没有(RedisStandaloneConfiguration,JedisPoolConfig)的构造函数,对此
//我们用JedisClientConfiguration接口的builder方法实例化一个构造器,还得类型转换
JedisClientConfiguration
.
JedisPoolingClientConfigurationBuilder
jpcf
=
(
JedisClientConfiguration
.
JedisPoolingClientConfigurationBuilder
)
JedisClientConfiguration
.
builder
();
//修改我们的连接池配置
jpcf
.
poolConfig
(
jedisPoolConfig
);
//通过构造器来构造jedis客户端配置
JedisClientConfiguration
jedisClientConfiguration
=
jpcf
.
build
();
return
new
JedisConnectionFactory
(
redisStandaloneConfiguration
,
jedisClientConfiguration
);
}
@Bean
public
RedisTemplate
<
String
,
Object
>
redisTemplate
(
RedisConnectionFactory
factory
)
{
RedisTemplate
<
String
,
Object
>
template
=
new
RedisTemplate
<
String
,
Object
>();
template
.
setConnectionFactory
(
factory
);
Jackson2JsonRedisSerializer
<
Object
>
jackson2JsonRedisSerializer
=
new
Jackson2JsonRedisSerializer
<>(
Object
.
class
);
ObjectMapper
om
=
new
ObjectMapper
();
om
.
setVisibility
(
PropertyAccessor
.
ALL
,
JsonAutoDetect
.
Visibility
.
ANY
);
om
.
enableDefaultTyping
(
ObjectMapper
.
DefaultTyping
.
NON_FINAL
);
jackson2JsonRedisSerializer
.
setObjectMapper
(
om
);
StringRedisSerializer
stringRedisSerializer
=
new
StringRedisSerializer
();
// key采用String的序列化方式
template
.
setKeySerializer
(
stringRedisSerializer
);
// hash的key也采用String的序列化方式
template
.
setHashKeySerializer
(
stringRedisSerializer
);
// value序列化方式采用jackson
template
.
setValueSerializer
(
jackson2JsonRedisSerializer
);
// hash的value序列化方式采用jackson
template
.
setHashValueSerializer
(
jackson2JsonRedisSerializer
);
template
.
setEnableTransactionSupport
(
true
);
template
.
afterPropertiesSet
();
return
template
;
}
public
String
getUsername
()
{
return
username
;
}
...
...
@@ -246,4 +183,83 @@ public class RedisConfig {
this
.
testWhileIdle
=
testWhileIdle
;
}
private
LettuceClientConfiguration
lettuceClientConfiguration
(){
LettucePoolingClientConfiguration
.
LettucePoolingClientConfigurationBuilder
builder
=
LettucePoolingClientConfiguration
.
builder
();
builder
.
poolConfig
(
genericObjectPoolConfig
());
if
(
this
.
redisProperties
.
isSsl
())
{
builder
.
useSsl
();
}
if
(
this
.
redisProperties
.
getTimeout
()
!=
null
)
{
Duration
timeout
=
this
.
redisProperties
.
getTimeout
();
builder
.
commandTimeout
(
timeout
).
shutdownTimeout
(
timeout
);
}
builder
.
poolConfig
(
genericObjectPoolConfig
());
return
builder
.
build
();
}
/**
* GenericObjectPoolConfig 连接池
* @return
*/
@Bean
(
"genericObjectPoolConfig"
)
public
GenericObjectPoolConfig
<?>
genericObjectPoolConfig
()
{
//连接池配置
GenericObjectPoolConfig
<?>
genericObjectPoolConfig
=
new
GenericObjectPoolConfig
<>();
// 最大空闲数
genericObjectPoolConfig
.
setMaxIdle
(
maxIdle
);
// 连接池的最大数据库连接数
genericObjectPoolConfig
.
setMaxTotal
(
maxTotal
);
// 最大建立连接等待时间
genericObjectPoolConfig
.
setMaxWaitMillis
(
maxWaitMillis
);
// 逐出连接的最小空闲时间 默认1800000毫秒(30分钟)
genericObjectPoolConfig
.
setMinEvictableIdleTimeMillis
(
minEvictableIdleTimeMillis
);
// 每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3
genericObjectPoolConfig
.
setNumTestsPerEvictionRun
(
numTestsPerEvictionRun
);
// 逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
genericObjectPoolConfig
.
setTimeBetweenEvictionRunsMillis
(
timeBetweenEvictionRunsMillis
);
// 是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
genericObjectPoolConfig
.
setTestOnBorrow
(
testOnBorrow
);
// 在空闲时检查有效性, 默认false
genericObjectPoolConfig
.
setTestWhileIdle
(
testWhileIdle
);
return
genericObjectPoolConfig
;
}
/**
* jedis连接工厂
* @param jedisPoolConfig
* @return
*/
@Primary
@Bean
(
"lettuceConnectionFactory"
)
public
LettuceConnectionFactory
lettuceConnectionFactory
()
{
RedisStandaloneConfiguration
redisStandaloneConfiguration
=
new
RedisStandaloneConfiguration
();
//设置redis服务器的host或者ip地址
redisStandaloneConfiguration
.
setHostName
(
host
);
redisStandaloneConfiguration
.
setPort
(
port
);
redisStandaloneConfiguration
.
setPassword
(
RedisPassword
.
of
(
password
));
redisStandaloneConfiguration
.
setDatabase
(
database
);
return
new
LettuceConnectionFactory
(
redisStandaloneConfiguration
,
lettuceClientConfiguration
());
}
@Bean
public
RedisTemplate
<
String
,
Object
>
redisTemplate
()
{
RedisTemplate
<
String
,
Object
>
template
=
new
RedisTemplate
<
String
,
Object
>();
template
.
setConnectionFactory
(
lettuceConnectionFactory
());
Jackson2JsonRedisSerializer
<
Object
>
jackson2JsonRedisSerializer
=
new
Jackson2JsonRedisSerializer
<>(
Object
.
class
);
ObjectMapper
om
=
new
ObjectMapper
();
om
.
setVisibility
(
PropertyAccessor
.
ALL
,
JsonAutoDetect
.
Visibility
.
ANY
);
om
.
enableDefaultTyping
(
ObjectMapper
.
DefaultTyping
.
NON_FINAL
);
jackson2JsonRedisSerializer
.
setObjectMapper
(
om
);
StringRedisSerializer
stringRedisSerializer
=
new
StringRedisSerializer
();
// key采用String的序列化方式
template
.
setKeySerializer
(
stringRedisSerializer
);
// hash的key也采用String的序列化方式
template
.
setHashKeySerializer
(
stringRedisSerializer
);
// value序列化方式采用jackson
template
.
setValueSerializer
(
new
GenericJackson2JsonRedisSerializer
());
// hash的value序列化方式采用jackson
template
.
setHashValueSerializer
(
new
GenericJackson2JsonRedisSerializer
());
template
.
afterPropertiesSet
();
return
template
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment