分布式ID选型对比(3)

时间:2024-03-03 19:25:05

redis自增

一, 引入依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>2.6.5</version>
</dependency>

二, 配置信息:

spring:
  redis:
    # 地址
    host: localhost
    # 端口号
    port: 6379
    # 密码
    password:
    # 超时时间,单位毫秒
    timeout: 3000
    # 数据库编号
    database: 0

JedisConfig:

package org.com.spi.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

@Configuration
public class JedisConfig {
    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate template = new StringRedisTemplate(factory);
        //定义key序列化方式
        //RedisSerializer<String> redisSerializer = new StringRedisSerializer();//Long类型会出现异常信息;需要我们上面的自定义key生成策略,一般没必要
        //定义value的序列化方式
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);

        // template.setKeySerializer(redisSerializer);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}

三, redis配置类RedisUtils:

package org.com.spi.utils;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class RedisUtils {
    @Resource
    private RedisTemplate<String, Object> redisTemplate; //这里使用的是redisTemplate

    /**
     * redis 自增
     * @param key 键
    //     * @param by 要增加几(大于0)
     * @return
     */
    public long incr(String key, long delta){
        if(delta<0){
            throw new RuntimeException("递增因子必须大于0");
        }
        return redisTemplate.opsForValue().increment(key, delta);
    }
}

4, 编写接口controller:

@Autowired
RedisUtils redisUtils;
/**
 * 根据数据库ID自增生成主键
 * 缺点在于数据库不能集群,只能单机搞,不适合ID作为业务传递的场景
 */
@PostMapping("/redisInc")
public Long redisInc(){
    return redisUtils.incr("generate2",1);
}

特点: redis自增仅适合单业务场景,对于多模块、复杂业务场景不太适用;另外,redis的两种持久化机制都有一定的弊端: RDB方式-宕机存在丢失的风险, AOF方式-宕机能恢复数据,但数据恢复时间较长,在并发量大的时候可能导致ID重复的情况.

------------------------------------------------------------------------------------------------

雪花算法

主要类: IdWorker

package org.com.spi.config;

import org.springframework.stereotype.Component;

import java.lang.management.