简体   繁体   English

如何将 django 网站分解为微服务

[英]How to break a django website into microservices

So, I am new to this "monolith vs microservices" architecture debate and I have pretty much understood most of it.所以,我是这个“单体与微服务”架构辩论的新手,我已经非常了解其中的大部分内容。 From my limited understanding I get it that in microservice architecture each feature(lets say) is a separate app.根据我有限的理解,我知道在微服务架构中,每个功能(比如说)都是一个单独的应用程序。 Now I need some clarification with respect to django for implementing microservices.现在我需要对 django 进行一些说明,以实现微服务。 Heres my question这是我的问题

Should I make every microservice aka the app a different django project altogether OR should I make every app aka the microservice inside one django project and keep them isolated (as in loosely coupled)?我应该将每个微服务也称为应用程序完全不同的 django 项目还是应该将每个应用程序又称为微服务在一个 django 项目中并保持它们隔离(如松散耦合)?

Microservice architecture simply states that your each service should be independent of each other.微服务架构只是声明您的每个服务应该相互独立。

Its also not necessary to create one micro-service in java and one in python since they are not related.也没有必要在 java 中创建一个微服务,在 python 中创建一个,因为它们不相关。

So yes, ideally your each micro-service is a separate django project.所以是的,理想情况下,您的每个微服务都是一个单独的 Django 项目。 The best way to break this, first list down all the possible modules in your site or app.打破这一点的最佳方法是,首先列出您网站或应用程序中所有可能的模块。

Then go through : https://microservices.io/patterns/decomposition/decompose-by-business-capability.html https://microservices.io/patterns/decomposition/decompose-by-subdomain.html然后通过: https : //microservices.io/patterns/decomposition/decompose-by-business-capability.html https://microservices.io/patterns/decomposition/decompose-by-subdomain.html

These are two recommended pattern of how you should divide modules / domain into micro-services.这是您应该如何将模块/域划分为微服务的两种推荐模式。

The microservice should be completely independent so it should not belong to one Django project.微服务应该是完全独立的,所以它不应该属于一个 Django 项目。 You should be able to deploy each service independently with its own database, so even you split the project into separate apps they still share one physical database and you can not deploy them separately.您应该能够使用自己的数据库独立部署每个服务,因此即使您将项目拆分为单独的应用程序,它们仍然共享一个物理数据库,您无法单独部署它们。 Therefore, you could potentially create separate Django project for each 'microservice' but this does not make much sense.因此,您可以为每个“微服务”创建单独的 Django 项目,但这没有多大意义。 You put lot of overhead for creating a microservice and also using Django framework is not a best choice for MSA , have a look at Flask .您为创建微服务投入了大量开销,并且使用 Django 框架也不是 MSA 的最佳选择,请查看Flask

好吧,你可以在 django 中使用数据库路由器......这就是诀窍......每个应用程序一个 wSGI 文件与你的 NGINX 服务器通信

The main purpose of micro service is to serve the specific business, for example you are running a cloud kitchen then Django project can have many sub projects under one master project or you can run each project as separate service, it up to you.微服务的主要目的是为特定业务服务,例如您正在运行一个cloud kitchen ,那么 Django 项目可以在一个主项目下有许多sub projects ,或者您可以将每个项目作为单独的服务运行,这取决于您。

Service服务

  1. backoffice后台
  2. finance金融
  3. rider骑士
  4. kitchen厨房

now we will set the url for each service like现在我们将为每个服务设置 url

Endpoints端点

  1. backoffice.mycompany.com backoffice.mycompany.com
  2. finance.mycompany.com财务.mycompany.com
  3. rider.mycompany.com骑手.mycompany.com
  4. kitchen.mycompany.com kitchen.mycompany.com

Once our app is up the service load will not effect other service.一旦我们的应用程序启动,服务负载将不会影响其他服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM