Connector Common Attributes 属性源码分析之二

5.parseBodyMethod

parseBodyMethods

A comma-separated list of HTTP methods for which request bodies will be parsed for request parameters identically to POST. This is useful in RESTful applications that want to support POST-style semantics for PUT requests. Note that any setting other than POST causes Tomcat to behave in a way that goes against the intent of the servlet specification. The HTTP method TRACE is specifically forbidden here in accordance with the HTTP specification. The default is POST

默认的Tomcat只能接受http的post请求,这对于非restful来说,其实已经足够了;

但是在目前restful为主的框架,光光post是不够的,我们需要put,delete这种操作,因此Tomcat必须放出接口来对http请求进行改变;

parseBodyMethods就是一个以逗号分隔的字符串,在初始化的时候,Connector会将其转为允许的methodSet集合(parseBodyMethodsSet):


该集合在Request进行解析的时候进行判断,如上述代码,当restful这种put,delete请求来了之后,但你没有配置parseBodyMethods的话,restful请求就不会响应,直接退出;


6.port

port

The TCP port number on which this Connector will create a server socket and await incoming connections. 

Your operating system will allow only one server application to listen to a particular port number on a particular IP address. 

If the special value of 0 (zero) is used, then Tomcat will select a free port at random to use for this connector. 

This is typically only useful in embedded and testing applications.

监听端口,也就是Tomcat前端的连接器接受serversocket绑定的port;

这个port属性从配置文件进行读取,然后设置到Connector的port属性中,并通过反射到不同的protocolHandler中去:


因为我们不知道使用哪一个protocol,这个protocol是需要在Tomcat的Connector属性中去配置的,所以这里需要用反射;

随后,这个port随着这个protocolhandler的setPort方法,最终设置到各个通道的Endpoint的port属性上,最终传递给serversocket进行绑定:


7.protocol协议

protocol

Sets the protocol to handle incoming traffic. 

The default value is HTTP/1.1 which uses an auto-switching mechanism to select either a non blocking Java NIO based connector or an APR/native based connector. 

If the PATH (Windows) or LD_LIBRARY_PATH (on most unix systems) environment variables contain the Tomcat native library, the APR/native connector will be used. 

If the native library cannot be found, the non blocking Java based connector will be used. 

Note that the APR/native connector has different settings for HTTPS than the Java connectors.
To use an explicit protocol rather than rely on the auto-switching mechanism described above, the following values may be used:
org.apache.coyote.http11.Http11Protocol - blocking Java connector
org.apache.coyote.http11.Http11NioProtocol - non blocking Java NIO connector
org.apache.coyote.http11.Http11Nio2Protocol - non blocking Java NIO2 connector
org.apache.coyote.http11.Http11AprProtocol - the APR/native connector.
Custom implementations may also be used.

该属性就是抽象的通道;

目前Tomcat8+之后,一共支持4个通道,如Http11Protocol就代指BIO通道:


我们从整体前端架构图中就可以看到,Http11Protocol持有2个对象;

xxEndpoint:可以理解为socket连接工厂,启动,绑定端口,监听的存根对象,有多个线程池在里面进行工作

xxConnectionHandler:可以理解为工作任务处理handler,一个工作线程对应一个processor;


8.useIPVhosts

useIPVHosts

Set this attribute to true to cause Tomcat to use the IP address that the request was received on to determine the Host to send the request to.

 The default value is false.

默认的host是基于name的,也就是说一个tomcat实例可以有1个n个虚拟主机,通过name进行区分;

如:

http://yuantong:8080/xxxxx 

http://yuantong1:8080/xxxxx 

http://yuantong2:8080/xxxxx 

yuantong, yuantong1, yuantong2就是,虽然host的名字不同,但是这些域名都可以配置在同一台机器的同一个tomcat上;

但有时候,用户需要一种IP和host的映射,例如:

http://10.12.33.21:8080/xxxxx 

http://10.12.33.22:8080/xxxxx 

http://10.12.33.23:8080/xxxxx 

这种在同一个机器上是怎么实现的呢?

方法一:很多机器的网卡可以有多个,就是在一台物理机上配置多个网卡,每一个IP对应一块网卡,这样自然而然一台服务器有多个IP,

方法二:利用虚拟技术,可以在一台机器中虚拟多个网卡出来,很多这样的软件,这样也可以做到每一个机器有n个IP

总之,无论用哪一种方法,还需要Tomcat以这种方式进行支持,让host的name是IP;

我们看源码就很明白了:


serverName根据这个配置,得到IP或者是字符串名字,用这个serverName进行Mapper.map,得到不同host下的wrapper进行执行;


9.xpoweredBy

xpoweredBy

Set this attribute to true to cause Tomcat to advertise support for the Servlet specification using the header recommended in the specification. 

The default value is false.

不同的Tomcat版本对应不同的后端Servlet引擎和容器,也对应支持不同的Servlet,jsp的规范。


该属性就是是否向前端写入当前容器支持的Servlet,jsp的规范版本,jvm的版本信息,供客户端浏览器;

本站公众号
   欢迎关注本站公众号,获取更多程序园信息
开发小院