ApolloCore 0.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package ApolloCore --version 0.0.1
                    
NuGet\Install-Package ApolloCore -Version 0.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ApolloCore" Version="0.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ApolloCore" Version="0.0.1" />
                    
Directory.Packages.props
<PackageReference Include="ApolloCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ApolloCore --version 0.0.1
                    
#r "nuget: ApolloCore, 0.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ApolloCore@0.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ApolloCore&version=0.0.1
                    
Install as a Cake Addin
#tool nuget:?package=ApolloCore&version=0.0.1
                    
Install as a Cake Tool

快速开始

apollo 服务搭建详见 Apollo 快速部署

引入包

Install-Package ApolloCore -Version 0.0.1

启动

// Program.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureApolloConfiguration()
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
	...
	services.AddApolloClient(Configuration);
	...
}

增加 AppSettings.json 节点

"apollo": {
    "AppId": "msdemo-client",
    //"Cluster": "node1",
    "MetaServer": "http://192.168.3.67:8080/",
    "Namespaces": [ "msdemonamespace.json", "application" ],
    "ConfigServer": [ "http://192.168.3.67:8080/" ]
}

使用

[ApiController]
[Route("apollo")]
public class LoadApolloConfigureController : Controller
{
    private readonly IApolloClient _apolloClient;
    public LoadApolloConfigureController(IApolloClient apolloClient)
    {
        _apolloClient = apolloClient;
    }

    [HttpGet("{key}")]
    public async Task<string> GetAsync(string key)
    {
        var obj = await _apolloClient.GetAsync<ModConfig>(key);
        return await Task.FromResult(JsonSerializer.Serialize(obj));
    }
}

配置监控功能

如果想要开启配置文件监听功能,即 apollo 发生配置更改,客户端能触发回调事件。需要做以下配置

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
	...
	// 开启监听
	app.StartConfigChangeListening();
	...
}

除此之外还要添加监听的节点类型:

public void ConfigureServices(IServiceCollection services)
{
	services.AddApolloClient(Configuration)
		.AddMonitor<ModConfig>("ModConfig");
}

这样就能通过手动注入 IOptionMonitor<TConfig> 来实现对文件的监听了。

该组件内置了统一调度监听功能,需要添加对应的 IConfigWatcher 的实现即可:

public class ModConfigWatcher : ConfigWatcherBase<ModConfig>, IConfigWatcher
{
    public ModConfigWatcher(IOptionsMonitor<ModConfig> monitor) : base(monitor)
    {
    }

    protected override void RaiseChange(ModConfig config, string arg)
    {
        if (_monitor.CurrentValue.AppId != config.AppId
            || _monitor.CurrentValue.Platform != config.Platform
            || _monitor.CurrentValue.RequestId != config.RequestId
            || _monitor.CurrentValue.Userflag != config.Userflag)
            Console.WriteLine($"配置发生变动 新值为:{JsonSerializer.Serialize(config)}");
    }
}
// 注入
services.AddTransient<IConfigWatcher, ModConfigWatcher>();

后续会自动注入...

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.0.2 586 2/20/2021
0.0.1 535 1/13/2021