spring boot 多redis的配置

  Seves

    一、首先要修改RedisConfig

    @Configuration

    @Component

    @EnableAutoConfiguration

    public class RedisConfig

    {

    private static Logger logger = LoggerFactory.getLogger(RedisConfig.class);

    @Value(“${spring.redis.hostName}”)

    String redisHosName;

    @Value(“${spring.redis.port}”)

    int redisPort;

    @Value(“${spring.redis.timeout}”)

    int redisTimeout;

    @Bean

    @ConfigurationProperties(prefix = “spring.redis”)

    public JedisPoolConfig getRedisConfig()

    {

    logger.info(“读取Redis配置信息”);

    JedisPoolConfig config = new JedisPoolConfig();

    return config;

    }

    /**

    • @Description 默认redis的JedisConnectionFactory的配置方式

    • @return

    */

    private JedisConnectionFactory getConnectionFactory()

    {

    logger.info(“getConnectionFactory初始化完成!”);

    JedisShardInfo jedisShardInfo = new JedisShardInfo(redisHosName, redisPort, redisTimeout);

    JedisConnectionFactory factory = new JedisConnectionFactory(jedisShardInfo);

    JedisPoolConfig config = getRedisConfig();

    factory.setPoolConfig(config);

    logger.info(“JedisConnectionFactory初始化完成!”);

    return factory;

    }

    /**

    • @Description 主要为多redis提供构建JedisConnectionFactory的方法

    • @param ip

    • @param port

    • @param timeout

    • @return

    */

    private JedisConnectionFactory getConnectionFactory(String ip, int port, int timeout)

    {

    logger.info(“getConnectionFactory初始化完成!”);

    JedisShardInfo jedisShardInfo = new JedisShardInfo(ip, port, timeout);

    JedisConnectionFactory factory = new JedisConnectionFactory(jedisShardInfo);

    JedisPoolConfig config = getRedisConfig();

    factory.setPoolConfig(config);

    logger.info(“JedisConnectionFactory初始化完成!”);

    return factory;

    }

    /**

    • @Description 主要为多redis提供构建RedisTemplate的方法

    • @param ip

    • @param port

    • @param timeout

    • @return

    */

    public RedisTemplate getRedisTemplate(String ip, int port, int timeout)

    {

    logger.info(“获取RedisTemplate.”);

    RedisTemplate template = new StringRedisTemplate(getConnectionFactory(ip, port, timeout));

    return template;

    }

    /**

    • @Description 默认redis的RedisTemplate的配置方式

    • @return

    */

    public RedisTemplate getRedisTemplate()

    {

    logger.info(“获取RedisTemplate.”);

    RedisTemplate template = new StringRedisTemplate(getConnectionFactory());

    return template;

    }

    /**

    • @Description 默认redis的getRedisServiceImp

    • @return

    */

    @Bean(name = “redis”)

    public RedisServiceImpl getRedisServiceImp()

    {

    return new RedisServiceImpl(getRedisTemplate());

    }
    }
    二、多redis配置

    @Configuration

    public class RedisSecondConfig

    {

    @Value(“${spring.redis2.hostName}”)

    String ip;

    @Value(“${spring.redis2.port}”)

    int port;

    @Value(“${spring.redis2.timeout}”)

    int timeout;

    }

    三、 重写RedisServiceImpl或者RedisTemplate

    @Component(“redis2”)

    public class RedisService1 extends RedisServiceImpl

    {

    /**

    • @Description 提供RedisService1默认的构造方式 在构造方法上使用@Autowired

    • 主要解决在这个构造器构造时RedisService1当前类里配置@Autowiede无效和@value无效

    • @param redisConfig

    • @param config

    */

    @Autowired

    public RedisService1(RedisConfig redisConfig, RedisSecondConfig config)

    {

    this.redisTemplate = redisConfig.getRedisTemplate(config.ip, config.port, config.timeout);

    }

    }

    四、使用时配置

    如果配置了多redis 读默认的redis 必须通过 @Resource(name=“redis”) 的方式去注入 不然会报错

    如果没有多redis配置,仍然可以使用@Autowired去注入

    @RequestMapping(“/test”)

    @RestController

    public class TestRedisController

    {

    /*

    • @Resource(name=“redis”) RedisServiceImpl service1;

    *

    • @Resource(name=“redis2”) RedisService1 service2;

    */

    @Autowired

    RedisServiceImpl service;

    @PostMapping(“/redisPut”)

    public String test()

    {

    for (int i = 0; i < 100; i++)

    {

    service.hset(“TestRedis2”, String.valueOf(i), i);

    /* service2.hset(“TestRedis2”, String.valueOf(i), i); */

    }

    return “success”;

    }

    }

    第二种方式

    一、新建一个sevice或者template继承默认配置

    public abstract class ManyRedisService extends RedisServiceImpl

    {

    protected RedisConfig config;

    protected String ip;

    protected int port;

    protected int timeout;

    @PostConstruct

    public void init()

    {

    initParam();

    this.redisTemplate=config.getRedisTemplate(ip, port, timeout);

    }

    /**

    • 初始化几个本地的参数

    */

    public abstract void initParam();

    }

    二、配置多redis sevice或者template

    @Component(“redis3”)

    public class RedisService2 extends ManyRedisService

    {

    @Value(“${spring.redis2.hostName}”)

    String ip;

    @Value(“${spring.redis2.port}”)

    int port;

    @Value(“${spring.redis2.timeout}”)

    int timeout;

    @Autowired

    RedisConfig config;

    /* (non-Javadoc)

    • @see com.madecare.mcloud.redis.ManyRedisService#initParam()

    */

    @Override

    public void initParam()

    {

    super.config=config;

    super.ip=ip;

    super.port=port;

    super.timeout=timeout;

    }

    }

    第三种

    @Autowired

    RedisConfig redisConfig;

    @Bean(name = “redis1”)

    public RedisServiceImpl first(@Value(“${spring.rabbitmq2.host}”) String ip,

    @Value(“${spring.rabbitmq2.port}”) int port,

    @Value(“${spring.rabbitmq2.username}”) int timeout)

    {

    RedisServiceImpl impl=new RedisServiceImpl();

    impl.redisTemplate=redisConfig.getRedisTemplate(ip, port, timeout);

    return impl;

    }

    csdn地址:https://blog.csdn.net/qq_36236890

    微信订阅号:
    942d3f36d8214ebeafab7807027778c2-qrcodeforgh0250283075b22581.jpg

    手在键盘敲很轻,我写的代码很小心。
    429