简体   繁体   English

在不同服务器上托管表示层和应用程序层

[英]Hosting presentation layer and application layer on different servers

My presentation layer encompasses an MVC pattern and the application layer includes services and DTO's, all of which uses the ASP.NET framework. 我的表示层包含MVC模式,应用程序层包含服务和DTO,所有服务和DTO都使用ASP.NET框架。

The idea behind splitting the code up like this was to make it possible to host the presentation layer on one load balanced cluster of servers and the application layer on another cluster of load balanced servers. 像这样拆分代码的想法是,可以将表示层托管在一个负载均衡的服务器群集上,而将应用程序层托管在另一个负载均衡的服务器群集上。

We don't want to have to serialize the data between servers, and would prefer to be able to activate the classes and invoke the methods in the application layer as if they were part of the application which makes up the presentation layer. 我们不想在服务器之间序列化数据,而是希望能够激活类并在应用程序层中调用方法,就像它们是构成表示层的应用程序的一部分一样。

I can't find any documentation on how to implement such a setup nor can I find out what the name of such a setup would be so I am struggling to get started. 我找不到任何有关如何实现这样的设置的文档,也找不到这样的设置的名称,因此我很难入门。 Is what I'm trying to achieve possible? 我要实现的目标有可能吗?

Your first challenge will be to connect your presentation and service layers. 您的第一个挑战将是连接演示文稿层和服务层。 It should be pretty painless to expose your service layers using Web API or SOAP. 使用Web API或SOAP公开服务层应该很轻松。 You could even use Windows RPC or DCOM, though I suspect this might be invasive and force changes to your service layer code. 您甚至可以使用Windows RPC或DCOM,尽管我怀疑这可能是侵入性的,并且会强制更改服务层代码。

When you do this you'll be introducing an extra network hop and marshalling/unmarshalling of your data (as objects are converted to string representation and then converted back to objects after transmission on the network), Depending on your app this may not make any difference at all, but it's something to keep in mind. 执行此操作时,您将引入额外的网络跃点和数据编组/解组(因为将对象转换为字符串表示形式,然后在网络上传输后又转换回对象),根据您的应用,这可能不会完全不同,但这是要牢记的。

You also need to solve the problem of how to scale. 您还需要解决如何缩放的问题。 The most usual approach here would be to introduce two load balancers, one for the front and and another for the back ends. 这里最常用的方法是引入两个负载平衡器,一个用于前端,另一个用于后端。 Scaling and adding another layer of complexity. 扩展并增加另一层复杂性。

A more modern (simpler and more performant) approach might be to organize your app as a set of micro-services. 一种更现代(更简单,更高效)的方法可能是将您的应用组织为一组微服务。 You'd deploy a silo of your apps functionality (say user management or user preferences) and package and deploy your user service, DTO, and your presentation controllers together. 您将部署应用程序功能的孤岛(例如用户管理或用户首选项),并将用户服务,DTO和演示控制器一起打包和部署。 You can then deploy scale each slice of your application individually (and get native performance within each slice). 然后,您可以分别部署应用程序的每个切片(并在每个切片中获得本机性能)。

This approach really starts to shine as you move your controller logic into the client (the web browser). 当您将控制器逻辑移至客户端(Web浏览器)时,这种方法真正开始崭露头角。 A single client talking to a multifarious backend via REST. 单个客户端通过REST与众多后端通信。

If you don't, if you template on the server in classic mvc.net fashion, then the microservices approach becomes less elegant. 否则,如果您以经典的mvc.net方式在服务器上模板化,那么微服务方法将变得不那么优雅。 At the very least, you have to solve the problem of distributing common template assets (css, js, images) across multiple deployables. 至少,您必须解决在多个可部署对象之间分配通用模板资产(css,js,图像)的问题。 It's not a huge problem, but you'd probably need to make this part of your build process. 这不是一个大问题,但是您可能需要将其纳入构建过程中。 Also you'll have to consider what happens when you change these assets. 另外,您还必须考虑更改这些资产时会发生什么。 Will you need to redeploy all of your full stack microservices? 您是否需要重新部署所有全栈微服务?

You could use WCF... 您可以使用WCF ...

The idea behind WCF is to allow you to implement the effect of server-side classes exposed over a network connection. WCF的思想是允许您实现通过网络连接公开的服务器端类的效果。

You define your interfaces with their properties and methods, build out their server-side implementation, and on the client side of the implementation, have Visual Studio auto-generate an API for you. 您可以使用其属性和方法定义接口,构建其服务器端实现,并在实现的客户端使Visual Studio为您自动生成API。

You can use WCF over TCP/IP or HTTP connections (not sure if TCP/IP lends itself to load balancing/scaling). 您可以通过TCP / IP或HTTP连接使用WCF(不确定TCP / IP是否适合负载平衡/扩展)。 TCP/IP is obviously intended for connectivity between machines on the same LAN and I believe carries less overhead than HTTP. TCP / IP显然旨在用于同一LAN上的计算机之间的连接,我认为其开销要小于HTTP。 WCF also works over named pipes, but that is for process to process communication on the same machine - not relevant here. WCF还可以在命名管道上工作,但这是供进程在同一台计算机上处​​理通信的方法-在此不相关。 In any case, there is still a serialization process no matter which protocol you use; 无论如何,无论您使用哪种协议,都仍然需要一个序列化过程。 for HTTP it will be SOAP (somewhat heavy); 对于HTTP,它将是SOAP(有点沉重); not sure about TCP/IP. 不确定TCP / IP。

I have successfully implemented solutions using WCF over TCP/IP, HTTP, and named pipes, but scaling was never a consideration for any of them. 我已经使用WCF通过TCP / IP,HTTP和命名管道成功实现了解决方案,但是扩展从来都不是其中的考虑因素。

For most purposes, I am moving away from WCF now in favour of RESTful .NET WEB API 2 services, using JSON as the serialization format. 对于大多数目的,我现在不再使用WCF,而是使用JSON作为序列化格式,而转而使用RESTful .NET WEB API 2服务。 Consuming them may be a little more work than with WCF, but it's much easier to set up on the server side. 与使用WCF相比,使用它们可能需要更多的工作,但是在服务器端进行设置要容易得多。

Addendum: 附录:

I should add a benefit for the RESTful WEB API approach: flexibility. 我应该为RESTful WEB API方法增加一个好处:灵活性。 Today you don't want the client browser to process javascript, but if that should change, you won't have to make any changes to the application server. 今天,您不希望客户端浏览器处理javascript,但是如果应该更改javascript,则无需对应用程序服务器进行任何更改。 The change would be limited to your presentation server offloading processing from itself to the browser. 更改将仅限于演示服务器将处理过程从其自身转移到浏览器。 If you pick an RPC approach (like WCF), you have a bigger commitment to that architecture and lose the flexibility to easily move away from it. 如果选择RPC方法(如WCF),则您对该架构的承诺更大,并且失去了轻松脱离该架构的灵活性。

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

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