2013-12-08 09:55:02
来 源
itjs.cn
Apache
本文介绍解决Apache+Tomcat+mod_jk配置负载均衡后出现乱码的问题,希望对于初学Apache服务器相关的朋友有帮助,更多Apache安装、配置、报错处理等资源请本站内搜索。

Aapache2.2.4+Tomcat6.0.20/26+mod_jk-1.2.26-httpd-2.2.4.so 配置集群后乱码问题解决

http.conf 配置如下

#集群

LoadModule jk_module modules/mod_jk-1.2.26-httpd-2.2.4.so

JkWorkersFile conf/workers.properties

#分配给tomcat 的请求

JkMount /* lbcontroller

同目录下的workers.properties 文件

#分发控制器

worker.list=lbcontoller

#tomcat1 实例配置  这里要和tomcat 配置文件server.xml 的jvmRout 保持一致

worker.tomcat1.host=127.0.0.1

worker.tomcat1.port=8009

worker.tomcat1.type=ajp13

#分发权重 值越大负载越大

worker.tomcat1.lbfactor=1

#tomcat 2 实例配置

worker.tomcat2.host=127.0.0.1

worker.tomcat2.port=9009

worker.tomcat2.type=ajp13

#分发权重

worker.tomcat2.lbfactor=1

#负载均衡分发控制器

worker.list =lbcontroller

worker.lbcontroller.type=lb

worker.lbcontroller.balance_workers=tomcat1,tomcat2

worker.controller.sticky_session=false

#worker.controller.sticky_session=1

tomcat 的server.xml 配置如下 <?xml version='1.0' encoding='utf-8'?>

<!--

  Licensed to the Apache Software Foundation (ASF) under one or more

  contributor license agreements.  See the NOTICE file distributed with

  this work for additional information regarding copyright ownership.

  The ASF licenses this file to You under the Apache License, Version 2.0

  (the "License"); you may not use this file except in compliance with

  the License.  You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software

  distributed under the License is distributed on an "AS IS" BASIS,

  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

  See the License for the specific language governing permissions and

  limitations under the License.

-->

<!-- Note:  A "Server" is not itself a "Container", so you may not

define subcomponents such as "Valves" at this level.

Documentation at /docs/config/server.html

 -->

<Server port="8005" shutdown="SHUTDOWN">

  <!--APR library loader. Documentation at /docs/apr.html -->

  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />

  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->

  <Listener className="org.apache.catalina.core.JasperListener" />

  <!-- Prevent memory leaks due to use of particular java/javax APIs-->

  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />

  <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->

  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />

  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />

  <!-- Global JNDI resources

Documentation at /docs/jndi-resources-howto.html

  -->

  <GlobalNamingResources>

<!-- Editable user database that can also be used by

UserDatabaseRealm to authenticate users

-->

<Resource name="UserDatabase" auth="Container"

type="org.apache.catalina.UserDatabase"

description="User database that can be updated and saved"

factory="org.apache.catalina.users.MemoryUserDatabaseFactory"

pathname="conf/tomcat-users.xml" />

  </GlobalNamingResources>

  <!-- A "Service" is a collection of one or more "Connectors" that share

a single "Container" Note:  A "Service" is not itself a "Container",

so you may not define subcomponents such as "Valves" at this level.

Documentation at /docs/config/service.html

   -->

  <Service name="Catalina">

<!--The connectors can use a shared executor, you can define one or more named thread pools-->

<!--

<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"

maxThreads="150" minSpareThreads="4"/>

-->

<!-- A "Connector" represents an endpoint by which requests are received

and responses are returned. Documentation at :

Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)

Java AJP  Connector: /docs/config/ajp.html

APR (HTTP/AJP) Connector: /docs/apr.html

Define a non-SSL HTTP/1.1 Connector on port 8080

-->

<Connector port="8080" protocol="HTTP/1.1"

connectionTimeout="20000"

redirectPort="8443" disableUploadTimeout="true" URIEncoding="UTF-8"/>

<!-- A "Connector" using the shared thread pool-->

<!--

<Connector executor="tomcatThreadPool"

port="8080" protocol="HTTP/1.1"

connectionTimeout="20000"

redirectPort="8443" />

-->

<!-- Define a SSL HTTP/1.1 Connector on port 8443

This connector uses the JSSE configuration, when using APR, the

connector should be using the OpenSSL style configuration

described in the APR documentation -->

<!--

<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"

maxThreads="150" scheme="https" secure="true"

clientAuth="false" sslProtocol="TLS" />

-->

<!-- Define an AJP 1.3 Connector on port 8009 -->

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>

<!-- An Engine represents the entry point (within Catalina) that processes

every request.  The Engine implementation for Tomcat stand alone

analyzes the HTTP headers included with the request, and passes them

on to the appropriate Host (virtual host).

Documentation at /docs/config/engine.html -->

<!-- You should set jvmRoute to support load-balancing via AJP ie :

<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">

-->

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">

<!--For clustering, please take a look at documentation at:

/docs/cluster-howto.html  (simple how to)

/docs/config/cluster.html (reference documentation) -->

<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="6">

<!-- The request dumper valve dumps useful debugging information about

the request and response data received and sent by Tomcat.

Documentation at: /docs/config/valve.html -->

<Valve className="org.apache.catalina.valves.RequestDumperValve"/>

<Manager className="org.apache.catalina.ha.session.BackupManager"

expireSessionsOnShutdown="false"

notifyListenersOnReplication="true"

mapSendOptions="6"/>

<!--

<Manager className="org.apache.catalina.ha.session.DeltaManager"

expireSessionsOnShutdown="false"

notifyListenersOnReplication="true"/>

-->

<Channel className="org.apache.catalina.tribes.group.GroupChannel">

<Membership className="org.apache.catalina.tribes.membership.McastService"

address="228.0.0.4"

port="45562"

frequency="500"

dropTime="3000"/>

<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"

address="auto"

port="5002"

selectorTimeout="100"

maxThreads="6"/>

<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">

<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>

</Sender>

<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>

<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>

<Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>

</Channel>

<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"

filter=".*/.gif;.*/.js;.*/.jpg;.*/.png;.*/.htm;.*/.html;.*/.css;.*/.txt;"/>

<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>

</Cluster>

<!-- This Realm uses the UserDatabase configured in the global JNDI

resources under the key "UserDatabase".  Any edits

that are performed against this UserDatabase are immediately

available for use by the Realm.  -->

<Realm className="org.apache.catalina.realm.UserDatabaseRealm"

resourceName="UserDatabase"/>

<!-- Define the default virtual host

Note: XML Schema validation will not work with Xerces 2.2.

-->

<Host name="localhost"  appBase="webapps"

unpackWARs="true" autoDeploy="true"

xmlValidation="false" xmlNamespaceAware="false">

<!-- SingleSignOn valve, share authentication between web applications

Documentation at: /docs/config/valve.html -->

<!--

<Valve className="org.apache.catalina.authenticator.SingleSignOn" />

-->

<!-- Access log processes all example.

Documentation at: /docs/config/valve.html -->

<!--

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"

prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>

-->

</Host>

</Engine>

  </Service>

</Server>

配置成功后在使用post 提交中文时会产生乱

这个问题折腾了两天,终于解决了

解决过程中,使用request.setChartsetEncoding("utf-8") 、过滤器都无法正常转化中文,最后找了问题所有,主要是为因在server.xml 的中配置中有个   <Valve className="org.apache.catalina.valves.RequestDumperValve"/>  这个RequestDumperValue 这个类,

用于跟踪集群过程中的参数,在post提交参数时,RequestDumperValue 会从请求中获取参数的值并显示[大至看了一下这个类的源码是这样的],而此时参数应该是tomcat 的默认的iso8859-1 编码,显示的肯定是乱码;在网上看到有人说在request 进行重新编码之前,不能从request 中取值,如果执行了此操作,会导致后面的编码request.setChartsetEncoding("utf-8")  起使用!按照这样的理解,把<Valve className="org.apache.catalina.valves.RequestDumperValve"/> 配置从server.xml 中取掉,或移到<Manager className="org.apache.catalina.ha.session.BackupManager"  > 之前,然后测试乱码解决!

中间有一次东西自己也了解不太清楚,具体request 在重新编码之间不能重新编码是不是这回事,没有经过确认!我只是按照这种思路解决了问题,希望对遇到此问题的朋友有所帮助!呵呵!

声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。