package com.example.demo;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import redis.clients.jedis.JedisPoolConfig;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
@Configuration
class RedisConfig {
@Bean
@Qualifier("jedisConnectionFactory")
public JedisConnectionFactory connectionFactory() {
RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
.master("test")
.sentinel("XX.XX.XX.XX", 6379);//Replace this with the private IPv4 address and port of your database instance
sentinelConfig.setPassword(RedisPassword.of("xxx"));//Replace this with your database access password
sentinelConfig.setSentinelPassword(RedisPassword.of("xxx"));//Replace this with your database access password
JedisPoolConfig poolConfig = new JedisPoolConfig();
JedisConnectionFactory connectionFactory = new JedisConnectionFactory(sentinelConfig, poolConfig);
connectionFactory.afterPropertiesSet();
return connectionFactory;
}
@Bean
@ConditionalOnBean(JedisConnectionFactory.class)
public RedisTemplate<String, String> redisTemplate(@Qualifier("jedisConnectionFactory") JedisConnectionFactory factory) {
RedisTemplate<String, String> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
template.afterPropertiesSet();
//test
template.opsForValue().set("test", "test1");
System.out.println("template.opsForValue().get(\\"test\\") = " + template.opsForValue().get("test"));
return template;
}
}
この記事はお役に立ちましたか?