顯示具有 Tomcat 標籤的文章。 顯示所有文章
顯示具有 Tomcat 標籤的文章。 顯示所有文章

2014年1月22日 星期三

Install Tomcat 7 as Linux Service

基本上在Tomcat 7裡面已經自帶了安裝成service的必要source code和shell script,但安裝到Linux上,例如:Fedora、CentOS還是需要動一點手腳。

如何compile daemon出來請參考:http://tomcat.apache.org/tomcat-7.0-doc/setup.html#Unix_daemon

只要做完第一個步驟,將compile出來的jsvc執行檔複製到$CATALINA_HOME/bin底下即可,此時在bin目錄下除了jsvc之外,還有一個重要的daemon.sh。

編輯daemon.sh增加兩行comment,如下方所示紅色部分:
daemon.sh
#!/bin/sh
#
# chkconfig: 35 90 10
# description: Start/Stop the Tomcat daemon.
#
# 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
35代表level 3和5會啟動這個service,90代表啟動順序,10代表關閉順序。

接下來切換到root身分,到/etc/init.d底下執行:
  1. ln -s $CATALINA_HOME/bin/daemon.sh tomcat
  2. chkconfig -add tomcat
這樣就已經完成service安裝,可以執行以下指令試驗看看開關Tomcat:
  1. /sbin/service tomcat start
  2. /sbin/service tomcat stop
預設service會用tomcat這個user啟動,如果沒有這個user可以先建一個,或是在$CATALINA_HOME/bin/setenv.sh裡面加入TOMCAT_USER=user name就可以了。

2013年1月7日 星期一

Tomcat Charset Encoding

使用Tomcat如遇到編碼問題,在網路上查詢通常會有三種方法如下:
  1. 加上java系統變數-Dfile.encoding=UTF-8
  2. 在取得parameter時先呼叫request.setCharacterEncoding("UTF-8")
  3. 在Tomcat server.xml中的connector加上URIEncoding="UTF-8"
事實上這三者的影響範圍不同,差異如下:
  1. -Dfile.encoding主要是影響java.io.*,若沒有設定,java預設會以OS的編碼為主。
  2. Tomcat預設會以ISO8859-1抓取parameter,request.setCharacterEncoding("UTF-8")也只決定parameter的編碼為何。這部分我慣用org.springframework.web.filter.CharacterEncodingFilter解決,設定如下:
    代碼:
    <filter>
      <filter-name>characterEncoding</filter-name>
      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
      <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
      </init-param>
      <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
      </init-param>
    </filter>
    代碼:
    <filter-mapping>
      <filter-name>characterEncoding</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
  3. URIEncoding="UTF-8"這個設定在Tomcat 5之後才開始重要,在Tomcat 4之前,query string的編碼和上項一致,但是在Tomcat 5之後被分開了,若是parameter被串成query string,則編碼還是為ISO8859-1,通常會在使用ajax時比較容易出錯,加上這個設定就會讓Tomcat一樣用指定編碼抓取query string所帶的parameter。
    範例:
    代碼:
    <Connector port="8080"
      maxHttpHeaderSize="8192" maxThreads="150"
      minSpareThreads="25" maxSpareThreads="75"
      enableLookups="false" redirectPort="8443"
      acceptCount="100" connectionTimeout="20000"
      disableUploadTimeout="true" URIEncoding="UTF-8" />

2013年1月5日 星期六

以Apache HTTP Server做為Tomcat Load Balancer

原則上有兩種方式:
  1. Apache Module mod_proxy
  2. The Apache Tomcat Connector
以前我慣用第二個,也就是jk2 connector,但不可諱言,它的設定比較複雜,而且後端AP server也只能接tomcat。
最近有機會重新看了一下mod_proxy,發現它的功能其實也能符合基本cluster的需求,除了沒辦法設定loading strategy之外,像stickysession和failover的功能是有的,在一般狀況下,或許mod_proxy就能滿足。