Redis学习(三)——SpringBoot整合Redis-5.0.4集群

先学习Redis-5.0.4集群搭建,请移步Redis-5.0.4集群搭建(ubuntu)。

  1. 依赖包

            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
            redis.clients
            jedis
        
  1. 配置文件
spring:
  application:
    name: service-redis
  redis:
    # 连接超时时间(毫秒)
    timeout: 0
    commandTimeout: 5000
    # redis 集群
    cluster:
      nodes: 192.168.182.130:7001,192.168.182.130:7002,192.168.182.130:7003,192.168.182.130:7004,192.168.182.130:7005,192.168.182.130:7006
    jedis:
      pool:
        # 连接池最大连接数(使用负值表示没有限制)
        max-active: 8
        # 连接池最大阻塞等待时间(使用负值表示没有限制)
        max-wait: -1
        # 连接池中的最小空闲连接
        max-idle: 8
server:
  port: 7000
  1. 配置类
@Configuration
@ConditionalOnClass({JedisCluster.class})
public class RedisConfig {

    @Value("${spring.redis.cluster.nodes}")
    private String clusterNodes;
    @Value("${spring.redis.timeout}")
    private int timeout;
    @Value("${spring.redis.jedis.pool.max-idle}")
    private int maxIdle;
    @Value("${spring.redis.jedis.pool.max-wait}")
    private long maxWaitMillis;
    @Value("${spring.redis.commandTimeout}")
    private int commandTimeout;

    @Bean
    public JedisCluster getJedisCluster() {
        String[] cNodes = clusterNodes.split(",");
        Set nodes = new HashSet<>();
        //分割出集群节点
        for (String node : cNodes) {
            String[] hp = node.split(":");
            nodes.add(new HostAndPort(hp[0], Integer.parseInt(hp[1])));
        }
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        //创建集群对象
//      JedisCluster jedisCluster = new JedisCluster(nodes,commandTimeout);
        return new JedisCluster(nodes, commandTimeout, jedisPoolConfig);
    }

    /**
     * 设置数据存入redis 的序列化方式
     * 
redisTemplate序列化默认使用的jdkSerializeable,存储二进制字节码,导致key会出现乱码,所以自定义 * 序列化类 * * @paramredisConnectionFactory */ @Bean public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { RedisTemplate redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.afterPropertiesSet(); return redisTemplate; } }
  1. 简单操作
    controller:
@RestController
@RequestMapping("/string")
public class StringController {
    @Autowired
    private StringService stringService;

    @GetMapping("/get/{key}")
    public String getStr(@PathVariable String key) {
        String str = stringService.get(key);
        return str;
    }

    @PostMapping("/set")
    public void setStr(@RequestParam("key") String key, @RequestParam("value") String value) {
        stringService.set(key, value);
    }
}

service:

public interface StringService {
    String get(String key);

    void set(String key, String value);
}

service.impl:

@Service
public class StringServiceImpl implements StringService {
    @Autowired private JedisCluster jedisCluster;
    @Override
    public String get(String key) {
        String str = jedisCluster.get(key);
        return str;
    }

    @Override
    public void set(String key, String value) {
        jedisCluster.set(key,value);
    }
}

你可能感兴趣的:(Redis,JAVA)