2015-12-10 72 views
9

私はSpring Redisをspring-data-redisで動作させ、すべてのデフォルト設定がlocalhostのデフォルトportなどとなっています。Spring Redis - application.propertiesファイルから設定を読み込みます

今、同じ設定をapplication.propertiesファイルで設定しようとしています。しかし、私はどのように私のプロパティ値が読み取られる正確に豆を作成する必要があるかわかりません。

Redisの設定application.propertiesに

@EnableRedisHttpSession 
@Configuration 
public class SpringSessionRedisConfiguration { 

@Bean 
JedisConnectionFactory connectionFactory() { 
    return new JedisConnectionFactory(); 
} 

@Autowired 
@Bean 
RedisCacheManager redisCacheManager(final StringRedisTemplate stringRedisTemplate) { 
    return new RedisCacheManager(stringRedisTemplate); 
} 

@Autowired 
@Bean 
StringRedisTemplate template(final RedisConnectionFactory connectionFactory) { 
    return new StringRedisTemplate(connectionFactory); 
} 
} 

標準的なパラメータをファイル

spring.redis.sentinel.master =ザ・マスター

spring.redis.sentinel.nodes = 192.168.188.231 :26379

spring.redis.password = 12345

私が試した何

  1. 私はおそらく@PropertySourceを使用して、@Valueを注入し、値を取得することができます。しかし私はそれらのプロパティが私によって定義されていないが、春からであるので、それをしたくありません。
  2. このドキュメントでは、Spring Redis Documentationは、プロパティを使用して構成できますが、具体的な例は示していません。
  3. 私もSpring Data Redis APIクラスを見て、RedisPropertiesが助けてくれるはずですが、Springにプロパティファイルから読み込む方法を正確に理解できないことがわかりました。
+0

現在使用してやって[値]注釈@、任意のより良い提案 –

+0

このコメントを書き込むことによって、疑問をぶつけための正しい方法であります: - ) –

答えて

17

@PropertySourceを使用して、application.propertiesまたは他のプロパティファイルからオプションを読み取ることができます。 PropertySource usage exampleとなり、example of usage spring-redis-cacheとなります。

現在の点に注意してください。(2015年12月application.propertiesspring.redis.sentinelオプションRedisSentinelConfigurationの限定的なサポートがあり、現時点では

@Configuration 
@PropertySource("application.properties") 
public class SpringSessionRedisConfiguration { 

    @Value("${redis.hostname}") 
    private String redisHostName; 

    @Value("${redis.port}") 
    private int redisPort; 

    @Bean 
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { 
     return new PropertySourcesPlaceholderConfigurer(); 
    } 

    @Bean 
    JedisConnectionFactory jedisConnectionFactory() { 
     JedisConnectionFactory factory = new JedisConnectionFactory(); 
     factory.setHostName(redisHostName); 
     factory.setPort(redisPort); 
     factory.setUsePool(true); 
     return factory; 
    } 

    @Bean 
    RedisTemplate<Object, Object> redisTemplate() { 
     RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>(); 
     redisTemplate.setConnectionFactory(jedisConnectionFactory()); 
     return redisTemplate; 
    } 

    @Bean 
    RedisCacheManager cacheManager() { 
     RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate()); 
     return redisCacheManager; 
    } 
} 

:それとも、この小さなサンプルを見てJedisとレタスLettuceのみRedis Sentinelをサポートしています。

詳しくは、official documentationを参照してください。

+0

答えていただきありがとうございます、これは私のアプローチでした私はこの質問をする前に使っていました。しかし、 'spring-boot-starter-data-jpa'の例を挙げましょう。ここでデータソースを設定するには' spring.datasource.url'プロパティを設定し、springはその魔法を設定するだけです。標準アプリケーションプロパティ(https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html)の1つであるためです。私たちはそれを読むための特定のロジックを書く必要はありません。そして、Redisのためにこれを行ういくつかの方法があります。また、Beanの作成方法かもしれません。 –

+0

はい、わかります。しかし、sentinelプロパティは、デフォルトのapplication.propertiesではサポートされていません。だから私は上記の方法を使用する必要があります。 – misterion

+1

'RedisSentinelConfiguration'には' PropertySource'を持つコンストラクタがあり、このコンストラクタはすでに2つのセンチネル固有のプロパティを読み込むためのロジックを持っています。私は今、どのように私の 'application.properties'の' PropertySource'を渡すことができるか把握しようとしています。 –

1

私は24の段落7

@Component 
@ConfigurationProperties(prefix="connection") 
public class ConnectionSettings { 

    private String username; 

    private InetAddress remoteAddress; 

    // ... getters and setters 

} 

プロパティは、その後connection.property

参考リンクを介して変更することができ、スプリングブーツドキュメントセクション内でこれを見つけた:私はこれを見つけた深い探しの際にhttps://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-typesafe-configuration-properties

+0

これは私が本当にしたい場合は、これを試して、あなたを更新します。 –

+0

動作しましたか、さらにサポートが必要ですか? –

+0

私はこのアプローチを試していませんでしたが、回答は私にとってはうまくいきましたが、回答がありましたら助けになるかもしれません –

3

それはあなたが探しているものかもしれませんか?

# REDIS (RedisProperties) 
spring.redis.database=0 # Database index used by the connection factory. 
spring.redis.host=localhost # Redis server host. 
spring.redis.password= # Login password of the redis server. 
spring.redis.pool.max-active=8 # Max number of connections that can be allocated by the pool at a given time. Use a negative value for no limit. 
spring.redis.pool.max-idle=8 # Max number of "idle" connections in the pool. Use a negative value to indicate an unlimited number of idle connections. 
spring.redis.pool.max-wait=-1 # Maximum amount of time (in milliseconds) a connection allocation should block before throwing an exception when the pool is exhausted. Use a negative value to block indefinitely. 
spring.redis.pool.min-idle=0 # Target for the minimum number of idle connections to maintain in the pool. This setting only has an effect if it is positive. 
spring.redis.port=6379 # Redis server port. 
spring.redis.sentinel.master= # Name of Redis server. 
spring.redis.sentinel.nodes= # Comma-separated list of host:port pairs. 
spring.redis.timeout=0 # Connection timeout in milliseconds. 

Refernce:https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html SEARCHTERM Redisの

私は値が既に存在して見ることができますし、独自のプロパティを作成したい場合は、あなたが私の前で見ることができ

spring.redis.host=localhost # Redis server host. 
spring.redis.port=6379 # Redis server port. 

として定義されているものから、このスレッドに投稿してください。

+1

これはあなたのリンクに記載されているプロパティのみで動作します。 'センチネル(sentinel)'の設定に変更します。また、 'sentinel'もデフォルトの春のプロパティですが、それでもフレームワークはそれらを読み込まず、接続が確立されません。 –

0
+0

これはあなたのリンクに記載されているプロパティに対してのみ機能します。私の質問は、 'sentinel'設定に関連するプロパティに固有です。また、 'sentinel'もデフォルトの春のプロパティですが、それでもフレームワークはそれらを読み込まず、接続が確立されません。 –

0

探しているものをあなたはPropertySourceオブジェクトを生成するためにResourcePropertySourceを使用することができると思います。

PropertySource propertySource = new ResourcePropertySource("path/to/your/application.properties"); 

次に、それをRedisSentinelConfigurationのコンストラクタに渡します。ここで

0
@Autowired 
private JedisConnectionFactory connectionFactory; 

@Bean 
JedisConnectionFactory connectionFactory() { 
    return connectionFactory 
} 
+0

これは答えではありません – sebadagostino

0

はあなたの問題を解決するためのエレガントなソリューションです:

ので
@Configuration 
@PropertySource(name="application", value="classpath:application.properties") 
public class SpringSessionRedisConfiguration { 

    @Resource 
    ConfigurableEnvironment environment; 

    @Bean 
    public PropertiesPropertySource propertySource() { 
     return (PropertiesPropertySource) environment.getPropertySources().get("application"); 
    } 

    @Bean 
    public JedisConnectionFactory jedisConnectionFactory() { 
     return new JedisConnectionFactory(sentinelConfiguration(), poolConfiguration()); 
    } 

    @Bean 
    public RedisSentinelConfiguration sentinelConfiguration() { 
     return new RedisSentinelConfiguration(propertySource()); 
    } 

    @Bean 
    public JedisPoolConfig poolConfiguration() { 
     JedisPoolConfiguration config = new JedisPoolConfiguration(); 
     // add your customized configuration if needed 
     return config; 
    } 

    @Bean 
    RedisTemplate<Object, Object> redisTemplate() { 
     RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>(); 
     redisTemplate.setConnectionFactory(jedisConnectionFactory()); 
     return redisTemplate; 
    } 

    @Bean 
    RedisCacheManager cacheManager() { 
     return new RedisCacheManager(redisTemplate()); 
    } 

} 

、再開するには:

  • @PropertySource
  • ための具体的な名前ではなくConfigurableEnvironmentを注入追加しますの環境
  • あなたのConfigurableEnvironmentでPropertiesPropertySourceを取得する私たちあなたはRedisSentinelConfigurationオブジェクト
  • は「spring.redis.sentinel.master」と 'spring.redis.sentinel.nodesのプロパティを追加することを忘れないでください構築するためにあなたの@PropertySource
  • 使用にこのPropertySourceオブジェクトを言及した名前をINGのあなたのプロパティファイルに

は、私のワークスペースでテスト よろしく

関連する問題