From 0c83a19d21a5a6f448eeadb989387cef5850b695 Mon Sep 17 00:00:00 2001 From: YangQiang Date: Fri, 6 Feb 2026 17:48:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=A1=B9=E7=9B=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MicoService.Demo.slnx | 3 ++ .../Controllers/WeatherForecastController.cs | 38 ++++++++++++++++ MicoService.Demo/MicoService.Demo.csproj | 15 +++++++ MicoService.Demo/Program.cs | 44 +++++++++++++++++++ .../Properties/launchSettings.json | 14 ++++++ MicoService.Demo/WeatherForecast.cs | 13 ++++++ MicoService.Demo/appsettings.Development.json | 29 ++++++++++++ MicoService.Demo/appsettings.json | 9 ++++ 8 files changed, 165 insertions(+) create mode 100644 MicoService.Demo.slnx create mode 100644 MicoService.Demo/Controllers/WeatherForecastController.cs create mode 100644 MicoService.Demo/MicoService.Demo.csproj create mode 100644 MicoService.Demo/Program.cs create mode 100644 MicoService.Demo/Properties/launchSettings.json create mode 100644 MicoService.Demo/WeatherForecast.cs create mode 100644 MicoService.Demo/appsettings.Development.json create mode 100644 MicoService.Demo/appsettings.json diff --git a/MicoService.Demo.slnx b/MicoService.Demo.slnx new file mode 100644 index 0000000..7cbfcce --- /dev/null +++ b/MicoService.Demo.slnx @@ -0,0 +1,3 @@ + + + diff --git a/MicoService.Demo/Controllers/WeatherForecastController.cs b/MicoService.Demo/Controllers/WeatherForecastController.cs new file mode 100644 index 0000000..02925e4 --- /dev/null +++ b/MicoService.Demo/Controllers/WeatherForecastController.cs @@ -0,0 +1,38 @@ +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/MicoService.Demo.csproj b/MicoService.Demo/MicoService.Demo.csproj new file mode 100644 index 0000000..25d4627 --- /dev/null +++ b/MicoService.Demo/MicoService.Demo.csproj @@ -0,0 +1,15 @@ + + + + net10.0 + enable + enable + + + + + + + + + diff --git a/MicoService.Demo/Program.cs b/MicoService.Demo/Program.cs new file mode 100644 index 0000000..edd7fb8 --- /dev/null +++ b/MicoService.Demo/Program.cs @@ -0,0 +1,44 @@ + +using Nacos.AspNetCore.V2; +using Nacos.V2.DependencyInjection; + +namespace MicoService.Demo +{ + public class Program + { + public static void Main(string[] args) + { + var builder = WebApplication.CreateBuilder(args); + + + builder.Host.UseNacosConfig("nacos"); + builder.Services.AddNacosV2Config(builder.Configuration); + + + builder.Services.AddNacosAspNet(builder.Configuration); + builder.Services.AddNacosV2Naming(builder.Configuration); + + + // 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.Demo/Properties/launchSettings.json b/MicoService.Demo/Properties/launchSettings.json new file mode 100644 index 0000000..70c78b8 --- /dev/null +++ b/MicoService.Demo/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:5252", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/MicoService.Demo/WeatherForecast.cs b/MicoService.Demo/WeatherForecast.cs new file mode 100644 index 0000000..3e0cdf0 --- /dev/null +++ b/MicoService.Demo/WeatherForecast.cs @@ -0,0 +1,13 @@ +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 new file mode 100644 index 0000000..4c7e361 --- /dev/null +++ b/MicoService.Demo/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": "demo_dev", + "Group": "DEFAULT_GROUP" + } + ], + + // Ncaos 注册中心的配置 + "DefaultTimeOut": 15000, + "ListenInterval": 1000, + "ServiceName": "Mico_Demo1", + "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.Demo/appsettings.json b/MicoService.Demo/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/MicoService.Demo/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +}