设为首页 收藏本站
查看: 692|回复: 0

[经验分享] SpringBoot 中使用redis以及redisTemplate

[复制链接]

尚未签到

发表于 2017-12-21 14:00:47 | 显示全部楼层 |阅读模式
  1.首先在pom.xml中添加依赖
  <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
  <version>1.5.2.RELEASE</version>
  </dependency>
  <dependency>
  <groupId>redis.clients</groupId>
  <artifactId>jedis</artifactId>
  </dependency>
  <dependency>
  <groupId>org.springframework.session</groupId>  
  <artifactId>spring-session-data-redis</artifactId>  
  </dependency>
  2.在git上的配置文件中添加redis集群配置
  spring.redis.cluster.nodes=10.10.10.236:7001,10.10.10.236:7002,10.10.10.236:7003,10.10.10.236:7004,10.10.10.236:7005,10.10.10.236:7006
  spring.redis.cluster.host=10.10.10.236
  spring.redis.cluster.port=7001
  spring.redis.cluster.passwd=
  spring.redis.cluster.timeOut=2000
  spring.redis.cluster.max-redirects=8
  3.在程序中读取配置文件中的值
  package cn.lz.auth.redis;
  import java.util.HashMap;
  import java.util.Map;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.core.env.MapPropertySource;
  import org.springframework.data.redis.connection.RedisClusterConfiguration;
  import org.springframework.data.redis.connection.RedisConnectionFactory;
  import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
  import org.springframework.data.redis.core.RedisTemplate;
  import org.springframework.data.redis.serializer.RedisSerializer;
  import org.springframework.data.redis.serializer.SerializationException;
  import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
  //@Component
  @EnableRedisHttpSession
  @Configuration

  public>  {
  @Value("${spring.redis.cluster.nodes}")
  private String clusterNodes;
  @Value("${spring.redis.cluster.host}")
  private String redisHost;
  @Value("${spring.redis.cluster.port}")
  private int redisPort;
  @Value("${spring.redis.cluster.passwd}")
  private String redisPasswd;
  @Value("${spring.redis.cluster.timeOut}")
  private int timeOut = 2000;
  @Value("${spring.redis.cluster.max-redirects}")
  private int redirects = 8;
  @Bean
  public RedisClusterConfiguration getClusterConfiguration()
  {
  Map<String, Object> source = new HashMap<String, Object>();
  source.put("spring.redis.cluster.nodes", clusterNodes);
  source.put("spring.redis.cluster.timeout", timeOut);
  source.put("spring.redis.cluster.max-redirects", redirects);
  return new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source));
  }
  @Bean
  public RedisConnectionFactory redisConnectionFactory()
  {
  JedisConnectionFactory cf = null;
  if( clusterNodes != null && !clusterNodes.isEmpty() ){
  cf = new JedisConnectionFactory( getClusterConfiguration() );
  }
  else{
  cf = new JedisConnectionFactory();
  cf.setHostName( redisHost );
  cf.setPort( redisPort );
  cf.setPassword( redisPasswd);
  cf.setTimeout( timeOut );
  }
  cf.afterPropertiesSet();
  return cf;
  }
  @Bean
  public RedisTemplate<String, Object> redisTemplate()
  {
  RedisTemplate<String, Object> rt = new RedisTemplate<String, Object>();
  rt.setConnectionFactory( redisConnectionFactory() );
  return rt;
  }
  public static enum StringSerializer implements RedisSerializer<String>
  {
  INSTANCE;
  @Override
  public byte[] serialize(String s) throws SerializationException
  {
  return (null != s ? s.getBytes() : new byte[0]);
  }
  @Override
  public String deserialize(byte[] bytes) throws SerializationException
  {
  if (bytes.length > 0){
  return new String(bytes);
  }
  else{
  return null;
  }
  }
  }
  public static enum LongSerializer implements RedisSerializer<Long>
  {
  INSTANCE;
  @Override
  public byte[] serialize(Long aLong) throws SerializationException
  {
  if (null != aLong){
  return aLong.toString().getBytes();
  }
  else{
  return new byte[0];
  }
  }
  @Override
  public Long deserialize(byte[] bytes) throws SerializationException
  {
  if (bytes.length > 0){
  return Long.parseLong(new String(bytes));
  }
  else{
  return null;
  }
  }
  }
  public static enum IntSerializer implements RedisSerializer<Integer>
  {
  INSTANCE;
  @Override
  public byte[] serialize(Integer i) throws SerializationException
  {
  if (null != i){
  return i.toString().getBytes();
  }
  else{
  return new byte[0];
  }
  }
  @Override
  public Integer deserialize(byte[] bytes) throws SerializationException
  {
  if (bytes.length > 0){
  return Integer.parseInt(new String(bytes));
  }
  else{
  return null;
  }
  }
  }
  }
  4.根据redisTemplate编写set,get,delete方法
  package cn.lz.auth.redis;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.data.redis.core.RedisTemplate;
  import org.springframework.data.redis.core.ValueOperations;
  import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
  import org.springframework.stereotype.Component;
  @Component

  public>  {
  @Autowired
  private RedisTemplate<String, Object> redis;

  public void setObject( String uuid, T object,>  {
  redis.setKeySerializer( RedisConfig.StringSerializer.INSTANCE );
  redis.setValueSerializer( new Jackson2JsonRedisSerializer<T>(type) );
  redis.afterPropertiesSet();
  ValueOperations<String, Object> ops = redis.opsForValue();
  ops.set( uuid, object );
  }
  @SuppressWarnings("unchecked")

  public T getObject( String uuid,>  {
  redis.setKeySerializer( RedisConfig.StringSerializer.INSTANCE );
  redis.setValueSerializer( new Jackson2JsonRedisSerializer<T>(type) );
  redis.afterPropertiesSet();
  ValueOperations<String, Object> ops = redis.opsForValue();
  return (T)ops.get( uuid );
  }
  public void delObject( String uuid )
  {
  redis.setKeySerializer( RedisConfig.StringSerializer.INSTANCE );
  redis.afterPropertiesSet();
  redis.delete( uuid );
  }
  }
  5.调用set,get,delete方法操作redis中的数据
  好文链接:http://www.cnblogs.com/langtianya/p/5190201.html

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-426461-1-1.html 上篇帖子: 沙沙沙沙、半城烟 下篇帖子: laravel安装 redis 并驱动 session
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表