springboot 中的redis配置(Jedis )(非集群)

本文详细介绍了如何在SpringBoot应用中配置并使用Jedis连接Redis,包括pom.xml的依赖添加,application.properties的配置,以及相关类的注入和使用方法。
  1. pom.xml

       <!--redis 相关jar包-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    
  2. application.properties

     spring.redis.host= 127.0.0.1
     spring.redis.port= 6379
    
  3. config

    	import org.slf4j.Logger;
    	import org.slf4j.LoggerFactory;
    	import org.springframework.beans.factory.annotation.Value;
    	import org.springframework.context.annotation.Bean;
    	import org.springframework.context.annotation.Configuration;
    	import org.springframework.context.annotation.PropertySource;
    	import redis.clients.jedis.JedisPool;
    	import redis.clients.jedis.JedisPoolConfig;
    	
    	@Configuration
    	@PropertySource(value="classpath:bootstrap.properties")
    	public class RedisConfig {
    
        private Logger log = (Logger) LoggerFactory.getLogger(RedisConfig.class);
    
        @Value("${spring.redis.host}")
        private String host;
    
        @Value("${spring.redis.port}")
        private int port;
    
        @Bean
        public JedisPool getJedisPool(){
            log.info("==>初始化jedis连接池");
            JedisPoolConfig config = new JedisPoolConfig();
            JedisPool pool = new JedisPool(config, host, port);
            return pool;
        }
    }
    
  4. 在 要用的类中注入:

     	 @Autowired
         private JedisPool jedisPool;
    
  5. 方法中使用:

      Jedis redis = jedisPool.getResource();
    
    	redis.set("key值","value");
        redis.lpush("key值","value");
        redis.expire("key值",时间:秒);
    
     //llen  的方法  取出list长度
     redis.llen(phoneNumber + format);
    
<think>好的,我需要帮助用户找到关于Spring Boot整合Spring Data RedisJedis在单机、哨兵和集群模式下的详细配置方案。用户提到了引用1到5,这些引用提供了一些相关的信息,我需要仔细参考这些内容,并结合我的知识来构建一个完整的回答。 首先,我需要确认用户的需求。他们希望得到详细的配置教程,涵盖单机、哨兵和集群三种模式,并且使用Spring Boot、Spring Data Redis以及Jedis。根据引用2,Spring Boot通过自动配置简化了Redis使用,只需要添加依赖和配置连接信息。引用4提到Jedis连接池的配置,而引用5则涉及集群模式的配置步骤。 接下来,我需要整理各个模式的配置步骤。对于单机模式,基本配置包括依赖添加和application.properties中的基本设置。引用2和引用3中的代码示例可能有用,比如RedisApplication.java中的使用示例。然后,哨兵模式需要配置哨兵节点和主节点名称,这在引用5中有提到集群配置命令,但可能需要调整用于哨兵。引用4中的连接池优化配置也适用于哨兵和集群集群模式的配置较为复杂,引用5提到需要6台服务器,3主3从,并执行redis-cli命令创建集群。在Spring Boot中,需要配置集群节点和连接池参数,引用4中的连接池设置和日志监控配置需要包含进来。此外,引用1提到的Spring Data Redis的抽象层可能需要在配置中体现,确保与Spring框架的集成。 我还需要确保配置示例正确无误,比如Jedis和Lettuce的区别。根据引用4,若使用Jedis,需要配置spring.redis.jedis.pool相关参数,并启用自动刷新。而Lettuce的连接池配置参数可能同,但用户明确提到了Jedis,所以需要专注于Jedis配置。 然后,检查各个引用中的信息是否准确,并正确引用。例如,引用5中的集群配置命令可能需要详细说明每个参数的作用,如--cluster-replicas 1表示每个主节点有一个从节点。同时,引用3中的代码示例展示了如何使用RedisUtil进行操作,这可以作为配置后的使用示例。 最后,确保回答结构清晰,分单机、哨兵、集群三个部分,每个部分包含依赖配置、application.properties设置、代码示例(如果需要)以及注意事项。同时,生成的相关问题需要与主题相关,帮助用户进一步探索。 可能遇到的问题:如何区分Jedis和Lettuce的配置?用户明确要求Jedis,因此需要确保所有配置都是针对Jedis的,如连接池参数。另外,哨兵模式的配置可能需要指定sentinel.nodes和master名称,这些需要正确示例。集群模式需要列出所有节点,并确保连接池参数优化。 总结步骤: 1. 单机模式配置:依赖、application.properties、连接池设置。 2. 哨兵模式配置:哨兵节点、主名称、连接池。 3. 集群模式配置集群节点、连接池、可能的安全认证。 4. 添加相关代码示例,如RedisUtil的使用。 5. 注意事项,如连接池优化、日志监控、Jedis与Lettuce区别。 6. 生成相关问题,引导用户深入配置优化或实际应用。 现在需要将以上思路转化为结构化的回答,确保每个部分准确引用相关文献,并正确应用LaTeX格式。</think>以下是Spring Boot整合Spring Data RedisJedis在单机、哨兵、集群模式下的详细配置方案: ### 一、单机模式配置 1. **添加依赖** ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> ``` 2. **application.properties配置** ```properties # 基础配置 spring.redis.database=0 spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password=123456 # Jedis连接池 spring.redis.jedis.pool.max-active=200 spring.redis.jedis.pool.max-wait=3000ms spring.redis.jedis.pool.test-on-borrow=true ``` 3. **代码示例** ```java @SpringBootApplication public class RedisApplication { public static void main(String[] args) { SpringApplication.run(RedisApplication.class, args); redisTemplate.opsForValue().set("key", "value"); // 通过自动注入的RedisTemplate操作 } } ``` ### 二、哨兵模式配置 ```properties # 哨兵节点配置 spring.redis.sentinel.master=mymaster spring.redis.sentinel.nodes=192.168.1.101:26379,192.168.1.102:26379,192.168.1.103:26379 # 连接池复用单机配置 spring.redis.jedis.pool.max-active=300 # 建议增大连接数 spring.redis.timeout=5000 # 增加超时时间 ``` ### 三、集群模式配置 1. **节点配置** ```properties spring.redis.cluster.nodes=172.16.1.101:6379,172.16.1.102:6380,172.16.1.103:6381,172.16.1.104:7379,172.16.1.105:7380,172.16.1.106:7381 spring.redis.cluster.max-redirects=3 # 最大重定向次数 spring.redis.password=123456 # 集群专用连接池 spring.redis.jedis.pool.max-active=500 spring.redis.jedis.pool.max-idle=50 spring.redis.jedis.pool.min-idle=10 ``` 2. **集群创建命令** ```shell redis-cli --cluster create 节点列表 --cluster-replicas 1 -a 密码 ``` ### 四、通用优化配置 ```properties # 日志监控 logging.level.org.springframework.data.redis=DEBUG logging.level.redis.clients.jedis=INFO # 连接保活 spring.redis.jedis.pool.test-while-idle=true spring.redis.jedis.pool.time-between-eviction-runs=60000ms ``` ### 五、注意事项 1. **Jedis与Lettuce区别** Jedis是直连模式,需配置连接池;Lettuce基于Netty实现异步通信[^4] 2. **配置验证** 通过`RedisConnectionFactory`的`getConnection().ping()`验证配置 3. **安全认证** 集群模式需在所有节点配置相同密码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值