Intellij IDEA 搭建Spring Boot项目(三)配置事务和Redis缓存
2018-09-12 14:49
453 查看
在application.peoperties文件中添加以下关于redis的相关配置
[code]#redis缓存的相关配置 #host redis.hostname=127.0.0.1 #redis端口号 默认6379 redis.port=6379 redis.database=0 redis.pool.maxActive=600 redis.pool.maxIdle=300 redis.pool.maxWait=3000 redis.pool.testOnBorrow=true
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
类似数据连接池的创建,创建RedisConfiguration配置类
[code] @Configuration public class RedisConfiguration { @Value("${redis.hostname}") private String hostname; @Value("${redis.port}") private int port; @Value("${redis.database}") private int database; @Value("${redis.pool.maxActive}") private int maxActive; @Value("${redis.pool.maxIdle}") private int maxIdle; @Value("${redis.pool.maxWait}") private long maxWait; @Value("${redis.pool.testOnBorrow}") private boolean testOnBorrow; @Autowired private JedisPoolConfig jedisPoolConfig; @Autowired private JedisPoolWriper jedisPoolWriper; @Autowired private JedisUtil jedisUtil; @Bean(name = "jedisPoolConfig") public JedisPoolConfig createJedisPoolConfig() { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); //控制一个pool可以分配的多少个jedis实例 jedisPoolConfig.setMaxTotal(maxActive); //连接池中最多可空闲的maxIdle个连接 这里取值20 //表示即使没有数据库连接时候依然可以保持20个空闲的连接 而不被清除 随时处于待命状态 jedisPoolConfig.setMaxIdle(maxIdle); //最大等待时间:当没有可以用的连接的时候 连接池等待连接被归还的最大时间(以毫秒技术) 超过时间则抛出异常 jedisPoolConfig.setMaxWaitMillis(maxWait); //在获取连接的时候检查有效性 jedisPoolConfig.setTestOnBorrow(testOnBorrow); return jedisPoolConfig; } @Bean(name = "jedisPoolWriper") public JedisPoolWriper createJedisPoolWriper() { JedisPoolWriper jedisPoolWriper = new JedisPoolWriper(jedisPoolConfig, hostname, port); return jedisPoolWriper; } /** * 创建Redis工具类 封装好Redis的连接以进行相关操作 * * @return */ @Bean(name = "jedisUtil") public JedisUtil createJedisUtil() { JedisUtil jedisUtil = new JedisUtil(); jedisUtil.setJedisPool(jedisPoolWriper); return jedisUtil; } @Bean(name = "jedisKeys") public JedisUtil.Keys createJedisKeys() { JedisUtil.Keys jedisKeys = jedisUtil.new Keys(); return jedisKeys; } @Bean(name = "jedisStrings") public JedisUtil.Strings createJedisStrings() { JedisUtil.Strings jedisStrings = jedisUtil.new Strings(); return jedisStrings; } @Bean(name = "jedisLists") public JedisUtil.Lists createJedisLists() { JedisUtil.Lists jedisLists = jedisUtil.new Lists(); return jedisLists; } @Bean(name = "jedisSets") public JedisUtil.Sets createJedisSets() { JedisUtil.Sets jedisSets = jedisUtil.new Sets(); return jedisSets; } @Bean(name = "jedisHash") public JedisUtil.Hash createJedisHash() { JedisUtil.Hash jedisHash = jedisUtil.new Hash(); return jedisHash; } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
上述两个类(事务和Redis配置类)的目录结构图如下图所示:
我们在service里面这样使用Redis缓存,代码如下:
[code] @Autowired private JedisUtil.Strings jedisStrings; // Redis的安装和配置 启动之后再来运行这个是可以的 也就是走缓存了 // https://www.jianshu.com/p/6b5eca8d908b @Autowired private JedisUtil.Keys jedisKeys; @Autowired private AreaDao areaDao; private static String AREALISTKEY = "arealist"; @Override public List<Area> getAreaList() throws IOException { String key = AREALISTKEY; List<Area> areaList = null; ObjectMapper mapper = new ObjectMapper(); //判断是否有缓存 if (!jedisKeys.exists(key)) { //没哟缓存 则查询数据库 areaList = areaDao.queryArea(); String jsonString = mapper.writeValueAsString(areaList); jedisStrings.set(key, jsonString); } else { //有缓存 这个时候从缓存中通过key获取到缓存的数据 然后转换为我们需要的数据 String jsonString = jedisStrings.get(key); JavaType javaType = mapper.getTypeFactory() .constructParametricType(ArrayList.class, Area.class); areaList = mapper.readValue(jsonString, javaType); } return areaList; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
测试service
[code] //测试类加入如下的两个注解 @RunWith(SpringRunner.class) @SpringBootTest public class AreaServiceTest { @Autowired private AreaService areaService; // Redis的安装和配置 启动之后再来运行这个是可以的 也就是走缓存了 // https://www.jianshu.com/p/6b5eca8d908b @Test public void testGetAreaList() throws IOException { List<Area> areaList = areaService.getAreaList(); System.out.println("查询出来的地区集合的个数是:" + areaList.size()); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
我们看下输入的结果:
三、遇到的bug
刚开始没了解redis是什么,只是感觉是是一个缓存,并没想到需要各种配置–>导致没有没有开启redis–server,在测试的时候会报
Could not get a resource from the pool的错误,在配置好redis的相关之后并启动之后再次运行时可以正常运行的。
springboot测试项目地址
- https://github.com/wuyinlei/springtest
- https://github.com/wuyinlei/springtest
- https://github.com/wuyinlei/springtest
相关链接文章
阅读更多相关文章推荐
- Intellij IDEA 搭建Spring Boot项目(三)配置事务和Redis缓存
- Intellij IDEA 搭建Spring Boot项目(一)
- spring-boot项目搭建及mysql配置实例
- IntelliJ IDEA搭建Springboot项目解决热加载问题
- Intellij IDEA 搭建Spring Boot项目(二)spring-dao的相关配置
- 1.2 使用IntelliJ IDEA搭建Spring-Boot与MongoDB数据的简单登录项目
- IntelliJ IDEA 配置Gradle运行SpringBoot Web项目(tomcat容器)
- (四)从零搭建maven+spring+mybatis项目之mybatis与事务配置
- IntelliJ IDEA 配置Gradle运行SpringBoot Web项目(tomcat容器)
- SpringBoot1-spirng项目快速搭建+maven+spring的基本配置
- 在IntelliJ IDEA下搭建springboot项目
- VS Code打开使用IDEA搭建的Spring Boot项目运行提示"snakeyaml was not found on the classpath"错误
- SpringBoot项目在IntelliJ IDEA中实现热部署
- springBoot快速搭建项目
- 【死磕springboot2.0】idea中springboot项目进行热部署配置。
- SpringBoot + MyBatis + MySQL + Maven在Intellij IDEA下开发环境的搭建
- Springboot整合dubbo构建maven多模块项目(一)- 项目创建和pom.xml中jar包配置
- 【项目搭建】使用spring-boot进行REST风格的微服务开发入门
- Spring Boot 多库分布式事务,最简配置,没有之一
- Springboot 项目pom.xml 配置文件以及启动类的一些配置