Bladeren bron

自定义spring redis@20210309@自定义spring redis,application.yml配置示例

huangbo 4 jaren geleden
bovenliggende
commit
e2f6d01f1b

+ 55 - 0
ipu-rest-demo/src/main/java/com/ai/ipu/server/demo/cache/DefineRedisConfig.java

@ -0,0 +1,55 @@
1
package com.ai.ipu.server.demo.cache;
2

3
import org.springframework.beans.factory.annotation.Qualifier;
4
import org.springframework.beans.factory.annotation.Value;
5
import org.springframework.cache.annotation.EnableCaching;
6
import org.springframework.context.annotation.Bean;
7
import org.springframework.context.annotation.Configuration;
8
import org.springframework.context.annotation.Primary;
9
import org.springframework.data.redis.connection.RedisConnectionFactory;
10
import org.springframework.data.redis.core.RedisTemplate;
11

12
@EnableCaching
13
@Configuration
14
public class DefineRedisConfig extends RedisConfig{
15
	@Value("${define.redis1.database}")
16
    private int dbIndex;
17

18
    @Value("${define.redis1.host}")
19
    private String host;
20

21
    @Value("${define.redis1.port}")
22
    private int port;
23

24
    @Value("${define.redis1.password}")
25
    private String password;
26

27
    @Value("${define.redis1.timeout}")
28
    private int timeout;
29

30
    /**
31
     * 配置redis连接工厂
32
     *
33
     * @return
34
     */
35
    @Primary
36
    @Bean(name = "defaultRedisConnectionFactory")
37
    public RedisConnectionFactory defaultRedisConnectionFactory() {
38
        return createJedisConnectionFactory(dbIndex, host, port, password, timeout);
39
    }
40

41
    /**
42
     * 配置redisTemplate 注入方式使用@Resource(name="") 方式注入
43
     *
44
     * @return
45
     */
46
    @Bean(name = "defaultRedisTemplate")
47
    @Qualifier
48
    public RedisTemplate defaultRedisTemplate() {
49
        RedisTemplate template = new RedisTemplate();
50
        template.setConnectionFactory(defaultRedisConnectionFactory());
51
        setSerializer(template);
52
        template.afterPropertiesSet();
53
        return template;
54
    }
55
}

+ 97 - 0
ipu-rest-demo/src/main/java/com/ai/ipu/server/demo/cache/RedisConfig.java

@ -0,0 +1,97 @@
1
package com.ai.ipu.server.demo.cache;
2

3
import org.springframework.beans.factory.annotation.Value;
4
import org.springframework.cache.CacheManager;
5
import org.springframework.cache.annotation.EnableCaching;
6
import org.springframework.context.annotation.Bean;
7
import org.springframework.context.annotation.Configuration;
8
import org.springframework.data.redis.cache.RedisCacheManager;
9
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
10
import org.springframework.data.redis.core.RedisTemplate;
11
import org.springframework.data.redis.serializer.StringRedisSerializer;
12

13
import redis.clients.jedis.JedisPoolConfig;
14

15
@EnableCaching
16
@Configuration
17
public class RedisConfig {
18
    @Value("${spring.redis.pool.max-active}")
19
    private int redisPoolMaxActive;
20

21
    @Value("${spring.redis.pool.max-wait}")
22
    private int redisPoolMaxWait;
23

24
    @Value("${spring.redis.pool.max-idle}")
25
    private int redisPoolMaxIdle;
26

27
    @Value("${spring.redis.pool.min-idle}")
28
    private int redisPoolMinIdle;
29

30
 
31
    /**
32
     * 创建redis连接工厂
33
     *
34
     * @param dbIndex
35
     * @param host
36
     * @param port
37
     * @param password
38
     * @param timeout
39
     * @return
40
     */
41
    public JedisConnectionFactory createJedisConnectionFactory(int dbIndex, String host, int port, String password, int timeout) {
42
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
43
        jedisConnectionFactory.setDatabase(dbIndex);
44
        jedisConnectionFactory.setHostName(host);
45
        jedisConnectionFactory.setPort(port);
46
        jedisConnectionFactory.setPassword(password);
47
        jedisConnectionFactory.setTimeout(timeout);
48
        jedisConnectionFactory.setPoolConfig(setPoolConfig(redisPoolMaxIdle, redisPoolMinIdle, redisPoolMaxActive, redisPoolMaxWait, true));
49
        return jedisConnectionFactory;
50

51
    }
52

53
    /**
54
     * 配置CacheManager
55
     *
56
     * @param redisTemplate
57
     * @return
58
     */
59
    @Bean
60
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
61
        RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);
62
        return redisCacheManager;
63
    }
64

65
    /**
66
     * 设置连接池属性
67
     *
68
     * @param maxIdle
69
     * @param minIdle
70
     * @param maxActive
71
     * @param maxWait
72
     * @param testOnBorrow
73
     * @return
74
     */
75
    public JedisPoolConfig setPoolConfig(int maxIdle, int minIdle, int maxActive, int maxWait, boolean testOnBorrow) {
76
        JedisPoolConfig poolConfig = new JedisPoolConfig();
77
        poolConfig.setMaxIdle(maxIdle);
78
        poolConfig.setMinIdle(minIdle);
79
        poolConfig.setMaxTotal(maxActive);
80
        poolConfig.setMaxWaitMillis(maxWait);
81
        poolConfig.setTestOnBorrow(testOnBorrow);
82
        return poolConfig;
83
    }
84

85
    /**
86
     * 设置RedisTemplate的序列化方式
87
     *
88
     * @param redisTemplate
89
     */
90
    public void setSerializer(RedisTemplate redisTemplate) {
91
        //设置键(key)的序列化方式
92
        redisTemplate.setKeySerializer(new StringRedisSerializer());
93
        //设置值(value)的序列化方式
94
        redisTemplate.setValueSerializer(new StringRedisSerializer());
95
        redisTemplate.afterPropertiesSet();
96
    }
97
}

+ 18 - 0
ipu-rest-demo/src/main/java/com/ai/ipu/server/demo/cache/package-info.java

@ -0,0 +1,18 @@
1
/**
2
 * 自定义spring redis空间,application.yml配置示例:
3
spring:
4
  redis:
5
    pool:
6
      max-active: -1 # 连接池最大连接数(使用负值表示没有限制)
7
      max-wait: -1  # 连接池最大阻塞等待时间(使用负值表示没有限制)
8
      max-idle: 8  # 连接池中的最大空闲连接
9
      min-idle: 0  # 连接池中的最小空闲连接
10
define:
11
  redis1:
12
    database: 2   # Redis数据库索引(默认为0)
13
    host: 192.168.128.132  # Redis服务器地址
14
    port: 6371  # Redis服务器连接端口
15
    password:    # Redis服务器连接密码(默认为空)
16
    timeout: 0  # 连接超时时间(毫秒)
17
 */
18
package com.ai.ipu.server.demo.cache;

+ 0 - 35
ipu-rest-demo/src/main/java/com/ai/ipu/server/demo/interceptor/DemoInterceptor.java

@ -1,35 +0,0 @@
1
package com.ai.ipu.server.demo.interceptor;
2

3
import javax.servlet.http.HttpServletRequest;
4
import javax.servlet.http.HttpServletResponse;
5

6
import org.springframework.web.servlet.HandlerInterceptor;
7
import org.springframework.web.servlet.ModelAndView;
8

9
public class DemoInterceptor implements HandlerInterceptor {
10

11
	@Override
12
	public boolean preHandle(HttpServletRequest request,
13
			HttpServletResponse response, Object handler) throws Exception {
14
		// TODO Auto-generated method stub
15
		System.out.println("特定拦截器:" + this);
16
		return true;
17
	}
18

19
	@Override
20
	public void postHandle(HttpServletRequest request,
21
			HttpServletResponse response, Object handler,
22
			ModelAndView modelAndView) throws Exception {
23
		// TODO Auto-generated method stub
24

25
	}
26

27
	@Override
28
	public void afterCompletion(HttpServletRequest request,
29
			HttpServletResponse response, Object handler, Exception ex)
30
			throws Exception {
31
		// TODO Auto-generated method stub
32

33
	}
34

35
}