“Microservices architecture with Django” 可以翻译为 “使用Django的微服务架构”。

huangapple go评论64阅读模式
英文:

Microservices architecture with Django

问题

我有关使用Django创建微服务的一些问题。

假设我们有一个在线商店或一个拥有许多数据库请求和用户的较大系统。我想要练习并模拟一个简单的微服务,以学习一些新知识。

我们希望创建一个基于微服务的系统,其中包括以下组件:

  1. 一个基于Django的微服务,带有其管理面板和完整功能(不包括DRF)。
  2. 一个或多个带有React/Angular前端的微服务。
  3. 几个额外的微服务来分离功能。

我对架构感到不确定。假设我们希望使用Django管理面板来管理数据。

最简单的解决方案是将DRF添加到第一个微服务中,并扩展其功能(REST应用程序),而不是创建不同的服务(3.)。

  1. 但是,如果我们想将功能分离到不同的微服务中该怎么办呢?
  2. 点3中的微服务是否应连接到同一个数据库,并被视为不同的Django项目(带有DRF)?
  3. 我们可以在第三个微服务中使用GoLang、FastAPI或Java Spring吗?如果可以,所有模型是否应在第一个微服务中复制并注册?
  4. 或者,有更好的方法来处理这个问题吗?

听到您关于如何处理这个问题的观点和方法将会很好。

祝您有一个美好的一天!

英文:

I have some questions about creating microservices with Django.

Let's say we have an online shop or a larger system with many database requests and users. I want to practice and simulate a simple microservice to learn something new.

We want to create a microservices-based system with the following components:

  1. A Django-based microservice with its admin panel and full functionality (excluding DRF).
  2. One or more microservices with a React/Angular frontend.
  3. Several additional microservices to separate functionalities.
    I'm unsure about the architecture. Let's assume we want to manage data using the Django admin panel.

The simplest solution would be to add DRF to the first microservice and extend its functionality (REST app) - instead of creating different services (3.).

  1. But what if we want to separate functionality into different microservices?
  2. Should the microservices in point 3 be connected to the same database and treated as different Django projects (with DRF)?
  3. Can we use GoLang, FastAPI, or Java Spring for the third microservice? If yes, should all models be duplicated and registered in the first microservice?
  4. Alternatively, is there a better way to approach this?

It would be great to hear your perspective and methods on how to proceed with this.

Have a wonderful day!

答案1

得分: 6

Sure, here are the translated parts:

Microservices:

[ PROS ]

  • 可伸缩性 (它们可以独立扩展)
  • 灵活性 (每个微服务可以使用自己的堆栈和硬件设置)
  • 隔离性 (一个微服务的失败不会影响另一个,只有它的服务失败。)

[ CONS ]

  • 复杂性 (在每个层面设置和维护如此多的基础设施)
  • 数据一致性 (每个数据库都是独立的,因此确保一致性增加了复杂性)
  • 分布式系统挑战 (延迟/容错和测试更加困难)

Now for your questions:

  1. separating functionality into different microservices.
    这就是 Django 项目中应用程序的用途,也是软件工程的核心原则,关注点分离仍然可以应用于单片应用程序。当讨论微服务时,问题应该是为了在复杂性成本下带来什么好处,比如拥有一个执行纯 GPU 计算的服务,可能会从成为一个在优化语言和系统上运行且具有 GPU 访问权限的微服务中受益。我甚至会认为,只有在探索了所有其他解决方案并与团队共同组成了无可辩驳的理由之后,才应该过渡到使用微服务。

  2. Should microservices be connected to the same DB.
    微服务应该有自己的数据库,请参考隔离性。否则,它与仅使用具有额外复杂性但没有好处的单体应用程序相同。

  3. Can you use a different stack, and should duplicated models be registered.
    这再次涉及对微服务是什么的误解。您的微服务应该封装它需要独立运行所需的最小数据量。

  4. Alternative: Design your Monolithic application very well, separate out your concerns and business logic in a de-coupled design, even if it's a monolith.
    备选方案:非常好地设计您的单体应用程序,在脱耦合的设计中分离出您的关注点和业务逻辑,即使它是一个单体应用程序。您必须具备这样的思维方式:“如果我想将这个功能替换为微服务,那么它会有多容易移除,耦合度如何,等等...”良好的设计可以实现可伸缩性和可维护性,这是最重要的。它还允许其他人在项目的子集上贡献他们的专业知识,而无需了解整个项目。

英文:

First a quick summary of Microservices vs Monolithic apps pros and cons (this is important).

Microservices:

[ PROS ]

  • scalability (they scale independently)
  • flexibility (each microservice can use its own stack & hardware setup)
  • isolation (the failure of one microservice does not affect another, only its service fails.)

[ CONS ]

  • Complexity (so much infrastructure to setup and maintain at every layer)
  • Data consistency (each db is independent so makink sure consistency is maintained is added complexity)
  • Distributed system challenges ( latency/fault tolerance and testing is much harder)

Now for your questions:

  1. separating functionality into different microservices.
    That is what apps in a Django project are for, and is a core principle of software engineering, separation of concerns can still be applied in a monolithic application.
    When discussing microservices, the questions should be about what benefit would it bring at the cost of complexity, such having a service that does pure gpu computation, perhaps would benefit from being a microservice running in on an optimized language and system with access to GPUs. I would even argue you should only transition to using microservices, when you have explored all other solutions, and have composed an irrefutable argument to do so with the team.

  2. Should microservices be connected to the same DB.
    Microservices should have their own db, see Isolation. otherwise it's the same as just using a monolithic app with extra complexity and no benefit.

  3. Can you use a different stack, and should duplicated models be registered. This again comes under a missunderstanding of what a microservice is. Your microservice should encapsulate the minimum amount of data it needs to function independently.

  4. Alternative: Design your Monolithic application very well, separate out your concerns and business logic in a de-coupled design, even if it's a monolith. you have to have the mindset of: "if I want to swap this functionality for a microservice, how easily will it be to rip it out, what is the coupling, etc...) A good design leads to scalability and maintainability which is most important. It also allows other people to contribute their expertise on a subset of the project, without needing to know the whole.

huangapple
  • 本文由 发表于 2023年5月21日 03:13:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定