Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of integrating redis and generating Global unique Index ID in springboot

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "the method of springboot integrating redis and generating global unique index ID". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "the method of springboot integrating redis and generating globally unique index ID".

Springboot integrated redispom file org.springframework.boot spring-boot-starter-data-redisapplication.yaml file spring: # redis cache redis: connect-timeout: 180000 # connection timeout lettuce: pool: # connection pool maximum number of connections max-active: 8 # maximum blocking wait time (negative number indicates no limit) max-wait: 1 # connection Pool maximum idle connection max-idle: 5 # connection pool minimum idle connection min-idle: 0 # stand-alone mode # database: 0 # cluster mode this parameter does not take effect # host: 127.0.0.The port: 6379 # cluster mode is enabled cluster: nodes: 127.0.0.1database 6379127.0.0.1host 7001127.0. 0.1VR 7002127.0.0.1VOR 7003127.0.0.1VOUR 7004127.0.0.1 max-redirects: 3 password: test verification

If you call this API and return 22, the integration of redis is successful.

Redis generates globally unique index ID

Use the RedisAtomicLong of redis to generate a distributed self-increasing ID value; code directly:

Import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;import com.wd.basic.common.support.component.CustomIdGenerator;import lombok.extern.slf4j.Slf4j;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.core.StringRedisTemplate;import javax.annotation.Resource;/** * mybatis configuration * * @ author Shangguan Waner * @ date 2022-03-21 * / @ Slf4j@Configurationpublic class MybatisPlusConfig {@ Resource private StringRedisTemplate stringRedisTemplate @ Bean public IdentifierGenerator idGenerator () {return new CustomIdGenerator (stringRedisTemplate);}}

Since this utility class needs to be placed in the common package, add MybatisPlusConfig to the calling system and send bean (stringRedisTemplate) to CustomIdGenerator when the project starts (as I understand it, it may not be explained this way)

Import cn.hutool.core.date.DatePattern;import cn.hutool.core.date.DateUtil;import cn.hutool.core.util.StrUtil;import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;import com.wd.basic.common.exception.BasicException;import com.wd.basic.common.exception.enums.IDGeneratorExceptionEnum;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.data.redis.support.atomic.RedisAtomicLong;import java.util.Date;import java.util.Objects;import java.util.concurrent.TimeUnit / * * Custom id generator * * @ author Shangguan Waner * @ date 2022-03-21 * / public class CustomIdGenerator {private static final String I_KEY_PREFIX = "IKey:generator"; public static final int KEY_EXPIRE_TIME = 2; private final StringRedisTemplate stringRedisTemplate; public CustomIdGenerator (StringRedisTemplate stringRedisTemplate) {this.stringRedisTemplate = stringRedisTemplate } / * * year, month, day, minute, second + 6-bit self-increasing sequence returned by redis (such as 000001, 000002, 000003...) * the self-increasing sequence returned by redis: * generate a self-increasing sequence based on the passed key (equivalent to field name), and then increase it again after 2 seconds. * because of the incr atomicity of redis, it is also guaranteed that the result returned each time will not have the same value, * / @ Override public String nextUUID (Object entity) {String bizKey = entity.getClass () .getName (); String dateStr = DateUtil.format (new Date (), DatePattern.PURE_DATETIME_MS_FORMATTER); RedisAtomicLong counter = new RedisAtomicLong (I_KEY_PREFIX + bizKey, Objects.requireNonNull (stringRedisTemplate.getConnectionFactory () Counter.expire (KEY_EXPIRE_TIME, TimeUnit.SECONDS); long redisId = counter.incrementAndGet (); String redisIdStr = StrUtil.fillBefore (String.valueOf (redisId),'0# redisId 6); return dateStr + redisIdStr;}} Test Verification

Start 10 threads and run 1000 times:

The results are as follows: in 2s, you can run 1000 as usual without repetition.

Change it to 10000, or run to 10000. It turns out that after each thread goes in, go to the code counter.expire (KEY_EXPIRE_TIME, TimeUnit.SECONDS). Reset the failure time of this key, but it is enough to generate 100000 in 2 seconds. You can shorten or increase the failure time according to the system business.

At this point, I believe you have a deeper understanding of "springboot integrates redis and generates a globally unique index ID". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report