• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Java > 详解springboot配置多个redis连接

详解springboot配置多个redis连接

作者:十丿四 字体:[增加 减小] 来源:互联网 时间:2017-05-28

十丿四 通过本文主要向大家介绍了springboot详解,springboot注解详解,springboot redis,springboot整合redis,springboot 集成redis等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

一、springboot nosql 简介

Spring Data提供其他项目,用来帮你使用各种各样的NoSQL技术,包括MongoDB, Neo4J, Elasticsearch, Solr, Redis,Gemfire, Couchbase和Cassandra。Spring Boot为Redis, MongoDB, Elasticsearch, Solr和Gemfire提供自动配置。你可以充分利用其他项目,但你需要自己配置它们。

1.1、Redis

Redis是一个缓存,消息中间件及具有丰富特性的键值存储系统。Spring Boot为Jedis客户端库和由Spring Data Redis提供的基于Jedis客户端的抽象提供自动配置。 spring-boot-starter-redis 'Starter POM'为收集依赖提供一种便利的方式。
Redis添加maven依赖

   <dependency> 
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-test</artifactId> 
  <scope>test</scope> 
</dependency> 
<dependency> 
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter</artifactId> 
  <!-- <version>1.3.5.RELEASE</version> --> 
</dependency> 
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-commons --> 
<dependency> 
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-redis</artifactId> 
  <!-- <version>1.3.6.RELEASE</version> --> 
</dependency> 
</div>

1.2连接Redis

你可以注入一个自动配置的RedisConnectionFactory,StringRedisTemplate或普通的跟其他Spring Bean相同的RedisTemplate实例。默认情况下,这个实例将尝试使用localhost:6379连接Redis服务器。

@Component 
public class MyBean { 
private StringRedisTemplate template; 
@Autowired 
public MyBean(StringRedisTemplate template) { 
this.template = template; 
} 
// ... 
} 
</div>

如果你添加一个你自己的任何自动配置类型的@Bean,它将替换默认的(除了RedisTemplate的情况,它是根据bean的名称'redisTemplate'而不是它的类型进行排除的)。如果在classpath路径下存在commons-pool2,默认你会获得一个连接池工厂。

1.3 建立多个redis连接

使用redis的默认配置可以连接到redis中的0库中,如果指定库连接需要配置indexdb,同时如果需要连接多个redis服务,也需要同时配置多个数据源

1.3.1、application.yml 文件 中增加:

@Component 
public class MyBean { 
private StringRedisTemplate template; 
@Autowired 
public MyBean(StringRedisTemplate template) { 
this.template = template; 
} 
// ... 
} 
</div>

1.3.2、创建redisconfiguration

@Configuration 
public class Redis137_11Configuration { 
 
  @Bean(name = "redis123Template") 
  public StringRedisTemplate redisTemplate( 
      @Value("${redis123.hostName}") String hostName, 
      @Value("${redis123.port}") int port, 
      @Value("${redis123.password}") String password, 
      @Value("${redis123.maxIdle}") int maxIdle, 
      @Value("${redis123.maxTotal}") int maxTotal, 
      @Value("${redis123.index}") int index, 
      @Value("${redis123.maxWaitMillis}") long maxWaitMillis, 
      @Value("${redis123.testOnBorrow}") boolean testOnBorrow) { 
    StringRedisTemplate temple = new StringRedisTemplate(); 
    temple.setConnectionFactory(connectionFactory(hostName, port, password, 
        maxIdle, maxTotal, index, maxWaitMillis, testOnBorrow)); 
 
    return temple; 
  } 
 
  public RedisConnectionFactory connectionFactory(String hostName, int port, 
      String password, int maxIdle, int maxTotal, int index, 
      long maxWaitMillis, boolean testOnBorrow) { 
    JedisConnectionFactory jedis = new JedisConnectionFactory(); 
    jedis.setHostName(hostName); 
    jedis.setPort(port); 
    if (!StringUtils.isEmpty(password)) { 
      jedis.setPassword(password); 
    } 
    if (index != 0) { 
      jedis.setDatabase(index); 
    } 
    jedis.setPoolConfig(poolCofig(maxIdle, maxTotal, maxWaitMillis, 
        testOnBorrow)); 
    // 初始化连接pool 
    jedis.afterPropertiesSet(); 
    RedisConnectionFactory factory = jedis; 
 
    return factory; 
  } 
 
  public JedisPoolConfig poolCofig(int maxIdle, int maxTotal, 
      long maxWaitMillis, boolean testOnBorrow) { 
    JedisPoolConfig poolCofig = new JedisPoolConfig(); 
    poolCofig.setMaxIdle(maxIdle); 
    poolCofig.setMaxTotal(maxTotal); 
    poolCofig.setMaxWaitMillis(maxWaitMillis); 
    poolCofig.setTestOnBorrow(testOnBorrow); 
    return poolCofig; 
  } 
} 
</div>

1.3.3、声明redis抽象基类,创建redis的操作方法

public abstract class AbRedisConfiguration { 
  protected StringRedisTemplate temple; 
 
  public void setData(String key, String value) { 
    getTemple().opsForValue().set(key, value); 
  } 
 
  public String getData(String key) { 
    return getTemple().opsForValue().get(key); 
  } 
 
  public StringRedisTemplate getTemple() { 
    return temple; 
  } 
} 

</div>

1.3.4、根据数据源,创建不同的子类@Component

public class Redis123 extends AbRedisConfiguration { 
 
  @Resource(name = "redis123Template") 
  private StringRedisTemplate temple; 
 
  public StringRedisTemplate getTemple() { 
    return temple; 
  } 
} 
</div>

ps:类和子类中同时声明了getTemple方法和 StringRedisTemple属性,子类通过重写父类的getTeimple方法,把子类的自己StringRedisTemple 属性 传给 父类,父类通过子类传递过来的StringRedisTemple使用不通的数据链接来操作缓存。至此,父类完成所有的操作方法,而当需要创建一个数据库连接时,只需要在创建一个子类,被声明自己的StringRedisTemple,并传给父类即可。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • springboot整合freemarker详解
  • 详解SpringBoot AOP 拦截器(Aspect注解方式)
  • 详解SpringBoot配置devtools实现热部署
  • springboot全局异常处理详解
  • 详解springboot + profile(不同环境读取不同配置)
  • springboot中thymeleaf模板使用详解
  • 详解springboot+mybatis多数据源最简解决方案
  • 详解springboot整合mongodb
  • 详解在SpringBoot应用中获取应用上下文方法
  • 详解springboot配置多个redis连接

相关文章

  • 2017-05-28Java中的对象和引用详解
  • 2017-07-23Java并发编程实践:Callable异步回调Future、FutureTask用法
  • 2017-05-28Java创建内部类对象实例详解
  • 2017-05-28Java中使用fileupload组件实现文件上传功能的实例代码
  • 2017-05-28eclipse/intellij idea 查看java源码和注释方法
  • 2017-05-28详解Java 中程序内存的分析
  • 2017-05-28Java 中的HashMap详解和使用示例_动力节点Java学院整理
  • 2017-05-28java文件操作之Path,Paths,Files
  • 2017-05-28C# 中Excel导入时判断是否被占用三种方法
  • 2017-05-28Java中SpringSecurity密码错误5次锁定用户的实现方法

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • 判断二叉树是否为完全二叉树的实例
    • java实现对服务器的自动巡检邮件通知
    • SWT JFace Bookmark 制作
    • springboot整合freemarker详解
    • Java微信公众平台开发(11) 微信三大平台的关联
    • Java实现纪元秒和本地日期时间互换的方法【经典实例】
    • java 中 阻塞队列BlockingQueue详解及实例
    • 详解在SpringBoot应用中获取应用上下文方法
    • 详解SpringBoot配置连接池
    • Eclipse 开发java 出现Failed to create the Java Virtual Machine错误解决办法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有