多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] # maven ~~~ <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> ~~~ # 建立jedis连接池 ~~~ import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class JedisPoolUtils { private static JedisPool pool; //建立连接池 private static void createJedisPool() { //建立连接池配置参数 JedisPoolConfig config = new JedisPoolConfig(); //设置最大连接数 config.setMaxTotal(100); //设置最大空闲连接数 config.setMaxIdle(5); //设置最小空闲连接数 config.setMinIdle(2); //连接耗尽时是否阻塞, false报异常,true阻塞直到超时, 默认true config.setBlockWhenExhausted(true); //获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认-1(单位为毫秒) config.setMaxWaitMillis(1000); //是否启用pool的jmx管理功能, 默认true config.setJmxEnabled(true); //是否启用后进先出, 默认true config.setLifo(true); //逐出连接的最小空闲时间 默认1800000毫秒(30分钟) config.setMinEvictableIdleTimeMillis(1800000); //在获取连接的时候检查有效性, 默认false config.setTestOnBorrow(true); //在空闲时检查有效性, 默认false config.setTestWhileIdle(false); //读取超时,2000 pool = new JedisPool(config, "127.0.0.1", 6379, 2000, "root"); } //多线程环境下,同步初始化 private static synchronized void poolInit() { if (pool == null) { createJedisPool(); } } //获取一个jedis对象 public static Jedis getJedis() { if (pool == null) { poolInit(); } return pool.getResource(); } //归还一个连接 public static void returnRes(Jedis jedis) { pool.returnResource(jedis); } } ~~~ # jedis实现分片 Jedis分片采用Hash算法和基于的Key模式匹配。Jedis定义一个Hash接口,如果觉得自带的不爽,可以自己实现一个Hash算法。Jedis自带的Hash的算法是MurmurHash 2.0 。 ~~~ /** * 创建分片对象 */ public static ShardedJedis createShardJedis() { //建立服务器列表 List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); //添加第一台服务器信息 JedisShardInfo si = new JedisShardInfo("localhost", 6379); si.setPassword("123"); shards.add(si); //添加第二台服务器信息 si = new JedisShardInfo("localhost", 6399); si.setPassword("123"); shards.add(si); //建立分片连接对象 ShardedJedis jedis = new ShardedJedis(shards); //建立分片连接对象,并指定Hash算法 //ShardedJedis jedis = new ShardedJedis(shards,selfHash); return jedis; } ~~~ 分片也可以支持连接池,具体如下 ~~~ private static void createPool() { List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); JedisShardInfo si = new JedisShardInfo("localhost", 6379); si.setPassword("123"); shards.add(si); si = new JedisShardInfo("localhost", 6399); si.setPassword("123"); shards.add(si); pool = new ShardedJedisPool(new JedisPoolConfig(), shards); } ~~~ # 分布式直连同步调用 这个是分布式直接连接,并且是同步调用,每步执行都返回执行结果。类似地,还有异步管道调用。 其实就是分片 ~~~ public void jedisShardNormal() { List<JedisShardInfo> shards = Arrays.asList( new JedisShardInfo("localhost",6379), new JedisShardInfo("localhost",6380)); ShardedJedis sharding = new ShardedJedis(shards); long start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { String result = sharding.set("sn" + i, "n" + i); } long end = System.currentTimeMillis(); System.out.println("Simple@Sharing SET: " + ((end - start)/1000.0) + " seconds"); sharding.disconnect(); } ~~~ # 分布式直连异步调用 ~~~ public void jedisShardpipelined() { List<JedisShardInfo> shards = Arrays.asList( new JedisShardInfo("localhost",6379), new JedisShardInfo("localhost",6380)); ShardedJedis sharding = new ShardedJedis(shards); ShardedJedisPipeline pipeline = sharding.pipelined(); long start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { pipeline.set("sp" + i, "p" + i); } List<Object> results = pipeline.syncAndReturnAll(); long end = System.currentTimeMillis(); System.out.println("Pipelined@Sharing SET: " + ((end - start)/1000.0) + " seconds"); sharding.disconnect(); } ~~~ # 分布式连接池同步调用 如果,你的分布式调用代码是运行在线程中,那么上面两个直连调用方式就不合适了,因为直连方式是非线程安全的,这个时候,你就必须选择连接池调用。 连接池的调用方式,适合大规模的redis集群,并且多客户端的操作 ~~~ public void jedisShardSimplePool() { List<JedisShardInfo> shards = Arrays.asList( new JedisShardInfo("localhost",6379), new JedisShardInfo("localhost",6380)); ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shards); ShardedJedis one = pool.getResource(); long start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { String result = one.set("spn" + i, "n" + i); } long end = System.currentTimeMillis(); pool.returnResource(one); System.out.println("Simple@Pool SET: " + ((end - start)/1000.0) + " seconds"); pool.destroy(); } ~~~ # 分布式连接池异步调用 ~~~ public void jedisShardPipelinedPool() { List<JedisShardInfo> shards = Arrays.asList( new JedisShardInfo("localhost",6379), new JedisShardInfo("localhost",6380)); ShardedJedisPool pool = new ShardedJedisPool(new JedisPoolConfig(), shards); ShardedJedis one = pool.getResource(); ShardedJedisPipeline pipeline = one.pipelined(); long start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { pipeline.set("sppn" + i, "n" + i); } List<Object> results = pipeline.syncAndReturnAll(); long end = System.currentTimeMillis(); pool.returnResource(one); System.out.println("Pipelined@Pool SET: " + ((end - start)/1000.0) + " seconds"); pool.destroy(); } ~~~