From b1a1c313ad7a6d615009a7da1bdb7c0e00b25efc Mon Sep 17 00:00:00 2001 From: "Dylan.Liu" Date: Wed, 4 Dec 2019 10:05:34 +0800 Subject: [PATCH] =?UTF-8?q?DEC-11271=20Redis=20=E5=93=A8=E5=85=B5=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=E4=B8=8D=E5=BA=94=E8=AF=A5=E7=9B=B4=E6=8E=A5=E4=BE=9D?= =?UTF-8?q?=E8=B5=96=E9=9D=9E=E6=8E=A5=E5=8F=A3=E5=92=8C=E9=9D=9E=E5=BC=80?= =?UTF-8?q?=E6=94=BE=E6=8A=BD=E8=B1=A1=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../state/BaseRedisCollectionConfig.java | 33 +++++++++++ .../state/BaseRedisCollectionsClient.java | 23 ++++++++ .../api/cluster/state/BaseRedisLock.java | 16 ++++++ .../api/cluster/state/BaseRedisStore.java | 16 ++++++ .../cluster/state/DetectorConstantsKit.java | 17 ++++++ .../api/cluster/state/FineJedisPoolKit.java | 21 +++++++ .../api/cluster/state/RedisConfigKit.java | 29 ++++++++++ .../api/cluster/state/StandAloneJedisKit.java | 26 +++++++++ .../cluster/state/StateServerConfigKit.java | 49 ++++++++++++++++ .../java/com/fanruan/api/ModulePrepare.java | 4 +- .../cluster/state/FineJedisPoolKitTest.java | 57 +++++++++++++++++++ .../api/cluster/state/RedisConfigKitTest.java | 34 +++++++++++ .../cluster/state/StandAloneJedisKitTest.java | 31 ++++++++++ .../state/StateServerConfigKitTest.java | 50 ++++++++++++++++ 14 files changed, 404 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/fanruan/api/cluster/state/BaseRedisCollectionConfig.java create mode 100644 src/main/java/com/fanruan/api/cluster/state/BaseRedisCollectionsClient.java create mode 100644 src/main/java/com/fanruan/api/cluster/state/BaseRedisLock.java create mode 100644 src/main/java/com/fanruan/api/cluster/state/BaseRedisStore.java create mode 100644 src/main/java/com/fanruan/api/cluster/state/DetectorConstantsKit.java create mode 100644 src/main/java/com/fanruan/api/cluster/state/FineJedisPoolKit.java create mode 100644 src/main/java/com/fanruan/api/cluster/state/RedisConfigKit.java create mode 100644 src/main/java/com/fanruan/api/cluster/state/StandAloneJedisKit.java create mode 100644 src/main/java/com/fanruan/api/cluster/state/StateServerConfigKit.java create mode 100644 src/test/java/com/fanruan/api/cluster/state/FineJedisPoolKitTest.java create mode 100644 src/test/java/com/fanruan/api/cluster/state/RedisConfigKitTest.java create mode 100644 src/test/java/com/fanruan/api/cluster/state/StandAloneJedisKitTest.java create mode 100644 src/test/java/com/fanruan/api/cluster/state/StateServerConfigKitTest.java diff --git a/src/main/java/com/fanruan/api/cluster/state/BaseRedisCollectionConfig.java b/src/main/java/com/fanruan/api/cluster/state/BaseRedisCollectionConfig.java new file mode 100644 index 0000000..4a620ef --- /dev/null +++ b/src/main/java/com/fanruan/api/cluster/state/BaseRedisCollectionConfig.java @@ -0,0 +1,33 @@ +package com.fanruan.api.cluster.state; + +import com.fr.collections.api.Codec; +import com.fr.collections.config.redis.RedisCollectionConfig; + +/** + * Redis 集合相关配置 + * @author Dylan.Liu + * @version 10.0 + * Created by Dylan.Liu on 2019/12/3 + */ +public class BaseRedisCollectionConfig extends RedisCollectionConfig { + + public BaseRedisCollectionConfig(Codec codec, String host, int port) { + + super(codec, host, port); + } + + public BaseRedisCollectionConfig(String host, int port, String password) { + + super(host, port, password); + } + + public BaseRedisCollectionConfig() { + + super(); + } + + public BaseRedisCollectionConfig(String host, int port) { + + super(host, port); + } +} diff --git a/src/main/java/com/fanruan/api/cluster/state/BaseRedisCollectionsClient.java b/src/main/java/com/fanruan/api/cluster/state/BaseRedisCollectionsClient.java new file mode 100644 index 0000000..ca75924 --- /dev/null +++ b/src/main/java/com/fanruan/api/cluster/state/BaseRedisCollectionsClient.java @@ -0,0 +1,23 @@ +package com.fanruan.api.cluster.state; + +import com.fr.collections.cluster.client.RedisCollectionsClient; +import com.fr.collections.config.redis.RedisCollectionConfig; + +/** + * 集群集合客户端 + * @author Dylan.Liu + * @version 10.0 + * Created by Dylan.Liu on 2019/12/3 + */ +public class BaseRedisCollectionsClient extends RedisCollectionsClient { + + public BaseRedisCollectionsClient() { + + super(); + } + + public BaseRedisCollectionsClient(RedisCollectionConfig config) { + + super(config); + } +} diff --git a/src/main/java/com/fanruan/api/cluster/state/BaseRedisLock.java b/src/main/java/com/fanruan/api/cluster/state/BaseRedisLock.java new file mode 100644 index 0000000..f24f7b7 --- /dev/null +++ b/src/main/java/com/fanruan/api/cluster/state/BaseRedisLock.java @@ -0,0 +1,16 @@ +package com.fanruan.api.cluster.state; + +import com.fr.store.impl.redis.RedisLock; + +/** + * Redis 锁 + * @author Dylan.Liu + * @version 10.0 + * Created by Dylan.Liu on 2019/12/3 + */ +public class BaseRedisLock extends RedisLock { + + public BaseRedisLock() { + super(); + } +} diff --git a/src/main/java/com/fanruan/api/cluster/state/BaseRedisStore.java b/src/main/java/com/fanruan/api/cluster/state/BaseRedisStore.java new file mode 100644 index 0000000..b4c47a8 --- /dev/null +++ b/src/main/java/com/fanruan/api/cluster/state/BaseRedisStore.java @@ -0,0 +1,16 @@ +package com.fanruan.api.cluster.state; + +import com.fr.store.RedisStore; + +/** + * Redis Store + * @author Dylan.Liu + * @version 10.0 + * Created by Dylan.Liu on 2019/12/3 + */ +public class BaseRedisStore extends RedisStore { + + public BaseRedisStore(String tableNamePrefix, int expireStateRemoveInterval) { + super(tableNamePrefix, expireStateRemoveInterval); + } +} diff --git a/src/main/java/com/fanruan/api/cluster/state/DetectorConstantsKit.java b/src/main/java/com/fanruan/api/cluster/state/DetectorConstantsKit.java new file mode 100644 index 0000000..0a757fc --- /dev/null +++ b/src/main/java/com/fanruan/api/cluster/state/DetectorConstantsKit.java @@ -0,0 +1,17 @@ +package com.fanruan.api.cluster.state; + +import com.fr.store.detector.utils.DetectorUtils; + +/** + * @author Dylan.Liu + * @version 10.0 + * Created by Dylan.Liu on 2019/12/3 + */ +public class DetectorConstantsKit { + + /** + * 状态服务器的前端密码显示 + * 如果密码是 DEFAULT_PWD(******) 则从数据库取真实密码 + */ + public static final String DEFAULT_PWD = DetectorUtils.DEFAULT_PWD; +} diff --git a/src/main/java/com/fanruan/api/cluster/state/FineJedisPoolKit.java b/src/main/java/com/fanruan/api/cluster/state/FineJedisPoolKit.java new file mode 100644 index 0000000..eb848d0 --- /dev/null +++ b/src/main/java/com/fanruan/api/cluster/state/FineJedisPoolKit.java @@ -0,0 +1,21 @@ +package com.fanruan.api.cluster.state; + +import com.fr.store.impl.redis.accessor.FineJedisPool; +import com.fr.store.impl.redis.accessor.FineJedisWrapper; + +/** + * @author Dylan.Liu + * @version 10.0 + * Created by Dylan.Liu on 2019/12/3 + */ +public class FineJedisPoolKit { + + /** + * 设置提供真实资源的 Jedis 包装对象 + * @param jedisWrapper Jedis 包装对象 + */ + public static void setRealClient(FineJedisWrapper jedisWrapper) { + + FineJedisPool.getInstance().setRealClient(jedisWrapper); + } +} diff --git a/src/main/java/com/fanruan/api/cluster/state/RedisConfigKit.java b/src/main/java/com/fanruan/api/cluster/state/RedisConfigKit.java new file mode 100644 index 0000000..6a9407c --- /dev/null +++ b/src/main/java/com/fanruan/api/cluster/state/RedisConfigKit.java @@ -0,0 +1,29 @@ +package com.fanruan.api.cluster.state; + +import com.fr.store.impl.redis.RedisConfig; + +/** + * @author Dylan.Liu + * @version 10.0 + * Created by Dylan.Liu on 2019/12/3 + */ +public class RedisConfigKit { + + /** + * 获取 Redis 里表名的统一的前缀 + * @return Redis 里表名的统一的前缀 + */ + public static String getTableNamePrefix() { + + return RedisConfig.getInstance().getTableNamePrefix(); + } + + /** + * 获取 Redis 里过期 key 删除间隔时间 + * @return Redis 里过期 key 删除间隔时间 + */ + public static int getExpireStateRemoveInterval() { + + return RedisConfig.getInstance().getExpireStateRemoveInterval(); + } +} diff --git a/src/main/java/com/fanruan/api/cluster/state/StandAloneJedisKit.java b/src/main/java/com/fanruan/api/cluster/state/StandAloneJedisKit.java new file mode 100644 index 0000000..02aea54 --- /dev/null +++ b/src/main/java/com/fanruan/api/cluster/state/StandAloneJedisKit.java @@ -0,0 +1,26 @@ +package com.fanruan.api.cluster.state; + +import com.fr.store.impl.redis.accessor.FineJedis; +import com.fr.store.impl.redis.accessor.StandAloneJedis; +import com.fr.third.redis.clients.jedis.Jedis; + +/** + * @author Dylan.Liu + * @version 10.0 + * Created by Dylan.Liu on 2019/12/3 + */ +public class StandAloneJedisKit { + + /** + * 创建单机版 Jedis Api集合 + * @param jedis Jedis 资源 + * @return 单机版 Jedis Api集合 + */ + public static FineJedis createStandAloneJedis(Jedis jedis) { + + if (jedis == null) { + throw new NullPointerException("jedis is null"); + } + return new StandAloneJedis(jedis); + } +} diff --git a/src/main/java/com/fanruan/api/cluster/state/StateServerConfigKit.java b/src/main/java/com/fanruan/api/cluster/state/StateServerConfigKit.java new file mode 100644 index 0000000..1dd6be0 --- /dev/null +++ b/src/main/java/com/fanruan/api/cluster/state/StateServerConfigKit.java @@ -0,0 +1,49 @@ +package com.fanruan.api.cluster.state; + +import com.fr.config.Configuration; +import com.fr.store.StateServerConfig; + +/** + * 状态服务器配置 + * @author Dylan.Liu + * @version 10.0 + * Created by Dylan.Liu on 2019/12/3 + */ +public class StateServerConfigKit { + + /** + * 获取当前状态服务器类型 + * @return 当前状态服务器类型 + */ + public static String getType() { + + return StateServerConfig.getInstance().getType(); + } + + /** + * 获取当前是否开启状态服务器 + * @param isShareMode 当前是否开启状态服务器 + */ + public static void setShareMode(boolean isShareMode) { + + StateServerConfig.getInstance().setShareMode(isShareMode); + } + + /** + * 设置状态服务器类型 + * @param type 状态服务器类型 + */ + public static void setType(String type) { + + StateServerConfig.getInstance().setType(type); + } + + /** + * 获取 StateServerConfig 类对象 + * @return StateServerConfig 类对象 + */ + public static Class getConfigClass() { + + return StateServerConfig.class; + } +} diff --git a/src/test/java/com/fanruan/api/ModulePrepare.java b/src/test/java/com/fanruan/api/ModulePrepare.java index 619513c..b0ae31f 100644 --- a/src/test/java/com/fanruan/api/ModulePrepare.java +++ b/src/test/java/com/fanruan/api/ModulePrepare.java @@ -12,7 +12,7 @@ import com.fr.report.ReportActivator; import com.fr.report.RestrictionActivator; import com.fr.report.module.ReportBaseActivator; import com.fr.scheduler.SchedulerActivator; -import com.fr.store.StateServerActivator; +import com.fr.store.StateServiceActivator; import com.fr.workspace.simple.SimpleWork; import org.junit.After; import org.junit.Before; @@ -31,7 +31,7 @@ public class ModulePrepare { new BaseDBActivator(), new ConfigurationActivator(), new StandaloneModeActivator(), - new StateServerActivator(), + new StateServiceActivator(), new SchedulerActivator(), new ReportBaseActivator(), new RestrictionActivator(), diff --git a/src/test/java/com/fanruan/api/cluster/state/FineJedisPoolKitTest.java b/src/test/java/com/fanruan/api/cluster/state/FineJedisPoolKitTest.java new file mode 100644 index 0000000..c0cbd17 --- /dev/null +++ b/src/test/java/com/fanruan/api/cluster/state/FineJedisPoolKitTest.java @@ -0,0 +1,57 @@ +package com.fanruan.api.cluster.state; + +import com.fr.store.impl.redis.accessor.FineJedis; +import com.fr.store.impl.redis.accessor.FineJedisPool; +import com.fr.store.impl.redis.accessor.FineJedisWrapper; +import org.easymock.EasyMock; +import org.junit.Assert; +import org.junit.Test; + +import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.mock; + +/** + * @author Dylan.Liu + * @version 10.0 + * Created by Dylan.Liu on 2019/12/3 + */ +public class FineJedisPoolKitTest { + + @Test + public void TestSetRealClient() { + + FineJedisPoolKit.setRealClient(new FineJedisWrapper() { + + @Override + public FineJedis getResource() { + return null; + } + + @Override + public void destroy() { + + } + }); + Assert.assertNull(FineJedisPool.getInstance().getResource()); + + FineJedis mockFineJedis = mock(FineJedis.class); + expect(mockFineJedis.type(anyObject())).andReturn("TestFineJedis"); + EasyMock.replay(mockFineJedis); + + FineJedisPoolKit.setRealClient(new FineJedisWrapper() { + + @Override + public FineJedis getResource() { + return mockFineJedis; + } + + @Override + public void destroy() { + + } + }); + Assert.assertEquals("TestFineJedis", FineJedisPool.getInstance().getResource().type(new byte[]{})); + } + +} \ No newline at end of file diff --git a/src/test/java/com/fanruan/api/cluster/state/RedisConfigKitTest.java b/src/test/java/com/fanruan/api/cluster/state/RedisConfigKitTest.java new file mode 100644 index 0000000..077c45c --- /dev/null +++ b/src/test/java/com/fanruan/api/cluster/state/RedisConfigKitTest.java @@ -0,0 +1,34 @@ +package com.fanruan.api.cluster.state; + +import com.fanruan.api.Prepare; +import com.fr.store.impl.redis.RedisConfig; +import org.junit.Assert; +import org.junit.Test; + +/** + * @author Dylan.Liu + * @version 10.0 + * Created by Dylan.Liu on 2019/12/3 + */ +public class RedisConfigKitTest extends Prepare { + + @Test + public void TestGetTableNamePrefix() { + + RedisConfig.getInstance().setTableNamePrefix("test_table name_prefix"); + Assert.assertEquals("test_table name_prefix", RedisConfigKit.getTableNamePrefix()); + + RedisConfig.getInstance().setTableNamePrefix("test_table name_prefix2"); + Assert.assertEquals("test_table name_prefix2", RedisConfigKit.getTableNamePrefix()); + } + + @Test + public void TestGetExpireStateRemoveInterval() { + + RedisConfig.getInstance().setExpireStateRemoveInterval(111); + Assert.assertEquals(111, RedisConfig.getInstance().getExpireStateRemoveInterval()); + + RedisConfig.getInstance().setExpireStateRemoveInterval(222); + Assert.assertEquals(222, RedisConfig.getInstance().getExpireStateRemoveInterval()); + } +} \ No newline at end of file diff --git a/src/test/java/com/fanruan/api/cluster/state/StandAloneJedisKitTest.java b/src/test/java/com/fanruan/api/cluster/state/StandAloneJedisKitTest.java new file mode 100644 index 0000000..61fcb52 --- /dev/null +++ b/src/test/java/com/fanruan/api/cluster/state/StandAloneJedisKitTest.java @@ -0,0 +1,31 @@ +package com.fanruan.api.cluster.state; + +import com.fr.store.impl.redis.accessor.FineJedis; +import com.fr.third.redis.clients.jedis.Jedis; +import org.easymock.EasyMock; +import org.junit.Assert; +import org.junit.Test; + +import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.mock; + +/** + * @author Dylan.Liu + * @version 10.0 + * Created by Dylan.Liu on 2019/12/3 + */ +public class StandAloneJedisKitTest { + + @Test + public void TestCreateStandAloneJedis() { + + Jedis mockJedis = mock(Jedis.class); + expect(mockJedis.type(anyObject(byte[].class))).andReturn("test_echo"); + EasyMock.replay(mockJedis); + + FineJedis standAloneJedis = StandAloneJedisKit.createStandAloneJedis(mockJedis); + + Assert.assertEquals("test_echo", standAloneJedis.type(new byte[]{})); + } +} \ No newline at end of file diff --git a/src/test/java/com/fanruan/api/cluster/state/StateServerConfigKitTest.java b/src/test/java/com/fanruan/api/cluster/state/StateServerConfigKitTest.java new file mode 100644 index 0000000..1818c99 --- /dev/null +++ b/src/test/java/com/fanruan/api/cluster/state/StateServerConfigKitTest.java @@ -0,0 +1,50 @@ +package com.fanruan.api.cluster.state; + +import com.fanruan.api.Prepare; +import com.fr.store.StateServerConfig; +import org.junit.Assert; +import org.junit.Test; + +/** + * @author Dylan.Liu + * @version 10.0 + * Created by Dylan.Liu on 2019/12/3 + */ +public class StateServerConfigKitTest extends Prepare { + + @Test + public void TestGetType() { + + StateServerConfig.getInstance().setType("test_type"); + Assert.assertEquals("test_type", StateServerConfigKit.getType()); + + StateServerConfig.getInstance().setType("test_type2"); + Assert.assertEquals("test_type2", StateServerConfigKit.getType()); + } + + @Test + public void TestSetShareMode() { + + StateServerConfigKit.setShareMode(true); + Assert.assertTrue(StateServerConfig.getInstance().isShareMode()); + + StateServerConfigKit.setShareMode(false); + Assert.assertFalse(StateServerConfig.getInstance().isShareMode()); + } + + @Test + public void TestSetType() { + + StateServerConfigKit.setType("test_type3"); + Assert.assertEquals("test_type3", StateServerConfig.getInstance().getType()); + + StateServerConfigKit.setType("test_type4"); + Assert.assertEquals("test_type4", StateServerConfig.getInstance().getType()); + } + + @Test + public void TestGetConfigClass() { + + Assert.assertEquals(StateServerConfigKit.getConfigClass(), StateServerConfig.class); + } +} \ No newline at end of file