├── redis-pubsub
├── README.md
├── src
│ ├── main
│ │ ├── resources
│ │ │ ├── conf.properties
│ │ │ ├── spring
│ │ │ │ ├── applicationContext.xml
│ │ │ │ ├── redis_context.xml
│ │ │ │ ├── redis_listener.xml
│ │ │ │ └── redis_spring_listener.xml
│ │ │ └── log4j2.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── redis
│ │ │ └── pubsub
│ │ │ ├── service
│ │ │ ├── SelfDefineMessageDelegate.java
│ │ │ └── impl
│ │ │ │ ├── RedisPubSubListener.java
│ │ │ │ ├── MessageDelegateListenerImpl.java
│ │ │ │ └── SelfDefineMessageDelegateImpl.java
│ │ │ ├── server
│ │ │ ├── PublishSubscribeServer.java
│ │ │ └── PubSubWithoutSpringServer.java
│ │ │ ├── util
│ │ │ └── FastJsonMessageConverter.java
│ │ │ ├── monitor
│ │ │ └── ZookeeperMonitor.java
│ │ │ └── RedisOpt.java
│ └── test
│ │ └── java
│ │ └── com
│ │ └── redis
│ │ └── pubsub
│ │ └── TestServer.java
└── pom.xml
├── redis-disconf
├── src
│ ├── main
│ │ ├── resources
│ │ │ ├── disconf.properties
│ │ │ ├── conf.properties
│ │ │ └── applicationContext.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── redis
│ │ │ └── disconf
│ │ │ ├── server
│ │ │ └── RedisServer.java
│ │ │ ├── service
│ │ │ ├── RedisService.java
│ │ │ └── DisconfTask.java
│ │ │ ├── conf
│ │ │ └── JedisConfig.java
│ │ │ └── util
│ │ │ └── JedisUtil.java
│ └── test
│ │ └── java
│ │ └── com
│ │ └── redis
│ │ └── disconf
│ │ └── AppTest.java
└── pom.xml
├── .gitignore
├── redis-base
├── target
│ └── classes
│ │ └── conf.properties
├── src
│ ├── main
│ │ ├── resources
│ │ │ ├── conf.properties
│ │ │ ├── spring
│ │ │ │ ├── applicationContext.xml
│ │ │ │ ├── jedis_pool_context.xml
│ │ │ │ ├── jedis_sentinel_context.xml
│ │ │ │ ├── jedis_sharded_context.xml
│ │ │ │ └── jedis_sentinel_spring.xml
│ │ │ └── log4j2.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── redis
│ │ │ └── base
│ │ │ ├── list
│ │ │ ├── RedisListPCServer.java
│ │ │ ├── RedisListProducer.java
│ │ │ └── RedisListConsumer.java
│ │ │ ├── core
│ │ │ ├── PoolRedis.java
│ │ │ ├── ClusterRedis.java
│ │ │ └── ShardedRedis.java
│ │ │ ├── BaseConstants.java
│ │ │ ├── cluster
│ │ │ └── RedisCluster.java
│ │ │ ├── multi
│ │ │ └── RedisMulti.java
│ │ │ ├── JedisOpt.java
│ │ │ ├── bithll
│ │ │ └── RedisBitHll.java
│ │ │ ├── set
│ │ │ └── RedisSetZset.java
│ │ │ └── pipeline
│ │ │ └── RedisPipeline.java
│ └── test
│ │ └── java
│ │ └── com
│ │ └── redis
│ │ └── base
│ │ └── JedisTest.java
├── pom.xml
└── README.md
├── README.md
└── pom.xml
/redis-pubsub/README.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/windwant/redis-service/HEAD/redis-pubsub/README.md
--------------------------------------------------------------------------------
/redis-disconf/src/main/resources/disconf.properties:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/windwant/redis-service/HEAD/redis-disconf/src/main/resources/disconf.properties
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea/
2 | *.iml
3 | redis-pubsub/target/
4 | *.class
5 | redis-disconf/target/classes/applicationContext.xml
6 | redis-disconf/
7 | redis-test/target/
8 |
--------------------------------------------------------------------------------
/redis-base/target/classes/conf.properties:
--------------------------------------------------------------------------------
1 | # Redis settings
2 | redis.master.host=127.0.0.1
3 | redis.master.port=6379
4 |
5 | redis.slaver.host=127.0.0.1
6 | redis.slaver.port=6379
7 |
8 | redis.maxIdle=300
9 | redis.maxTotal=300
10 | redis.minIdle=300
11 | redis.maxActive=600
12 | redis.maxWait=1000
13 | redis.testOnBorrow=true
--------------------------------------------------------------------------------
/redis-base/src/main/resources/conf.properties:
--------------------------------------------------------------------------------
1 | # Redis settings
2 | redis.master.host=127.0.0.1
3 | redis.master.port=6379
4 |
5 | redis.slaver.host=127.0.0.1
6 | redis.slaver.port=6379
7 |
8 | redis.maxIdle=300
9 | redis.maxTotal=300
10 | redis.minIdle=300
11 | redis.maxActive=600
12 | redis.maxWait=1000
13 | redis.testOnBorrow=true
--------------------------------------------------------------------------------
/redis-disconf/src/main/resources/conf.properties:
--------------------------------------------------------------------------------
1 | # Redis settings
2 | redis.master.host=127.0.0.1
3 | redis.master.port=6379
4 |
5 | redis.slaver.host=127.0.0.1
6 | redis.slaver.port=6379
7 |
8 | redis.maxIdle=300
9 | redis.maxTotal=300
10 | redis.minIdle=300
11 | redis.maxActive=600
12 | redis.maxWait=1000
13 | redis.testOnBorrow=true
--------------------------------------------------------------------------------
/redis-pubsub/src/main/resources/conf.properties:
--------------------------------------------------------------------------------
1 | # Redis settings
2 | redis.master.host=127.0.0.1
3 | redis.master.port=6379
4 |
5 | redis.slaver.host=192.168.7.162
6 | redis.slaver.port=6379
7 |
8 | redis.maxIdle=300
9 | redis.maxTotal=300
10 | redis.minIdle=300
11 | redis.maxActive=600
12 | redis.maxWait=1000
13 | redis.testOnBorrow=true
14 |
15 | zookeeper.host=127.0.0.1
16 | zookeeper.parent.path=/redis
17 | zookeeper.path=/springdata
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | redis-service:java spring redis
2 | ====================
3 |
4 | * 订阅/发布系统: pubsub;
5 |
6 | * 统一配置管理: disconf;
7 |
8 | * lua脚本实现分布式锁;
9 |
10 | * 缓存应用(连接池,切片连接池,哨兵模式)
11 |
12 | ### 附注:哨兵、分片、集群
13 | ```
14 | 哨兵模式集群(一主多从):每个节点拥有所有数据,内存限制
15 | |
16 | 改进(解决内存问题)
17 | |
18 | 分片多主多从:客户端分配key存储。数据分布在多个节点。扩容麻烦(预分片:初期设置预估需要的分片数)。
19 | |
20 | 升级(解决扩容问题)
21 | |
22 | 3.0集群(数据分散在集群中的库节点上,支持一定的访问性及故障恢复,拥有与单节点同样的性能)(注意不同节点上key的多健命令;只能使用0号数据库,禁select)
23 | ```
24 | 哨兵为集群的子集,是用于不需数据分片,或者使用客户端分片的情况。
25 |
--------------------------------------------------------------------------------
/redis-pubsub/src/main/java/com/redis/pubsub/service/SelfDefineMessageDelegate.java:
--------------------------------------------------------------------------------
1 | package com.redis.pubsub.service;
2 |
3 | import java.io.Serializable;
4 | import java.util.Map;
5 |
6 | /**
7 | * 自定义消息接口
8 | */
9 | public interface SelfDefineMessageDelegate {
10 | void handleMessage(String message);
11 |
12 | void handleMessage(Map, ?> message);
13 |
14 | void handleMessage(byte[] message);
15 |
16 | void handleMessage(Serializable message);
17 |
18 | // pass the channel/pattern as well
19 | void handleMessage(Serializable message, String channel);
20 | }
21 |
--------------------------------------------------------------------------------
/redis-base/src/main/java/com/redis/base/list/RedisListPCServer.java:
--------------------------------------------------------------------------------
1 | package com.redis.base.list;
2 |
3 | import org.springframework.context.support.ClassPathXmlApplicationContext;
4 |
5 | /**
6 | * Created by Administrator on 18-3-20.
7 | */
8 | public class RedisListPCServer {
9 | public static void main(String[] args) {
10 | ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
11 | new String[]{"spring/applicationContext.xml",
12 | "spring/jedis_sharded_context.xml"});
13 | ctx.start();
14 | ctx.registerShutdownHook();
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/redis-disconf/src/main/java/com/redis/disconf/server/RedisServer.java:
--------------------------------------------------------------------------------
1 | package com.redis.disconf.server;
2 |
3 | import org.springframework.context.support.ClassPathXmlApplicationContext;
4 |
5 | import java.io.IOException;
6 |
7 | /**
8 | * Hello world!
9 | *
10 | */
11 | public class RedisServer
12 | {
13 | public static void main( String[] args ) {
14 | ClassPathXmlApplicationContext ct = new ClassPathXmlApplicationContext("classpath:/*.xml");
15 | ct.registerShutdownHook();
16 | try {
17 | System.in.read();
18 | } catch (IOException e) {
19 | e.printStackTrace();
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/redis-pubsub/src/main/java/com/redis/pubsub/service/impl/RedisPubSubListener.java:
--------------------------------------------------------------------------------
1 | package com.redis.pubsub.service.impl;
2 |
3 | import redis.clients.jedis.BinaryJedisPubSub;
4 |
5 | import java.io.UnsupportedEncodingException;
6 |
7 | /**
8 | * Created by Administrator on 18-3-20.
9 | */
10 | public class RedisPubSubListener extends BinaryJedisPubSub {
11 | @Override
12 | public void onMessage(byte[] channel, byte[] message) {
13 | try {
14 | System.out.println("RedisPubSubListener onMessage---channel: " + new String(channel, "utf-8") + ", message: " + new String(message, "utf-8"));
15 | } catch (UnsupportedEncodingException e) {
16 |
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/redis-base/src/main/resources/spring/applicationContext.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/redis-base/src/main/java/com/redis/base/core/PoolRedis.java:
--------------------------------------------------------------------------------
1 | package com.redis.base.core;
2 |
3 | import com.redis.base.BaseConstants;
4 | import redis.clients.jedis.*;
5 |
6 | import java.util.ArrayList;
7 | import java.util.List;
8 |
9 | /**
10 | * Created by Administrator on 18-6-26.
11 | */
12 | public class PoolRedis {
13 | public static JedisPool jedisPool;
14 |
15 | static {
16 | if(jedisPool == null) {
17 | synchronized (PoolRedis.class) {
18 | if (jedisPool == null) {
19 | jedisPool = new JedisPool(BaseConstants.REDIS_DEFAULT_HOST, BaseConstants.REDIS_DEFAULT_PORT);
20 | }
21 | }
22 | }
23 | }
24 |
25 | public void destroy(){
26 | if(jedisPool != null){
27 | jedisPool.close();
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/redis-disconf/src/test/java/com/redis/disconf/AppTest.java:
--------------------------------------------------------------------------------
1 | package com.redis.disconf;
2 |
3 | import junit.framework.Test;
4 | import junit.framework.TestCase;
5 | import junit.framework.TestSuite;
6 |
7 | /**
8 | * Unit test for simple RedisServer.
9 | */
10 | public class AppTest
11 | extends TestCase
12 | {
13 | /**
14 | * Create the test case
15 | *
16 | * @param testName name of the test case
17 | */
18 | public AppTest( String testName )
19 | {
20 | super( testName );
21 | }
22 |
23 | /**
24 | * @return the suite of tests being tested
25 | */
26 | public static Test suite()
27 | {
28 | return new TestSuite( AppTest.class );
29 | }
30 |
31 | /**
32 | * Rigourous Test :-)
33 | */
34 | public void testApp()
35 | {
36 | assertTrue( true );
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/redis-pubsub/src/main/resources/spring/applicationContext.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/redis-base/src/main/java/com/redis/base/core/ClusterRedis.java:
--------------------------------------------------------------------------------
1 | package com.redis.base.core;
2 |
3 | import com.redis.base.BaseConstants;
4 | import redis.clients.jedis.*;
5 |
6 | import java.io.IOException;
7 |
8 | /**
9 | * Created by Administrator on 18-6-26.
10 | */
11 | public class ClusterRedis {
12 | public static JedisCluster jedisCluster;
13 |
14 | static {
15 | if(jedisCluster == null) {
16 | synchronized (ClusterRedis.class) {
17 | if (jedisCluster == null) {
18 | jedisCluster = new JedisCluster(BaseConstants.CLUSTER_HOST_AND_PORTS);
19 | }
20 | }
21 | }
22 | }
23 |
24 | public void destroy(){
25 | if(jedisCluster != null){
26 | try {
27 | jedisCluster.close();
28 | } catch (IOException e) {
29 | e.printStackTrace();
30 | }
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/redis-base/src/main/java/com/redis/base/core/ShardedRedis.java:
--------------------------------------------------------------------------------
1 | package com.redis.base.core;
2 |
3 | import com.redis.base.BaseConstants;
4 | import redis.clients.jedis.*;
5 |
6 | import java.util.ArrayList;
7 | import java.util.List;
8 |
9 | /**
10 | * Created by Administrator on 18-6-26.
11 | */
12 | public class ShardedRedis {
13 | public static ShardedJedisPool shardedJedisPool;
14 |
15 | static {
16 | if(shardedJedisPool == null) {
17 | synchronized (ShardedRedis.class) {
18 | if (shardedJedisPool == null) {
19 | List shards = new ArrayList<>();
20 | shards.add(new JedisShardInfo(BaseConstants.REDIS_DEFAULT_HOST, BaseConstants.REDIS_DEFAULT_PORT));
21 | shardedJedisPool = new ShardedJedisPool(new JedisPoolConfig(), shards);
22 | }
23 | }
24 | }
25 | }
26 |
27 | public void destroy(){
28 | if(shardedJedisPool != null){
29 | shardedJedisPool.close();
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/redis-base/pom.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 | redis
5 | com.redis
6 | 1.0-SNAPSHOT
7 |
8 | 4.0.0
9 |
10 | com.redis.base
11 | redis-base
12 | jar
13 |
14 | redis-base
15 | http://maven.apache.org
16 |
17 |
18 | org.springframework
19 | spring-test
20 | 5.0.4.RELEASE
21 | test
22 |
23 |
24 |
25 |
26 | UTF-8
27 |
28 |
29 |
--------------------------------------------------------------------------------
/redis-base/src/main/java/com/redis/base/BaseConstants.java:
--------------------------------------------------------------------------------
1 | package com.redis.base;
2 |
3 | import redis.clients.jedis.HostAndPort;
4 |
5 | import java.util.HashSet;
6 | import java.util.Set;
7 |
8 | /**
9 | * Created by Administrator on 18-3-20.
10 | */
11 | public interface BaseConstants {
12 | String REDIS_LIST_KEY = "redis_list";
13 | String REDIS_HASH_KEY = "redis_hash";
14 | String REDIS_SET_KEY = "redis_set";
15 | String REDIS_ZSET_KEY = "redis_zset";
16 | String REDIS_BIT_KEY = "redis_bit";
17 | String REDIS_HLL_KEY = "redis_hll";
18 |
19 | String REDIS_VISIT_USER = "user";
20 |
21 | String REDIS_DEFAULT_HOST = "localhost";
22 | int REDIS_DEFAULT_PORT = 6379;
23 |
24 | Set CLUSTER_HOST_AND_PORTS = new HashSet(){{
25 | add(new HostAndPort("127.0.0.1", 6380));
26 | add(new HostAndPort("127.0.0.1", 6381));
27 | add(new HostAndPort("127.0.0.1", 6382));
28 | add(new HostAndPort("127.0.0.1", 6383));
29 | add(new HostAndPort("127.0.0.1", 6384));
30 | add(new HostAndPort("127.0.0.1", 6385));
31 | }};
32 | }
33 |
--------------------------------------------------------------------------------
/redis-pubsub/src/main/java/com/redis/pubsub/service/impl/MessageDelegateListenerImpl.java:
--------------------------------------------------------------------------------
1 | package com.redis.pubsub.service.impl;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.springframework.data.redis.connection.Message;
6 | import org.springframework.data.redis.connection.MessageListener;
7 |
8 | import javax.annotation.PostConstruct;
9 | import java.io.UnsupportedEncodingException;
10 |
11 | /**
12 | * Spring 消息接口实现
13 | */
14 | public class MessageDelegateListenerImpl implements MessageListener {
15 | private static final Logger logger = LoggerFactory.getLogger(MessageDelegateListenerImpl.class);
16 |
17 | @PostConstruct
18 | public void postConstruct(){
19 |
20 | }
21 |
22 | public void onMessage(Message message, byte[] bytes) {
23 | try {
24 | logger.info("CCCCC-----{}: subscribe message, channel: {}, message: {}",
25 | getClass().getSimpleName(), new String(message.getChannel()),new String(message.getBody(), "UTF-8"));
26 | } catch (UnsupportedEncodingException e) {
27 | e.printStackTrace();
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/redis-disconf/pom.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 | redis
5 | com.redis
6 | 1.0-SNAPSHOT
7 |
8 | 4.0.0
9 |
10 | redis-disconf
11 | jar
12 |
13 | redis-disconf
14 | http://maven.apache.org
15 |
16 |
17 | UTF-8
18 |
19 |
20 |
21 |
22 | org.apache.commons
23 | commons-lang3
24 | 3.1
25 |
26 |
27 |
28 |
29 | com.baidu.disconf
30 | disconf-client
31 | 2.6.36
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/redis-pubsub/pom.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 | redis
5 | com.redis
6 | 1.0-SNAPSHOT
7 |
8 | 4.0.0
9 |
10 | com.redis.pubsub
11 | redis-pubsub
12 | jar
13 |
14 | redis-pubsub
15 | http://maven.apache.org
16 |
17 |
18 | org.apache.curator
19 | curator-framework
20 | 2.8.0
21 |
22 |
23 | org.apache.curator
24 | curator-recipes
25 | 2.8.0
26 |
27 |
28 |
29 |
30 | UTF-8
31 |
32 |
33 |
--------------------------------------------------------------------------------
/redis-pubsub/src/test/java/com/redis/pubsub/TestServer.java:
--------------------------------------------------------------------------------
1 | package com.redis.pubsub;
2 |
3 | import junit.framework.TestCase;
4 | import org.junit.Test;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.context.support.ClassPathXmlApplicationContext;
7 |
8 | import java.io.UnsupportedEncodingException;
9 |
10 | /**
11 | */
12 | public class TestServer extends TestCase {
13 |
14 | @Autowired
15 | RedisOpt opt;
16 | @Test
17 | public void testJedisBlpop() throws InterruptedException, UnsupportedEncodingException {
18 | opt.blockPull("test");
19 | }
20 |
21 | @Test
22 | public void testJedisSetValue() throws InterruptedException, UnsupportedEncodingException {
23 | opt.setJedisValue("test", "test");
24 | }
25 |
26 | @Override
27 | public void setUp() throws Exception {
28 | super.setUp();
29 | ClassPathXmlApplicationContext ct = new ClassPathXmlApplicationContext("classpath:/spring/*.xml");
30 | opt = (RedisOpt) ct.getBean("trr");
31 | }
32 |
33 | @Test
34 | public void testPub(){
35 | opt.jedisPublish("test");
36 | }
37 |
38 | @Test
39 | public void testSub(){
40 | opt.jedisSubscribe("test");
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/redis-base/src/main/java/com/redis/base/cluster/RedisCluster.java:
--------------------------------------------------------------------------------
1 | package com.redis.base.cluster;
2 |
3 | import com.redis.base.core.ClusterRedis;
4 | import org.slf4j.Logger;
5 | import org.slf4j.LoggerFactory;
6 | import java.util.concurrent.ThreadLocalRandom;
7 |
8 | /**
9 | * Redis multi 事务执行,执行一系列命令,每次命令返回QUEUED,表示执行命令已经放入队列等待执行
10 | * Created by Administrator on 18-3-20.
11 | */
12 | public class RedisCluster extends ClusterRedis {
13 | private static final Logger logger = LoggerFactory.getLogger(RedisCluster.class);
14 |
15 | public void set(String key, String value){
16 | if(jedisCluster == null) return;
17 | String result = jedisCluster.set(key, value);
18 | logger.info("cluster jedis set key: {} value: {} result: {}", key, value, result);
19 | }
20 |
21 | public void get(String key){
22 | if(jedisCluster == null) return;
23 | String result = jedisCluster.get(key);
24 | logger.info("cluster jedis get key: {} result: {}", key, result);
25 | }
26 |
27 | public static void main(String[] args) {
28 | RedisCluster cluster= new RedisCluster();
29 | cluster.set("test", "test" + ThreadLocalRandom.current().nextInt(155));
30 | cluster.get("test");
31 | cluster.destroy();
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/redis-disconf/src/main/java/com/redis/disconf/service/RedisService.java:
--------------------------------------------------------------------------------
1 | package com.redis.disconf.service;
2 |
3 | import com.redis.disconf.conf.JedisConfig;
4 | import com.redis.disconf.util.JedisUtil;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.stereotype.Service;
7 | import redis.clients.jedis.Jedis;
8 |
9 | import javax.annotation.PostConstruct;
10 | import javax.annotation.PreDestroy;
11 |
12 | /**
13 | * Created by windwant on 2017/6/8.
14 | */
15 | @Service
16 | public class RedisService {
17 |
18 | @Autowired
19 | private JedisConfig jedisConfig;
20 |
21 | @PostConstruct
22 | private void init(){
23 | JedisUtil.initJedisPool(jedisConfig.getHost(), jedisConfig.getPort());
24 | }
25 |
26 | @PreDestroy
27 | private void destroy(){
28 | JedisUtil.destory();
29 | }
30 |
31 | /**
32 | * 获取一个值
33 | *
34 | * @param key
35 | * @return
36 | */
37 | public String getKey(String key) {
38 | Jedis jedis = null;
39 | String value;
40 | try {
41 | jedis = JedisUtil.getJedis();
42 | value = jedis.get(key);
43 | }finally {
44 | JedisUtil.returnResource(jedis);
45 | }
46 | return value;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/redis-base/src/main/resources/log4j2.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | ${log.path}
5 | ${log.level}
6 |
7 |
8 |
9 |
10 |
12 |
13 |
14 |
16 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/redis-pubsub/src/main/resources/spring/redis_context.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
14 |
15 |
16 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/redis-base/src/main/resources/spring/jedis_pool_context.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/redis-base/src/main/java/com/redis/base/list/RedisListProducer.java:
--------------------------------------------------------------------------------
1 | package com.redis.base.list;
2 | import com.redis.base.BaseConstants;
3 | import org.springframework.beans.factory.annotation.Autowired;
4 | import org.springframework.stereotype.Component;
5 | import redis.clients.jedis.ShardedJedis;
6 | import redis.clients.jedis.ShardedJedisPool;
7 |
8 | import javax.annotation.PostConstruct;
9 | import java.util.concurrent.ThreadLocalRandom;
10 |
11 | /**
12 | * Created by Administrator on 18-3-20.
13 | */
14 | @Component
15 | public class RedisListProducer implements Runnable {
16 |
17 | @Autowired
18 | private ShardedJedisPool shardedJedisPool;
19 |
20 | public void run(){
21 | ShardedJedis jedis = shardedJedisPool.getResource();
22 | if(jedis == null) return;
23 | int i = 0;
24 | try {
25 | while (i < Integer.MAX_VALUE){
26 | jedis.lpush(BaseConstants.REDIS_LIST_KEY, String.valueOf(i));
27 | System.out.println("produce list ele: " + i);
28 | Thread.sleep(ThreadLocalRandom.current().nextInt(3)*1000);
29 | i++;
30 | }
31 | } catch (InterruptedException e) {
32 | } finally {
33 | if(jedis != null){
34 | jedis.close();
35 | }
36 | }
37 | }
38 |
39 | @PostConstruct
40 | public void produce(){
41 | new Thread(this).start();
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/redis-base/src/main/java/com/redis/base/multi/RedisMulti.java:
--------------------------------------------------------------------------------
1 | package com.redis.base.multi;
2 |
3 | import com.redis.base.BaseConstants;
4 | import com.redis.base.core.PoolRedis;
5 | import org.slf4j.Logger;
6 | import org.slf4j.LoggerFactory;
7 | import redis.clients.jedis.*;
8 |
9 | import java.util.ArrayList;
10 | import java.util.List;
11 |
12 | /**
13 | * Redis multi 事务执行,执行一系列命令,每次命令返回QUEUED,表示执行命令已经放入队列等待执行
14 | * Created by Administrator on 18-3-20.
15 | */
16 | public class RedisMulti extends PoolRedis {
17 | private static final Logger logger = LoggerFactory.getLogger(RedisMulti.class);
18 |
19 | public void multiLPush(String key, List values){
20 | Jedis jedis = jedisPool.getResource();
21 |
22 | if(jedis == null) return;
23 | try {
24 | Transaction t = jedis.multi();
25 | values.stream().forEach(value->t.lpush(key, value));
26 | List> responses = t.execGetResponse();
27 | logger.info("multi opt result list {} size {}", key, jedis.llen(key));
28 | } finally {
29 | if(jedis != null){
30 | jedis.close();
31 | }
32 | }
33 | }
34 |
35 | public static void main(String[] args) {
36 | RedisMulti multi = new RedisMulti();
37 | multi.multiLPush(BaseConstants.REDIS_LIST_KEY, new ArrayList() {{
38 | add("a");
39 | add("b");
40 | }});
41 | multi.destroy();
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/redis-pubsub/src/main/resources/log4j2.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | /
5 | info
6 |
7 |
8 |
9 |
10 |
12 |
13 |
14 |
16 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/redis-pubsub/src/main/java/com/redis/pubsub/service/impl/SelfDefineMessageDelegateImpl.java:
--------------------------------------------------------------------------------
1 | package com.redis.pubsub.service.impl;
2 |
3 | import com.redis.pubsub.service.SelfDefineMessageDelegate;
4 | import org.slf4j.Logger;
5 | import org.slf4j.LoggerFactory;
6 |
7 | import java.io.Serializable;
8 | import java.util.Map;
9 |
10 | /**
11 | * 自定义消息接口实现
12 | */
13 | public class SelfDefineMessageDelegateImpl implements SelfDefineMessageDelegate {
14 | private static final Logger logger = LoggerFactory.getLogger(SelfDefineMessageDelegateImpl.class);
15 |
16 | public void handleMessage(String message) {
17 | logger.info("handleMessage(String message): {}", message);
18 | }
19 |
20 | public void handleMessage(Map, ?> message) {
21 | logger.info("handleMessage(Map, ?> message): {}", message);
22 | }
23 |
24 | public void handleMessage(byte[] message) {
25 | logger.info("CCCCC-----: " + getClass().getSimpleName() + " handleMessage(byte[] message):"
26 | + new String(message));
27 | }
28 |
29 | public void handleMessage(Serializable message) {
30 | logger.info("CCCCC-----: {} handleMessage(Serializable message):", getClass().getSimpleName(), message.toString());
31 | }
32 |
33 | public void handleMessage(Serializable message, String channel) {
34 | logger.info("CCCCC-----: {}, handleMessage(Serializable message, String channel): {}, channel: {}",
35 | getClass().getSimpleName(), message.toString(), channel);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/redis-pubsub/src/main/java/com/redis/pubsub/server/PublishSubscribeServer.java:
--------------------------------------------------------------------------------
1 | package com.redis.pubsub.server;
2 |
3 | import com.redis.pubsub.RedisOpt;
4 | import org.slf4j.Logger;
5 | import org.slf4j.LoggerFactory;
6 | import org.springframework.context.support.ClassPathXmlApplicationContext;
7 | import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
8 |
9 | /**
10 | * pubsub server.
11 | */
12 | public class PublishSubscribeServer {
13 |
14 | private static final Logger logger = LoggerFactory.getLogger(PublishSubscribeServer.class);
15 |
16 | public static void main(String[] args) {
17 | ClassPathXmlApplicationContext ct = new ClassPathXmlApplicationContext("classpath:/spring/*.xml");
18 | JedisConnectionFactory jf = (JedisConnectionFactory) ct.getBean("jedisConnFactory");
19 | RedisOpt tr = (RedisOpt) ct.getBean("trr");
20 |
21 | String channel = "springdata";
22 | String message = "010_京u8264".trim();
23 | while (!Thread.currentThread().isInterrupted()) {
24 | try {
25 | for (int i = 0; i < Integer.MAX_VALUE; i++) {
26 | tr.convertAndSend(channel, message);
27 | logger.info("PPPPP+++++: publish message, channel: {}, message: {}", channel, message);
28 | Thread.sleep(2000);
29 | }
30 | break;
31 | } catch (Exception e) {
32 | e.printStackTrace();
33 | jf.destroy();//重连
34 | jf.afterPropertiesSet();
35 | }
36 | }
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/redis-pubsub/src/main/resources/spring/redis_listener.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/redis-disconf/src/main/resources/applicationContext.xml:
--------------------------------------------------------------------------------
1 |
2 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
23 |
24 |
25 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/redis-disconf/src/main/java/com/redis/disconf/conf/JedisConfig.java:
--------------------------------------------------------------------------------
1 | package com.redis.disconf.conf;
2 |
3 | import com.baidu.disconf.client.common.annotations.DisconfFile;
4 | import com.baidu.disconf.client.common.annotations.DisconfFileItem;
5 | import com.baidu.disconf.client.common.update.IDisconfUpdate;
6 | import org.slf4j.Logger;
7 | import org.slf4j.LoggerFactory;
8 | import org.springframework.context.annotation.Scope;
9 | import org.springframework.stereotype.Service;
10 |
11 | /**
12 | * Redis配置文件 获取不到则使用本地
13 | *
14 | * @author windwant
15 | * @version 2017-6-8
16 | */
17 | @Service
18 | @Scope("singleton")
19 | @DisconfFile(filename = "redis.properties") //配置中心 配置文件名
20 | public class JedisConfig implements IDisconfUpdate {
21 |
22 | private static final Logger logger = LoggerFactory.getLogger(JedisConfig.class);
23 |
24 | // 代表连接地址
25 | private String host;
26 |
27 | // 代表连接port
28 | private int port;
29 |
30 | /**
31 | * 地址
32 | *
33 | * @return
34 | */
35 | @DisconfFileItem(name = "redis.host", associateField = "host") //对应配置item
36 | public String getHost() {
37 | return host;
38 | }
39 |
40 | public void setHost(String host) {
41 | this.host = host;
42 | }
43 |
44 | /**
45 | * 端口
46 | *
47 | * @return
48 | */
49 | @DisconfFileItem(name = "redis.port", associateField = "port")
50 | public int getPort() {
51 | return port;
52 | }
53 |
54 | public void setPort(int port) {
55 | this.port = port;
56 | }
57 |
58 | //监听重载
59 | public void reload() throws Exception {
60 | logger.info("properties reload: host {}, port {}", host, port);
61 | }
62 | }
--------------------------------------------------------------------------------
/redis-base/src/main/resources/spring/jedis_sentinel_context.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 | 127.0.0.1:6377
25 | 127.0.0.1:6376
26 |
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/redis-pubsub/src/main/java/com/redis/pubsub/server/PubSubWithoutSpringServer.java:
--------------------------------------------------------------------------------
1 | package com.redis.pubsub.server;
2 |
3 | import com.redis.pubsub.service.impl.RedisPubSubListener;
4 | import org.slf4j.Logger;
5 | import org.slf4j.LoggerFactory;
6 | import redis.clients.jedis.Jedis;
7 | import redis.clients.jedis.JedisPool;
8 |
9 | /**
10 | * pubsub server.
11 | */
12 | public class PubSubWithoutSpringServer {
13 | static JedisPool jedisPool = null;
14 | static String channel = "springdata";
15 | static{
16 | jedisPool = new JedisPool("localhost", 6379);
17 | }
18 |
19 | private static final Logger logger = LoggerFactory.getLogger(PubSubWithoutSpringServer.class);
20 |
21 | public static void main(String[] args) {
22 | new Thread(new Runnable() {
23 | @Override
24 | public void run() {
25 | jedisPool.getResource().subscribe(new RedisPubSubListener(), channel.getBytes());
26 | }
27 | }).start();
28 |
29 | String message = "010_京u8264".trim();
30 | Jedis jedis = jedisPool.getResource();
31 | try {
32 | while (!Thread.currentThread().isInterrupted()) {
33 |
34 | for (int i = 0; i < Integer.MAX_VALUE; i++) {
35 | jedis.publish(channel, message);
36 | logger.info("PPPPP+++++: publish message, channel: {}, message: {}", channel, message);
37 | Thread.sleep(2000);
38 | }
39 | break;
40 | }
41 | } catch (Exception e) {
42 | e.printStackTrace();
43 | }finally {
44 | jedis.close();//重连
45 | jedisPool.close();
46 | }
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/redis-base/src/main/resources/spring/jedis_sharded_context.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/redis-disconf/src/main/java/com/redis/disconf/service/DisconfTask.java:
--------------------------------------------------------------------------------
1 | package com.redis.disconf.service;
2 |
3 | import com.redis.disconf.conf.JedisConfig;
4 | import com.redis.disconf.util.JedisUtil;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.stereotype.Service;
7 |
8 | /**
9 | * Created by windwant on 2017/6/8.
10 | */
11 | @Service
12 | public class DisconfTask {
13 |
14 | @Autowired
15 | private RedisService redisService;
16 |
17 | @Autowired
18 | private JedisConfig jedisConfig;
19 |
20 | private static final String REDIS_KEY = "distribute_key";
21 | private static final String REDIS_VALUE = "distribute_value";
22 |
23 | /**
24 | *
25 | */
26 | public int run() {
27 | try {
28 | boolean lock = generateLock(); //设置测试分布式锁 k v 过期时间 10s
29 | while (lock) {
30 | Thread.sleep(5000);//每个5秒 获取一次 k v
31 | String value = redisService.getKey(REDIS_KEY);
32 | System.out.println("redis( " + jedisConfig.getHost() + ","
33 | + jedisConfig.getPort() + ") get key: " + REDIS_KEY + " value: " + value);
34 | if(value == null) {
35 | System.out.println("ttl, lock key invalid");
36 | break; //锁过期 跳出循环
37 | }
38 | }
39 | } catch (Exception e) {
40 | e.printStackTrace();
41 | }
42 |
43 | return 0;
44 | }
45 |
46 | /**
47 | * 生成锁 k v
48 | * @return
49 | */
50 | private boolean generateLock(){
51 | boolean result = JedisUtil.tryLock(REDIS_KEY, REDIS_VALUE, 10);
52 | System.out.println("set lock key: " + REDIS_KEY + ", value: " + REDIS_VALUE + ", seconds: " + 10 + ", result: " + result);
53 | return result;
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/redis-pubsub/src/main/resources/spring/redis_spring_listener.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
11 |
12 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
22 |
23 |
24 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/redis-pubsub/src/main/java/com/redis/pubsub/util/FastJsonMessageConverter.java:
--------------------------------------------------------------------------------
1 | package com.redis.pubsub.util;
2 |
3 | import com.alibaba.fastjson.JSON;
4 | import org.apache.commons.logging.Log;
5 | import org.apache.commons.logging.LogFactory;
6 | import org.springframework.amqp.core.Message;
7 | import org.springframework.amqp.core.MessageProperties;
8 | import org.springframework.amqp.support.converter.AbstractMessageConverter;
9 | import org.springframework.amqp.support.converter.MessageConversionException;
10 |
11 | import java.io.UnsupportedEncodingException;
12 |
13 | /**
14 | * json 数据转换
15 | */
16 | public class FastJsonMessageConverter extends AbstractMessageConverter {
17 |
18 | private static Log log = LogFactory.getLog(FastJsonMessageConverter.class);
19 |
20 | public static final String DEFAULT_CHARSET = "UTF-8";
21 |
22 | private volatile String defaultCharset = DEFAULT_CHARSET;
23 |
24 | public FastJsonMessageConverter() {
25 | super();
26 | }
27 |
28 | public void setDefaultCharset(String defaultCharset) {
29 | this.defaultCharset = defaultCharset != null?defaultCharset:DEFAULT_CHARSET;
30 | }
31 |
32 | @Override
33 | protected Message createMessage(Object objectToConvert, MessageProperties messageProperties) {
34 | byte[] bytes = null;
35 | try {
36 | String jsonString = JSON.toJSONString(objectToConvert);
37 | bytes = jsonString.getBytes(this.defaultCharset);
38 | } catch (UnsupportedEncodingException e) {
39 | throw new MessageConversionException(
40 | "Failed to convert Message content", e);
41 | }
42 | messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
43 | messageProperties.setContentEncoding(this.defaultCharset);
44 | if (bytes != null) {
45 | messageProperties.setContentLength(bytes.length);
46 | }
47 | return new Message(bytes, messageProperties);
48 | }
49 |
50 | @Override
51 | public Object fromMessage(Message message) throws MessageConversionException {
52 | return null;
53 | }
54 |
55 | public T fromMessage(Message message,T t) {
56 | String json = "";
57 | try {
58 | json = new String(message.getBody(),defaultCharset);
59 | } catch (UnsupportedEncodingException e) {
60 | e.printStackTrace();
61 | }
62 | return (T) JSON.parseObject(json, t.getClass());
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/redis-base/README.md:
--------------------------------------------------------------------------------
1 | #cache
2 |
3 | JedisPool jedis 链接连接池 单机
4 |
5 | ShardedJedisPool 切片连接池 分布式(多个redis 运行实例) 根据一致性那个hash算法动态均匀存储及获取key-value
6 |
7 | JedisSentinelPool 哨兵模式
8 |
9 | 运行多个redis实例 配置主从配置 slaveof host port
10 |
11 | 配置sentinel实例
12 |
13 | port 6376 //sentinel端口
14 |
15 | sentinel monitor mymaster 127.0.0.1 6379 2 //master配置
16 |
17 | sentinel down-after-milliseconds mymaster 60000
18 |
19 | sentinel failover-timeout mymaster 180000
20 |
21 | sentinel parallel-syncs mymaster 1
22 |
23 | port 6377 //sentinel端口
24 |
25 | sentinel monitor mymaster 127.0.0.1 6379 2 //master配置
26 |
27 | sentinel down-after-milliseconds mymaster 60000
28 |
29 | sentinel failover-timeout mymaster 180000
30 |
31 | sentinel parallel-syncs mymaster 1
32 |
33 |
34 |
35 | Redis Sentinel provides high availability for Redis. In practical terms this means that using Sentinel you can create a Redis deployment that resists without human intervention to certain kind of failures.
36 | Redis Sentinel also provides other collateral tasks such as monitoring, notifications and acts as a configuration provider for clients.
37 | This is the full list of Sentinel capabilities at a macroscopical level (i.e. the big picture):
38 |
39 | * Monitoring. Sentinel constantly checks if your master and slave instances are working as expected.
40 |
41 | * Notification. Sentinel can notify the system administrator, another computer programs, via an API,
42 | that something is wrong with one of the monitored Redis instances.
43 |
44 | * Automatic failover. If a master is not working as expected, Sentinel can start a failover process where a slave is promoted to master,
45 | the other additional slaves are reconfigured to use the new master, and the applications using the Redis server informed about the
46 | new address to use when connecting.
47 |
48 | * Configuration provider. Sentinel acts as a source of authority for clients service discovery: clients connect to Sentinels in order to
49 | ask for the address of the current Redis master responsible for a given service. If a failover occurs, Sentinels will report the new address.
50 |
51 | 二进制安全的字符串
52 | Lists: 按插入顺序排序的字符串元素的集合。他们基本上就是链表(linked lists)。
53 | Sets: 不重复且无序的字符串元素的集合。
54 | Sorted sets,类似Sets,但是每个字符串元素都关联到一个叫score浮动数值(floating number value)。里面的元素总是通过score进行着排序,所以不同的是,它是可以检索的一系列元素。(例如你可能会问:给我前面10个或者后面10个元素)。
55 | Hashes,由field和关联的value组成的map。field和value都是字符串的。这和Ruby、Python的hashes很像。
56 | Bit arrays (或者说 simply bitmaps): 通过特殊的命令,你可以将 String 值当作一系列 bits 处理:可以设置和清除单独的 bits,数出所有设为 1 的 bits 的数量,找到最前的被设为 1 或 0 的 bit,等等。
57 | HyperLogLogs: 这是被用于估计一个 set 中元素数量的概率性的数据结构。别害怕,它比看起来的样子要简单…参见本教程的 HyperLogLog 部分。D
--------------------------------------------------------------------------------
/redis-base/src/main/resources/spring/jedis_sentinel_spring.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/redis-base/src/main/java/com/redis/base/list/RedisListConsumer.java:
--------------------------------------------------------------------------------
1 | package com.redis.base.list;
2 |
3 | import com.redis.base.BaseConstants;
4 | import org.slf4j.Logger;
5 | import org.slf4j.LoggerFactory;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.stereotype.Component;
8 | import redis.clients.jedis.ShardedJedis;
9 | import redis.clients.jedis.ShardedJedisPool;
10 |
11 | import javax.annotation.PostConstruct;
12 | import java.util.List;
13 | import java.util.concurrent.ThreadLocalRandom;
14 |
15 | /**
16 | * Created by Administrator on 18-3-20.
17 | */
18 | @Component
19 | public class RedisListConsumer implements Runnable {
20 | private static final Logger logger = LoggerFactory.getLogger(RedisListConsumer.class);
21 |
22 | @Autowired
23 | private ShardedJedisPool shardedJedisPool;
24 |
25 | public void run(){
26 | ShardedJedis jedis = shardedJedisPool.getResource();
27 | if(jedis == null) return;
28 | try {
29 | Thread.sleep(10000);
30 | while (true) {
31 | printList(jedis);
32 | System.out.println("consumer begin rpop " + BaseConstants.REDIS_LIST_KEY + " list ele ... ");
33 | //brpop get ele from list tail
34 | List redisList = jedis.brpop(5, BaseConstants.REDIS_LIST_KEY);
35 | if (redisList != null && !redisList.isEmpty()) {
36 | System.out.println("consume " + BaseConstants.REDIS_LIST_KEY + " list ele: " + redisList.get(1));
37 | }
38 | // Thread.sleep(ThreadLocalRandom.current().nextInt(3)*1000);
39 | }
40 | } catch (InterruptedException e) {
41 |
42 | } finally {
43 | if(jedis != null){
44 | jedis.close();
45 | }
46 | }
47 | }
48 |
49 | private void printList(ShardedJedis jedis){
50 | long size = jedis.llen(BaseConstants.REDIS_LIST_KEY);
51 | System.out.println(BaseConstants.REDIS_LIST_KEY + " list size: " + size);
52 | if(size > 0) {
53 | System.out.println(BaseConstants.REDIS_LIST_KEY + " eles: ");
54 | int from = 0;
55 | int pageSize = 10;
56 | long page = size%pageSize == 0? size/pageSize: (size/pageSize + 1);
57 | for (int i = 0; i < page; i++) {
58 | System.out.println(jedis.lrange(BaseConstants.REDIS_LIST_KEY, from, from + pageSize - 1));
59 | from += pageSize;
60 | }
61 | }
62 | }
63 |
64 | @PostConstruct
65 | public void consume(){
66 | new Thread(this).start();
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/redis-base/src/main/java/com/redis/base/JedisOpt.java:
--------------------------------------------------------------------------------
1 | package com.redis.base;
2 |
3 | import org.springframework.beans.factory.annotation.Autowired;
4 | import org.springframework.context.ApplicationContext;
5 | import org.springframework.context.support.ClassPathXmlApplicationContext;
6 | import org.springframework.data.redis.core.StringRedisTemplate;
7 | import org.springframework.stereotype.Component;
8 | import redis.clients.jedis.Jedis;
9 | import redis.clients.jedis.JedisPool;
10 | import redis.clients.jedis.JedisSentinelPool;
11 | import redis.clients.jedis.ShardedJedis;
12 | import redis.clients.jedis.ShardedJedisPool;
13 |
14 | /**
15 | * Hello world!
16 | *
17 | */
18 | public class JedisOpt
19 | {
20 |
21 | @Autowired
22 | private ShardedJedisPool shardedJedisPool;
23 |
24 | @Autowired
25 | private JedisPool jedisPool;
26 |
27 | @Autowired
28 | private JedisSentinelPool jedisSentinelPool;
29 |
30 | @Autowired
31 | private StringRedisTemplate stringRedisTemplate;
32 |
33 | public ShardedJedis getShardedRedisClient() {
34 | ShardedJedis shardJedis = null;
35 | try {
36 | shardJedis = shardedJedisPool.getResource();
37 | return shardJedis;
38 | } catch (Exception e) {
39 | if (null != shardJedis)
40 | shardJedis.close();
41 | }
42 | return null;
43 | }
44 |
45 | public void returnShardedResource(ShardedJedis shardedJedis) {
46 | shardedJedis.close();
47 | }
48 |
49 | public void returnShardedResource(ShardedJedis shardedJedis, boolean broken) {
50 | shardedJedis.close();
51 | }
52 |
53 | public Jedis getRedisClient() {
54 | Jedis jedis = null;
55 | try {
56 | jedis = jedisPool.getResource();
57 | return jedis;
58 | } catch (Exception e) {
59 | if (null != jedis)
60 | jedis.close();
61 | }
62 | return null;
63 | }
64 |
65 | public void returnResource(Jedis jedis) {
66 | jedis.close();
67 | }
68 |
69 | public void returnResource(Jedis jedis, boolean broken) {
70 | jedis.close();
71 | }
72 |
73 | public Jedis getRedisSentinelClient() {
74 | Jedis jedis = null;
75 | try {
76 | jedis = jedisSentinelPool.getResource();
77 | return jedis;
78 | } catch (Exception e) {
79 | if (null != jedis)
80 | jedis.close();
81 | }
82 | return null;
83 | }
84 |
85 | public void returnSentinelResource(Jedis jedis) {
86 | jedis.close();
87 | }
88 |
89 | public void returnSentinelResource(Jedis jedis, boolean broken) {
90 | jedis.close();
91 | }
92 |
93 | public StringRedisTemplate getSentinelClientOne(){
94 | return stringRedisTemplate;
95 | }
96 | }
97 |
--------------------------------------------------------------------------------
/redis-base/src/test/java/com/redis/base/JedisTest.java:
--------------------------------------------------------------------------------
1 | package com.redis.base;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.data.redis.core.StringRedisTemplate;
7 | import org.springframework.test.context.ContextConfiguration;
8 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
9 | import redis.clients.jedis.Jedis;
10 | import redis.clients.jedis.JedisSentinelPool;
11 | import redis.clients.jedis.ShardedJedis;
12 |
13 | import java.util.HashSet;
14 | import java.util.Set;
15 |
16 | /**
17 | * Unit test for simple JedisOpt.
18 | */
19 | @RunWith(SpringJUnit4ClassRunner.class)
20 | @ContextConfiguration({"classpath:spring/applicationContext.xml",
21 | "classpath:spring/jedis_sharded_context.xml",
22 | "classpath:spring/jedis_sentinel_context.xml",
23 | "classpath:spring/jedis_sentinel_spring.xml",
24 | "classpath:spring/jedis_pool_context.xml"})
25 | public class JedisTest
26 | {
27 |
28 | @Autowired
29 | JedisOpt jedisOpt;
30 |
31 | @Test
32 | public void testShardedJedis(){
33 | ShardedJedis shardedJedis = jedisOpt.getShardedRedisClient();
34 | // shardedJedis.set("name", "lilei");
35 | System.out.println(shardedJedis.get("name"));
36 | shardedJedis.append("name", "&hanmeimei");
37 | System.out.println(shardedJedis.get("name"));
38 | }
39 |
40 | @Test
41 | public void testJedis(){
42 | Jedis jedis = jedisOpt.getRedisClient();
43 | jedis.set("name", "lilei");
44 | System.out.println(jedis.get("name"));
45 | jedis.append("name", "&hanmeimei");
46 | System.out.println(jedis.get("name"));
47 | }
48 |
49 | @Test
50 | public void testSentinelJedis(){
51 | Jedis jedis = jedisOpt.getRedisClient();
52 | jedis.set("name", "lilei");
53 | System.out.println(jedis.get("name"));
54 | jedis.append("name", "&hanmeimei");
55 | System.out.println(jedis.get("name"));
56 | }
57 |
58 | @Test
59 | public void test() {
60 | Set sentinels = new HashSet();
61 | sentinels.add("127.0.0.1:6377");
62 |
63 | /**
64 | * masterName 分片的名称
65 | * sentinels Redis Sentinel 服务地址列表
66 | */
67 | JedisSentinelPool poolA = new JedisSentinelPool("mymaster", sentinels);
68 | //获取Jedis主服务器客户端实例
69 | Jedis jedisA = poolA.getResource();
70 | jedisA.set("key", "abc");
71 |
72 |
73 | System.out.println("jedisA key:"+jedisA.get("key"));
74 | //输出结果
75 | //jedisA key:abc
76 | }
77 |
78 | @Test
79 | public void testSentinel(){
80 | StringRedisTemplate st = jedisOpt.getSentinelClientOne();
81 | st.opsForValue().set("word", "hello");
82 | System.out.println(st.opsForValue().get("word"));
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/redis-disconf/src/main/java/com/redis/disconf/util/JedisUtil.java:
--------------------------------------------------------------------------------
1 | package com.redis.disconf.util;
2 |
3 | import redis.clients.jedis.Jedis;
4 | import redis.clients.jedis.JedisPool;
5 |
6 | /**
7 | * Created by windwant on 2017/6/8.
8 | */
9 | public class JedisUtil {
10 |
11 | private static volatile JedisPool jp;
12 |
13 | /**
14 | * 初始化redis pool
15 | * @param host
16 | * @param port
17 | */
18 | public static void initJedisPool(String host, Integer port){
19 | if(jp == null){
20 | synchronized (JedisUtil.class) {
21 | jp = new JedisPool(host, port);
22 | }
23 | }
24 | }
25 |
26 | /**
27 | * 获取Jedis实例
28 | * @return
29 | */
30 | public static Jedis getJedis(){
31 | if(jp != null){
32 | return jp.getResource();
33 | }
34 | return null;
35 | }
36 |
37 | /**
38 | * 注销pool
39 | */
40 | public static void destory(){
41 | if(jp != null) {
42 | synchronized (JedisUtil.class) {
43 | if(jp != null){
44 | jp.close();
45 | jp = null;
46 | }
47 | }
48 | }
49 | }
50 |
51 | /**
52 | * 释放
53 | * @param jedis
54 | */
55 | public static void returnResource(Jedis jedis){
56 | if(jedis != null){
57 | jedis.close();
58 | }
59 | }
60 |
61 |
62 | //设置锁的lua脚本
63 | private static final String SETNX_EXPIRE_SCRIPT = "if redis.call('setnx', KEYS[1], KEYS[2]) == 1 then\n"
64 | + "return redis.call('expire', KEYS[1], KEYS[3]);\n" + "end\n" + "return nil;";
65 |
66 | /**
67 | * 设置锁的lua脚本
68 | * private static final String SETNX_EXPIRE_SCRIPT = "if redis.call('setnx', KEYS[1], KEYS[2]) == 1 then\n"
69 | * "return redis.call('expire', KEYS[1], KEYS[3]);\n" + "end\n" + "return nil;";
70 | *
71 | * @param key
72 | * @return
73 | */
74 | public static boolean tryLock(String key, String value, Integer seconds) {
75 | Jedis jedis = getJedis();
76 | if (jedis == null) return false;
77 | boolean lock = false;
78 | try{
79 | lock = jedis.eval(SETNX_EXPIRE_SCRIPT, 3, key, value, String.valueOf(seconds)) != null;
80 | }finally {
81 | returnResource(jedis);
82 | }
83 | return lock;
84 | }
85 |
86 | /**
87 | * 释放锁
88 | * @param key
89 | * @return
90 | */
91 | public static boolean unLock(String key) {
92 | Jedis jedis = getJedis();
93 | if (jedis == null) return false;
94 | boolean unlock = false;
95 | try{
96 | unlock = jedis.del(key) != null;
97 | }finally {
98 | returnResource(jedis);
99 | }
100 | return unlock;
101 | }
102 |
103 | }
104 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 | com.redis
5 | redis
6 | pom
7 | 1.0-SNAPSHOT
8 |
9 | redis-base
10 | redis-pubsub
11 | redis-disconf
12 |
13 | redis Maven Webapp
14 | http://maven.apache.org
15 |
16 |
17 |
18 |
19 | org.apache.maven.plugins
20 | maven-compiler-plugin
21 | 3.1
22 |
23 | 1.8
24 | 1.8
25 | UTF8
26 |
27 |
28 |
29 |
30 |
31 |
32 | junit
33 | junit
34 | 4.12
35 | test
36 |
37 |
38 |
39 | org.springframework.data
40 | spring-data-commons
41 | 2.0.5.RELEASE
42 |
43 |
44 | org.springframework.data
45 | spring-data-redis
46 | 2.0.5.RELEASE
47 |
48 |
49 |
50 | redis.clients
51 | jedis
52 | 2.9.0
53 |
54 |
55 | org.springframework
56 | spring-web
57 | 5.0.4.RELEASE
58 |
59 |
60 |
61 | com.rabbitmq
62 | amqp-client
63 | 3.6.0
64 |
65 |
66 |
67 | org.springframework.amqp
68 | spring-rabbit
69 | 2.0.2.RELEASE
70 |
71 |
72 |
73 | com.alibaba
74 | fastjson
75 | 1.2.7
76 |
77 |
78 |
79 |
80 | org.apache.logging.log4j
81 | log4j-core
82 | 2.10.0
83 |
84 |
85 |
86 | org.slf4j
87 | slf4j-api
88 | 1.7.25
89 |
90 |
91 |
92 | org.apache.logging.log4j
93 | log4j-slf4j-impl
94 | 2.10.0
95 |
96 |
97 |
98 |
99 |
--------------------------------------------------------------------------------
/redis-pubsub/src/main/java/com/redis/pubsub/monitor/ZookeeperMonitor.java:
--------------------------------------------------------------------------------
1 | package com.redis.pubsub.monitor;
2 |
3 | import org.apache.curator.RetryPolicy;
4 | import org.apache.curator.framework.CuratorFramework;
5 | import org.apache.curator.framework.CuratorFrameworkFactory;
6 | import org.apache.curator.framework.recipes.cache.PathChildrenCache;
7 | import org.apache.curator.framework.recipes.cache.PathChildrenCache.StartMode;
8 | import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
9 | import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent.Type;
10 | import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
11 | import org.apache.curator.retry.ExponentialBackoffRetry;
12 | import org.apache.zookeeper.CreateMode;
13 | import org.slf4j.Logger;
14 | import org.slf4j.LoggerFactory;
15 | import org.springframework.beans.factory.annotation.Value;
16 | import org.springframework.stereotype.Component;
17 |
18 | import javax.annotation.PostConstruct;
19 |
20 | /**
21 | * zookeeper服务监控
22 | */
23 | @Component
24 | public class ZookeeperMonitor {
25 | private static final Logger logger = LoggerFactory.getLogger(ZookeeperMonitor.class);
26 |
27 | @Value("${zookeeper.host}")
28 | private String host;
29 | @Value("${zookeeper.path}")
30 | private String path;
31 |
32 | public String getHost() {
33 | return host;
34 | }
35 |
36 | public void setHost(String host) {
37 | this.host = host;
38 | }
39 |
40 | public String getPath() {
41 | return path;
42 | }
43 |
44 | public void setPath(String path) {
45 | this.path = path;
46 | }
47 |
48 | public String getParentPath() {
49 | return parentPath;
50 | }
51 |
52 | public void setParentPath(String parentPath) {
53 | this.parentPath = parentPath;
54 | }
55 |
56 | @Value("${zookeeper.parent.path}")
57 | private String parentPath; //注册父路径
58 |
59 | private CuratorFramework curatorFramework; //high-level API that greatly simplifies using ZooKeeper
60 |
61 | public ZookeeperMonitor() {
62 |
63 | }
64 |
65 | @PostConstruct //method that needs to be executed after dependency injection is done to perform any initialization
66 | public void start(){
67 | //重试策略 ExponentialBackoffRetry:重试指定的次数, 且每一次重试之间停顿的时间逐渐增加
68 | RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
69 | //Factory methods for creating framework-style clients
70 | curatorFramework = CuratorFrameworkFactory.builder().connectString(host).retryPolicy(retryPolicy).build();
71 | curatorFramework.start();
72 |
73 | //utility that attempts to keep all data from all children of a ZK path locally cached watch and response
74 | final PathChildrenCache pathChildrenCache = new PathChildrenCache(curatorFramework, parentPath, true);
75 | try {
76 | //cached之后分发事件
77 | pathChildrenCache.start(StartMode.POST_INITIALIZED_EVENT);
78 | //添加监听
79 | pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
80 | public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
81 | if(event.getType() == Type.CHILD_REMOVED){
82 | if((parentPath + path).equals(event.getData().getPath())){
83 | logger.info("path: {}, removed, begin recreate!", path);
84 | createNode();
85 | }
86 | }
87 | }
88 | });
89 | } catch (Exception e) {
90 | e.printStackTrace();
91 | }
92 |
93 | createNode();
94 | }
95 |
96 | /**
97 | * 创建节点
98 | * @return
99 | */
100 | protected boolean createNode(){
101 | try {
102 | String nodePath = parentPath + path;
103 | if(curatorFramework.checkExists().forPath(nodePath) == null){
104 | curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(nodePath);
105 | logger.info("create node path: {}", nodePath);
106 | return true;
107 | }
108 | } catch (Exception e) {
109 | e.printStackTrace();
110 | }
111 | return false;
112 | }
113 | }
114 |
--------------------------------------------------------------------------------
/redis-base/src/main/java/com/redis/base/bithll/RedisBitHll.java:
--------------------------------------------------------------------------------
1 | package com.redis.base.bithll;
2 |
3 | import com.redis.base.BaseConstants;
4 | import com.redis.base.core.PoolRedis;
5 | import org.slf4j.Logger;
6 | import org.slf4j.LoggerFactory;
7 | import redis.clients.jedis.BitOP;
8 | import redis.clients.jedis.Jedis;
9 |
10 | import java.util.concurrent.ThreadLocalRandom;
11 |
12 | /**
13 | * Redis bit visit statistic
14 | * Created by Administrator on 18-3-20.
15 | */
16 | public class RedisBitHll extends PoolRedis implements Runnable{
17 | private static final Logger logger = LoggerFactory.getLogger(RedisBitHll.class);
18 |
19 | private int duration;
20 |
21 | public void setbit(String key, long offset, int isOrNot){
22 | Jedis jedis = jedisPool.getResource();
23 | if(jedis == null) return;
24 | try {
25 | jedis.setbit(key, offset, String.valueOf(isOrNot));
26 | }finally {
27 | jedis.close();
28 | }
29 | }
30 |
31 | /**
32 | * hll pfadd
33 | * @param key
34 | * @param value
35 | */
36 | public void pfadd(String key, String value){
37 | Jedis jedis = jedisPool.getResource();
38 | if(jedis == null) return;
39 | try {
40 | jedis.pfadd(key, value);
41 | }finally {
42 | jedis.close();
43 | }
44 | }
45 |
46 | /**
47 | * count distinct ele
48 | * 因为 HyperLogLog 只会根据输入元素来计算基数,而不会储存输入元素本身,所以 HyperLogLog 不能像集合那样,返回输入的各个元素。
49 | * @param key
50 | */
51 | public long pfcount(String key){
52 | Jedis jedis = jedisPool.getResource();
53 | if(jedis == null) return 0;
54 | try {
55 | return jedis.pfcount(key);
56 | }finally {
57 | jedis.close();
58 | }
59 | }
60 |
61 | /**
62 | * count 1
63 | * @param key
64 | * @return
65 | */
66 | public long bitcount(String key){
67 | Jedis jedis = jedisPool.getResource();
68 | if(jedis == null) return 0;
69 | try {
70 | return jedis.bitcount(key);
71 | }finally {
72 | jedis.close();
73 | }
74 | }
75 |
76 | /**
77 | * index fisrt 1
78 | * @param key
79 | * @return
80 | */
81 | public long bitpos(String key){
82 | Jedis jedis = jedisPool.getResource();
83 | if(jedis == null) return 0;
84 | try {
85 | return jedis.bitpos(key, true);
86 | }finally {
87 | jedis.close();
88 | }
89 | }
90 |
91 | /**
92 | * operate two bit
93 | * @param key1
94 | * @param key2
95 | * @param op AND, OR, XOR, NOT;
96 | * @return
97 | */
98 | public long bitop(String key1, String key2, BitOP op){
99 | Jedis jedis = jedisPool.getResource();
100 | if(jedis == null) return 0;
101 | try {
102 | return jedis.bitop(op, key1, key2);
103 | }finally {
104 | jedis.close();
105 | }
106 | }
107 |
108 | @Override
109 | public void run() {
110 | //clear bit offset
111 | bitop(BaseConstants.REDIS_BIT_KEY, "SS", BitOP.AND);
112 | long seconds = 0;
113 | for (int i = 0; i < duration ; i++) {
114 | //simulate visitor come or not 0 1
115 | int visit = ThreadLocalRandom.current().nextInt(2);
116 | setbit(BaseConstants.REDIS_BIT_KEY, i, visit);
117 | if(visit == 1) {
118 | //simulate visit user random same
119 | String user = BaseConstants.REDIS_VISIT_USER + ThreadLocalRandom.current().nextInt(10);
120 | pfadd(BaseConstants.REDIS_HLL_KEY, user);
121 | logger.info("time eclipse {}s, user: {}, current visit: {}", seconds, user, bitcount(BaseConstants.REDIS_BIT_KEY));
122 | }else {
123 | logger.info("time eclipse {}s", seconds);
124 | }
125 | seconds++;
126 | try {
127 | Thread.sleep(1000);
128 | } catch (InterruptedException e) {
129 | e.printStackTrace();
130 | }
131 | }
132 | logger.info("time eclipse seconds: {}, total visit: {}, user count: {}",
133 | seconds, bitcount(BaseConstants.REDIS_BIT_KEY), pfcount(BaseConstants.REDIS_HLL_KEY));
134 | logger.info("fisrt visit at: {}", bitpos(BaseConstants.REDIS_BIT_KEY));
135 |
136 | destroy();
137 | }
138 |
139 | RedisBitHll(){
140 |
141 | }
142 |
143 | RedisBitHll(int duration){
144 | this.duration = duration;
145 | }
146 |
147 | public static void main(String[] args) {
148 | RedisBitHll bit = new RedisBitHll(20); //visit stastic in 20 seconds
149 | new Thread(bit).start();
150 | }
151 | }
152 |
--------------------------------------------------------------------------------
/redis-base/src/main/java/com/redis/base/set/RedisSetZset.java:
--------------------------------------------------------------------------------
1 | package com.redis.base.set;
2 |
3 | import com.redis.base.BaseConstants;
4 | import com.redis.base.core.PoolRedis;
5 | import org.slf4j.Logger;
6 | import org.slf4j.LoggerFactory;
7 | import redis.clients.jedis.*;
8 |
9 | import java.util.*;
10 | import java.util.concurrent.ThreadLocalRandom;
11 |
12 | /**
13 | * Redis set
14 | * Created by Administrator on 18-3-20.
15 | */
16 | public class RedisSetZset extends PoolRedis {
17 | private static final Logger logger = LoggerFactory.getLogger(RedisSetZset.class);
18 |
19 | /**
20 | * string set sinter
21 | * get tow set inter
22 | */
23 | public void sinter(String key1, String key2){
24 | Jedis jedis = jedisPool.getResource();
25 | if(jedis == null) return;
26 | try {
27 | logger.info("{}: {}, size: {}", key1, jedis.smembers(key1), jedis.scard(key1));
28 | logger.info("{}: {}, size: {}", key2, jedis.smembers(key2), jedis.scard(key2));
29 | Set results = jedis.sinter(key1, key2);
30 | logger.info("set {} {} sinter result: {}", key1, key2, results);
31 | }catch (Exception e){}
32 | finally {
33 | if(jedis != null){
34 | jedis.close();
35 | }
36 | }
37 |
38 | }
39 |
40 | /**
41 | * string set sunion
42 | * union two set and return
43 | */
44 | public void sunion(String key1, String key2){
45 | Jedis jedis = jedisPool.getResource();
46 | if(jedis == null) return;
47 | try {
48 | logger.info("{}: {}, size: {}", key1, jedis.smembers(key1), jedis.scard(key1));
49 | logger.info("{}: {}, size: {}", key2, jedis.smembers(key2), jedis.scard(key2));
50 | Set results = jedis.sunion(key1, key2);
51 | logger.info("set {} {} sunion result: {}", key1, key2, results);
52 | }catch (Exception e){}
53 | finally {
54 | if(jedis != null){
55 | jedis.close();
56 | }
57 | }
58 |
59 | }
60 |
61 | /**
62 | * string set sunionstore
63 | * union two set to new set
64 | */
65 | public void sunionstore(String dest, String key1, String key2){
66 | Jedis jedis = jedisPool.getResource();
67 | if(jedis == null) return;
68 | try {
69 | logger.info("{}: {}, size: {}", key1, jedis.smembers(key1), jedis.scard(key1));
70 | logger.info("{}: {}, size: {}", key2, jedis.smembers(key2), jedis.scard(key2));
71 | long result = jedis.sunionstore(dest, key1, key2);
72 | logger.info("set {} {} sunionstore dest set size: {}", key1, key2, result);
73 | logger.info("dest: {}", jedis.smembers(dest));
74 | }catch (Exception e){}
75 | finally {
76 | if(jedis != null){
77 | jedis.close();
78 | }
79 | }
80 |
81 | }
82 |
83 | /**
84 | * string set srandmember
85 | * get num random elements from set
86 | */
87 | public void srandnumber(String key1, int num){
88 | Jedis jedis = jedisPool.getResource();
89 | if(jedis == null) return;
90 | try {
91 | logger.info("{}: {}, size: {}", key1, jedis.smembers(key1), jedis.scard(key1));
92 | List randoms = jedis.srandmember(key1, num);
93 | logger.info("set {} srandmember result: {}", key1, randoms);
94 | }catch (Exception e){}
95 | finally {
96 | if(jedis != null){
97 | jedis.close();
98 | }
99 | }
100 |
101 | }
102 |
103 | /**
104 | * string zset zremrange
105 | * remove range ele from zset
106 | */
107 | public void zremrange(String key1, int start, int end){
108 | Jedis jedis = jedisPool.getResource();
109 | if(jedis == null) return;
110 | try {
111 | logger.info("{}: {}, size: {}", key1, jedis.zrange(key1, 0, -1), jedis.zcard(key1));
112 | long result = jedis.zremrangeByRank(key1, start, end);
113 | logger.info("set {} zremrangeByScore result: {}", key1, result == 1?"success":"failed");
114 | logger.info("{}: {}, size: {}", key1, jedis.zrange(key1, 0, -1), jedis.zcard(key1));
115 | }catch (Exception e){}
116 | finally {
117 | if(jedis != null){
118 | jedis.close();
119 | }
120 | }
121 |
122 | }
123 |
124 | /**
125 | * string zset zadd
126 | * update zset ele score
127 | */
128 | public void zupdate(String key1, String field, int score){
129 | Jedis jedis = jedisPool.getResource();
130 | if(jedis == null) return;
131 | try {
132 | logger.info("{}: {}, size: {}", key1, jedis.zrange(key1, 0, -1), jedis.zcard(key1));
133 | long result = jedis.zadd(key1, score, field);
134 | logger.info("set {} update result: {}", key1, result == 1?"success":"failed");
135 | logger.info("{}: {}, size: {}", key1, jedis.zrange(key1, 0, -1), jedis.zcard(key1));
136 | }catch (Exception e){}
137 | finally {
138 | if(jedis != null){
139 | jedis.close();
140 | }
141 | }
142 |
143 | }
144 |
145 | public static void main(String[] args) {
146 | RedisSetZset redisSet = new RedisSetZset();
147 | redisSet.sinter("deck", "deck1");
148 | redisSet.sunion("deck", "deck1");
149 | redisSet.sunionstore("dest", "deck", "deck1");
150 | redisSet.srandnumber("deck", ThreadLocalRandom.current().nextInt(1, 3));
151 | redisSet.zremrange(BaseConstants.REDIS_ZSET_KEY, 0, 1);
152 | redisSet.destroy();
153 | }
154 | }
155 |
--------------------------------------------------------------------------------
/redis-pubsub/src/main/java/com/redis/pubsub/RedisOpt.java:
--------------------------------------------------------------------------------
1 | package com.redis.pubsub;
2 |
3 | import org.springframework.beans.factory.annotation.Value;
4 | import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
5 | import org.springframework.data.redis.core.RedisTemplate;
6 | import org.springframework.stereotype.Component;
7 | import redis.clients.jedis.Jedis;
8 | import redis.clients.jedis.JedisPool;
9 | import redis.clients.jedis.JedisPoolConfig;
10 | import redis.clients.jedis.JedisPubSub;
11 |
12 |
13 | import javax.annotation.Resource;
14 | import java.util.Collection;
15 | import java.util.List;
16 | import java.util.Map;
17 | import java.util.concurrent.ThreadLocalRandom;
18 | import java.util.concurrent.TimeUnit;
19 |
20 | /**
21 | * Redis 基本操作
22 | */
23 | @Component("trr")
24 | public class RedisOpt {
25 |
26 |
27 | // inject the template as ListOperations
28 | // can also inject as Value, Set, ZSet, and HashOperations
29 | @Resource(name="redisTemplate")
30 | public RedisTemplate redisTemplate;
31 |
32 | /**
33 | * 添加 k v
34 | * @param userId
35 | * @param name
36 | */
37 | public void add(String userId, String name) {
38 | redisTemplate.opsForValue().set(userId, name);
39 | System.out.println(redisTemplate.opsForValue().get(userId));
40 | }
41 |
42 | /**
43 | * 获取 k v
44 | * @param userId
45 | * @return
46 | */
47 | public Object get(String userId) {
48 | return redisTemplate.opsForValue().get(userId);
49 | }
50 |
51 | /**
52 | * 检验存在
53 | * @param userId
54 | */
55 | public void check(String userId) {
56 | System.out.println(redisTemplate.hasKey(userId));
57 | }
58 |
59 | /**
60 | * 删除
61 | * @param userId
62 | */
63 | public void del(String userId) {
64 | redisTemplate.delete(userId);
65 | }
66 |
67 | /**
68 | * 过期
69 | * @param userId
70 | * @param name
71 | * @param time
72 | */
73 | public void addExpire(String userId, String name, int time) {
74 | redisTemplate.opsForValue().set(userId, name);
75 | redisTemplate.expire(userId, time, TimeUnit.SECONDS);
76 | }
77 | public void flushAll() {
78 | redisTemplate.getConnectionFactory().getConnection().flushAll();
79 | }
80 |
81 | public void multiSet(Map userId) {
82 | redisTemplate.opsForValue().multiSet(userId);
83 | }
84 |
85 | public void multiGet(List userId) {
86 | List s = redisTemplate.opsForValue().multiGet(userId);
87 | System.out.println(s.toString());
88 | }
89 |
90 | public void opsListLPush(String userId, String name) {
91 | redisTemplate.opsForList().leftPush(userId, name);
92 | }
93 |
94 | public List opsListRange(String userId, int start, int end) {
95 | return redisTemplate.opsForList().range(userId, start, end);
96 | }
97 |
98 | public Object opsListIndex(String userId, int index) {
99 | return redisTemplate.opsForList().index(userId, index);
100 | }
101 |
102 | public void opsListRemove(String userId, int index, String value) {
103 | redisTemplate.opsForList().remove(userId, index, value);
104 | }
105 |
106 | public void opsListSet(String userId, Collection s) {
107 | redisTemplate.opsForList().leftPushAll(userId, s);
108 | }
109 |
110 | public void convertAndSend(String channel, Object message){
111 | redisTemplate.convertAndSend(channel, message);
112 | }
113 |
114 | JedisPool jedisPool;
115 |
116 | @Value("${redis.master.host}")
117 | public String redisHost;
118 |
119 | @Value("${redis.master.port}")
120 | public Integer redisport;
121 |
122 | public JedisConnectionFactory getJedisConnectionFactory() {
123 | if(jedisConnectionFactory == null){
124 | jedisConnectionFactory = new JedisConnectionFactory();
125 | //TODO
126 | }
127 | return jedisConnectionFactory;
128 | }
129 |
130 | public void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory) {
131 | this.jedisConnectionFactory = jedisConnectionFactory;
132 | }
133 |
134 | public JedisConnectionFactory jedisConnectionFactory;
135 |
136 |
137 | public JedisPool getJedisPool() {
138 | if(jedisPool == null){
139 | jedisPool = new JedisPool(new JedisPoolConfig(), redisHost, redisport);
140 | }
141 | return jedisPool;
142 | }
143 |
144 | /**
145 | * jedis blpop 阻塞取redis中队列头部值 超时3s
146 | */
147 | public void blockPull(String queue){
148 | Jedis jedis = getJedisPool().getResource();
149 | while (true) {
150 | System.out.println(jedis.blpop(3000, queue));
151 | try {
152 | Thread.sleep(1000);
153 | } catch (InterruptedException e) {
154 | e.printStackTrace();
155 | }
156 | }
157 | }
158 |
159 | /**
160 | * jedis 写入redis队列值
161 | */
162 | public void setJedisValue(String queue, String value){
163 | Jedis jedis = getJedisPool().getResource();
164 | while (true) {
165 | // String value = String.valueOf(ThreadLocalRandom.current().nextInt(100));
166 | jedis.lpush(queue, value);
167 | try {
168 | Thread.sleep(2000);
169 | } catch (InterruptedException e) {
170 | e.printStackTrace();
171 | }
172 | System.out.println(queue + " value: " + value);
173 | }
174 | }
175 |
176 | /**
177 | * 发布主题
178 | * @param topic
179 | */
180 | public void jedisPublish(String topic){
181 | Jedis jedis = getJedisPool().getResource();
182 | while (true) {
183 | String value = String.valueOf(ThreadLocalRandom.current().nextInt(100));
184 | jedis.publish(topic, value);
185 | System.out.println("publish value: " + value);
186 | try {
187 | Thread.sleep(1000);
188 | } catch (InterruptedException e) {
189 | e.printStackTrace();
190 | }
191 | }
192 | }
193 |
194 | /**
195 | * 订阅主题
196 | */
197 | public void jedisSubscribe(String topic){
198 | Jedis jedis = getJedisPool().getResource();
199 | jedis.subscribe(new JedisPubSub() {
200 | @Override
201 | public void onMessage(String channel, String message) {
202 | super.onMessage(channel, message);
203 | System.out.println("subscribe channel: " + channel + ", message: " + message);
204 | }
205 | }, topic);
206 | }
207 | }
208 |
--------------------------------------------------------------------------------
/redis-base/src/main/java/com/redis/base/pipeline/RedisPipeline.java:
--------------------------------------------------------------------------------
1 | package com.redis.base.pipeline;
2 |
3 | import com.redis.base.BaseConstants;
4 | import com.redis.base.core.PoolRedis;
5 | import com.redis.base.core.ShardedRedis;
6 | import org.slf4j.Logger;
7 | import org.slf4j.LoggerFactory;
8 | import redis.clients.jedis.*;
9 |
10 | import java.util.*;
11 | import java.util.concurrent.ThreadLocalRandom;
12 | import java.util.stream.Collectors;
13 |
14 | /**
15 | * Redis pipeline 异步执行
16 | * Created by Administrator on 18-3-20.
17 | */
18 | public class RedisPipeline extends ShardedRedis {
19 | private static final Logger logger = LoggerFactory.getLogger(RedisPipeline.class);
20 |
21 | /**
22 | * string set pipe
23 | * @param kvs
24 | */
25 | public void pipeSet(Map kvs){
26 | ShardedJedis jedis = shardedJedisPool.getResource();
27 | if(jedis == null) return;
28 | try {
29 | ShardedJedisPipeline pipeline = jedis.pipelined();
30 | kvs.entrySet().forEach(entry -> {
31 | pipeline.set(entry.getKey(), entry.getValue());
32 | logger.info("pipe set: {}", entry.toString());
33 | });
34 | List