简体   繁体   English

在两个物理服务器中托管ASP.NET MVC项目

[英]Host ASP.NET MVC project in two physical servers

I have a ASP.NET MVC project that needed to be hosted on two different servers. 我有一个ASP.NET MVC项目需要托管在两个不同的服务器上。 I know this seems weird but this's the requirement from my client. 我知道这看起来很奇怪,但这是我客户的要求。

In details, I will have 2 load balanced servers 详细说来,我将有2个负载平衡服务器

  • Web Server - expose to public (domain will point to public ip of this server) Web服务器 - 向公众公开(域将指向此服务器的公共IP)
  • App Server - communicate with internal infrastructures (Active Directory, Database, Security, etc) App Server - 与内部基础架构(Active Directory,数据库,安全性等)通信

简单的图表

I'm thinking of creating another layer (ASP.NET Web API) , so that the webserver only serve HTML pages, the app server will contains the business logics and expose endpoints for all clients (web, mobile) to call. 我正在考虑创建另一个层(ASP.NET Web API) ,以便Web服务器仅提供HTML页面,应用服务器将包含业务逻辑并公开要调用的所有客户端(Web,移动)的端点。 Web Server will communicate with App Server via RESTFUL services. Web Server将通过RESTFUL服务与App Server通信。

Is there any better way to do? 还有更好的方法吗? Any solution will be greatly appreciated. 任何解决方案将不胜感激。

Thanks in advance, 提前致谢,

This is a fairly normal way of doing things - let the web servers concentrate on serving pages and let the back end servers do the hard work with the application business logic. 这是一种相当正常的做事方式 - 让Web服务器专注于提供服务页面,让后端服务器使用应用程序业务逻辑进行艰苦的工作。 If it's heavily used with a large data throughput, I would consider making it three tiers with separate web, application and database servers. 如果它大量使用大数据吞吐量,我会考虑使用单独的Web,应用程序和数据库服务器进行三层。

Web API is a pretty good choice for communication between the two servers too, but it might be worth considering WCF as an alternative if you find you need to go beyond the basic REST operations. Web API也是两个服务器之间通信的不错选择,但如果您发现需要超越基本的REST操作,则可能值得考虑将WCF作为替代方案。 It's a bigger overhead to get it running though, and it's definitely not for the faint-hearted! 让它运行起来是一个更大的开销,而且绝对不适合胆小的人!

EDIT 编辑

So what you'll need to do is move all your current business logic out of your existing controllers and into a corresponding set of Web API controllers which will sit on the second server. 因此,您需要做的是将所有当前业务逻辑从现有控制器中移出,并放入将位于第二台服务器上的相应Web API控制器集中。 If you're careful, you should just be able to copy your MVC controller methods straight into your Web API controllers and add the appropriate routing attributes. 如果您小心,您应该能够将MVC控制器方法直接复制到Web API控制器中并添加适当的路由属性。 Your database (if you have one) will need to sit on the second server too. 您的数据库(如果有的话)也需要坐在第二台服务器上。

Once you've done that, all your MVC controllers will do is make calls to the Web API running on the second server. 完成后,所有MVC控制器都会调用第二台服务器上运行的Web API。 The MVC controllers shouldn't do any kind of processing on your data beyond basic tweaks to make it look nice (keeping your controllers clean is good practice anyway). 除了基本的调整之外,MVC控制器不应对您的数据进行任何处理,以使其看起来很好(保持控制器清洁无论如何都是好的做法)。

That should give you a basic idea of what you need to do. 这应该让您基本了解您需要做什么。 If you need anything more specific about any of the steps, just shout and I'll see if I can elaborate. 如果您需要更具体的任何步骤,请大声说,我会看看我是否可以详细说明。

We are using a similar structure in our project. 我们在项目中使用了类似的结构。 A service layer is exposing REST APIs which are being consumed by multiple websites and mobile apps. 服务层正在公开REST API,这些REST API由多个网站和移动应用程序使用。 The beauty of this architecture is that all the business complexities are hidden behind the APIs whereas the front end mostly deals with presentation needs. 这种架构的优点在于所有业务复杂性都隐藏在API背后,而前端主要处理演示需求。

But you need to be careful about two things while developing this architecture: 但是在开发这种架构时需要注意两件事:

1. Protecting the end points (REST APIs) - If you are planning to develop mobile apps that will consume the APIs then you have to expose the endpoints through the firewall and make accessible to the internet. 1.保护端点(REST API) - 如果您计划开发将使用API​​的移动应用程序,则必须通过防火墙公开端点并使Internet可访问。 One of option is to use a bearer token validation to authenticate request. 其中一个选择是使用承载令牌验证来验证请求。 You can use Oauth protocol to secure the endpoints. 您可以使用Oauth协议来保护端点。

2. Challenge on Serializing and Deserializing: Since REST uses JSON as a standard format of data transfer and Json is not strongly typed, the challenge is to map data to appropriate models at both ends. 2.序列化和反序列化的挑战:由于REST使用JSON作为数据传输的标准格式,而Json没有强类型,因此面临的挑战是将数据映射到两端的适当模型。 To solve this we created a common project for models and added to both the (api and web) projects. 为了解决这个问题,我们为模型创建了一个通用项目,并添加到(api和web)项目中。 When at API end we serilized a model we deserilized it to the same model at the web project. 在API结束时,我们为一个模型提供了服务,我们将它在Web项目中将其保存到同一模型中。 They mapped perfectly without hiccups. 他们完美地绘制而没有打嗝。

Hope the tips above will help you. 希望上面的提示对您有所帮助。

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

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