Skip to content
This repository was archived by the owner on Nov 8, 2020. It is now read-only.

Commit b97c257

Browse files
author
uless
committed
简化observer的数据库配置方式
1 parent 588cb12 commit b97c257

11 files changed

Lines changed: 54 additions & 27 deletions

File tree

examples/Ray.Client/Program.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ static int Main(string[] args)
1818

1919
private static async Task<int> RunMainAsync()
2020
{
21-
try
21+
22+
using (var client = await StartClientWithRetries())
2223
{
23-
using (var client = await StartClientWithRetries())
24+
while (true)
2425
{
25-
while (true)
26+
try
2627
{
2728
Console.WriteLine("start");
2829
var times = int.Parse(Console.ReadLine());
@@ -34,13 +35,12 @@ private static async Task<int> RunMainAsync()
3435
await Task.Delay(200);
3536
Console.WriteLine($"余额为{await client.GetGrain<IAccountRep>(1).GetBalance()}");
3637
}
38+
catch (Exception e)
39+
{
40+
Console.WriteLine(e);
41+
}
3742
}
3843
}
39-
catch (Exception e)
40-
{
41-
Console.WriteLine(e);
42-
return 1;
43-
}
4444
}
4545

4646
private static async Task<IClusterClient> StartClientWithRetries(int initializeAttemptsBeforeFailing = 5)

examples/Ray.Grain/Account/Account.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Orleans;
22
using Orleans.Concurrency;
3-
using Ray.Core.Core.Observer;
3+
using Ray.Core;
44
using Ray.Core.Event;
55
using Ray.DistributedTransaction;
66
using Ray.EventBus.RabbitMQ;

examples/Ray.Grain/Account/AccountDb.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
using Ray.Core.Event;
55
using Ray.IGrains.Actors;
66
using Ray.Grain.Events;
7-
using Ray.Core.Core.Observer;
87
using Ray.Core;
98

109
namespace Ray.Grain
1110
{
12-
[Observer(DefaultObserverGroup.secondary, typeof(Account))]
11+
[Observer(DefaultObserverGroup.secondary, "db", typeof(Account))]
1312
public sealed class AccountDb : DbGrain<Account, long>, IAccountDb
1413
{
1514
public AccountDb(ILogger<AccountDb> logger) : base(logger)

examples/Ray.Grain/Account/AccountFlow.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
using Ray.Core.Event;
66
using Ray.IGrains.Actors;
77
using Ray.Grain.Events;
8-
using Ray.Core.Core.Observer;
98

109
namespace Ray.Grain
1110
{
12-
[Observer(DefaultObserverGroup.primary, typeof(Account))]
11+
[Observer(DefaultObserverGroup.primary,"flow", typeof(Account))]
1312
public sealed class AccountFlow : ConcurrentObserverGrain<Account, long>, IAccountFlow
1413
{
1514
public AccountFlow(ILogger<AccountFlow> logger) : base(logger)

examples/Ray.Grain/Account/AccountRep.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
using Orleans;
44
using Ray.Core;
55
using Ray.Core.Core.Grains;
6-
using Ray.Core.Core.Observer;
76
using Ray.IGrains.Actors;
87
using Ray.IGrains.States;
98

109
namespace Ray.Grain
1110
{
12-
[Observer(DefaultObserverGroup.primary, typeof(Account))]
11+
[Observer(DefaultObserverGroup.primary, null, typeof(Account))]
1312
public sealed class AccountRep : TxShadowGrain<Account, long, AccountState>, IAccountRep
1413
{
1514
public AccountRep(ILogger<AccountRep> logger) : base(logger)

examples/Ray.Grain/SQLStorageConfig.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ public static class SQLStorageConfig
1111
public static IServiceCollection PSQLConfigure(this IServiceCollection serviceCollection)
1212
{
1313
serviceCollection.AddSingleton<IConfigureBuilder<long, Account>>(new PSQLConfigureBuilder<long, Account>((provider, id, parameter) =>
14-
new IntegerKeyOptions(provider, "core_event", "account")).Observe<AccountRep>().Observe<AccountDb>("db").Observe<AccountFlow>("observer"));
14+
new IntegerKeyOptions(provider, "core_event", "account")).AutoRegistrationObserver());
1515

1616
return serviceCollection;
1717
}
1818
public static IServiceCollection MySQLConfigure(this IServiceCollection serviceCollection)
1919
{
2020
serviceCollection.AddSingleton<IConfigureBuilder<long, Account>>(new MySQLConfigureBuilder<long, Account>((provider, id, parameter) =>
21-
new IntegerKeyOptions(provider, "core_event", "account")).Observe<AccountRep>().Observe<AccountDb>("db").Observe<AccountFlow>("observer"));
21+
new IntegerKeyOptions(provider, "core_event", "account")).AutoRegistrationObserver());
2222

2323
return serviceCollection;
2424
}

src/Ray.Core/Core/Observer/Attributes/ObservableAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
namespace Ray.Core.Core.Observer
3+
namespace Ray.Core
44
{
55
/// <summary>
66
/// 标记为可观察

src/Ray.Core/Core/Observer/Attributes/ObserverAttribute.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
using System;
22

3-
namespace Ray.Core.Core.Observer
3+
namespace Ray.Core
44
{
55
/// <summary>
66
/// 标记为观察者
77
/// </summary>
88
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
99
public class ObserverAttribute : Attribute
1010
{
11-
public ObserverAttribute(string group, Type observable, Type observer = default)
11+
/// <summary>
12+
/// 事件监听者标记
13+
/// </summary>
14+
/// <param name="group">监听者分组</param>
15+
/// <param name="name">监听者名称(如果是shadow请设置为null)</param>
16+
/// <param name="observable">被监听的Type</param>
17+
/// <param name="observer">监听者的Type</param>
18+
public ObserverAttribute(string group, string name, Type observable, Type observer = default)
1219
{
1320
Group = group;
21+
Name = name;
1422
Observable = observable;
1523
Observer = observer;
1624
}
1725
public string Group { get; set; }
26+
public string Name { get; set; }
1827
public Type Observable { get; set; }
1928
public Type Observer { get; set; }
2029
}

src/Ray.Core/Core/Observer/ObserverUnitContainer.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Collections.Concurrent;
55
using Ray.Core.Abstractions;
66
using Ray.Core.Exceptions;
7-
using Ray.Core.Core.Observer;
87
using Orleans;
98

109
namespace Ray.Core
@@ -94,11 +93,11 @@ public object GetUnit(Type grainType)
9493
throw new UnfindObserverUnitException(grainType.FullName);
9594
}
9695

97-
public void Register(IGrainID followUnit)
96+
public void Register(IGrainID observerUnit)
9897
{
99-
if (!unitDict.TryAdd(followUnit.GrainType, followUnit))
98+
if (!unitDict.TryAdd(observerUnit.GrainType, observerUnit))
10099
{
101-
throw new ObserverUnitRepeatedException(followUnit.GrainType.FullName);
100+
throw new ObserverUnitRepeatedException(observerUnit.GrainType.FullName);
102101
}
103102
}
104103
}

src/Ray.Core/Storage/Configuration/ConfigureBuilder.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ public ConfigureBuilder(
2727

2828
protected void Observe<Follow>(Func<IServiceProvider, PrimaryKey, Parameter, FollowConfig> generator)
2929
{
30-
followConfigGeneratorDict.Add(typeof(Follow), generator);
30+
Observe(typeof(Follow), generator);
31+
}
32+
protected void Observe(Type type, Func<IServiceProvider, PrimaryKey, Parameter, FollowConfig> generator)
33+
{
34+
followConfigGeneratorDict.Add(type, generator);
3135
}
3236
readonly SemaphoreSlim seamphore = new SemaphoreSlim(1, 1);
3337
public async ValueTask<IStorageOptions> GetConfig(IServiceProvider serviceProvider, PrimaryKey primaryKey)

0 commit comments

Comments
 (0)