From 52f1121acc4c99ef3c1037afad2a1212d3624520 Mon Sep 17 00:00:00 2001 From: YangQiang Date: Tue, 3 Mar 2026 16:37:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=95=E5=85=A5=E7=94=A8=E6=88=B7=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E6=8E=A5=E5=8F=A3=E5=8F=8A=E5=AE=9E=E7=8E=B0=EF=BC=8C?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=9C=8D=E5=8A=A1=E8=B0=83=E7=94=A8=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增 IUserService 接口及 UserService 实现,抽象用户微服务调用逻辑。TestController 通过 IUserService 获取用户信息,替换原有直接调用方式。Program.cs 中完成依赖注入注册,提升代码可维护性与扩展性。补充相关 using 引用,保证服务正常集成。 --- .../Controllers/TestController.cs | 8 +++-- MicoService.Demo/Program.cs | 6 +++- Microservice.Common/Services/IUserService.cs | 17 ++++++++++ Microservice.Common/Services/UserService.cs | 32 +++++++++++++++++++ 4 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 Microservice.Common/Services/IUserService.cs create mode 100644 Microservice.Common/Services/UserService.cs diff --git a/MicoService.Demo/Controllers/TestController.cs b/MicoService.Demo/Controllers/TestController.cs index a7c15d8..590e0f2 100644 --- a/MicoService.Demo/Controllers/TestController.cs +++ b/MicoService.Demo/Controllers/TestController.cs @@ -5,6 +5,7 @@ using Nacos.V2; using System.Threading.Tasks; using Microservice.Common; using Microservice.Common.Models; +using Microservice.Common.Services; namespace MicoService.Demo.Controllers { @@ -16,16 +17,19 @@ namespace MicoService.Demo.Controllers private readonly IConfiguration _configuration; private readonly INacosNamingService _nacosNamingService; private readonly IServiceClient _serviceClient; + private readonly IUserService _userService; private readonly ILogger _logger; public TestController(IConfiguration configuration, INacosNamingService nacosNamingService, IServiceClient serviceClient, + IUserService userService, ILogger logger) { this._configuration = configuration; this._nacosNamingService = nacosNamingService; this._serviceClient = serviceClient; + this._userService = userService; this._logger = logger; } @@ -61,8 +65,8 @@ namespace MicoService.Demo.Controllers { try { - // 使用微服务通讯客户端调用 Mico_Demo2222 服务 - var result = await this._serviceClient.GetAsync("Mico_Demo2222", "/User/config/info"); + // 使用用户服务调用用户微服务 + var result = await this._userService.GetUserInfoAsync(); var data = new ServiceCallResultModel("调用成功", result); return Ok(ApiResponseHelper.Success(data, "服务调用成功")); } diff --git a/MicoService.Demo/Program.cs b/MicoService.Demo/Program.cs index 47c62e3..159b287 100644 --- a/MicoService.Demo/Program.cs +++ b/MicoService.Demo/Program.cs @@ -1,7 +1,8 @@ - + using Nacos.AspNetCore.V2; using Nacos.V2.DependencyInjection; using Microservice.Common; +using Microservice.Common.Services; namespace MicoService.Demo { @@ -31,6 +32,9 @@ namespace MicoService.Demo // 注册微服务通讯客户端 builder.Services.AddServiceClient(); + + // 注册用户服务 + builder.Services.AddScoped(); var app = builder.Build(); diff --git a/Microservice.Common/Services/IUserService.cs b/Microservice.Common/Services/IUserService.cs new file mode 100644 index 0000000..bbb59cc --- /dev/null +++ b/Microservice.Common/Services/IUserService.cs @@ -0,0 +1,17 @@ +using System.Threading.Tasks; +using Microservice.Common.Models; + +namespace Microservice.Common.Services +{ + /// + /// 用户服务接口 + /// + public interface IUserService + { + /// + /// 获取用户配置信息 + /// + /// 配置信息模型 + Task GetUserInfoAsync(); + } +} diff --git a/Microservice.Common/Services/UserService.cs b/Microservice.Common/Services/UserService.cs new file mode 100644 index 0000000..7d9cf0d --- /dev/null +++ b/Microservice.Common/Services/UserService.cs @@ -0,0 +1,32 @@ +using System.Threading.Tasks; +using Microservice.Common.Models; + +namespace Microservice.Common.Services +{ + /// + /// 用户服务实现类 + /// + public class UserService : IUserService + { + private readonly IServiceClient _serviceClient; + + /// + /// 构造函数 + /// + /// 微服务客户端 + public UserService(IServiceClient serviceClient) + { + _serviceClient = serviceClient; + } + + /// + /// 获取用户配置信息 + /// + /// 配置信息模型 + public async Task GetUserInfoAsync() + { + // 直接使用硬编码的服务名称和路径,暂时不使用 ServiceRegistry + return await _serviceClient.GetAsync("Mico_Demo2222", "/User/config/info"); + } + } +}