Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Марк Шевченко "Микросервисы на С#"

Марк Шевченко "Микросервисы на С#"

Введение в тему микросервисов и конкретно их разработку на С#. Немного о подводных камнях и том, как с ними справляться.

DotNetRu

May 29, 2020
Tweet

More Decks by DotNetRu

Other Decks in Programming

Transcript

  1. Remote Procedure Call 1967 Simula SmallTalk 1972 1975 RFC 684

    RPC term 1981 1983 C++ Turbo C++ 1991 1991 CORBA DCOM 1996 1996 HTTP 2000 SOAP AJAX 2005 2006 REST JSON 2013 2015 gRPC 13
  2. Remote Procedure Call 1967 Simula SmallTalk 1972 1975 RFC 684

    RPC term 1981 1983 C++ Turbo C++ 1991 1991 CORBA DCOM 1996 1996 HTTP 2000 SOAP AJAX 2005 2006 REST JSON 2013 2015 gRPC 14
  3. Виртуализация Hardware Emulator Host OS Host OS Guest OS Bin/Lib

    App Guest OS Hypervisor Bin/Lib App Container Engine Bin/Lib App 17 Host OS
  4. 18

  5. Ссылки про микросервисы - https://auth0.com/blog/an-introduction-to-microservices-p art-1/ https://auth0.com/blog/an-introduction-to-microservices-p art-2-API-gateway/ https://auth0.com/blog/an-introduction-to-microservices-p art-3-the-service-registry/

    https://auth0.com/blog/introduction-to-microservices-part -4-dependencies/ - https://12factor.net/ru/ https://microservices.io/patterns/apigateway.html https://martinfowler.com/articles/microservices.html 25
  6. Web в .NET 2003 WebForms WCF 2006 2009 MVC Web

    API 2009 2013 OWIN Katana 2013 .NET Core 1 2016 2016 Kestrel 2017 .NET Core 2 27
  7. Пример [Route("api/v1/todo-items")] public class TodoItemsController : ControllerBase { private readonly

    TodoDbContext dbContext; [HttpGet(“{id}”)] public async Task<TodoItem> GetByIdAsync(int id) { return await dbContext.TodoItems .SingleAsync(x => x.Id == id); } } 29
  8. Пример [Route("api/v1/todo-items")] public class TodoItemsController : ControllerBase { private readonly

    TodoDbContext dbContext; [HttpGet(“{id}”)] public async Task<TodoItem> GetByIdAsync(int id) { return await dbContext.TodoItems .SingleAsync(x => x.Id == id); } } 30
  9. Пример [Route("api/v1/todo-items")] public class TodoItemsController : ControllerBase { private readonly

    TodoDbContext dbContext; [HttpGet(“{id}”)] public async Task<TodoItem> GetByIdAsync(int id) { return await dbContext.TodoItems .SingleAsync(x => x.Id == id); } } GET /api/v1/todo-items/123 31
  10. Пример [Route("api/v1/todo-items")] public class TodoItemsController : ControllerBase { private readonly

    TodoDbContext dbContext; [HttpGet(“{id}”)] public async Task<TodoItem> GetByIdAsync(int id) { return await dbContext.TodoItems .SingleAsync(x => x.Id == id); } } GET /api/v1/todo-items/123 32
  11. Пример [Route("api/v1/todo-items")] public class TodoItemsController : ControllerBase { private readonly

    TodoDbContext dbContext; [HttpGet(“{id}”)] public async Task<TodoItem> GetByIdAsync(int id) { return await dbContext.TodoItems .SingleAsync(x => x.Id == id); } } 33
  12. Пример [Route("api/v1/todo-items")] public class TodoItemsController : ControllerBase { private readonly

    TodoDbContext dbContext; [HttpGet(“{id}”)] public async Task<TodoItem> GetByIdAsync(int id) { return await dbContext.TodoItems .SingleAsync(x => x.Id == id); } } 34
  13. 37

  14. 38

  15. 39

  16. 40

  17. 41

  18. Contract NuGet interface IDiscountService { DiscountDto[] Calculate(OrderDto orderDto); } class

    DiscountDto { public int ProductId { get; set; } public decimal Amount { get; set; } public string Description { get; set; } } 54 class OrderDto { . . . }
  19. Customer [Route(“api/v1/discounts”)] class DiscountController : ControllerBase, IDiscountService { [HttpPost] public

    DiscountDto[] Calculate(OrderDto orderDto) { var discounts = from item in orderDto.Items select new DiscountDto { ProductId = item.ProductId, Amount = item.Sum * 0.03m, Description = “Merry Christmas” }; return discounts.ToArray(); } } 55
  20. Supplier class DiscountProxy : IDiscountService { private readonly IHttpClientFactory clientFactory;

    public DiscountDto[] Calculate(OrderDto orderDto) { return clientFactory.Create() .Put<DiscountDto[]>(“api/v1/discount”, orderDto); } } 56
  21. paths: /: get: operationId: listVersionsv2 summary: List API versions responses:

    '200': description: |- 200 response content: application/json: examples: foo: value: { "versions": [ { "status": "CURRENT", "updated": "2011-01-21T11:33:21Z", "id": "v2.0", "links": [ 57
  22. Решения - Supplier/Customer или Shared Core - Интерфейсы служб -

    Косвенный вызовы - DTO, а не POCO - NuGet - Message Queue - Open-Closed Principle 59
  23. Содержание - не монолит - UNIX way - масштабирование -

    SOAP, REST, gRPC - CI/CD - контейнеры, оркестрация - шлюзы - manage its own data - web в .NET - MVC, маршрутизация - асинхронность - проблема: транзакции - проблема: сеть - проблема: версии - решения для транзакций - решения для сцепленности - деталь реализации, Open API - feature envy, MQ Марк Шевченко http://markshevchenko.pro @markshevchenko 61