Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
O
oms
Project
Project
Details
Activity
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
曲欣亮
oms
Commits
e2af9c93
Commit
e2af9c93
authored
Aug 27, 2019
by
张永
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1
parent
31ac204d
Pipeline
#164
failed with stages
Changes
6
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
81 additions
and
231 deletions
+81
-231
pom.xml
pom.xml
+4
-21
RedisConfig.java
src/main/java/com/egolm/oms/config/RedisConfig.java
+34
-0
RedisConfiguration.java
src/main/java/com/egolm/oms/config/RedisConfiguration.java
+0
-167
application-dev.properties
src/main/resources/application-dev.properties
+1
-1
application-pro.properties
src/main/resources/application-pro.properties
+21
-21
application-test.properties
src/main/resources/application-test.properties
+21
-21
No files found.
pom.xml
View file @
e2af9c93
...
...
@@ -29,23 +29,6 @@
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-data-redis
</artifactId>
<!-- https://blog.csdn.net/weixin_39723544/article/details/80743074 整合 -->
<!-- 1.5的版本默认采用的连接池技术是jedis 2.0以上版本默认连接池是lettuce, 在这里采用jedis,所以需要排除lettuce的jar -->
<exclusions>
<exclusion>
<groupId>
redis.clients
</groupId>
<artifactId>
jedis
</artifactId>
</exclusion>
<exclusion>
<groupId>
io.lettuce
</groupId>
<artifactId>
lettuce-core
</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 添加jedis客户端 -->
<dependency>
<groupId>
redis.clients
</groupId>
<artifactId>
jedis
</artifactId>
</dependency>
<dependency>
<groupId>
mysql
</groupId>
...
...
src/main/java/com/egolm/oms/config/RedisConfig.java
0 → 100644
View file @
e2af9c93
package
com
.
egolm
.
oms
.
config
;
import
org.springframework.beans.factory.annotation.Qualifier
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.data.redis.connection.RedisConnectionFactory
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer
;
import
org.springframework.data.redis.serializer.StringRedisSerializer
;
import
com.fasterxml.jackson.annotation.JsonAutoDetect
;
import
com.fasterxml.jackson.annotation.PropertyAccessor
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
@Configuration
public
class
RedisConfig
{
@Bean
@Qualifier
(
"redisTemplate"
)
public
RedisTemplate
<
String
,
?>
redisTemplate
(
RedisConnectionFactory
connectionFactory
)
{
RedisTemplate
<
String
,
?>
template
=
new
RedisTemplate
<>();
template
.
setConnectionFactory
(
connectionFactory
);
Jackson2JsonRedisSerializer
<?>
valueSerializer
=
new
Jackson2JsonRedisSerializer
<
Object
>(
Object
.
class
);
ObjectMapper
mapper
=
new
ObjectMapper
();
mapper
.
setVisibility
(
PropertyAccessor
.
ALL
,
JsonAutoDetect
.
Visibility
.
ANY
);
mapper
.
enableDefaultTyping
(
ObjectMapper
.
DefaultTyping
.
NON_FINAL
);
valueSerializer
.
setObjectMapper
(
mapper
);
template
.
setValueSerializer
(
valueSerializer
);
template
.
setKeySerializer
(
new
StringRedisSerializer
());
template
.
afterPropertiesSet
();
return
template
;
}
}
src/main/java/com/egolm/oms/config/RedisConfiguration.java
deleted
100644 → 0
View file @
31ac204d
package
com
.
egolm
.
oms
.
config
;
import
com.fasterxml.jackson.annotation.JsonAutoDetect
;
import
com.fasterxml.jackson.annotation.PropertyAccessor
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
redis.clients.jedis.JedisPool
;
import
redis.clients.jedis.JedisPoolConfig
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.cache.Cache
;
import
org.springframework.cache.CacheManager
;
import
org.springframework.cache.annotation.CachingConfigurerSupport
;
import
org.springframework.cache.annotation.EnableCaching
;
import
org.springframework.cache.interceptor.CacheErrorHandler
;
import
org.springframework.cache.interceptor.KeyGenerator
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.data.redis.cache.RedisCacheManager
;
import
org.springframework.data.redis.connection.jedis.JedisConnectionFactory
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer
;
import
org.springframework.data.redis.serializer.RedisSerializer
;
import
org.springframework.data.redis.serializer.StringRedisSerializer
;
@Configuration
// 必须加,使配置生效
@EnableCaching
public
class
RedisConfiguration
extends
CachingConfigurerSupport
{
/**
* Logger
*/
private
static
final
Logger
lg
=
LoggerFactory
.
getLogger
(
RedisConfiguration
.
class
);
@Autowired
private
JedisConnectionFactory
jedisConnectionFactory
;
@Bean
@Override
public
KeyGenerator
keyGenerator
()
{
// 设置自动key的生成规则,配置spring boot的注解,进行方法级别的缓存
// 使用:进行分割,可以很多显示出层级关系
// 这里其实就是new了一个KeyGenerator对象,只是这是lambda表达式的写法,我感觉很好用,大家感兴趣可以去了解下
return
(
target
,
method
,
params
)
->
{
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
target
.
getClass
().
getName
());
sb
.
append
(
":"
);
sb
.
append
(
method
.
getName
());
for
(
Object
obj
:
params
)
{
sb
.
append
(
":"
+
String
.
valueOf
(
obj
));
}
String
rsToUse
=
String
.
valueOf
(
sb
);
lg
.
info
(
"自动生成Redis Key -> [{}]"
,
rsToUse
);
return
rsToUse
;
};
}
@Bean
@Override
public
CacheManager
cacheManager
()
{
// 初始化缓存管理器,在这里我们可以缓存的整体过期时间什么的,我这里默认没有配置
lg
.
info
(
"初始化 -> [{}]"
,
"CacheManager RedisCacheManager Start"
);
RedisCacheManager
.
RedisCacheManagerBuilder
builder
=
RedisCacheManager
.
RedisCacheManagerBuilder
.
fromConnectionFactory
(
jedisConnectionFactory
);
return
builder
.
build
();
}
@Bean
public
RedisTemplate
<
String
,
Object
>
redisTemplate
(
JedisConnectionFactory
jedisConnectionFactory
)
{
//设置序列化
Jackson2JsonRedisSerializer
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
);
// 配置redisTemplate
RedisTemplate
<
String
,
Object
>
redisTemplate
=
new
RedisTemplate
<
String
,
Object
>();
redisTemplate
.
setConnectionFactory
(
jedisConnectionFactory
);
RedisSerializer
stringSerializer
=
new
StringRedisSerializer
();
redisTemplate
.
setKeySerializer
(
stringSerializer
);
// key序列化
redisTemplate
.
setValueSerializer
(
jackson2JsonRedisSerializer
);
// value序列化
redisTemplate
.
setHashKeySerializer
(
stringSerializer
);
// Hash key序列化
redisTemplate
.
setHashValueSerializer
(
jackson2JsonRedisSerializer
);
// Hash value序列化
redisTemplate
.
afterPropertiesSet
();
return
redisTemplate
;
}
@Override
@Bean
public
CacheErrorHandler
errorHandler
()
{
// 异常处理,当Redis发生异常时,打印日志,但是程序正常走
lg
.
info
(
"初始化 -> [{}]"
,
"Redis CacheErrorHandler"
);
CacheErrorHandler
cacheErrorHandler
=
new
CacheErrorHandler
()
{
@Override
public
void
handleCacheGetError
(
RuntimeException
e
,
Cache
cache
,
Object
key
)
{
lg
.
error
(
"Redis occur handleCacheGetError:key -> [{}]"
,
key
,
e
);
}
@Override
public
void
handleCachePutError
(
RuntimeException
e
,
Cache
cache
,
Object
key
,
Object
value
)
{
lg
.
error
(
"Redis occur handleCachePutError:key -> [{}];value -> [{}]"
,
key
,
value
,
e
);
}
@Override
public
void
handleCacheEvictError
(
RuntimeException
e
,
Cache
cache
,
Object
key
)
{
lg
.
error
(
"Redis occur handleCacheEvictError:key -> [{}]"
,
key
,
e
);
}
@Override
public
void
handleCacheClearError
(
RuntimeException
e
,
Cache
cache
)
{
lg
.
error
(
"Redis occur handleCacheClearError:"
,
e
);
}
};
return
cacheErrorHandler
;
}
/**
* 此内部类就是把yml的配置数据,进行读取,创建JedisConnectionFactory和JedisPool,以供外部类初始化缓存管理器使用
* 不了解的同学可以去看@ConfigurationProperties和@Value的作用
*
*/
@ConfigurationProperties
class
DataJedisProperties
{
@Value
(
"${spring.redis.host}"
)
private
String
host
;
@Value
(
"${spring.redis.password}"
)
private
String
password
;
@Value
(
"${spring.redis.port}"
)
private
int
port
;
@Value
(
"${spring.redis.timeout}"
)
private
int
timeout
;
@Value
(
"${spring.redis.jedis.max-idle}"
)
private
int
maxIdle
;
@Value
(
"${spring.redis.jedis.max-wait}"
)
private
long
maxWaitMillis
;
@SuppressWarnings
(
"deprecation"
)
@Bean
JedisConnectionFactory
jedisConnectionFactory
()
{
lg
.
info
(
"Create JedisConnectionFactory successful"
);
JedisConnectionFactory
factory
=
new
JedisConnectionFactory
();
factory
.
setHostName
(
host
);
factory
.
setPort
(
port
);
factory
.
setTimeout
(
timeout
);
factory
.
setPassword
(
password
);
return
factory
;
}
@Bean
public
JedisPool
redisPoolFactory
()
{
lg
.
info
(
"JedisPool init successful,host -> [{}];port -> [{}]"
,
host
,
port
);
JedisPoolConfig
jedisPoolConfig
=
new
JedisPoolConfig
();
jedisPoolConfig
.
setMaxIdle
(
maxIdle
);
jedisPoolConfig
.
setMaxWaitMillis
(
maxWaitMillis
);
JedisPool
jedisPool
=
new
JedisPool
(
jedisPoolConfig
,
host
,
port
,
timeout
,
password
);
return
jedisPool
;
}
}
}
\ No newline at end of file
src/main/resources/application-dev.properties
View file @
e2af9c93
server.tomcat.max-threads
=
1000
server.tomcat.min-spare-threads
=
30
server.port
=
30003
server.context-path
=
/oms
server.
servlet.
context-path
=
/oms
server.session.timeout
=
90000
server.tomcat.uri-encoding
=
utf-8
...
...
src/main/resources/application-pro.properties
View file @
e2af9c93
server.tomcat.max-threads
=
1000
server.tomcat.min-spare-threads
=
30
server.port
=
8089
server.
context-path
=
/
server.
servlet.context-path
=
/oms
server.session.timeout
=
90000
server.tomcat.uri-encoding
=
utf-8
...
...
src/main/resources/application-test.properties
View file @
e2af9c93
server.tomcat.max-threads
=
1000
server.tomcat.min-spare-threads
=
30
server.port
=
8089
server.
context-path
=
/
server.
servlet.context-path
=
/oms
server.session.timeout
=
90000
server.tomcat.uri-encoding
=
utf-8
...
...
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