【问题标题】:Duplicate data with multiple instance of microservice具有多个微服务实例的重复数据
【发布时间】:2019-04-10 00:55:39
【问题描述】:

我遇到了重复数据的问题。 我有一个客户端多次调用 API 来填充一些网格。 API 实例是一种检查数据是否存在的服务,如果不存在则创建所有数据。 问题是当创建数据的方法很重并且需要很长时间时。所以 2 个方法同时运行,并且都保存创建重复的数据 我可以将 API 调用排队并一次执行一个吗? 还是有另一种方法可以实现这一目标? 这里是API的代码

[Produces("application/json")]
[Route("api/v1/Combinations")]
public class CombinationsController : Controller
{
    private readonly ICommonService _CommonService;
    private readonly ILogger _logger;

    public CombinationsController(ICommonService CommonService,
        ILogger<CombinationsController> logger)
    {
        _logger = logger;
        _CommonService = CommonService;
    } 

    [HttpPost("GenerateCombinationsByAttributes",Name ="GenerateCombinationsByAttributes")]
    public async Task<IActionResult> GenerateCombinationsByAttributes([FromBody] List<Guid> attributeIds)
    {
        if (attributeIds == null || attributeIds.Count == 0 || attributeIds.Contains(Guid.Empty))
            return BadRequest();

        var ret = await _CommonService.GenerateCombinationByAttributesList(attributeIds);

        return Ok();
    }

}

【问题讨论】:

  • lock(或MethodImpl(MethodImplOptions.Synchronized)-不确定.net核心是否支持它)放在GenerateCombinationByAttributesList实现中......或在GenerateCombinationByAttributesList中使用事务......这取决于
  • @Selvin 我认为锁是本地的并且仅在微服务实例内部有效,因此每个实例都有自己的锁。微服务的实例是瞬态的services.AddTransient&lt;ICommonService, CommonService&gt;();
  • 好吧,我不知道services 是什么……但我知道的大多数 IoC 都在使用单个服务实例……但即使你仍然可以锁定 static object mylock = new object();
  • @Selvin services 是Microsoft.Extensions.DependencyInjectionIServiceCollection services 并且lock 内部的async Task 也不会导致某种问题?不管怎样,我会试着放一个锁
  • 数据库事务呢? (当然,如果您在 GenerateCombinationByAttributesList 中使用 db)

标签: c# entity-framework asp.net-core-2.0 microservices


【解决方案1】:

我想我已经设法用一个简单的信号量来解决它 我已经声明了一个

static SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1, 1);

在微服务类中 然后

 public async Task<Result> GenerateCombinationByAttributesList(List<Guid> attributeIds)
    {
        var result = new Result();
        await semaphoreSlim.WaitAsync();
        try
        {
            DoHeavyStuffHere();

        }
        catch (Exception ex)
        {
            _logger.LogError(ex.Message);
            result.SetError(ex.Message);
        }
        finally
        {
            semaphoreSlim.Release();
        }
        return result;
    }

【讨论】:

  • 为什么不ManualResetEventSlim
猜你喜欢
  • 2021-06-05
  • 1970-01-01
  • 2015-06-28
  • 2016-01-28
  • 1970-01-01
  • 1970-01-01
  • 2021-09-21
  • 2019-11-05
  • 2021-10-04
相关资源
最近更新 更多