引入用户服务接口及实现,优化服务调用逻辑
新增 IUserService 接口及 UserService 实现,抽象用户微服务调用逻辑。TestController 通过 IUserService 获取用户信息,替换原有直接调用方式。Program.cs 中完成依赖注入注册,提升代码可维护性与扩展性。补充相关 using 引用,保证服务正常集成。
This commit is contained in:
parent
a2538ebbb9
commit
52f1121acc
|
|
@ -5,6 +5,7 @@ using Nacos.V2;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microservice.Common;
|
using Microservice.Common;
|
||||||
using Microservice.Common.Models;
|
using Microservice.Common.Models;
|
||||||
|
using Microservice.Common.Services;
|
||||||
|
|
||||||
namespace MicoService.Demo.Controllers
|
namespace MicoService.Demo.Controllers
|
||||||
{
|
{
|
||||||
|
|
@ -16,16 +17,19 @@ namespace MicoService.Demo.Controllers
|
||||||
private readonly IConfiguration _configuration;
|
private readonly IConfiguration _configuration;
|
||||||
private readonly INacosNamingService _nacosNamingService;
|
private readonly INacosNamingService _nacosNamingService;
|
||||||
private readonly IServiceClient _serviceClient;
|
private readonly IServiceClient _serviceClient;
|
||||||
|
private readonly IUserService _userService;
|
||||||
private readonly ILogger<TestController> _logger;
|
private readonly ILogger<TestController> _logger;
|
||||||
|
|
||||||
public TestController(IConfiguration configuration,
|
public TestController(IConfiguration configuration,
|
||||||
INacosNamingService nacosNamingService,
|
INacosNamingService nacosNamingService,
|
||||||
IServiceClient serviceClient,
|
IServiceClient serviceClient,
|
||||||
|
IUserService userService,
|
||||||
ILogger<TestController> logger)
|
ILogger<TestController> logger)
|
||||||
{
|
{
|
||||||
this._configuration = configuration;
|
this._configuration = configuration;
|
||||||
this._nacosNamingService = nacosNamingService;
|
this._nacosNamingService = nacosNamingService;
|
||||||
this._serviceClient = serviceClient;
|
this._serviceClient = serviceClient;
|
||||||
|
this._userService = userService;
|
||||||
this._logger = logger;
|
this._logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -61,8 +65,8 @@ namespace MicoService.Demo.Controllers
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// 使用微服务通讯客户端调用 Mico_Demo2222 服务
|
// 使用用户服务调用用户微服务
|
||||||
var result = await this._serviceClient.GetAsync<ConfigInfoModel>("Mico_Demo2222", "/User/config/info");
|
var result = await this._userService.GetUserInfoAsync();
|
||||||
var data = new ServiceCallResultModel<ConfigInfoModel>("调用成功", result);
|
var data = new ServiceCallResultModel<ConfigInfoModel>("调用成功", result);
|
||||||
return Ok(ApiResponseHelper.Success(data, "服务调用成功"));
|
return Ok(ApiResponseHelper.Success(data, "服务调用成功"));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
|
|
||||||
using Nacos.AspNetCore.V2;
|
using Nacos.AspNetCore.V2;
|
||||||
using Nacos.V2.DependencyInjection;
|
using Nacos.V2.DependencyInjection;
|
||||||
using Microservice.Common;
|
using Microservice.Common;
|
||||||
|
using Microservice.Common.Services;
|
||||||
|
|
||||||
namespace MicoService.Demo
|
namespace MicoService.Demo
|
||||||
{
|
{
|
||||||
|
|
@ -31,6 +32,9 @@ namespace MicoService.Demo
|
||||||
|
|
||||||
// 注册微服务通讯客户端
|
// 注册微服务通讯客户端
|
||||||
builder.Services.AddServiceClient();
|
builder.Services.AddServiceClient();
|
||||||
|
|
||||||
|
// 注册用户服务
|
||||||
|
builder.Services.AddScoped<IUserService, UserService>();
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microservice.Common.Models;
|
||||||
|
|
||||||
|
namespace Microservice.Common.Services
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 用户服务接口
|
||||||
|
/// </summary>
|
||||||
|
public interface IUserService
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 获取用户配置信息
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>配置信息模型</returns>
|
||||||
|
Task<ConfigInfoModel> GetUserInfoAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,32 @@
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microservice.Common.Models;
|
||||||
|
|
||||||
|
namespace Microservice.Common.Services
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 用户服务实现类
|
||||||
|
/// </summary>
|
||||||
|
public class UserService : IUserService
|
||||||
|
{
|
||||||
|
private readonly IServiceClient _serviceClient;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 构造函数
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="serviceClient">微服务客户端</param>
|
||||||
|
public UserService(IServiceClient serviceClient)
|
||||||
|
{
|
||||||
|
_serviceClient = serviceClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取用户配置信息
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>配置信息模型</returns>
|
||||||
|
public async Task<ConfigInfoModel> GetUserInfoAsync()
|
||||||
|
{
|
||||||
|
// 直接使用硬编码的服务名称和路径,暂时不使用 ServiceRegistry
|
||||||
|
return await _serviceClient.GetAsync<ConfigInfoModel>("Mico_Demo2222", "/User/config/info");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue