Springboot整合Redis--基于Jedis

环境

springboot 2.4.5

spring-boot-starter-data-redis 2.4.5

jedis 3.3.0

功能点

基于Jedis进行整合

可支持单机模式、集群模式的灵活配置切换(基于springboot的自动配置实现)

完整代码

共包含三个文件:pom依赖、RedisConfig.java配置类、application.properties配置文件

POM.XML



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.4.5
		 
	
	XXX
	XXX
	0.0.1-SNAPSHOT
	XXX
	redis学习工程
	
		1.8
	
	
		
			org.springframework.boot
			spring-boot-starter
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		

		
			org.springframework.boot
			spring-boot-starter-web
			2.4.1
		


		
			redis.clients
			jedis
			3.3.0
		

		
			org.springframework.boot
			spring-boot-starter-data-redis
		


	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


application.properties

##当前配置为集群模式,可自定义调整为单机模式
redis.cluster.nodes=10.3.231.155:7001,10.3.231.155:7002,10.3.231.155:7003,10.3.231.155:7004,10.3.231.155:7005,10.3.231.155:7006
redis.password=password

RedisConfig.java

package com.stw.wyf.rediss1.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    @ConditionalOnProperty(name = "redis.cluster.nodes")
    public RedisConnectionFactory redisConnectionFactory(RedisClusterConfiguration redisClusterConfiguration) {
        return new JedisConnectionFactory(redisClusterConfiguration);
    }

    @Bean
    @ConditionalOnProperty(name = "redis.host")
    public RedisConnectionFactory redisConnectionFactory() {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        jedisConnectionFactory.setHostName("localhost");
        jedisConnectionFactory.setPort(6379);
        return jedisConnectionFactory;
    }

    @Value("${redis.cluster.nodes:''}")
    String clusternodes;

    @Value("${redis.password:''}")
    String passWord;

    @Bean
    @ConditionalOnProperty(name = "redis.cluster.nodes")
    public RedisClusterConfiguration redisClusterConfiguration() {
        RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
        // 解析配置文件中的集群节点信息,添加到RedisClusterConfiguration中
        // ...
        String[] redisNodeArr = clusternodes.split(",");
        for (String redisNodeS : redisNodeArr) {
            String[] split = redisNodeS.split(":");
            String ip = split[0];
            String host = split[1];
            RedisNode redisNode = new RedisNode(ip,Integer.valueOf(host));
            redisClusterConfiguration.addClusterNode(redisNode);
        }

        redisClusterConfiguration.setPassword(passWord);
        return redisClusterConfiguration;
    }

    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate redisTemplate = new StringRedisTemplate(redisConnectionFactory);
        redisTemplate.setDefaultSerializer(new StringRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

}

你可能感兴趣的:(Spring,spring,boot,redis)