开发网络服务器有哪些不同的方法?
开发网络服务器有哪些不同的方法? 所以我猜有(1)多线程(2)事件循环,还有其他吗?每种方法的优点/缺点是什么?你什么时候会使用每一个?你能列出每种方法的具体实现吗
What are the different approaches in developing a web-server?
So I guess there are (1) multi-thread (2) event-loop, is there anything else? What would be the pros/cons of each approach? when would you use each? can you list specific impl' for each approache
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不同的方法可以是:
单线程:所有连接均由单个线程处理,该线程
“监听”并等待连接并处理请求。
实现起来很简单,但它是最无用的服务器
一次仅服务请求
多线程:服务器监听请求和每个传入的请求
请求被分配给一个新的线程来处理它。所以每个客户端
连接由其专用线程处理。这种方法(不同于
1)支持并发处理客户端请求但不支持
可以很好地扩展,因为每个新请求都会在服务器上创建一个新线程
这需要大量资源。最终服务器会遇到
limit
多线程池:与(2)相同的想法,但不是创建一个新的
处理每个传入请求的线程来自线程池的线程
使用。即创建线程并将其放置在池中以供稍后使用
重用。这可以很好地支持多个客户端请求,并且
这是标准方法。例如,Tomcat 的工作方式如下。
Event-Queue:每个传入的请求都被放入一个队列中并被
由接受队列请求的后台线程处理。这是
非阻塞并且这种类型的异步处理也可以扩展
老实说,我不确定它是否比(3)更好
性能。我认为可以使用 tomcat 为此配置
NIO架构
Different approach can be:
Single threaded: All connections are handled by a single thread that
"listens" for and awaits for connections and processes requests.It
is simple to implement but it is the most useless server as it can
only serve request at a time
Multithreaded:The server listens for requests and each incoming
request is allocated to a new thread to handle it.So each client
connection is handled by its dedicated thread. This approach(unlike
1) supports concurrent processing of client requests but does not
scale well since each new request creates a new thread at the server
and this takes a lot of resources.Eventually the server will hit a
limit
Multithreaded-Pools:Same idea as (2) but instead of creating a new
thread to handle each incoming request a thread from a thread-pool
is used.I.e. threads are created and placed on a pool for later
reuse.This scales very well supporting multiple client requests and
it is the standard approach.E.g. Tomcat works like this.
Event-Queue:Each incoming request is placed into a queue and is
processed by a background thread taking requests of the queue. It is
non-blocking and this type of asynchronous processing also scales
well.To be honest I am not sure if it is better than (3) in
performance.I think that tomcat can be configured for this using the
NIO architecture
您应该添加非阻塞 I/O。看看 Netty。
You should add non-blocking I/O. Have a look at Netty.
一些服务器(例如 G-WAN)混合了多线程池和事件队列,让服务器的 CPU 核心饱和,每个线程处理许多连接。
免责声明:我参与了这个项目的开发。
Some servers like G-WAN mix Multithreaded-Pools and Event-Queues, letting the server saturate CPU Cores with each thread processing many connections.
Disclamer: I am involved in the development of this project.