@@ -4,6 +4,7 @@ | |||
<TargetFramework>net6.0</TargetFramework> | |||
<ImplicitUsings>enable</ImplicitUsings> | |||
<Nullable>enable</Nullable> | |||
<OutputType>Exe</OutputType> | |||
</PropertyGroup> | |||
<ItemGroup> | |||
@@ -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 | |||
/// <summary> | |||
/// 测试单例 | |||
/// </summary> | |||
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<CSRedisClient>(); | |||
// 扫描 | |||
var employeeLoginAuthKey = "testfKeys:*"; | |||
var scanKeys = redisClient.Keys(employeeLoginAuthKey); | |||
@@ -59,6 +61,37 @@ namespace BPA.Component.RedisClientTester | |||
}); | |||
} | |||
/// <summary> | |||
/// 测试redis 工厂 | |||
/// </summary> | |||
static void UseRedisFactory() | |||
{ | |||
var builder = new ConfigurationBuilder(); | |||
builder.AddEnvironmentVariables(); | |||
var configuration = builder.Build(); | |||
configuration.AddApolloConfiguration(); | |||
var services = new ServiceCollection(); | |||
services.AddApollo<TestConfig>(); | |||
services.UseRedisFactory<TestRedisClientFactory>(); | |||
var serviceProvider = services.BuildServiceProvider(); | |||
var redisClientFactory = serviceProvider.GetService<TestRedisClientFactory>(); | |||
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"); | |||
}; | |||
} | |||
/// <summary> | |||
/// 获取单例redis 连接字符串 | |||
/// </summary> | |||
@@ -71,5 +104,17 @@ namespace BPA.Component.RedisClientTester | |||
conStr += ""; | |||
return conStr; | |||
} | |||
/// <summary> | |||
/// 测试redis client 操作redis 集群 | |||
/// </summary> | |||
/// <param name="redisClient"></param> | |||
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); | |||
} | |||
} | |||
} |
@@ -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" | |||
} | |||
@@ -11,6 +11,12 @@ namespace BPA.Component.RedisClientTester | |||
public class TestConfig : ApolloBPAConfig<TestConfig> | |||
{ | |||
/// <summary> | |||
/// RedisConfig | |||
/// </summary> | |||
[AutoWrite()] | |||
public List<string> RedisConfig { protected set; get; } | |||
#pragma warning disable CS0618 // 类型或成员已过时 | |||
public TestConfig(ApolloConfigurationManager apolloConfigurationManager, IConfiguration configuration) : base(apolloConfigurationManager, configuration) | |||
#pragma warning restore CS0618 // 类型或成员已过时 | |||
@@ -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 | |||
{ | |||
/// <summary> | |||
/// redis 工厂, 假设连接了三个 | |||
/// </summary> | |||
public class TestRedisClientFactory : RedisClientFactory | |||
{ | |||
/// <summary> | |||
/// redis 集群1 | |||
/// </summary> | |||
public CSRedis.CSRedisClient CSRedisClient01 { private set; get; } | |||
/// <summary> | |||
/// redis 集群2 | |||
/// </summary> | |||
public CSRedis.CSRedisClient CSRedisClient02 { private set; get; } | |||
/// <summary> | |||
/// redis 集群3 | |||
/// </summary> | |||
public CSRedis.CSRedisClient CSRedisClient03 { private set; get; } | |||
/// <summary> | |||
/// testConfig | |||
/// </summary> | |||
private readonly TestConfig testConfig; | |||
/// <summary> | |||
/// 测试委托,方便配置改变后触发 | |||
/// </summary> | |||
public Action OnRedisChange; | |||
/// <summary> | |||
/// TestRedisClientFactory | |||
/// </summary> | |||
/// <param name="testConfig"></param> | |||
public TestRedisClientFactory(TestConfig testConfig) | |||
{ | |||
this.testConfig = testConfig; | |||
CreateRedisClients(testConfig); | |||
this.testConfig.OnConfigChange += TestConfig_OnConfigChange; | |||
} | |||
/// <summary> | |||
/// 监听 redis config 改变 | |||
/// </summary> | |||
/// <param name="config"></param> | |||
/// <param name="key"></param> | |||
/// <param name="newValue"></param> | |||
/// <param name="oldValue"></param> | |||
/// <param name="changeType"></param> | |||
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(); | |||
} | |||
/// <summary> | |||
/// 模拟创建了三个对象 | |||
/// </summary> | |||
/// <param name="config"></param> | |||
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)); | |||
} | |||
/// <summary> | |||
/// 处理 redis 连接字符串 | |||
/// </summary> | |||
/// <param name="connectionString"></param> | |||
/// <param name="index"></param> | |||
/// <returns></returns> | |||
private static string ProccessRedisConnectionString(string connectionString, int index) | |||
{ | |||
if (connectionString.IndexOf("prefix", StringComparison.Ordinal) <= 0) | |||
connectionString += $",prefix=testfactory{index}:"; | |||
return connectionString; | |||
} | |||
} | |||
} |