From 945c4a8d05132afc26790dedc8b6490802b81c21 Mon Sep 17 00:00:00 2001 From: YangQiang Date: Sat, 7 Feb 2026 17:59:09 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9B=86=E6=88=90Nacos=E5=B9=B6=E6=96=B0?= =?UTF-8?q?=E5=A2=9EDemo2=E5=BE=AE=E6=9C=8D=E5=8A=A1=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 本次提交实现了微服务架构扩展,新增 MicoService.Demo2 项目并集成 Nacos 配置中心与注册中心。主项目移除 WeatherForecast 相关代码,新增 TestController 支持服务间调用。完善配置文件,支持服务注册、发现与配置管理,为后续微服务通信和动态配置打下基础。 --- MicoService.Demo.slnx | 1 + .../Controllers/TestController.cs | 56 +++++++++++++++++++ .../Controllers/WeatherForecastController.cs | 38 ------------- MicoService.Demo/Program.cs | 9 ++- MicoService.Demo/WeatherForecast.cs | 13 ----- MicoService.Demo/appsettings.Development.json | 2 +- .../Controllers/UserController.cs | 28 ++++++++++ MicoService.Demo2/MicoService.Demo2.csproj | 15 +++++ MicoService.Demo2/Program.cs | 45 +++++++++++++++ .../Properties/launchSettings.json | 14 +++++ .../appsettings.Development.json | 29 ++++++++++ MicoService.Demo2/appsettings.json | 9 +++ 12 files changed, 204 insertions(+), 55 deletions(-) create mode 100644 MicoService.Demo/Controllers/TestController.cs delete mode 100644 MicoService.Demo/Controllers/WeatherForecastController.cs delete mode 100644 MicoService.Demo/WeatherForecast.cs create mode 100644 MicoService.Demo2/Controllers/UserController.cs create mode 100644 MicoService.Demo2/MicoService.Demo2.csproj create mode 100644 MicoService.Demo2/Program.cs create mode 100644 MicoService.Demo2/Properties/launchSettings.json create mode 100644 MicoService.Demo2/appsettings.Development.json create mode 100644 MicoService.Demo2/appsettings.json diff --git a/MicoService.Demo.slnx b/MicoService.Demo.slnx index 7cbfcce..e84a1f1 100644 --- a/MicoService.Demo.slnx +++ b/MicoService.Demo.slnx @@ -1,3 +1,4 @@ + diff --git a/MicoService.Demo/Controllers/TestController.cs b/MicoService.Demo/Controllers/TestController.cs new file mode 100644 index 0000000..66bfc08 --- /dev/null +++ b/MicoService.Demo/Controllers/TestController.cs @@ -0,0 +1,56 @@ +using Microsoft.AspNetCore.Mvc; +using Nacos.V2; +using System.Threading.Tasks; + +namespace MicoService.Demo.Controllers +{ + [ApiController] + [Route("[controller]")] + public class TestController : ControllerBase + { + + private readonly IConfiguration _configuration; + private readonly INacosNamingService _nacosNamingService; + + public TestController(IConfiguration configuration, + INacosNamingService nacosNamingService) + { + this._configuration = configuration; + this._nacosNamingService = nacosNamingService; + } + + + + /// + /// ʾȡ Nacos + /// + /// + /// + [HttpGet("config/{key}")] + public string GetConfig(string key) + { + return _configuration[key]; + } + + /// + /// ʾ΢ + /// + [HttpGet("call")] + public async Task CallOtherService() + { + // APIȡʵøؾ⣩ + var instance = await _nacosNamingService.SelectOneHealthyInstance("Mico_Demo2222"); + + if (instance == null) + { + Console.WriteLine("޿ʵ"); + } + + // ƴӵõַ磺http://192.168.2.10:8080/api/xxx + var callUrl = $"http://{instance.Ip}:{instance.Port}/user/config/info"; + + } + + + } +} diff --git a/MicoService.Demo/Controllers/WeatherForecastController.cs b/MicoService.Demo/Controllers/WeatherForecastController.cs deleted file mode 100644 index 02925e4..0000000 --- a/MicoService.Demo/Controllers/WeatherForecastController.cs +++ /dev/null @@ -1,38 +0,0 @@ -using Microsoft.AspNetCore.Mvc; - -namespace MicoService.Demo.Controllers -{ - [ApiController] - [Route("[controller]")] - public class WeatherForecastController : ControllerBase - { - private static readonly string[] Summaries = - [ - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" - ]; - private readonly IConfiguration _configuration; - - public WeatherForecastController(IConfiguration configuration) - { - this._configuration = configuration; - } - - [HttpGet(Name = "GetWeatherForecast")] - public IEnumerable Get() - { - return Enumerable.Range(1, 5).Select(index => new WeatherForecast - { - Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)), - TemperatureC = Random.Shared.Next(-20, 55), - Summary = Summaries[Random.Shared.Next(Summaries.Length)] - }) - .ToArray(); - } - - [HttpGet("config/{key}")] - public string GetConfig(string key) - { - return _configuration[key]; - } - } -} diff --git a/MicoService.Demo/Program.cs b/MicoService.Demo/Program.cs index edd7fb8..fe86fb6 100644 --- a/MicoService.Demo/Program.cs +++ b/MicoService.Demo/Program.cs @@ -10,13 +10,16 @@ namespace MicoService.Demo { var builder = WebApplication.CreateBuilder(args); - + + #region Nacos + // Nacos builder.Host.UseNacosConfig("nacos"); builder.Services.AddNacosV2Config(builder.Configuration); - + // Nacos עᷢ builder.Services.AddNacosAspNet(builder.Configuration); - builder.Services.AddNacosV2Naming(builder.Configuration); + builder.Services.AddNacosV2Naming(builder.Configuration); + #endregion // Add services to the container. diff --git a/MicoService.Demo/WeatherForecast.cs b/MicoService.Demo/WeatherForecast.cs deleted file mode 100644 index 3e0cdf0..0000000 --- a/MicoService.Demo/WeatherForecast.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace MicoService.Demo -{ - public class WeatherForecast - { - public DateOnly Date { get; set; } - - public int TemperatureC { get; set; } - - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); - - public string? Summary { get; set; } - } -} diff --git a/MicoService.Demo/appsettings.Development.json b/MicoService.Demo/appsettings.Development.json index 4c7e361..ddde512 100644 --- a/MicoService.Demo/appsettings.Development.json +++ b/MicoService.Demo/appsettings.Development.json @@ -17,7 +17,7 @@ // Ncaos 注册中心的配置 "DefaultTimeOut": 15000, "ListenInterval": 1000, - "ServiceName": "Mico_Demo1", + "ServiceName": "Mico_Demo1111", "GroupName": "DEFAULT_GROUP", "RegisterEnabled": true, "InstanceEnabled": true, diff --git a/MicoService.Demo2/Controllers/UserController.cs b/MicoService.Demo2/Controllers/UserController.cs new file mode 100644 index 0000000..94c5db9 --- /dev/null +++ b/MicoService.Demo2/Controllers/UserController.cs @@ -0,0 +1,28 @@ +using Microsoft.AspNetCore.Mvc; + +namespace MicoService.Demo2.Controllers +{ + [ApiController] + [Route("[controller]")] + public class UserController : ControllerBase + { + + public UserController( IConfiguration configuration) + { + _configuration = configuration; + } + private readonly IConfiguration _configuration; + /// + /// ʾȡ Nacos + /// + /// + /// + [HttpGet("config/{key}")] + public string GetConfig(string key) + { + return _configuration[key]; + } + + + } +} diff --git a/MicoService.Demo2/MicoService.Demo2.csproj b/MicoService.Demo2/MicoService.Demo2.csproj new file mode 100644 index 0000000..25d4627 --- /dev/null +++ b/MicoService.Demo2/MicoService.Demo2.csproj @@ -0,0 +1,15 @@ + + + + net10.0 + enable + enable + + + + + + + + + diff --git a/MicoService.Demo2/Program.cs b/MicoService.Demo2/Program.cs new file mode 100644 index 0000000..7745c94 --- /dev/null +++ b/MicoService.Demo2/Program.cs @@ -0,0 +1,45 @@ + +using Nacos.AspNetCore.V2; +using Nacos.V2.DependencyInjection; + +namespace MicoService.Demo2 +{ + public class Program + { + public static void Main(string[] args) + { + var builder = WebApplication.CreateBuilder(args); + + #region Nacos + // Nacos + builder.Host.UseNacosConfig("nacos"); + builder.Services.AddNacosV2Config(builder.Configuration); + + // Nacos עᷢ + builder.Services.AddNacosAspNet(builder.Configuration); + builder.Services.AddNacosV2Naming(builder.Configuration); + #endregion + + // Add services to the container. + + builder.Services.AddControllers(); + // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi + builder.Services.AddOpenApi(); + + var app = builder.Build(); + + // Configure the HTTP request pipeline. + if (app.Environment.IsDevelopment()) + { + app.MapOpenApi(); + } + + app.UseAuthorization(); + + + app.MapControllers(); + + app.Run(); + } + } +} diff --git a/MicoService.Demo2/Properties/launchSettings.json b/MicoService.Demo2/Properties/launchSettings.json new file mode 100644 index 0000000..f4639c3 --- /dev/null +++ b/MicoService.Demo2/Properties/launchSettings.json @@ -0,0 +1,14 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "http://localhost:5181", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/MicoService.Demo2/appsettings.Development.json b/MicoService.Demo2/appsettings.Development.json new file mode 100644 index 0000000..4e776a6 --- /dev/null +++ b/MicoService.Demo2/appsettings.Development.json @@ -0,0 +1,29 @@ +{ + "nacos": { + // Nacos 配置中心的配置 + "Namespace": "685d64ce-d088-400f-8cfe-a669a89015d0", // 你的命名空间ID + "ServerAddresses": [ "http://192.168.2.9:8848" ], // 自建Nacos地址(去掉末尾/,避免解析问题) + "UserName": "nacos", // 你的Nacos用户名 + "Password": "qwe123!@#", // 你的Nacos密码 + "Group": "DEFAULT_GROUP", // 默认配置分组,可按需修改 + "Listeners": [ + { + "Optional": false, + "DataId": "demo2", + "Group": "DEFAULT_GROUP" + } + ], + + // Ncaos 注册中心的配置 + "DefaultTimeOut": 15000, + "ListenInterval": 1000, + "ServiceName": "Mico_Demo2222", + "GroupName": "DEFAULT_GROUP", + "RegisterEnabled": true, + "InstanceEnabled": true, + "Ephemeral": true, + "ConfigUseRpc": true, + "NamingUseRpc": true, + "LBStrategy": "WeightRandom" //WeightRandom WeightRoundRobin + } +} \ No newline at end of file diff --git a/MicoService.Demo2/appsettings.json b/MicoService.Demo2/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/MicoService.Demo2/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +}