📄 balance.xtp
字号:
<example>c:\resin1.2> bin/httpd -install-as ResinA -server ac:\resin1.2> bin/httpd -install-as ResinB -server b</example><p>On Unix, the <var/-pid/> is used to keep track of the live srun soa later <var/httpd.sh stop/> will work. It just names a file that willcontain the process id (pid) of the started process.</p><p>To make sure that your web server understands the configuration,look at <var/http://host/caucho-status/>. caucho-status willshow the current state of all the JVMs.</p><s3 title="Summary"><ul><li>The webserver plugin (mod_caucho, or isapi_srun) looks in the resin.conffor <srun> and <srun-backup> elements.<li><var//caucho-status/> shows all the servlet runners and backups.<li><var/httpd.sh -server a/> start the srun with id="a".<li>The plugin will always send a session to the same backend srun.</ul></s3></s2><s2 title='Single Web Server Load Balancing'><p>Once you start using multiple computers, you can start distributingthe load between a single web server and multiple JVMs.This is a cheap alternative to a router-based load balancer. Also, byusing Resin's load balancing, you can make sure that sessions stay onthe same machine.</p><figure src="sticky-sessions.gif"/><example><caucho.com><http-server> <srun id="1" host='host1' port='6802' srun-index='1'/> <srun id="2" host='host2' port='6802' srun-index='2'/> <srun id="3" host='host3' port='6802' srun-index='3'/> ... </http-server></caucho.com></example><p>Each host and the plugin can share the same resin.conf. Sharing theresin.conf makes maintenance easier, since the files won't get out of sync.</p><p>However, you could use a different resin.conf for plugin andfor each of the JVMs. The resin.conf for the plugin, e.g. mod_caucho,just needs enough information to list the <srun> hosts and enoughservlet-mappings to tell the plugin where to send the requests.</p><s3 title="Summary"><ul><li>The webserver plugin (mod_caucho, or isapi_srun) looks in the resin.conffor <srun> and <srun-backup> elements.<li>The webserver plugin can share the same resin.conf as the JVMs.<li><var/httpd.sh -server a/> starts the srun with id="a".<li>The plugin sends a session to the same backend srun.<li>New sessions or non-session requests balance the load with a round-robinpolicy.</ul></s3></s2><s2 title='What about sessions?'><p>A session needs to stay on the same JVM that started it.Otherwise, each JVM would only see every second or third request andget confused.</p><p>To make sure that sessions stay on the same JVM, Resin encodes thecookie with the host number. In the previous example, the hosts wouldgenerate cookies like:</p><deftable><tr><th>srun-index<th>cookie prefix<tr><td>1<td><var/a/>xxx<tr><td>2<td><var/b/>xxx<tr><td>3<td><var/c/>xxx</deftable><note>srun-index only exists in Resin 1.2.3. If unspecified, it defaultsto the index of the srun in the srun list.</note><p>On the web server, mod_caucho will decode the cookie and send itto the appropriate host. So <var/bX8ZwooOz/> would go to host2.</p><p>In the infrequent case that host2 fails, Resin will send therequest to host3. The user will lose the session but that's a minorproblem compared to showing a connection failure error. To save sessions,you'll need to use <a href="sessions.xtp">distributed sessions</a>.Also take a look at<a href="../java_tut/tcp-sessions.xtp">tcp sessions</a>.</p><p>The following example is a typical configuration for a distributedserver using an external hardware load-balancer, i.e. where each Resin isacting as the HTTP server. Each server will be startedas <var/-server a/> or <var/-server b/> to grab its specific configuration.</p><p>In this example, sessions will only be stored when the server shuts down,either for maintenance or with a new version of the server. This is the mostlightweight configuration, and doesn't affect performance significantly.If the hardware or the JVM crashes, however, the sessions will be lost.(If you want to save sessions for hardware or JVM crashes,remove the <save-on-shutdown/> flag.)</p><example title="resin.conf"><caucho.com><http-server> <http id='a' port='80'/> <srun id='a' port='6802' host='192.168.0.1' srun-index='1'/> <http id='b' port='80'/> <srun id='b' port='6802' host='192.168.0.2' srun-index='2'/> <http id='c' port='80'/> <srun id='c' port='6802' host='192.168.0.3' srun-index='3'/> <!-- enable tcp-store for all hosts/web-apps --> <session-config> <tcp-store/> <save-on-shutdown/> </session-config> ...</http-server></caucho.com></example></s2><s2 title='Multiple Web Servers'><p>Many larger sites like to use multiple web servers with a JVM and aweb server on each machine. A router will distribute the load betweenthe machines.</p><p>In this configuration, the site needs to take control of its ownsessions. Because the router will distribute the load randomly, anypersistent session state needs to be handled by a centralized serverlike a database or use Resin's TCP-ring storage.</p><p>Even in this configuration, you can use Resin's load balancing toincrease reliability. Each web server should choose its own JVMfirst, but use another machine as a backup.</p><p>In this case, you can use the trick that <var/localhost/> refersto the preferred host. The configuration would look like:</p><example><caucho.com><http-server> <srun id="a" host='localhost' port='6802' srun-index='1'/> <srun-backup id="b" host='host1' port='6802' srun-index='2'/> <srun-backup id="c" host='host2' port='6802' srun-index='3'/> <srun-backup id="d" host='host3' port='6802' srun-index='4'/> ... </http-server></caucho.com></example><p>Alternately, if you're using Apache, you could configure the srunsin the httpd.conf.</p><example title='host1 httpd.conf'>CauchoConfigFile /home/www/resin/conf/resin.confCauchoHost host1 6802CauchoBackup host2 6802</example><example title='host2 httpd.conf'>CauchoConfigFile /home/www/resin/conf/resin.confCauchoBackup host1 6802CauchoHost host2 6802</example><p><b>The order must be consistent for all servers so sessions will alwaysgo to the correct machine.</b> <var/bXXX/> must always go to host2.</p></s2><s2 title='Multiple Web Servers, Single JVM'><p>Multiple web servers can use the same JVM. For example, a fastplain webserver and an SSL web server may only need a single JVM.(Although a backup would be good.) Since the JVM doesn't care wherethe request comes from, it can treat each request identically.</p><p>This simplifies SSL development. A servlet just needs to checkthe <var/request.isSecure()/> method to see if the request is SSL ornot. Other than that, all requests are handled identically.</p></s2><s2 title='When everything fails'><p>By default, if <var/mod_caucho/> can't connect to any of the JVMs,it will return a basic "can't connect" page to the user. Sites whichwant a more professional response can redirect the user to an errorpage.</p><p>In the resin.conf, you'll use:</p><example><caucho.com><http-server> <error-page exception-type='connection' location='/error.html'/></http-server></caucho.com></example><p>The error page must be an absolute path because it could be calledfrom any url. Of course, it can't refer to a servlet or to a JSP file.</p></s2></s1>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -