From 4bd7a5b5fa2238ee4540bda5b2137039c5bbe7ad Mon Sep 17 00:00:00 2001 From: stevelee Date: Wed, 30 Mar 2022 17:58:02 +0800 Subject: [PATCH] =?UTF-8?q?Redis=E9=9B=86=E7=BE=A4=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BPA.Component.RedisClientTester.csproj | 1 + .../Program.cs | 49 +++++++++- .../Properties/launchSettings.json | 2 +- .../TestConfig.cs | 6 ++ .../TestRedisClientFactory.cs | 96 +++++++++++++++++++ 5 files changed, 151 insertions(+), 3 deletions(-) create mode 100644 src/BPA.Component.RedisClient/BPA.Component.RedisClientTester/TestRedisClientFactory.cs diff --git a/src/BPA.Component.RedisClient/BPA.Component.RedisClientTester/BPA.Component.RedisClientTester.csproj b/src/BPA.Component.RedisClient/BPA.Component.RedisClientTester/BPA.Component.RedisClientTester.csproj index 99b99dd..547600b 100644 --- a/src/BPA.Component.RedisClient/BPA.Component.RedisClientTester/BPA.Component.RedisClientTester.csproj +++ b/src/BPA.Component.RedisClient/BPA.Component.RedisClientTester/BPA.Component.RedisClientTester.csproj @@ -4,6 +4,7 @@ net6.0 enable enable + Exe diff --git a/src/BPA.Component.RedisClient/BPA.Component.RedisClientTester/Program.cs b/src/BPA.Component.RedisClient/BPA.Component.RedisClientTester/Program.cs index 69134a3..7fe8b81 100644 --- a/src/BPA.Component.RedisClient/BPA.Component.RedisClientTester/Program.cs +++ b/src/BPA.Component.RedisClient/BPA.Component.RedisClientTester/Program.cs @@ -1,4 +1,5 @@ using BPA.Component.ApolloClient; +using BPA.Component.ApolloClientTester; using BPA.Component.Extensions; using BPA.Component.RedisClient; using CSRedis; @@ -14,7 +15,7 @@ namespace BPA.Component.RedisClientTester // Console.WriteLine("1=测试单例,2=测试工厂"); // var cmd = Console.ReadLine(); // if (cmd == "1") - TestUseRedisSingleton(); + UseRedisSingleton(); // else if (cmd == "2") // TestUseRedisFactory(); // else @@ -25,7 +26,7 @@ namespace BPA.Component.RedisClientTester /// /// 测试单例 /// - static void TestUseRedisSingleton() + static void UseRedisSingleton() { var builder = new ConfigurationBuilder(); builder.AddEnvironmentVariables(); @@ -38,6 +39,7 @@ namespace BPA.Component.RedisClientTester var serviceProvider = services.BuildServiceProvider(); var redisClient = serviceProvider.GetService(); + // 扫描 var employeeLoginAuthKey = "testfKeys:*"; var scanKeys = redisClient.Keys(employeeLoginAuthKey); @@ -59,6 +61,37 @@ namespace BPA.Component.RedisClientTester }); } + /// + /// 测试redis 工厂 + /// + static void UseRedisFactory() + { + var builder = new ConfigurationBuilder(); + builder.AddEnvironmentVariables(); + var configuration = builder.Build(); + configuration.AddApolloConfiguration(); + + var services = new ServiceCollection(); + services.AddApollo(); + services.UseRedisFactory(); + + var serviceProvider = services.BuildServiceProvider(); + var redisClientFactory = serviceProvider.GetService(); + + TestRedisClient(redisClientFactory.CSRedisClient01, "s01"); + TestRedisClient(redisClientFactory.CSRedisClient02, "s01"); + TestRedisClient(redisClientFactory.CSRedisClient03, "s01"); + + //监听配置变化后 在进行测试 + redisClientFactory.OnRedisChange = () => + { + TestRedisClient(redisClientFactory.CSRedisClient01, "s02"); + TestRedisClient(redisClientFactory.CSRedisClient02, "s02"); + TestRedisClient(redisClientFactory.CSRedisClient03, "s02"); + }; + } + + /// /// 获取单例redis 连接字符串 /// @@ -71,5 +104,17 @@ namespace BPA.Component.RedisClientTester conStr += ""; return conStr; } + + /// + /// 测试redis client 操作redis 集群 + /// + /// + static void TestRedisClient(CSRedisClient redisClient, string prefix) + { + var key = $"{prefix}:testredisclient"; + redisClient.Set(key, $"{DateTime.Now}-{Guid.NewGuid()}", 300); + Console.WriteLine(redisClient.Get(key)); + Thread.Sleep(2000); + } } } \ No newline at end of file diff --git a/src/BPA.Component.RedisClient/BPA.Component.RedisClientTester/Properties/launchSettings.json b/src/BPA.Component.RedisClient/BPA.Component.RedisClientTester/Properties/launchSettings.json index 6d19cb2..94acb1a 100644 --- a/src/BPA.Component.RedisClient/BPA.Component.RedisClientTester/Properties/launchSettings.json +++ b/src/BPA.Component.RedisClient/BPA.Component.RedisClientTester/Properties/launchSettings.json @@ -4,7 +4,7 @@ "commandName": "Project", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development", - "APOLLO_META_SERVER_URL": "http://192.168.1.222:28070", + "APOLLO_META_SERVER_URL": "http://10.2.1.21:28080", "APOLLO_COMMON_NAMESPACE": "Dev.Common", "APP_NAME": "BPA-DEV" } diff --git a/src/BPA.Component.RedisClient/BPA.Component.RedisClientTester/TestConfig.cs b/src/BPA.Component.RedisClient/BPA.Component.RedisClientTester/TestConfig.cs index dc1175c..0acf06d 100644 --- a/src/BPA.Component.RedisClient/BPA.Component.RedisClientTester/TestConfig.cs +++ b/src/BPA.Component.RedisClient/BPA.Component.RedisClientTester/TestConfig.cs @@ -11,6 +11,12 @@ namespace BPA.Component.RedisClientTester public class TestConfig : ApolloBPAConfig { + /// + /// RedisConfig + /// + [AutoWrite()] + public List RedisConfig { protected set; get; } + #pragma warning disable CS0618 // 类型或成员已过时 public TestConfig(ApolloConfigurationManager apolloConfigurationManager, IConfiguration configuration) : base(apolloConfigurationManager, configuration) #pragma warning restore CS0618 // 类型或成员已过时 diff --git a/src/BPA.Component.RedisClient/BPA.Component.RedisClientTester/TestRedisClientFactory.cs b/src/BPA.Component.RedisClient/BPA.Component.RedisClientTester/TestRedisClientFactory.cs new file mode 100644 index 0000000..58572aa --- /dev/null +++ b/src/BPA.Component.RedisClient/BPA.Component.RedisClientTester/TestRedisClientFactory.cs @@ -0,0 +1,96 @@ +using System.ComponentModel; +using BPA.Component.RedisClient; +using BPA.Component.RedisClientTester; +using Com.Ctrip.Framework.Apollo.Enums; + +namespace BPA.Component.ApolloClientTester +{ + /// + /// redis 工厂, 假设连接了三个 + /// + public class TestRedisClientFactory : RedisClientFactory + { + /// + /// redis 集群1 + /// + public CSRedis.CSRedisClient CSRedisClient01 { private set; get; } + + /// + /// redis 集群2 + /// + public CSRedis.CSRedisClient CSRedisClient02 { private set; get; } + + /// + /// redis 集群3 + /// + public CSRedis.CSRedisClient CSRedisClient03 { private set; get; } + + /// + /// testConfig + /// + private readonly TestConfig testConfig; + + /// + /// 测试委托,方便配置改变后触发 + /// + public Action OnRedisChange; + + /// + /// TestRedisClientFactory + /// + /// + public TestRedisClientFactory(TestConfig testConfig) + { + this.testConfig = testConfig; + CreateRedisClients(testConfig); + this.testConfig.OnConfigChange += TestConfig_OnConfigChange; + } + + /// + /// 监听 redis config 改变 + /// + /// + /// + /// + /// + /// + private void TestConfig_OnConfigChange(TestConfig config, string key, string newValue, string oldValue, + PropertyChangeType changeType) + { + if (!Enum.IsDefined(typeof(PropertyChangeType), changeType)) + throw new InvalidEnumArgumentException(nameof(changeType), (int) changeType, typeof(PropertyChangeType)); + + //这里方法上的 config 和 上面定义的 testConfig 是一样的,因为注入的配置对象是单例的 + if (nameof(config.RedisConfig) != key) return; + + //真实情况下,多个集群应该是多个配置,这里简单处理下 + CreateRedisClients(config); + + OnRedisChange?.Invoke(); + } + + /// + /// 模拟创建了三个对象 + /// + /// + private void CreateRedisClients(TestConfig config) + { + CSRedisClient01 = CreateCsRedisClient(ProccessRedisConnectionString(config.RedisConfig[0], 1)); + CSRedisClient02 = CreateCsRedisClient(ProccessRedisConnectionString(config.RedisConfig[1], 2)); + CSRedisClient03 = CreateCsRedisClient(ProccessRedisConnectionString(config.RedisConfig[2], 3)); + } + + /// + /// 处理 redis 连接字符串 + /// + /// + /// + /// + private static string ProccessRedisConnectionString(string connectionString, int index) + { + if (connectionString.IndexOf("prefix", StringComparison.Ordinal) <= 0) + connectionString += $",prefix=testfactory{index}:"; + return connectionString; + } + } +} \ No newline at end of file