Home >Java >javaTutorial >In-depth analysis of how springboot configures multiple redis connections

In-depth analysis of how springboot configures multiple redis connections

Y2J
Y2JOriginal
2017-05-04 10:18:303975browse

Spring Boot provides automatic configuration for Redis, MongoDB, Elasticsearch, Solr and Gemfire. This article introduces in detail the configuration of multiple redis connections in springboot. Those who are interested can learn more.

1. springboot nosql Introduction

Spring Data provides other projects to help you use various NoSQL technologies including MongoDB, Neo4J, Elasticsearch, Solr, Redis, Gemfire, Couchbase and Cassandra. Spring Boot provides automatic configuration for Redis, MongoDB, Elasticsearch, Solr and Gemfire. You can take full advantage of other projects, but you need to configure them yourself.

1.1. Redis

Redis is a cache, message middleware and key-value storage system with rich features . Spring Boot provides automatic configuration for the Jedis client library and the Jedis client-based abstractions provided by Spring Data Redis. spring-boot-starter-redis 'Starter POM' provides a convenient way to collect dependencies.
Redis adds maven dependency

   <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 Connecting to Redis

You can inject an automatically configured RedisConnectionFactory, StringRedisTemplate or an ordinary RedisTemplate instance that is the same as other Spring Beans . By default, this instance will try to connect to the Redis server using localhost:6379.

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

If you add a @Bean of your own of any auto-configuration type, it will replace the default one (except in the case of RedisTemplate, which is excluded based on the bean's name 'redisTemplate' rather than its type ). If commons-pool2 exists in the classpath, you will get a connection pool factory by default.

1.3 Establish multiple redis connections

Use the default configuration of redis to connect to library 0 in redis. If you specify a library connection, you need to configure indexdb. At the same time, if you need to connect to multiple redis services, you also need to configure multiple data sources at the same time

1.3.1. Add:

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

1.3.2. Create 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. Declare the redis abstract base class and create the redis operation method

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. Create different subclasses according to the data source @Component

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

ps: The getTemple method and StringRedisTemple attribute are declared in the class and subclass at the same time. The subclass passes the subclass's own StringRedisTemple attribute to the parent class by overriding the getTeimple method of the parent class, and the parent class passes it through the subclass. The StringRedisTemple uses different data links to operate the cache. At this point, the parent class has completed all operation methods, and when you need to create a database connection, you only need to create a subclass, declare its own StringRedisTemple, and pass it to the parent class.

The above is the detailed content of In-depth analysis of how springboot configures multiple redis connections. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn