Skip to content

Commit

Permalink
优化AddStardust注释,提供监控、配置和服务注册发现能力,先后从参数配置和本机StarAgent读取星尘平台地址,让用户直观看到星尘能力
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Oct 23, 2024
1 parent 66a0314 commit edf6d26
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 34 deletions.
2 changes: 1 addition & 1 deletion ClientTest/ConfigTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void Http_Test()
[Fact]
public void Redis_ConfigTest()
{
var star = new StarFactory("http://star.newlifex.com:6600", "Test", null);
using var star = new StarFactory("http://star.newlifex.com:6600", "Test", null);

var services = ObjectContainer.Current;
services.AddSingleton(star.Config);
Expand Down
2 changes: 1 addition & 1 deletion StarAgent/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ public void StartFactory()
if (!server.IsNullOrEmpty())
{
_factory = new StarFactory(server, "StarAgent", null);
_container.AddSingleton(_factory);
_factory.Register(_container);

// 激活配置中心,获取PluginServer
var config = _factory.GetConfig();
Expand Down
4 changes: 2 additions & 2 deletions Stardust.Extensions/StarFactoryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ namespace Microsoft.Extensions.DependencyInjection;
/// <summary>星尘工厂扩展</summary>
public static class StarFactoryExtensions
{
/// <summary>添加星尘</summary>
/// <summary>添加星尘,提供监控、配置和服务注册发现能力。先后从参数配置和本机StarAgent读取星尘平台地址</summary>
/// <param name="services"></param>
/// <param name="appId"></param>
/// <returns></returns>
public static StarFactory AddStardust(this IServiceCollection services, String appId) => AddStardust(services, null, appId, null);

/// <summary>添加星尘</summary>
/// <summary>添加星尘,提供监控、配置和服务注册发现能力。先后从参数配置和本机StarAgent读取星尘平台地址</summary>
/// <param name="services"></param>
/// <param name="server">服务端地址。为空时先后读取appsettings.json、本地StarAgent、star.config,初始值为空,不连接服务端</param>
/// <param name="appId">应用标识。为空时读取star.config,初始值为入口程序集名称</param>
Expand Down
23 changes: 14 additions & 9 deletions Stardust/StarFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ protected override void Dispose(Boolean disposing)
_tracer.TryDispose();
_config.TryDispose();
}
#endregion

#region 初始化注册
private void Init()
{
XTrace.WriteLine("正在初始化星尘……");
Expand Down Expand Up @@ -243,18 +245,21 @@ private void Init()
XTrace.WriteLine("星尘分布式服务 Server={0} AppId={1} ClientId={2}", Server, AppId, ClientId);

Valid();
}

var ioc = ObjectContainer.Current;
ioc.AddSingleton(this);
ioc.AddSingleton(p => Tracer ?? DefaultTracer.Instance ?? (DefaultTracer.Instance ??= new DefaultTracer()));
//ioc.AddSingleton(p => Config);
ioc.AddSingleton(p => Service!);
/// <summary>注册到对象容器</summary>
/// <param name="container"></param>
public void Register(IObjectContainer container)
{
container.AddSingleton(this);
container.AddSingleton(p => Tracer ?? DefaultTracer.Instance ?? (DefaultTracer.Instance ??= new DefaultTracer()));
container.AddSingleton(p => Service!);

// 替换为混合配置提供者,优先本地配置
ioc.AddSingleton(p => GetConfig()!);
container.AddSingleton(p => GetConfig()!);

ioc.TryAddSingleton(XTrace.Log);
ioc.TryAddSingleton(typeof(ICacheProvider), typeof(CacheProvider));
container.TryAddSingleton(XTrace.Log);
container.TryAddSingleton(typeof(ICacheProvider), typeof(CacheProvider));
}

[MemberNotNullWhen(true, nameof(_client))]
Expand Down Expand Up @@ -396,7 +401,7 @@ public void SetLocalConfig(IConfigProvider configProvider)

/// <summary>获取复合配置提供者</summary>
/// <returns></returns>
public IConfigProvider? GetConfig() => _configProvider ?? Config;
public IConfigProvider? GetConfig() => _configProvider ?? Config ?? JsonConfigProvider.LoadAppSettings();
#endregion

#region 注册中心
Expand Down
29 changes: 8 additions & 21 deletions Stardust/StarHelper.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
using NewLife.Caching;
using NewLife.Configuration;
using NewLife.Log;
using NewLife.Configuration;
using NewLife.Model;

namespace Stardust;

/// <summary>星尘辅助类</summary>
public static class StarHelper
{
/// <summary>添加星尘</summary>
/// <summary>添加星尘,提供监控、配置和服务注册发现能力。先后从参数配置和本机StarAgent读取星尘平台地址</summary>
/// <param name="services"></param>
/// <param name="appId"></param>
/// <returns></returns>
public static StarFactory AddStardust(this IObjectContainer services, String appId) => AddStardust(services, null, appId, null);

/// <summary>添加星尘</summary>
/// <summary>添加星尘,提供监控、配置和服务注册发现能力。先后从参数配置和本机StarAgent读取星尘平台地址</summary>
/// <param name="services"></param>
/// <param name="server">服务端地址。为空时先后读取appsettings.json、本地StarAgent、star.config,初始值为空,不连接服务端</param>
/// <param name="appId">应用标识。为空时读取star.config,初始值为入口程序集名称</param>
Expand All @@ -24,24 +22,13 @@ public static StarFactory AddStardust(this IObjectContainer services, String? se
{
var star = new StarFactory(server, appId, secret);

// 替换为混合配置提供者,优先本地配置
var old = JsonConfigProvider.LoadAppSettings();
star.SetLocalConfig(old);

if (services != ObjectContainer.Current && services is ObjectContainer container)
{
// 替换为混合配置提供者,优先本地配置
var old = JsonConfigProvider.LoadAppSettings();
star.SetLocalConfig(old);

services.AddSingleton(star);
services.AddSingleton(p => star.Tracer ?? DefaultTracer.Instance ?? (DefaultTracer.Instance ??= new DefaultTracer()));
//services.AddSingleton(p => star.Config);
services.AddSingleton(p => star.Service!);

// 替换为混合配置提供者,优先本地配置
services.AddSingleton(p => star.GetConfig()!);

// 分布式缓存
//services.TryAddSingleton<ICacheProvider, CacheProvider>();
services.TryAddSingleton(XTrace.Log);
services.TryAddSingleton(typeof(ICacheProvider), typeof(CacheProvider));
star.Register(services);
}

return star;
Expand Down

0 comments on commit edf6d26

Please sign in to comment.