springboot学习笔记-4 整合Druid数据源和使用@Cache简化redis配置
一.整合Druid数据源
Druid是一个关系型数据库连接池,是阿里巴巴的一个开源项目,Druid在监控,可扩展性,稳定性和性能方面具有比较明显的优势.通过Druid提供的监控功能,可以实时观察数据库连接池和SQL查询的工作情况.使用Druid在一定程度上可以提高数据库的访问技能.
1.1 在pom.xml中添加依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.18</version> </dependency>
1.2 Druid数据源配置
在application.properties中,去书写Druid数据源的配置信息.
##Druid## spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8 spring.datasource.username=root spring.datasource.password=root spring.datasource.initialSize=5 spring.datasource.minIdle=5 spring.datasource.maxActive=20 spring.datasource.maxWait=60000 spring.datasource.timeBetweenEvictionRunsMillis=60000 spring.datasource.minEvictableIdleTimeMillis=300000 spring.datasource.validationQuery=SELECT 1 FROM DUAL spring.datasource.testWhileIdle=true spring.datasource.testOnBorrow=false spring.datasource.testOnReturn=false spring.datasource.poolPreparedStatements=true spring.datasource.maxPoolPreparedStatementPerConnectionSize=20 spring.datasource.filters=stat,wall,log4j spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 spring.datasource.useGlobalDataSourceStat=true
1.3 建立DruidConfiguration配置类,配置过滤信息
@Configuration public class DruidConfiguration { @Bean public ServletRegistrationBean statViewServle(){ ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*"); //白名单: servletRegistrationBean.addInitParameter("allow","192.168.1.218,127.0.0.1"); //IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的即提示:Sorry, you are not permitted to view this page. servletRegistrationBean.addInitParameter("deny","192.168.1.100"); //登录查看信息的账号密码. servletRegistrationBean.addInitParameter("loginUsername","druid"); servletRegistrationBean.addInitParameter("loginPassword","12345678"); //是否能够重置数据. servletRegistrationBean.addInitParameter("resetEnable","false"); return servletRegistrationBean; } @Bean public FilterRegistrationBean statFilter(){ FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter()); //添加过滤规则. filterRegistrationBean.addUrlPatterns("/*"); //添加不需要忽略的格式信息. filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return filterRegistrationBean; } }
1.4 配置数据源的信息
告诉springboot采用Druid数据源:
@Bean(name = "dataSource") @Primary @ConfigurationProperties(prefix = "spring.datasource") public DataSource dataSource(){ return DataSourceBuilder.create().type(com.alibaba.druid.pool.DruidDataSource.class).build(); }
接下来就可以通过localhost:8080/druid/index.html去打开控制台,观察过滤信息了!
二.使用@Cache简化redis配置
在实体类比较简单的时候(例如:没有一对多,多对多这类复杂的关系,不是List,Map这类数据类型,只是一个Pojo类),可以使用@Cache去替代书写BeanRedis注入RedisTemplate的方式去访问Redis数据库.
2.1 建立RoleService.采用@Cacheable和@CachePut去访问Redis
实体类的主要属性如下:
@Service public class RoleService { @Autowired private RoleRepository roleRepository; @Autowired private RoleRedis roleRedis; @Cacheable(value = "mysql:findById:role", keyGenerator = "simpleKey") public Role findById(Long id) { System.out.println("从数据库中查询"); return roleRepository.findOne(id); } @CachePut(value = "mysql:findById:role", keyGenerator = "objectId") public Role create(Role role) { System.out.println("************在数据库中创建************"); return roleRepository.save(role); } //简单的操作使用注解的形式 @CachePut(value = "mysql:findById:role", keyGenerator = "objectId") public Role update(Role role) { System.out.println("************在数据库中更新************"); return roleRepository.save(role); } @CacheEvict(value = "mysql:findById:role", keyGenerator = "simpleKey") public void delete(Long id) { System.out.println("************在数据库中销毁************"); roleRepository.delete(id); } public List<Role> findAll(){ List<Role> roleList = roleRedis.getList("mysql:findAll:role"); if(roleList == null) { roleList = roleRepository.findAll(); if(roleList != null) roleRedis.add("mysql:findAll:role", 5L, roleList); } return roleList; } //复杂的依然使用RedisTemplate的形式 public Page<Role> findPage(RoleQo roleQo){ Pageable pageable = new PageRequest(roleQo.getPage(), roleQo.getSize(), new Sort(Sort.Direction.ASC, "id")); PredicateBuilder pb = new PredicateBuilder(); if (!StringUtils.isEmpty(roleQo.getName())) { pb.add("name","%" + roleQo.getName() + "%", LinkEnum.LIKE); } Page<Role> pages = roleRepository.findAll(pb.build(), Operator.AND, pageable); return pages; } }
注意,@Cacheable放置于方法上,当调用这个方法时,程序会先根据key(value加上KeyGenerator生成的key)去寻找有没有对应的对象结果,如果有的话,直接返回,没有才执行方法(从数据库找).而@CachePut则会更新Redis缓存,同时执行对应的方法.@CacheEvict会删除Redis缓存对应的对象.
2.2 建立Redis配置类,需要去配置Redis的注解
@ConfigurationProperties("application.properties") @Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport { @Value("${spring.redis.hostName}") private String hostName; @Value("${spring.redis.port}") private Integer port; @Bean public RedisConnectionFactory redisConnectionFactory() { JedisConnectionFactory cf = new JedisConnectionFactory(); cf.setHostName(hostName); cf.setPort(port); cf.afterPropertiesSet(); return cf; } //配置key的生成 @Bean public KeyGenerator simpleKey(){ return new KeyGenerator() { @Override public Object generate(Object target, Method method, Object... params) { StringBuilder sb = new StringBuilder(); sb.append(target.getClass().getName()+":"); for (Object obj : params) { sb.append(obj.toString()); } return sb.toString(); } }; } //配置key的生成 @Bean public KeyGenerator objectId(){ return new KeyGenerator() { @Override public Object generate(Object target, Method method, Object... params){ StringBuilder sb = new StringBuilder(); sb.append(target.getClass().getName()+":"); try { sb.append(params[0].getClass().getMethod("getId", null).invoke(params[0], null).toString()); }catch (NoSuchMethodException no){ no.printStackTrace(); }catch(IllegalAccessException il){ il.printStackTrace(); }catch(InvocationTargetException iv){ iv.printStackTrace(); } return sb.toString(); } }; } //配置注解 @Bean public CacheManager cacheManager(@SuppressWarnings("rawtypes") RedisTemplate redisTemplate) { RedisCacheManager manager = new RedisCacheManager(redisTemplate); manager.setDefaultExpiration(43200);//12小时 return manager; } //对于复杂的属性仍然使用RedisTemplate @Bean public RedisTemplate<String, String> redisTemplate( RedisConnectionFactory factory) { StringRedisTemplate template = new StringRedisTemplate(factory); 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); template.setValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); return template; } }
2.3 测试