ShardedJedisPool的使用

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介:
 1 package com.test;
 2
 3 import java.util.ArrayList;
 4 import java.util.List;
 5
 6 import redis.clients.jedis.JedisPoolConfig;
 7 import redis.clients.jedis.JedisShardInfo;
 8 import redis.clients.jedis.ShardedJedis;
 9 import redis.clients.jedis.ShardedJedisPool;
10
11 public class RedisShardPoolTest {
12
13     static ShardedJedisPool pool;
14
15     static{
16         JedisPoolConfig config =new JedisPoolConfig();//Jedis池配置
17         config.setMaxActive(500);//最大活动的对象个数
18         config.setMaxIdle(1000  60);//对象最大空闲时间
19         config.setMaxWait(1000  10);//获取对象时最大等待时间
20         config.setTestOnBorrow(true);
21         String hostA = “192.168.0.100”;
22         int portA = 6379;
23         String hostB = “192.168.0.115”;
24         int portB = 6379;
25         List jdsInfoList =new ArrayList(2);
26         JedisShardInfo infoA = new JedisShardInfo(hostA, portA);
27         infoA.setPassword(“admin”);
28         JedisShardInfo infoB = new JedisShardInfo(hostB, portB);
29         infoB.setPassword(“admin”);
30         jdsInfoList.add(infoA);
31         jdsInfoList.add(infoB);
32         pool =new ShardedJedisPool(config, jdsInfoList);
33      }
34
35
36
37     /*
38
39       @param args
40
41      */
42
43     public static void main(String[] args) {
44         for(int i=0; i<100; i++){
45             String key = generateKey();
46             ShardedJedis jds = null;
47             try {
48                 jds = pool.getResource();
49                 System.out.println(key+”:”+jds.getShard(key).getClient().getHost());
50                 System.out.println(jds.set(key,Math.random()+””));
51             } catch (Exception e) {
52                 e.printStackTrace();
53             } finally {
54                 pool.returnResource(jds);
55             }
56         }
57     }
58
59     private static int index = 1;
60     public static String generateKey(){
61         return String.valueOf(Thread.currentThread().getId())+”_”+(index++);
62     }
63 }

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
Java Spring
Spring AOP切点表达式(Pointcut)详解
Spring 的 AOP 中的一个核心概念是切点(Pointcut),切点表达式定义通知(Advice)执行的范围。
3106 0
Maven之阿里云镜像仓库配置
方式一:全局配置可以添加阿里云的镜像到maven的setting.xml配置中,这样就不需要每次在pom中,添加镜像仓库的配置,在mirrors节点下面添加子节点: <id>nexus-aliyun</id> <mirrorOf>central</mirrorOf> <name>Nexus aliyun</name> <url>http://maven.
|
11月前
|
XML 前端开发 JavaScript
JDK8升级JDK17过程中遇到的那些坑
JDK8虽然非常好,但是JDK版本已经发布到JDK20了,且JDK8后的版本升级了很多新的特性,如模块化、ZGC以虚拟线程、结构性并发等,也是非常有吸引力的,所以决定将基于JDK8的项目升级到最近的LTS版本JDK17。
1313 0
|
12月前
|
消息中间件 人工智能 JavaScript
风控系统就该这么设计(万能通用),稳的一批!
风控系统就该这么设计(万能通用),稳的一批!
|
消息中间件 Java 数据库连接
SpringBoot读写分离配置与事务
SpringBoot读写分离配置与事务
541 0
|
关系型数据库 MySQL
[Err] 1205 - Lock wait timeout exceeded; try restarting transaction Mysql 报错
Mysql 报错: [Err] 1205 - Lock wait timeout exceeded; try restarting transaction
325 0
|
JSON 缓存 算法
hutool介绍
hutool介绍
2629 0
hutool介绍
|
存储 JavaScript 前端开发
iOS逆向小知识: hook框架frida的安装和使用( frida Failed to spawn 的 解决方案)
iOS逆向小知识: hook框架frida的安装和使用( frida Failed to spawn 的 解决方案)
802 0