当前位置:网站首页>Redis implements distributed global unique ID
Redis implements distributed global unique ID
2022-07-21 17:38:00 【Small hundred vegetables】
use INCR Command to implement distributed global ID Generate .
1、lettuce Client implementation
<!--redis, There are two realizations :lettuce and jedis . The default is to use lettuce.-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
# Redis To configure
spring.redis.host=192.168.1.160
#Redis Server connection port
spring.redis.port=6379
#Redis Server connection password ( The default is empty. )
spring.redis.password=123456
# Maximum number of connections in connection pool ( Use a negative value to indicate that there is no limit )
spring.redis.max-active=8
# Connection pool maximum blocking wait time ( millisecond )( Use a negative value to indicate that there is no limit )
spring.redis.max-wait=30000
# The maximum free connection in the connection pool
spring.redis.max-idle=8
# The smallest free connection in the connection pool
spring.redis.min-idle=0
# Connection timeout ( millisecond )
spring.redis.timeout=30000
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
// Configure connection factory
template.setConnectionFactory(factory);
// Use Jackson2JsonRedisSerializer To serialize and deserialize redis Of value value ( By default JDK How to serialize )
Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
// Specify the domain to serialize ,field,get and set, And modifier range ,ANY Yes, they all include private and public
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// Specifies the type of serialized input , Class must be right or wrong final Embellished ,final Modified class , such as String,Integer I'll run out of order
om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
jacksonSeial.setObjectMapper(om);
// key serialize
template.setKeySerializer(new StringRedisSerializer());
// value serialize
template.setValueSerializer(jacksonSeial);
// Hash key serialize
template.setHashKeySerializer(new StringRedisSerializer());
// Hash value serialize
template.setHashValueSerializer(jacksonSeial);
template.afterPropertiesSet();
return template;
}
}
@Service
public class RedisService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
private static final String ID_KEY = "id:generator:order";
private static final long BASE_ID = 1000000000;
/**
* Generating uniqueness ID
* @date 2022/7/18 16:03
*/
public long getUniqueId() {
long inc = stringRedisTemplate.opsForValue().increment(ID_KEY);
System.out.println(BASE_ID + inc);
return BASE_ID + inc;
}
}
2、jedis Client implementation
<!-- Jedis Client dependency -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
jedis.pool.host=192.168.1.201
jedis.pool.port=6379
jedis.pool.password=123456
jedis.pool.timeout=7200
jedis.pool.config.maxTotal=1000
jedis.pool.config.maxIdle=100
jedis.pool.config.minIdle=50
jedis.pool.config.maxWaitMillis=1000
jedis.pool.config.testOnBorrow=true
@Configuration
public class RedisConfig {
@Bean
public JedisPoolConfig jedisPoolConfig(@Value("${jedis.pool.config.maxTotal}") int maxActive,
@Value("${jedis.pool.config.maxIdle}") int maxIdle,
@Value("${jedis.pool.config.minIdle}") int minIdle,
@Value("${jedis.pool.config.maxWaitMillis}") long maxWaitMillis,
@Value("${jedis.pool.config.testOnBorrow}") boolean testOnBorrow) {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(maxActive);
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMinIdle(minIdle);
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
jedisPoolConfig.setTestOnBorrow(testOnBorrow);
return jedisPoolConfig;
}
@Bean
public JedisPool jedisPool(@Value("${jedis.pool.host}") String host,
@Value("${jedis.pool.port}") int port,
@Value("${jedis.pool.password}") String password,
@Value("${jedis.pool.timeout}") int timeout, JedisPoolConfig jedisPoolConfig) {
if (password == null || password.length() == 0) {
return new JedisPool(jedisPoolConfig, host, port, timeout);
}
return new JedisPool(jedisPoolConfig, host, port, timeout, password);
}
}
@Service
public class RedisService {
@Autowired
private JedisPool jedisPool;
private static final String ID_KEY = "id:generator:order";
private static final long BASE_ID = 1000000000;
/**
* Generating uniqueness ID
* @date 2022/7/18 16:03
*/
public long getUniqueId() {
Jedis jedis = jedisPool.getResource();
long inc = jedis.incr(ID_KEY);
System.out.println(BASE_ID + inc);
return BASE_ID + inc;
}
}
边栏推荐
- Junda technology - Mobus to SNMP network monitoring terminal
- PHP FPM custom ZABBIX monitoring
- Rsync combined with inotify to realize real-time file synchronization (2)
- MD editor - typora
- JS array
- 【机器学习】如何在训练过程中挑选比较好的模型保存(pytorch)
- RuntimeError: CUDA out of memory. Tried to allocate 32.00 MiB||查看GPU内存
- ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值
- MySQL多行数据合成一行函数GROUP_CONCAT
- 动手学moveit2|介绍和安装
猜你喜欢
数学建模 - K-means聚类
同城两中心自适应同步模式部署
静态通讯录的实现
DeFi 对经济模式带来的改变和存在的局限
动手学moveit2|介绍和安装
How to transfer files on the server_ By customized hierarchical relationship_ Compress and download
RuntimeError: CUDA out of memory. Tried to allocate 32.00 MiB||查看GPU内存
每周推荐短视频:物联网开发提出更大挑战?
线程间有哪些资源是不可以共享的?
Yolov5 improvement 2: add CBAM attention mechanism
随机推荐
【RViz2】导入urdf模型时报错:Could not load resource xxx,Unable to open file xxx,Error retrieving file xxx
IT运维管理指什么?如何建立有效的IT运维管理系统?
C#(三十六)之文件夹、路径、环境特殊目录类
MD editor - typora
Outofmemoryerror troubleshooting of memory overflow
C#(四十五)之线程池
C#(三十五)之在滚动窗口中绘图
传输层 使用Web代理访问网站
ROS2入门教程 | 动作(Action)通信与自定义接口
What does it operation and maintenance management mean? How to establish an effective IT operation and maintenance management system?
RuntimeError: CUDA out of memory. Tried to allocate 32.00 MiB||查看GPU内存
Huawei wireless devices are configured with ACL based message filtering
已解决Module not found: Error: Can‘t resolve ‘core-js/fn/reflect‘ in
封装全局input组件,并通过v-model绑定父子数据
Configuring airtime scheduling for Huawei wireless devices
解决Metasploit中shell乱码的问题
Ccs3 comprehensive experiment -- stylesheet file -- design a menu page
新鲜出炉的 yoloV5可视化实战项目(1)
The difference between memory overflow and memory leak
超级奶思的笔记软件:Obsidain