Tuesday, January 10, 2012

Memcached Java Client Simple Usage

1. Download library from here.
2. Import java_memcached-release_2.6.3.jar into your project.
3. Simple code for testing:

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
public class MemCacheClient {
static{
String[] serverList = {"localhost:11211"};
Integer[] weight = {3};
SockIOPool pool = SockIOPool.getInstance();
pool.setServers(serverList); 
pool.setWeights(weight);
pool.setInitConn(5);
pool.setMinConn(5);
pool.setMaxConn(250);
pool.setMaxIdle(1000*60*60*6);
pool.initialize();
}
public static void main (String args[]){
MemCachedClient mcc = new MemCachedClient();
mcc.set("testA", "AA");
mcc.set("testB", "BB");
System.out.println(mcc.get("testA"));
System.out.println(mcc.get("testB"));
mcc.delete("testA");
System.out.println(mcc.get("testA"));
}
}


Sunday, January 8, 2012

Memcached Server Installation

For Windows
1. download memcached for windows (memcached-1.2.6-win32-bin.zip) from here
2. unzip memcached-1.2.6-win32-bin.zip in any folder (e.g., D:\memcached-1.2.6-win32\).
3. install: D:\memcached-1.2.6-win32\memcached.exe -d install.
4. start: memcached.exe -d start.



Yum install
1. using yum to install memcached
# yum install memcached
2. 開機啟動 memcached 設定
# chkconfig memcached on
3. 啟動 memcached server 
# service memcached start
4. 簡單測試 memcached service
# echo stats | nc localhost 11211
會產生以下內容: 
STAT pid 13576
STAT uptime 1114
STAT time 1326179848
STAT version 1.4.10
STAT libevent 1.4.13-stable
STAT pointer_size 64
STAT rusage_user 0.008998
STAT rusage_system 0.013997
STAT curr_connections 13
STAT total_connections 16
STAT connection_structures 14
STAT reserved_fds 20
STAT cmd_get 113
STAT cmd_set 226
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 0
STAT get_misses 113
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 31426
STAT bytes_written 3403
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT bytes 40793
STAT curr_items 226
STAT total_items 226
STAT evictions 0
STAT reclaimed 0
END
5. 參數設定
# vi /etc/sysconfig/memcached
以下為預設參數值: 
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
6. 防火牆設定
# vi/etc/sysconfig/iptables
增加以下內容
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 11211 -j ACCEPT
7. 重新啟動 memcached server
# service memcached restart


For Linux
1. 下载memcached-1.2.1.tar.gz和libevent-1.3b.tar.gz
2. 安装libevent
# tar xvfz  libevent-1.3.tar.gz
# cd libevent-1.3
# ./configure --prefix=/usr
# make & make install
# ls -al /usr/lib | grep libevent
3. 安装 memcached
# tar xvfz  memcached-1.2.5.tar.gz
#  cd  memcached-1.2.5
# ./configure --with-libevent=/usr
#  make & make install
#  ls -al /usr/local/bin/mem*
4. 將 libevent 安裝的/usr/local/lib 輸出到涵式庫路徑
# vi ~/.bash_profile
add the following sentence
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
5.  啟動 memcached: /usr/local/bin/memcached
# memcached  -d  -m 128 -l 192.168.152.143 -p 11211 -u root

Memcached Session Manager Installation

Environments: One lighttpd web server (192.168.56.1, installation), one memcaced server (192.168.56.2, installation), and two tomcat servers (192.168.152.140 & 192.168.152.143, here) with memcached session manager packages.

Tomcat server with memcached session manager package:
1. put the following eight jar files into your tomcat library folder (e.g., /usr/share/apache-tomcat-7.0.**/lib)
  • http://memcached-session-manager.googlecode.com/files/memcached-session-manager-tc7-1.4.1.jar
  • http://spymemcached.googlecode.com/files/spymemcached-2.7.jar
  • http://memcached-session-manager.googlecode.com/files/kryo-1.03.jar
  • http://memcached-session-manager.googlecode.com/files/minlog-1.2.jar
  • http://memcached-session-manager.googlecode.com/files/reflectasm-0.9.jar
  • http://mirrors.ibiblio.org/pub/mirrors/maven2/asm/asm/3.2/asm-3.2.jar
  • http://github.com/downloads/magro/kryo-serializers/kryo-serializers-0.8.jar
  • http://memcached-session-manager.googlecode.com/files/msm-kryo-serializer-1.4.0.jar
2. add the following paragraph into context.xml (e.g., /usr/share/apache-tomcat-7.0.**/context.xml)
        <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
         memcachedNodes="n1: 192.168.56.2:11211"
         transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" />
* sometimes if you meet "com.esotericsoftware.kryo.SerializationException: Buffer limit exceeded reading object of type: java.util.concurrent.ConcurrentHashMap" problem, my solution is to change transcoderFactory from "de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" to "de.javakaffee.web.msm.JavaSerializationTranscoderFactory"
3. before restart your tomcat servers, the memcached server and web server have to been started.

Test jsp file:

<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%><%@ page import="java.net.InetAddress;" %>
<%
    String server_ip = "";
    InetAddress inetAddress = InetAddress.getLocalHost();
    server_ip = inetAddress.getHostAddress();
    String abc = (String) session.getAttribute("abc");
    String in_session = "false";
    if (abc == null)  {
        session.setAttribute("abc", "value  in session");
        abc  = "value not in session";
        in_session = "false";
    } else {
        in_session = "true";
    }
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
hello world! This is <%=server_ip%><br>
session id : <%=session.getId()%> <br>
abc value in session ? <%=in_session%><br>
abc value = <%=abc%>
</body>
</html>


Ref:
http://napmas.blogspot.com/2011/06/tomcat-and-memcached-and-session.html
http://code.google.com/p/memcached-session-manager/wiki/SetupAndConfiguration



Friday, January 6, 2012

Java Installation

For Linux
1. Download jdk-7u2-linux-64.rpm from here.
2. # rpm -ivh jdk-7u2-linux-64.rpm
3. Perform java and javac instruction.

For Windows
1. Download jdk-7u2-windows-64.exe from here
2. Double click jdk-7u2-windows-64.exe to install.
3. Set the environment variable (i.e., path & JAVA_HOME)
4. Perform java and javac in your command to test.

Thursday, January 5, 2012

Nginx as a Load Balancer

Environments: 
There are three servers. One has been installed nginx (192.168.152.142, here). The other two servers (192.168.152.140, 192.168.152.143) have been install tomcat server (here).
Scenario:
The http request be submitted to nginx server, then we take nginx server as a load balancer to assign this request to one of tomcat servers equally.
Configuration Nginx (Windows)
1. Add the following paragraph content into nginx.conf (C:\nginx\conf\nginx.conf)

http {
upstream examples{
ip_hash;
server 192.168.152.140:8080;
server 192.168.152.143:8080;
}
       server {
         listen       80;
         server_name  localhost;
         location / { proxy_pass http://examples;
                       }
       }
}
2. restart your nginx. 

Nginx Installation

For Windows
1. Download nginx-1.0.11-win64-setup.exe from here.
2. Double click nginx-1.0.11-win64-setup.exe to install.
3. Double click Start-nginx in C:\nginx\Start-nginx.

Lighttp as a Load Balancer

Environments: 
There are three servers. One has been installed lighttp (192.168.152.142, here). The other two servers (192.168.152.140, 192.168.152.143) have been install tomcat server (here).

Scenario:
The http request be submitted to lighttp server, then we take lighttpd server as a load balancer to assign this request to one of tomcat servers equally.

Configuration Lighttp (Linux)
1. Add the sentence (include "conf.d/proxy.conf")  into lighttp.conf (/etc/lighttpd/lighttpd.conf).
2. Add the sentence ("mod_proxy") into module.conf (/etc/lighttpd/modules.conf) server.modules section, as follows:
server.modules = (
  "mod_access",
  "mod_proxy",
#  "mod_alias",
#  "mod_auth",
#  "mod_evasive",
#  "mod_redirect",
#  "mod_rewrite",
#  "mod_setenv",
#  "mod_usertrack",
)

3. Add the following paragraph into proxy.conf (/etc/lighttpd/conf.d/proxy.conf)
  • $HTTP["url"] =~ "^/examples/" {
  • proxy.balance = "hash"
  •     proxy.server = (
  •         "" => (
  •                   ("host" => "192.168.152.140", "port" => 8080),
  •                   ("host" => "192.168.152.143", "port" => 8080)
  •               )
  •     )
  • }
where "examples" is your project name, you can even use ^ symbol to represent all projects.
4. restart your lighttpd
# service lighttpd restart 

Configuration Lighttp (Windows) 

1. uncomment "mod_proxy" in lighttpd-inc.conf (C:\Program Files (x86)\LightTPD\conf\lighttpd-inc.conf) at server.modules section, as follows:

server.modules              = (
                                "mod_access",
                                "mod_accesslog",
                                "mod_alias",
#                               "mod_auth",
#                               "mod_cgi",
#                               "mod_cml",
#                               "mod_compress",
#                               "mod_dirlisting",
#                               "mod_evasive",
#                               "mod_evhost",
#                               "mod_expire",
#                               "mod_extforward",
#                               "mod_fastcgi",
#                               "mod_flv_streaming",
#                               "mod_indexfile",
#                               "mod_magnet",
#                               "mod_mysql_vhost",
        "mod_proxy",
}

2. modify the content about your proxy location in lighttpd-inc.conf (C:\Program Files (x86)\LightTPD\conf\lighttpd-inc.conf), as follows:

proxy.server               = ( ".jsp" =>
                               ( "localhost" =>
                                 ("host" => "192.168.152.140","port" => 8080),
("host" => "192.168.152.143","port" => 8080)
                               )
                             )


3. restart your lighttd server. 



Lighttp Installation

For Linux
RPMForge x86_64 or 64bit install
rpm --import http://dag.wieers.com/rpm/packages/RPM-GPG-KEY.dag.txt
rpm -Uvh http://apt.sw.be/redhat/el5/en/x86_64/RPMS.dag/rpmforge-release-0.3.6-1.el5.rf.x86_64.rpm
Install lighttpd
# yum install lighttpd
# chkconfig lighttpd on
Check that Apache is not running
# service httpd status
httpd is stopped
Start lighttpd server
# service lighttpd start
Lighttpd Default Paths
Directory Root (where to put your website files): /srv/www/lighttpd/
Lighttpd Config File: /etc/lighttpd/lighttpd.conf
Lighttpd log files: /var/log/lighttpd/


For Windows
Download LightTPD-1.4.29-1-Win32-SSL.exe from here
Double click LightTPD-1.4.29-1-Win32-SSL.exe to install
Check that lighttp is running
http://localhost
If lighttpd is not running
startup the service in [console > service]