Skip to content
This repository has been archived by the owner on Jan 1, 2023. It is now read-only.

Commit

Permalink
✨ nacos demo
Browse files Browse the repository at this point in the history
  • Loading branch information
catcherwong committed Sep 8, 2019
1 parent 01c4f49 commit b3818f2
Show file tree
Hide file tree
Showing 26 changed files with 614 additions and 0 deletions.
14 changes: 14 additions & 0 deletions 09/NacosDemo/BaseService/BaseService.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="nacos-sdk-csharp-unofficial.AspNetCore" Version="0.1.4" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions 09/NacosDemo/BaseService/Controllers/ValuesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace BaseService.Controllers
{
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return new string[] { "value1", "value2", "base111111111111" };
}
}
}
18 changes: 18 additions & 0 deletions 09/NacosDemo/BaseService/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace BaseService
{
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseUrls("http://localhost:9876");
}
}
37 changes: 37 additions & 0 deletions 09/NacosDemo/BaseService/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace BaseService
{
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Nacos.AspNetCore;

public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
services.AddNacosAspNetCore(Configuration);
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseMvc();

app.UseNacosAspNetCore();
}
}
}
9 changes: 9 additions & 0 deletions 09/NacosDemo/BaseService/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
16 changes: 16 additions & 0 deletions 09/NacosDemo/BaseService/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"nacos": {
"ServerAddresses": [ "localhost:8848" ],
"DefaultTimeOut": 15,
"Namespace": "",
"ListenInterval": 1000,
"ServiceName": "BaseService",
"Weight": 10
}
}
14 changes: 14 additions & 0 deletions 09/NacosDemo/BaseService2/BaseService2.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="nacos-sdk-csharp-unofficial.AspNetCore" Version="0.1.4" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions 09/NacosDemo/BaseService2/Controllers/ValuesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace BaseService2.Controllers
{
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return new string[] { "value1", "value2", "base2222222222222" };
}
}
}
18 changes: 18 additions & 0 deletions 09/NacosDemo/BaseService2/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace BaseService2
{
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseUrls("http://localhost:9877");
}
}
36 changes: 36 additions & 0 deletions 09/NacosDemo/BaseService2/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace BaseService2
{
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Nacos.AspNetCore;

public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
services.AddNacosAspNetCore(Configuration);
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseNacosAspNetCore();
app.UseMvc();
}
}
}
9 changes: 9 additions & 0 deletions 09/NacosDemo/BaseService2/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
16 changes: 16 additions & 0 deletions 09/NacosDemo/BaseService2/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"nacos": {
"ServerAddresses": [ "localhost:8848" ],
"DefaultTimeOut": 15,
"Namespace": "",
"ListenInterval": 1000,
"ServiceName": "BaseService",
"Weight": 10
}
}
14 changes: 14 additions & 0 deletions 09/NacosDemo/ConfigDemo/ConfigDemo.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="nacos-sdk-csharp-unofficial" Version="0.1.4" />
</ItemGroup>

</Project>
47 changes: 47 additions & 0 deletions 09/NacosDemo/ConfigDemo/Controllers/ConfigController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace ConfigDemo.Controllers
{
using Microsoft.AspNetCore.Mvc;
using Nacos;
using System.Threading.Tasks;

[Route("api/[controller]")]
[ApiController]
public class ConfigController : ControllerBase
{
private readonly INacosConfigClient _configClient;

public ConfigController(INacosConfigClient configClient)
{
_configClient = configClient;
}

// GET api/config?key=demo1
[HttpGet("")]
public async Task<string> Get([FromQuery]string key)
{
var res = await _configClient.GetConfigAsync(new GetConfigRequest
{
DataId = key,
Group = "DEFAULT_GROUP",
//Tenant = "tenant"
}) ;

return string.IsNullOrWhiteSpace(res) ? "Not Found" : res;
}

// GET api/config/add?key=demo1&value=123
[HttpGet("add")]
public async Task<string> Add([FromQuery]string key, [FromQuery]string value)
{
var res = await _configClient.PublishConfigAsync(new PublishConfigRequest
{
DataId = key,
Group = "DEFAULT_GROUP",
//Tenant = "tenant"
Content = value
});

return res.ToString();
}
}
}
60 changes: 60 additions & 0 deletions 09/NacosDemo/ConfigDemo/ListenConfigurationBgTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
namespace ConfigDemo
{
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Nacos;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

public class ListenConfigurationBgTask : BackgroundService
{
private readonly ILogger _logger;

private readonly INacosConfigClient _configClient;

public ListenConfigurationBgTask(ILoggerFactory loggerFactory, INacosConfigClient configClient)
{
_logger = loggerFactory.CreateLogger<ListenConfigurationBgTask>();

_configClient = configClient;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
// Add listener
await _configClient.AddListenerAsync(new AddListenerRequest
{
DataId = "demo1",
//Group = "DEFAULT_GROUP",
//Tenant = "tenant",
Callbacks = new List<Action<string>>
{
x =>
{
_logger.LogInformation($" We found something changed!!! {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} [{x}]");
},
}
});
}

public override async Task StopAsync(CancellationToken cancellationToken)
{
// Remove listener
await _configClient.RemoveListenerAsync(new RemoveListenerRequest
{
DataId = "demo1",
Callbacks = new List<Action>
{
() =>
{
_logger.LogInformation($" Removed listerner ");
},
}
});

await base.StopAsync(cancellationToken);
}
}
}
17 changes: 17 additions & 0 deletions 09/NacosDemo/ConfigDemo/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace ConfigDemo
{
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}
Loading

0 comments on commit b3818f2

Please sign in to comment.