当前位置: 移动技术网 > IT编程>开发语言>Java > 详解springboot配置多个redis连接

详解springboot配置多个redis连接

2019年07月22日  | 移动技术网IT编程  | 我要评论

一、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> 

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; 
} 
// ... 
} 

如果你添加一个你自己的任何自动配置类型的@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; 
} 
// ... 
} 

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; 
  } 
} 

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; 
  } 
} 

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

public class redis123 extends abredisconfiguration { 
 
  @resource(name = "redis123template") 
  private stringredistemplate temple; 
 
  public stringredistemplate gettemple() { 
    return temple; 
  } 
} 

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

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网