ホームページ  >  記事  >  Java  >  Springboot が複数の Redis 接続を構成する方法の詳細な分析

Springboot が複数の Redis 接続を構成する方法の詳細な分析

Y2J
Y2Jオリジナル
2017-05-04 10:18:303795ブラウズ

Spring Boot は、Redis、MongoDB、Elasticsearch、Solr、Gemfire の自動構成を提供します。この記事では、springboot での複数の Redis 接続の構成について詳しく紹介します。興味がある方は詳細をご覧ください。

1. springboot nosql はじめに

Spring Data は、MongoDB、Neo4J、Elasticsearch、Solr、Redis、Gemfire、Couchbase、Cassandra など、さまざまな NoSQL テクノロジーの使用を支援する他のプロジェクトを提供します。 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 を追加すると、デフォルトの @Bean が置き換えられます (RedisTemplate の場合を除き、タイプではなく Bean の名前「redisTemplate」に基づいて除外されます)。 commons-pool2 がクラスパスに存在する場合、デフォルトで接続プール ファクトリが取得されます。

1.3 複数のredis接続を確立する

redisの0ライブラリに接続するには、redisのデフォルト設定を使用します。ライブラリ接続を指定する場合、接続する必要がある場合は、同時にindexdbを設定する必要があります。複数の Redis サービスに接続するには、application.yml ファイルに追加された複数の Redis サービス

1.3.1 を同時に構成する必要があります:

@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 を宣言して、それを親クラスに渡すだけです。

以上がSpringboot が複数の Redis 接続を構成する方法の詳細な分析の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。