美文网首页
Springboot+Redis 多库操作

Springboot+Redis 多库操作

作者: HeloWxl | 来源:发表于2021-12-05 18:39 被阅读0次

参考:https://blog.csdn.net/Abysscarry/article/details/87495631

版本:

  • springboot版本: 2.3.1.RELEASE
  • jdk: 1.8

示例代码

  • pom.xml
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
       </dependency>
  • application.yml
#Redis配置
redis:
  database:
    a: 0
    b: 1
  host: localhost
  port: 6379
  password: 123456
  timeout: 5000
  pool:
    max-idle: 30
    min-idle: 0
    max-wait: -1
  • RedisConfig.java
package com.lin.chat.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
@EnableAutoConfiguration
public class RedisConfig {
    @Value("${redis.database.a}")
    private int foreDatabase;

    @Value("${redis.database.b}")
    private int wechatDatabase;

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

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

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

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

    @Value("${redis.pool.max-idle}")
    private int maxIdle;

    @Value("${redis.pool.min-idle}")
    private int minIdle;

    @Value("${redis.pool.max-wait}")
    private long maxWait;

    @Bean
    public JedisPoolConfig getJedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMinIdle(minIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWait);
        return jedisPoolConfig;
    }


    @Bean(name = "foreRedisTemplate")
    public RedisTemplate getForeRedisTemplate(){

        JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
        connectionFactory.setPoolConfig(getJedisPoolConfig());
        connectionFactory.setDatabase(foreDatabase);    //此处配置database
        connectionFactory.setHostName(host);
        connectionFactory.setPassword(password);
        connectionFactory.setPort(port);
        connectionFactory.setTimeout(timeout);
        connectionFactory.afterPropertiesSet();         //记得添加这行!
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(connectionFactory);
        return stringRedisTemplate;
    }

    @Bean(name = "wechatRedisTemplate")
    public RedisTemplate getWechatRedisTemplate(){
        JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
        connectionFactory.setPoolConfig(getJedisPoolConfig());
        connectionFactory.setDatabase(wechatDatabase);
        connectionFactory.setHostName(host);
        connectionFactory.setPassword(password);
        connectionFactory.setPort(port);
        connectionFactory.setTimeout(timeout);
        connectionFactory.afterPropertiesSet();
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(connectionFactory);
        return stringRedisTemplate;
    }
}
  • TestController.java
@RestController
public class TestController {

    @Autowired
    private RedisTemplate foreRedisTemplate;

    @Autowired
    private RedisTemplate wechatRedisTemplate;

    @RequestMapping(value = "/testRedis", method = RequestMethod.GET)
    public String test() {
        ValueOperations operations = foreRedisTemplate.opsForValue();
        operations.set("age", "11");
        ValueOperations operations2 = wechatRedisTemplate.opsForValue();
        operations2.set("address", "bj");
        return "OK";
    }
}

测试

image.png

相关文章

网友评论

      本文标题:Springboot+Redis 多库操作

      本文链接:https://www.haomeiwen.com/subject/iiqwxrtx.html