博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot整合redis
阅读量:6251 次
发布时间:2019-06-22

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

 

一.整合Druid数据源

  Druid是一个关系型数据库连接池,是阿里巴巴的一个开源项目,Druid在监控,可扩展性,稳定性和性能方面具有比较明显的优势.通过Druid提供的监控功能,可以实时观察数据库连接池和SQL查询的工作情况.使用Druid在一定程度上可以提高数据库的访问技能.

  1.1 在pom.xml中添加依赖

com.alibaba
druid
1.0.18

  1.2 Druid数据源配置

  在application.properties中,去书写Druid数据源的配置信息.

##Druid##spring.datasource.type=com.alibaba.druid.pool.DruidDataSourcespring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8spring.datasource.username=rootspring.datasource.password=rootspring.datasource.initialSize=5spring.datasource.minIdle=5spring.datasource.maxActive=20spring.datasource.maxWait=60000spring.datasource.timeBetweenEvictionRunsMillis=60000spring.datasource.minEvictableIdleTimeMillis=300000spring.datasource.validationQuery=SELECT 1 FROM DUALspring.datasource.testWhileIdle=truespring.datasource.testOnBorrow=falsespring.datasource.testOnReturn=falsespring.datasource.poolPreparedStatements=truespring.datasource.maxPoolPreparedStatementPerConnectionSize=20spring.datasource.filters=stat,wall,log4jspring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000spring.datasource.useGlobalDataSourceStat=true

  1.3 建立DruidConfiguration配置类,配置过滤信息

@Configurationpublic 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

  实体类的主要属性如下:

2.1 建立Redis配置类,需要去配置Redis的注解

@ConfigurationProperties("application.properties")@Configuration@EnableCachingpublic 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
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.2
  • 编写redis的service类操作edis数据库

package cn.springboot.mybatis.service;

import java.io.Serializable;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

@Service

public class RedisService {
@Autowired
private RedisTemplate redisTemplate;
/**
* 写入缓存
* @param key
* @param value
* @return
*/
public boolean set(final String key, Object value) {
boolean result = false;
try {
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(key, value);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 写入缓存设置时效时间
* @param key
* @param value
* @return
*/
public boolean set(final String key, Object value, Long expireTime) {
boolean result = false;
try {
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(key, value);
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 批量删除对应的value
* @param keys
*/
public void remove(final String... keys) {
for (String key : keys) {
remove(key);
}
}

/**

* 批量删除key
* @param pattern
*/
public void removePattern(final String pattern) {
Set<Serializable> keys = redisTemplate.keys(pattern);
if (keys.size() > 0)
redisTemplate.delete(keys);
}
/**
* 删除对应的value
* @param key
*/
public void remove(final String key) {
if (exists(key)) {
redisTemplate.delete(key);
}
}
/**
* 判断缓存中是否有对应的value
* @param key
* @return
*/
public boolean exists(final String key) {
return redisTemplate.hasKey(key);
}
/**
* 读取缓存
* @param key
* @return
*/
public Object get(final String key) {
Object result = null;
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
result = operations.get(key);
return result;
}
/**
* 哈希 添加
* @param key
* @param hashKey
* @param value
*/
public void hmSet(String key, Object hashKey, Object value){
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
hash.put(key,hashKey,value);
}

/**

* 哈希获取数据
* @param key
* @param hashKey
* @return
*/
public Object hmGet(String key, Object hashKey){
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
return hash.get(key,hashKey);
}

/**

* 列表添加
* @param k
* @param v
*/
public void lPush(String k,Object v){
ListOperations<String, Object> list = redisTemplate.opsForList();
list.rightPush(k,v);
}

/**

* 列表获取
* @param k
* @param l
* @param l1
* @return
*/
public List<Object> lRange(String k, long l, long l1){
ListOperations<String, Object> list = redisTemplate.opsForList();
return list.range(k,l,l1);
}

/**

* 集合添加
* @param key
* @param value
*/
public void add(String key,Object value){
SetOperations<String, Object> set = redisTemplate.opsForSet();
set.add(key,value);
}

/**

* 集合获取
* @param key
* @return
*/
public Set<Object> setMembers(String key){
SetOperations<String, Object> set = redisTemplate.opsForSet();
return set.members(key);
}

/**

* 有序集合添加
* @param key
* @param value
* @param scoure
*/
public void zAdd(String key,Object value,double scoure){
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
zset.add(key,value,scoure);
}

/**

* 有序集合获取
* @param key
* @param scoure
* @param scoure1
* @return
*/
public Set<Object> rangeByScore(String key,double scoure,double scoure1){
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
return zset.rangeByScore(key, scoure, scoure1);
}
}

 

2.3 controller测试

@RestController

public class MessageController {
@Autowired
private RedisService redisService;
/* @RequestMapping(value = "/message",method = RequestMethod.GET)
@ResponseBody
public List<String> greeting(String user) {
List<String> messages = cacheService.listMessages(user);
return messages;
}*/
@RequestMapping(value = "/message1",method = RequestMethod.GET)
@ResponseBody
public String saveGreeting(String user,String message) {
String usestrr="hello";
String message1="world";
redisService.set("002", message1);
return "OK";
}
}

 

 

补充:

使用@Cache简化redis配置

  在实体类比较简单的时候(例如:没有一对多,多对多这类复杂的关系,不是List,Map这类数据类型,只是一个Pojo类),可以使用@Cache去替代书写BeanRedis注入RedisTemplate的方式去访问Redis数据库.

  2.1 建立RoleService.采用@Cacheable和@CachePut去访问Redis

  实体类的主要属性如下:

 

本例子没用使用该方法赶兴趣的可以试一下下面是源码

@Servicepublic 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
findAll(){ List
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
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
pages = roleRepository.findAll(pb.build(), Operator.AND, pageable); return pages; }}

 

 

 

 

 

本文章参考一下两篇博客

1

http://www.cnblogs.com/hlhdidi/p/6350306.html

2

http://www.cnblogs.com/haitao-xie/p/6323423.html

 

 

 

 

 
你可能感兴趣的文章
病人排序
查看>>
git-修改远程的URL以及强制覆盖本地文件
查看>>
升级fedora 18到fedora 19
查看>>
为什么getline()后要两次回车????(将输入的字符串按单词倒序输出)
查看>>
Dictionary和数组查找效率对比
查看>>
alias命令详情
查看>>
iOS - UITouch
查看>>
学习C++语言的50条忠告
查看>>
mysql的innodb中事务日志ib_logfile
查看>>
大数乘法?
查看>>
C语言博客作业03--函数
查看>>
96. Unique Binary Search Trees(I 和 II)
查看>>
飘窗原生js效果
查看>>
自定义异步加载资源插件
查看>>
Mongodb windows 安装
查看>>
easyui combobox两种不同的数据加载方式
查看>>
报错:该页必须具有 <%@ webservice class="MyNamespace.MyClass" ... %> 指令。
查看>>
Smarty配置与实例化
查看>>
***Redis hash是一个string类型的field和value的映射表.它的添加、删除操作都是O(1)(平均)。hash特别适合用于存储对象...
查看>>
抽象方法和接口区别
查看>>