Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
有来技术
youlai-mall
提交
25b5dc1c
Y
youlai-mall
项目概览
有来技术
/
youlai-mall
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Y
youlai-mall
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
25b5dc1c
编写于
2月 16, 2021
作者:
H
haoxr
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat:SpringBoot整合Redis集群
上级
c653b63c
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
26 addition
and
32 deletion
+26
-32
pom.xml
pom.xml
+6
-2
youlai-common/common-redis/pom.xml
youlai-common/common-redis/pom.xml
+5
-4
youlai-common/common-redis/src/main/java/com/youlai/common/redis/RedisConfig.java
...is/src/main/java/com/youlai/common/redis/RedisConfig.java
+15
-26
未找到文件。
pom.xml
浏览文件 @
25b5dc1c
...
...
@@ -44,6 +44,7 @@
<weixin-java-miniapp.version>
4.0.4.B
</weixin-java-miniapp.version>
<hibernate-validator.version>
6.0.13.Final
</hibernate-validator.version>
<seata.version>
1.4.1
</seata.version>
<commons-pool2-version>
2.9.0
</commons-pool2-version>
</properties>
<dependencies>
...
...
@@ -59,7 +60,6 @@
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!--Spring Cloud 相关依赖-->
...
...
@@ -110,7 +110,6 @@
<version>
${hibernate-validator.version}
</version>
</dependency>
<dependency>
<groupId>
io.seata
</groupId>
<artifactId>
seata-all
</artifactId>
...
...
@@ -123,6 +122,11 @@
<version>
${seata.version}
</version>
</dependency>
<dependency>
<groupId>
org.apache.commons
</groupId>
<artifactId>
commons-pool2
</artifactId>
<version>
${commons-pool2-version}
</version>
</dependency>
</dependencies>
</dependencyManagement>
...
...
youlai-common/common-redis/pom.xml
浏览文件 @
25b5dc1c
...
...
@@ -11,23 +11,24 @@
<artifactId>
common-redis
</artifactId>
<dependencies>
<!-- spring boot redis -->
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-data-redis
</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>
org.apache.commons
</groupId>
<artifactId>
commons-pool2
</artifactId>
</dependency>
<dependency>
<groupId>
com.fasterxml.jackson.core
</groupId>
<artifactId>
jackson-databind
</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>
com.fasterxml.jackson.core
</groupId>
<artifactId>
jackson-core
</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>
com.fasterxml.jackson.core
</groupId>
<artifactId>
jackson-annotations
</artifactId>
...
...
youlai-common/common-redis/src/main/java/com/youlai/common/redis/RedisConfig.java
浏览文件 @
25b5dc1c
...
...
@@ -8,7 +8,7 @@ import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import
org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.data.redis.connection.
Redis
ConnectionFactory
;
import
org.springframework.data.redis.connection.
lettuce.Lettuce
ConnectionFactory
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer
;
import
org.springframework.data.redis.serializer.StringRedisSerializer
;
...
...
@@ -18,39 +18,28 @@ import org.springframework.data.redis.serializer.StringRedisSerializer;
public
class
RedisConfig
{
@Bean
public
RedisTemplate
<
String
,
Object
>
redisTemplate
(
RedisConnectionFactory
f
actory
)
{
public
RedisTemplate
<
String
,
Object
>
redisTemplate
(
LettuceConnectionFactory
lettuceConnectionF
actory
)
{
RedisTemplate
<
String
,
Object
>
redisTemplate
=
new
RedisTemplate
<>();
redisTemplate
.
setConnectionFactory
(
f
actory
);
redisTemplate
.
setConnectionFactory
(
lettuceConnectionF
actory
);
StringRedisSerializer
stringRedisSerializer
=
new
StringRedisSerializer
();
// 使用StringRedisSerializer来序列化和反序列化redis的key值
redisTemplate
.
setKeySerializer
(
stringRedisSerializer
);
// 用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
redisTemplate
.
setValueSerializer
(
serializer
());
// hash的key也采用String的序列化方式
redisTemplate
.
setHashKeySerializer
(
stringRedisSerializer
);
// hash的value序列化方式采用jackson
redisTemplate
.
setHashValueSerializer
(
serializer
());
redisTemplate
.
afterPropertiesSet
();
return
redisTemplate
;
}
/**
* 配置Jackson2JsonRedisSerializer序列化策略
*/
private
Jackson2JsonRedisSerializer
<
Object
>
serializer
()
{
// 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
Jackson2JsonRedisSerializer
<
Object
>
jackson2JsonRedisSerializer
=
new
Jackson2JsonRedisSerializer
<>(
Object
.
class
);
StringRedisSerializer
stringRedisSerializer
=
new
StringRedisSerializer
();
redisTemplate
.
setKeySerializer
(
stringRedisSerializer
);
// key
Jackson2JsonRedisSerializer
jackson2JsonRedisSerializer
=
new
Jackson2JsonRedisSerializer
(
Object
.
class
);
ObjectMapper
objectMapper
=
new
ObjectMapper
();
// 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
// 指定要序列化的域(field,get,set),访问修饰符(public,private,protected)
objectMapper
.
setVisibility
(
PropertyAccessor
.
ALL
,
JsonAutoDetect
.
Visibility
.
ANY
);
// 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常
objectMapper
.
activateDefaultTyping
(
LaissezFaireSubTypeValidator
.
instance
,
ObjectMapper
.
DefaultTyping
.
NON_FINAL
);
jackson2JsonRedisSerializer
.
setObjectMapper
(
objectMapper
);
return
jackson2JsonRedisSerializer
;
redisTemplate
.
setValueSerializer
(
jackson2JsonRedisSerializer
);
//value
redisTemplate
.
setHashKeySerializer
(
stringRedisSerializer
);
redisTemplate
.
setHashValueSerializer
(
jackson2JsonRedisSerializer
);
redisTemplate
.
afterPropertiesSet
();
return
redisTemplate
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录