Detail springboot configuration multiple redis connections

  • 2020-07-21 07:54:26
  • OfStack

Introduction to springboot nosql

Spring Data offers other programs to help you use a variety of 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 advantage of other projects, but you need to configure them yourself.

1.1, Redis

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


   <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 connect Redis

You can inject an auto-configured RedisConnectionFactory, StringRedisTemplate, or regular RedisTemplate instance that is the same as other Spring Bean. By default, this instance will attempt 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 1 of your own @Bean of any auto-configuration type, it will replace the default (except in the case of RedisTemplate, which is excluded by the name of bean 'redisTemplate' rather than its type). If es67EN-pool2 exists under the classpath path, you will get a connection pool factory by default.

1.3 Establish multiple redis connections

Using the default configuration of redis, you can connect to the zero library in redis, configuring indexdb if you specify library connections, and multiple data sources simultaneously if you need to connect to multiple redis services

1.3.1. Add in application. yml:


@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)); 
    //  Initializing connection 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 redis abstract base class and create 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 a different subclass @Component according to the data source


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

ps: Both the getTemple method and the StringRedisTemple attribute are declared in the class and the subclass. The subclass overrides the getTeimple method of the parent class and passes the subclass's own StringRedisTemple attribute to the parent class. The parent class USES the StringRedisTemple passed by the subclass to operate the cache using invalid data links. At this point, the parent class completes all the operation methods, and when it needs to create a database connection, it only needs to create a subclass, declare its own StringRedisTemple, and pass it to the parent class.


Related articles: