博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot 配置Redis
阅读量:7114 次
发布时间:2019-06-28

本文共 5281 字,大约阅读时间需要 17 分钟。

hot3.png

需要使用的包

org.springframework.boot
spring-boot-starter-redis
1.4.3.RELEASE

写配置文件application.properties

redis.host=10.211.55.8redis.port=6379redis.password=uwo_comredis.database=0redis.timeout=1000000redis.pool.max-total=1000redis.pool.max-idle=200redis.pool.min-idle=10redis.pool.max-wait=10000

编写RedisProperties文件

package com.uwo.oss.redis.configuration;import org.springframework.boot.context.properties.ConfigurationProperties;/** * Created by yanhao on 2017/6/1. */@ConfigurationProperties(prefix = "redis", locations = {"application-{profile}.properties"})public class RedisProperties {    private String host;    private Integer database;    private Integer port;    private String password;    private Integer timeout;    private RedisPoolProperties pool;    public String getHost() {        return host;    }    public void setHost(String host) {        this.host = host;    }    public Integer getDatabase() {        return database;    }    public void setDatabase(Integer database) {        this.database = database;    }    public Integer getPort() {        return port;    }    public void setPort(Integer port) {        this.port = port;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    public Integer getTimeout() {        return timeout;    }    public void setTimeout(Integer timeout) {        this.timeout = timeout;    }    public RedisPoolProperties getPool() {        return pool;    }    public void setPool(RedisPoolProperties pool) {        this.pool = pool;    }    public static class RedisPoolProperties{        private Integer maxTotal;        private Integer maxIdle;        private Integer minIdle;        private Integer maxWait;        public Integer getMaxTotal() {            return maxTotal;        }        public void setMaxTotal(Integer maxTotal) {            this.maxTotal = maxTotal;        }        public Integer getMaxIdle() {            return maxIdle;        }        public void setMaxIdle(Integer maxIdle) {            this.maxIdle = maxIdle;        }        public Integer getMinIdle() {            return minIdle;        }        public void setMinIdle(Integer minIdle) {            this.minIdle = minIdle;        }        public Integer getMaxWait() {            return maxWait;        }        public void setMaxWait(Integer maxWait) {            this.maxWait = maxWait;        }    }}

编写RedisConfiguration类

package com.uwo.oss.redis.configuration; import org.apache.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;import org.springframework.data.redis.core.StringRedisTemplate;import redis.clients.jedis.JedisPoolConfig; /** * Created by yanhao on 2017/6/1. */@Configuration@EnableConfigurationProperties({RedisProperties.class})public class RedisConfiguration {     private final Logger log = Logger.getLogger(RedisConfiguration.class);     @Autowired    private RedisProperties redisProperties;     @Bean    public RedisConnectionFactory jedisConnectionFactory(){        log.warn(String.format("redis connection factory host = %s, " +            "database = %d ,port = %d ,password = %s ,timeout = %d, " +            "pool max-idle = %d, min-idle = %d, max-wait = %d, max-total = %d",            redisProperties.getHost(), redisProperties.getDatabase(),            redisProperties.getPort(), redisProperties.getPassword(),            redisProperties.getTimeout(), redisProperties.getPool().getMaxIdle(),            redisProperties.getPool().getMinIdle(), redisProperties.getPool().getMaxWait(),            redisProperties.getPool().getMaxTotal()));         JedisPoolConfig poolConfig = new JedisPoolConfig();        poolConfig.setMaxTotal(redisProperties.getPool().getMaxTotal());        poolConfig.setMaxIdle(redisProperties.getPool().getMaxIdle());        poolConfig.setMinIdle(redisProperties.getPool().getMinIdle());        poolConfig.setMaxWaitMillis(redisProperties.getPool().getMaxWait());        poolConfig.setTestOnBorrow(true);        poolConfig.setTestOnReturn(true);        poolConfig.setTestWhileIdle(true);         JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(poolConfig);        jedisConnectionFactory.setHostName(redisProperties.getHost());        jedisConnectionFactory.setPassword(redisProperties.getPassword());        jedisConnectionFactory.setTimeout(redisProperties.getTimeout());        jedisConnectionFactory.setDatabase(redisProperties.getDatabase());        jedisConnectionFactory.setPort(redisProperties.getPort());        return jedisConnectionFactory;    }     @Bean    public StringRedisTemplate redisTemplate() {        log.warn("redis template");        StringRedisTemplate redisTemplate = new StringRedisTemplate(jedisConnectionFactory());        return redisTemplate;    } }

转载于:https://my.oschina.net/yan5845hao/blog/913394

你可能感兴趣的文章
后端开发面经
查看>>
使用Envoy 作Sidecar Proxy的微服务模式-3.分布式追踪
查看>>
焦虑、不安
查看>>
this是什么以及如何判断它
查看>>
【Go】string 优化误区及建议
查看>>
Perseus-BERT——业内性能极致优化的BERT训练方案【阿里云弹性人工智能】
查看>>
酷狗音乐快速转换MP3格式的方法
查看>>
原生JS 实现复杂对象深拷贝(对象值包含函数)
查看>>
优化体系结构 - 算法外置优化计算结构
查看>>
你应该知道的数据库数据类型及其设计原则
查看>>
解决vue报错Failed to mount component
查看>>
[LeetCode] 124. Binary Tree Maximum Path Sum
查看>>
活学活用! 用Local Storage实现多人聊天室
查看>>
一次爬虫实践记录
查看>>
炫酷粒子表白,双十一脱单靠它了!
查看>>
mysql锁以及实践总结
查看>>
【工具】MongoDB 与可视化工具 adminMongo 的安装、启动与连接
查看>>
Javascript--常用方法
查看>>
Swoft之服务注册发现Consul服务器配置
查看>>
[译]迁移到新的 React Context Api
查看>>