redis緩存服務器(Nginx+Tomcat+redis+MySQL實現session會話共享

一、redis介紹

redis是一個key-value存儲系統。和Memcached類似,它支持存儲的value類型相對更多,包括string(字符串)、list(鏈表)、set(集合)、zset(sorted set --有序集合)和hash(哈希類型)。與memcached一樣,爲了保證效率,數據都是緩存在內存中。區別的是redis會週期性的把更新的數據寫入磁盤或者把修改操作寫入追加的記錄文件,並且在此基礎上實現master-slave(主從)同步。

Redis是一個高性能的key-value數據庫。redis的出現,很大程度補償了memcached這類key/value存儲的不足,在部分場合可以對關係數據庫起到很好的補充作用。它提供了Java,C/C++,C#,PHP,JavaScript,Perl,Object-C,Python,Ruby等客戶端,使用很方便。

如果簡單地比較Redis與Memcached的區別,基本上有以下3點:
1、Redis不僅僅支持簡單的k/v類型的數據,同時還提供list,set,zset,hash等數據結構的存儲。
2、Redis支持數據的備份,即master-slave模式的數據備份。
3、Redis支持數據的持久化,可以將內存中的數據保持在磁盤中,重啓的時候可以再次加載進行使用。

在Redis中,並不是所有的數據都一直存儲在內存中的。這是和Memcached相比一個最大的區別。Redis只會緩存所有的key的信息,如果Redis發現內存的使用量超過了某一個閥值,將觸發swap的操作,Redis根據“swappability = age*log(size_in_memory)”計算出哪些key對應的value需要swap到磁盤。然後再將這些key對應的value持久化到磁盤中,同時在內存中清除。這種特性使得Redis可以保持超過其機器本身內存大小的數據。當然,機器本身的內存必須要能夠保持所有的key,因爲這些數據是不會進行swap操作的。

當從Redis中讀取數據的時候,如果讀取的key對應的value不在內存中,那麼Redis就需要從swap文件中加載相應數據,然後再返回給請求方。

memcached和redis的比較

1、網絡IO模型

Memcached是多線程,非阻塞IO複用的網絡模型,分爲監聽主線程和worker子線程,監聽線程監聽網絡連接,接受請求後,將連接描述字pipe 傳遞給worker線程,進行讀寫IO, 網絡層使用libevent封裝的事件庫,多線程模型可以發揮多核作用。

Redis使用單線程的IO複用模型,自己封裝了一個簡單的AeEvent事件處理框架,主要實現了epoll、kqueue和select,對於單純只有IO操作來說,單線程可以將速度優勢發揮到最大,但是Redis也提供了一些簡單的計算功能,比如排序、聚合等,對於這些操作,單線程模型實際會嚴重影響整體吞吐量,CPU計算過程中,整個IO調度都是被阻塞住的。

2、內存管理方面

Memcached使用預分配的內存池的方式,使用slab和大小不同的chunk來管理內存,value根據大小選擇合適的chunk存儲。Redis使用現場申請內存的方式來存儲數據。

3、存儲方式及其它方面

Memcached基本只支持簡單的key-value存儲,不支持持久化和複製等功能,Redis除key/value之外,還支持list,set,sortedset,hash等衆多數據結構

 

二、如何保持session會話

目前,爲了使web能適應大規模的訪問,需要實現應用的集羣部署。集羣最有效的方案就是負載均衡,而實現負載均衡用戶每一個請求都有可能被分配到不固定的服務器上,這樣我們首先要解決session的統一來保證無論用戶的請求被轉發到哪個服務器上都能保證用戶的正常使用,即需要實現session的共享機制。

在集羣系統下實現session統一的有如下幾種方案:

1、請求精確定位:sessionsticky,例如基於訪問ip的hash策略,即當前用戶的請求都集中定位到一臺服務器中,這樣單臺服務器保存了用戶的session登錄信息,如果宕機,則等同於單點部署,會丟失,會話不復制。

2、session複製共享:sessionreplication,如tomcat自帶session共享,主要是指集羣環境下,多臺應用服務器之間同步session,使session保持一致,對外透明。 如果其中一臺服務器發生故障,根據負載均衡的原理,調度器會遍歷尋找可用節點,分發請求,由於session已同步,故能保證用戶的session信息不會丟失,會話複製,。

此方案的不足之處:

必須在同一種中間件之間完成(如:tomcat-tomcat之間).

session複製帶來的性能損失會快速增加.特別是當session中保存了較大的對象,而且對象變化較快時, 性能下降更加顯著,會消耗系統性能。這種特性使得web應用的水平擴展受到了限制。

Session內容通過廣播同步給成員,會造成網絡流量瓶頸,即便是內網瓶頸。在大併發下表現並不好

3、基於cache DB緩存的session共享

基於memcache/redis緩存的 session 共享

即使用cacheDB存取session信息,應用服務器接受新請求將session信息保存在cache DB中,當應用服務器發生故障時,調度器會遍歷尋找可用節點,分發請求,當應用服務器發現session不在本機內存時,則去cache DB中查找,如果找到則複製到本機,這樣實現session共享和高可用。

 wKioL1jXUW2DE5ENAAAYwm1YUH8084.png-wh_50

三、nginx+tomcat+redis實現負載均衡、session共享

1、實驗環境


主機

操作系統

IP地址

Nginx

Centos7.2

192.168.31.141

Tomcat-1

192.168.31.83

Tomcat-2

192.168.31.250

Mysql

192.168.31.225

Redis

192.168.31.106


2、實驗拓撲


在這個圖中,nginx做爲反向代理,實現靜動分離,將客戶動態請求根據權重隨機分配給兩臺tomcat服務器,redis做爲兩臺tomcat的共享session數據服務器,mysql做爲兩臺tomcat的後端數據庫。

3、nginx安裝配置

使用Nginx作爲Tomcat的負載平衡器,Tomcat的會話Session數據存儲在Redis,能夠實現零宕機的7x24效果。因爲將會話存儲在Redis中,因此Nginx就不必配置成stick粘貼某個Tomcat方式,這樣才能真正實現後臺多個Tomcat負載平衡。

安裝nginx:

安裝zlib-devel、pcre-devel等依賴包


[root@www ~]# 

yum -y install gccgcc-c++ make libtoolzlibzlib-develpcrepcre-developensslopenssl-devel

注:

結合proxy和upstream模塊實現後端web負載均衡

結合nginx默認自帶的ngx_http_proxy_module模塊 和ngx_http_upstream_module模塊實現後端服務器的健康檢查

創建nginx程序用戶


[root@www ~]# useradd -s /sbin/nologin www

編譯安裝nginx

[root@www ~]# tar zxf nginx-1.10.2.tar.gz



3

4

[root@www ~]# cd nginx-1.10.2/

[root@www nginx-1.10.2]# ./configure --prefix=/usr/local/nginx1.10 --user=www -group=www --with-http_stub_status_module --with-http_realip_module --with-http_ssl_module--with-http_gzip_static_module  --with-pcre  --with-http_flv_module

[root@www nginx-1.10.2]# make&& make install

優化nginx程序的執行路徑


[root@www nginx-1.10.2]# ln -s /usr/local/nginx1.10/sbin/nginx /usr/local/sbin/

[root@www nginx-1.10.2]# nginx -t

nginx: the configuration file /usr/local/nginx1.10/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx1.10/conf/nginx.conf test is successful

編寫nginx服務腳本:腳本內容如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

[root@www ~]# cat /etc/init.d/nginx

#!/bin/bash

# nginx Startup script for the Nginx HTTP Server

# chkconfig: - 85 15

# pidfile: /usr/local/nginx1.10/logs/nginx.pid

# config: /usr/local/nginx1.10/conf/nginx.conf

nginxd=/usr/local/nginx1.10/sbin/nginx

nginx_config=/usr/local/nginx1.10/conf/nginx.conf

nginx_pid=/usr/local/nginx1.10/logs/nginx.pid

RETVAL=0

prog="nginx"

# Source function library.

. /etc/rc.d/init.d/functions

# Start nginx daemons functions.

start() {

if [ -f $nginx_pid ] ; then

echo "nginx already running...."

exit 1

fi

echo -n "Starting $prog: "

   $nginxd -c ${nginx_config}

   RETVAL=$?

[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx

}

# Stop nginx daemons functions.

stop() {

echo -n "Stopping $prog: "

        $nginxd -s stop

        RETVAL=$?

[ $RETVAL = 0 ] &&rm -f /var/lock/subsys/nginx

}

# reloadnginx service functions.

reload() {

echo -n "Reloading $prog: "

    $nginxd -s reload

}

# statusngnx service functions

status() {

if [ -f $nginx_pid ] ; then

echo  "$prog is running"

else

echo  "$prog is stop"

fi

}

case "$1" in

start)

start

        ;;

stop)

stop

        ;;

reload)

reload

        ;;

restart)

stop

start

        ;;

status)

status

        ;;

*)

echo "Usage: $prog {start|stop|restart|reload|status}"

exit 1

        ;;

esac

[root@www ~]# chmod +x /etc/init.d/nginx

[root@www ~]# chkconfig --add nginx

[root@www ~]# chkconfignginx on

[root@www ~]# systemctl daemon-reload

配置nginx反向代理:反向代理+負載均衡+健康探測,nginx.conf文件內容:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

[root@www ~]# cat /usr/local/nginx1.10/conf/nginx.conf

user  wwwwww;

worker_processes  4;

worker_cpu_affinity 0001 0010 0100 1000;

error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

worker_rlimit_nofile 10240;

pid        logs/nginx.pid;

events {

useepoll;

worker_connections  4096;

}

http {

includemime.types;

default_type  application/octet-stream;

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';

access_log  logs/access.log  main;

server_tokens off;

sendfile        on;

tcp_nopush     on;

    #keepalive_timeout  0;

keepalive_timeout  65;

    #Compression Settings

gzip on;

gzip_comp_level 6;

gzip_http_version 1.1;

gzip_proxied any;

gzip_min_length 1k;

gzip_buffers 16 8k;

gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;

gzip_vary on;

    #end gzip

    # http_proxy Settings

client_max_body_size   10m;

client_body_buffer_size   128k;

proxy_connect_timeout   75;

proxy_send_timeout   75;

proxy_read_timeout   75;

proxy_buffer_size   4k;

proxy_buffers   4 32k;

proxy_busy_buffers_size   64k;

proxy_temp_file_write_size  64k;

    #load balance Settings

upstreambackend_tomcat {

server 192.168.31.83:8080 weight=1 max_fails=2 fail_timeout=10s;

server 192.168.31.250:8080 weight=1 max_fails=2 fail_timeout=10s;

    }

    #virtual host Settings

server {

listen       80;

server_name  www.benet.com;

charset utf-8;

location / {

root html;

index  index.jsp index.html index.htm;

        }

location ~* \.(jsp|do)$ {

proxy_pass  http://backend_tomcat;

proxy_redirect off;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;

        }

location /nginx_status {

stub_status on;

access_log off;

allow 192.168.31.0/24;

deny all;

        }

    }

}

重啓nginx服務,使修改生效

1

[root@www ~]# service  nginx restart

配置防火牆規測

1

2

3

4

[root@www ~]# firewall-cmd --permanent --add-port=80/tcp

success

[root@www ~]# firewall-cmd --reload 

success

4、安裝部署tomcat應用程序服務器

在tomcat-1和tomcat-2節點上安裝JDK

在安裝tomcat之前必須先安裝JDK,JDK的全稱是java  development kit,是sun公司免費提供的java語言的軟件開發工具包,其中包含java虛擬機(JVM),編寫好的java源程序經過編譯可形成java字節碼,只要安裝了JDK,就可以利用JVM解釋這些字節碼文件,從而保證了java的跨平臺性。

安裝JDK,配置java環境:

將jdk-7u65-linux-x64.gz解壓

1

[root@tomcat-1 ~]# tar zxf jdk-7u65-linux-x64.gz

將解壓的jdk1.7.0_65目錄移致動到/usr/local/下並重命名爲java

1

[root@tomcat-1 ~]# mv jdk1.7.0_65/ /usr/local/java

在/etc/profile文件中添加內容如下:

1

2

export JAVA_HOME=/usr/local/java

export PATH=$JAVA_HOME/bin:$PATH

通過source命令執行profile文件,使其生效。

1

2

3

[root@tomcat-1 ~]# source /etc/profile

[root@tomcat-1 ~]# echo $PATH

/usr/local/java/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

按照相同方法在tomcat-2也安裝JDK

分別在在tomcat-1和tomcat-2節點運行java  -version命令查看java版本是否和之前安裝的一致。

1

2

3

4

[root@tomcat-1 ~]# java -version

java version "1.7.0_65"

Java(TM) SE Runtime Environment (build 1.7.0_65-b17)

Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

至此java環境已經配置完成

在tomcat-1和tomcat-2節點安裝配置tomcat

解壓apache-tomcat-7.0.54.tar.gz包

1

[root@tomcat-1 ~]# tar zxf apache-tomcat-7.0.54.tar.gz

將解壓生成的文件夾移動到/usr/local/下,並改名爲tomcat7

1

[root@tomcat-1 ~]# mv apache-tomcat-7.0.54 /usr/local/tomcat7

配置tomcat環境變量

/etc/profile文件內容如下:

1

2

3

export JAVA_HOME=/usr/local/java

export CATALINA_HOME=/usr/local/tomcat7

export PATH=$JAVA_HOME/bin:$CATALINA_HOME/bin:$PATH

通過source命令執行profile文件,使其生效。

1

2

3

[root@tomcat-1 ~]# source /etc/profile

[root@tomcat-1 ~]# echo $PATH

/usr/local/java/bin:/usr/local/tomcat7/bin:/usr/local/java/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

查看tomcat的版本信息

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

[root@tomcat-1 ~]# catalina.sh version

Using CATALINA_BASE:   /usr/local/tomcat7

Using CATALINA_HOME:   /usr/local/tomcat7

Using CATALINA_TMPDIR: /usr/local/tomcat7/temp

Using JRE_HOME:        /usr/local/java

Using CLASSPATH:       /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar

Server version: Apache Tomcat/7.0.54

Server built:   May 19 2014 10:26:15

Server number:  7.0.54.0

OS Name:        Linux

OS Version:     3.10.0-327.el7.x86_64

Architecture:   amd64

JVM Version:    1.7.0_65-b17

JVM Vendor:     Oracle Corporation

啓動tomcat

[root@tomcat-1 ~]# /usr/local/tomcat7/bin/startup.sh 

Using CATALINA_BASE:   /usr/local/tomcat7

Using CATALINA_HOME:   /usr/local/tomcat7

Using CATALINA_TMPDIR: /usr/local/tomcat7/temp

Using JRE_HOME:        /usr/local/java

Using CLASSPATH:       /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar

Tomcat started.

Tomcat默認運行在8080端口,運行netstat命令查看8080端口監聽的信息

1

2

3

[root@tomcat-1 ~]# netstat -anpt | grep java

tcp6       0      0 :::8009      :::*                    LISTEN      42330/java          

tcp6       0      0 :::8080      :::*                    LISTEN      42330/java

防火牆規則配置:

1

2

3

4

[root@tomcat-1 ~]# firewall-cmd --permanent --add-port=8080/tcp

success

[root@tomcat-1 ~]# firewall-cmd --reload

success

按照相同方法在tomcat-2也安裝

打開瀏覽器分別對tomcat-1和tomcat-2訪問測試

wKiom1jXUYLASK_mAABtoW-aNV4138.png-wh_50

如果想關閉tomcat則運行/usr/local/tomcat7/bin/shutdown.sh命令

好了,大家可以看到訪成功。說明我們的tomcat安裝完成,下面我們來修改配置文件

1

2

[root@tomcat-1 ~]# vim /usr/local/tomcat

7/conf/server.xml

設置默認虛擬主機,並增加jvmRoute

1

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat-1">

修改默認虛擬主機,並將網站文件路徑指向/web/webapp1,在host段增加context段

1

2

3

4

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

unpackWARs="true" autoDeploy="true">

<Context docBase="/web/webapp1" path="" reloadable="true"/>

</Host>

增加文檔目錄與測試文件

1

2

3

[root@tomcat-1 ~]# mkdir -p /web/webapp1

[root@tomcat-1 ~]# cd /web/webapp1/

[root@ tomcat-1 webapp1]# viindex.jsp

index.jsp內容如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<%@page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<html>

<head>

<title>tomcat-1</title>

</head>

<body>

<h1><font color="red">Session serviced by tomcat</font></h1>

<table aligh="center" border="1">

<tr>

<td>Session ID</td>

<td><%=session.getId() %></td>

<% session.setAttribute("abc","abc");%>

</tr>

<tr>

<td>Created on</td>

<td><%= session.getCreationTime() %></td>

</tr>

</table>

</body>

<html>

停止tomcat運行,檢查配置文件並啓動tomcat

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

[root@tomcat-1 ~]# shutdown.sh

[root@tomcat-1 ~]# netstat -anpt | grep java

[root@tomcat-1 ~]# catalina.shconfigtest

Using CATALINA_BASE:   /usr/local/tomcat7

Using CATALINA_HOME:   /usr/local/tomcat7

Using CATALINA_TMPDIR: /usr/local/tomcat7/temp

Using JRE_HOME:        /usr/local/java

Using CLASSPATH:       /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar

Nov 16, 2016 1:04:05 AM org.apache.catalina.core.AprLifecycleListenerinit

INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib

Nov 16, 2016 1:04:05 AM org.apache.coyote.AbstractProtocolinit

INFO: Initializing ProtocolHandler ["http-bio-8080"]

Nov 16, 2016 1:04:05 AM org.apache.coyote.AbstractProtocolinit

INFO: Initializing ProtocolHandler ["ajp-bio-8009"]

Nov 16, 2016 1:04:05 AM org.apache.catalina.startup.Catalina load

INFO: Initialization processed in 534 ms

[root@tomcat-1 ~]# startup.sh 

Using CATALINA_BASE:   /usr/local/tomcat7

Using CATALINA_HOME:   /usr/local/tomcat7

Using CATALINA_TMPDIR: /usr/local/tomcat7/temp

Using JRE_HOME:        /usr/local/java

Using CLASSPATH:       /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar

Tomcat started.

[root@tomcat-1 ~]# netstat -anpt | grep java

tcp6       0      0 :::8009    :::*                    LISTEN      8180/java           

tcp6       0      0 :::8080    :::*                    LISTEN      8180/java

Tomcat-2節點與tomcat-1節點配置基本類似,只是jvmRoute不同,另外爲了區分由哪個節點提供訪問,測試頁標題也不同(生產環境兩個tomcat服務器提供的網頁內容是相同的)。其他的配置都相同。

用瀏覽器訪問nginx主機,驗證負載均衡

第一次訪問的結果

wKiom1jXUdKjKxhMAAASRhD8deE337.png-wh_50

第二次訪問的結果

wKioL1jXUd7gVRnoAAASX8f_EkE880.png-wh_50

驗證健康檢查的方法可以關掉一臺tomcat主機,用客戶端瀏覽器測試訪問。

從上面的結果能看出兩次訪問,nginx把訪問請求分別分發給了後端的tomcat-1和tomcat-2,客戶端的訪問請求實現了負載均衡,但sessionid並一樣。所以,到這裏我們準備工作就全部完成了,下面我們來配置tomcat通過redis實現會話保持。

5、安裝redis

下載redis源碼,並進行相關操作,如下:

1

2

wget 

http://download.redis.io/releases/redis-3.2.3.tar.gz

解壓安裝redis

1

[root@redis ~]# tar zxf redis-3.2.3.tar.gz

解壓完畢後,現在開始安裝,如下:

1

2

[root@redis ~]# cd redis-3.2.3/

[root@redis redis-3.2.3]# make&& make install

wKiom1jXUf2jHYWhAAAJ-t_jz3A665.png-wh_50

wKiom1jXUgnD8_q-AAALkOr0KKQ166.png-wh_50

通過上圖,我們可以很容易的看出,redis安裝到/usr/local,/usr/local/bin,/usr/local/share,/usr/local/include,/usr/local/lib,/usr/local/share/man目錄下。

然後再切換到utils目錄下,執行redis初始化腳本install_server.sh,如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

[root@redis redis-3.2.3]# cdutils/

[root@redisutils]# ./install_server.sh

Welcome to the redis service installer

This script will help you easily set up a running redis server

  

Please select the redis port for this instance: [6379] 

Selecting default: 6379

Please select the redisconfig file name [/etc/redis/6379.conf] 

Selected default - /etc/redis/6379.conf

Please select the redis log file name [/var/log/redis_6379.log]

Selected default - /var/log/redis_6379.log

Please select the data directory for this instance [/var/lib/redis/6379] 

Selected default - /var/lib/redis/6379

Please select the redis executable path [/usr/local/bin/redis-server] 

Selected config:

Port           : 6379

Config file    : /etc/redis/6379.conf

Log file       : /var/log/redis_6379.log

Data dir       : /var/lib/redis/6379

Executable     : /usr/local/bin/redis-server

CliExecutable : /usr/local/bin/redis-cli

Is this ok? Then press ENTER to go on or Ctrl-C to abort.

Copied /tmp/6379.conf => /etc/init.d/redis_6379

Installing service...

Successfully added to chkconfig!

Successfully added to runlevels 345!

Starting Redis server...

Installation successful!

通過上面的安裝過程,我們可以看出redis初始化後redis配置文件爲/etc/redis/6379.conf,日誌文件爲/var/log/redis_6379.log,數據文件dump.rdb存放到/var/lib/redis/6379目錄下,啓動腳本爲/etc/init.d/redis_6379。

現在我們要使用systemd,所以在 /etc/systems/system 下創建一個單位文件名字爲 redis_6379.service。

1

[root@redisutils]# vi /etc/systemd/system/redis_6379.service

內容如下:

1

2

3

4

5

6

7

8

[Unit]

Description=Redis on port 6379

[Service]

Type=forking

ExecStart=/etc/init.d/redis_6379 start

ExecStop=/etc/init.d/redis_6379 stop

[Install]

WantedBy=multi-user.target

注:這裏Type=forking是後臺運行的形式

啓動redis

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

[root@redisutils]# systemctl daemon-reload 

[root@redisutils]# systemctl enable redis_6379.service

[root@redisutils]# systemctl start redis_6379.service

[root@redisutils]# systemctl status redis_6379.service 

● redis_6379.service - Redis on port 6379

   Loaded: loaded (/etc/systemd/system/redis_6379.service; enabled; vendor preset: disabled)

   Active: active (running) since Wed 2016-11-16 21:07:26 CST; 4min 25s ago

  Process: 7732 ExecStart=/etc/init.d/redis_6379 start (code=exited, status=0/SUCCESS)

 Main PID: 7734 (redis-server)

CGroup: /system.slice/redis_6379.service

└─7734 /usr/local/bin/redis-server 127.0.0.1:6379

  

Nov 16 21:07:26 redissystemd[1]: Starting Redis on port 6379...

Nov 16 21:07:26 redis redis_6379[7732]: Starting Redis server...

Nov 16 21:07:26 redissystemd[1]: Started Redis on port 6379.

[root@redisutils]# netstat -anpt | grep 6379

tcp        0      0 127.0.0.1:6379     0.0.0.0:*     LISTEN      7734/redis-server 1

從顯示結果可以看到redis默認監聽的是127.0.0.1的6379端口

防火牆規則設置

1

2

3

4

[root@redisutils]# firewall-cmd --permanent --add-port=6379/tcp

success

[root@redisutils]# firewall-cmd --reload 

success

現在來查看redis版本使用redis-cli –version命令,如下

1

2

[root@redisutils]# redis-cli --version

redis-cli 3.2.3

通過顯示結果,我們可以看到redis版本是3.2.3。

到此源碼方式安裝redis就介紹完畢。

redis安裝完畢之後,我們再來配置redis

設置redis監聽的地址,添加監聽redis主機的ip

考慮到安全性,我們需要啓用redis的密碼驗證功能requirepass參數

最終redis配置文件如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

[root@redis ~]# grep -Ev '^#|^$' /etc/redis/6379.conf 

bind 127.0.0.1 192.168.31.106

protected-mode yes

port 6379

tcp-backlog 511

timeout 0

tcp-keepalive 300

daemonize yes

supervised no

pidfile /var/run/redis_6379.pid

loglevel notice

logfile /var/log/redis_6379.log

databases 16

save 900 1

save 300 10

save 60 10000

stop-writes-on-bgsave-error yes

rdbcompression yes

rdbchecksum yes

dbfilenamedump.rdb

dir /var/lib/redis/6379

slave-serve-stale-data yes

slave-read-only yes

repl-diskless-sync no

repl-diskless-sync-delay 5

repl-disable-tcp-nodelay no

slave-priority 100

requirepass pwd@123

appendonly no

appendfilename "appendonly.aof"

appendfsynceverysec

no-appendfsync-on-rewrite no

auto-aof-rewrite-percentage 100

auto-aof-rewrite-min-size 64mb

aof-load-truncated yes

lua-time-limit 5000

slowlog-log-slower-than 10000

slowlog-max-len 128

latency-monitor-threshold 0

notify-keyspace-events ""

hash-max-ziplist-entries 512

hash-max-ziplist-value 64

list-max-ziplist-size -2

list-compress-depth 0

set-max-intset-entries 512

zset-max-ziplist-entries 128

zset-max-ziplist-value 64

hll-sparse-max-bytes 3000

activerehashing yes

client-output-buffer-limit normal 0 0 0

client-output-buffer-limit slave 256mb 64mb 60

client-output-buffer-limitpubsub 32mb 8mb 60

hz 10

aof-rewrite-incremental-fsync yes

重新啓動redis服務

1

2

3

[root@redis ~]# systemctl restart redis_6379.service

[root@redis ~]# netstat -anpt | grep redis

tcp   0   0 192.168.31.106:6379     0.0.0.0:*   LISTEN      8418/redis-server 1

redis配置文件配置完畢後,我們來啓動redis並進行簡單的操作。如下:

1

2

3

4

5

6

7

8

[root@redis ~]# redis-cli -h 192.168.31.106 -p 6379 -a pwd@123

192.168.31.106:6379> keys *

(empty list or set)

192.168.31.106:6379> set name lisi

OK

192.168.31.106:6379> get name

"lisi"

192.168.31.106:6379>

說明:

關於redis-cli -h 192.168.31.106 -p 6379 -a pwd@123的參數解釋

這條命令是說要連接redis服務器,IP是192.168.31.106,端口是6379,密碼是pwd@123。

keys *是查看redis所有的鍵值對。

set namelisi添加一個鍵值name,內容爲lisi。

get name查看name這個鍵值的內容。

redis的命令使用暫時我們就介紹這麼多

6、配置tomcat session redis同步

下載tomcat-redis-session-manager相應的jar包,主要有三個:

1

2

3

tomcat-redis-session-manage-tomcat7.jar

jedis-2.5.2.jar

commons-pool2-2.2.jar

下載完成後拷貝到$TOMCAT_HOME/lib中

1

[root@tomcat-1 ~]# cp tomcat-redis-session-manage-tomcat7.jar jedis-2.5.2.jar commons-pool2-2.2.jar /usr/local/tomcat7/lib/

修改tomcat的context.xml:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

[root@tomcat-1 ~]# cat /usr/local/tomcat7/conf/context.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.

-->

<!-- The contents of this file will be loaded for each web application -->

<Context>

<!-- Default set of monitored resources -->

<WatchedResource>WEB-INF/web.xml</WatchedResource>

<!-- Uncomment this to disable session persistence across Tomcat restarts -->

<!--

<Manager pathname="" />

    -->

<!-- Uncomment this to enable Comet connection tacking (provides events

on session expiration as well as webapp lifecycle) -->

<!--

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

    -->

<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />

<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"

host="192.168.31.106"

password="pwd@123"

port="6379"

database="0"

maxInactiveInterval="60" />

</Context>

重啓tomcat服務

說明:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

maxInactiveInterval="60":session的失效時間

[root@tomcat-1 ~]# shutdown.sh 

Using CATALINA_BASE:   /usr/local/tomcat7

Using CATALINA_HOME:   /usr/local/tomcat7

Using CATALINA_TMPDIR: /usr/local/tomcat7/temp

Using JRE_HOME:        /usr/local/java

Using CLASSPATH:       /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar

[root@tomcat-1 ~]# startup.sh 

Using CATALINA_BASE:   /usr/local/tomcat7

Using CATALINA_HOME:   /usr/local/tomcat7

Using CATALINA_TMPDIR: /usr/local/tomcat7/temp

Using JRE_HOME:        /usr/local/java

Using CLASSPATH:       /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar

Tomcat started.

tomcat-2執行和tomcat-1相同的操作

通過瀏覽器訪問http://192.168.31.141/index.jsp測試頁

wKiom1jXUoOD5GjCAAAn_mMzZ5E351.png-wh_50

刷新頁面

wKioL1jXUo-zQJfHAAAnXzLwnbU700.png-wh_50

可以看出,分別訪問了不同的tomcat,但是得到的session卻是相同的,說明達到了集羣的目的。

注:從Tomcat6開始默認開啓了Session持久化設置,測試時可以關閉本地Session持久化,其實也很簡單,在Tomcat的conf目錄下的context.xml文件中,取消註釋下面那段配置即可:

修改前:

1

2

3

4

<!-- Uncomment this to disable session persistence across Tomcat restarts -->

<!--

<Manager pathname="" />

-->

修改後:

1

2

<!-- Uncomment this to disable session persistence across Tomcat restarts -->

<Manager pathname="" />

重啓tomcat服務

查看redis:

1

2

3

4

5

6

[root@redis ~]# redis-cli -h 192.168.31.106 -p 6379 -a pwd@123

192.168.31.106:6379> keys *

1) "6C3F950BE6413AD2E0EF00F930881224.tomcat-1.tomcat-1"

2) "name"

3) "7A6D5D4C5B1EA52C4E9EED1C5523FEB5.tomcat-2.tomcat-2"

4) "32C35EEA064884F065E93CB00C690662.tomcat-1.tomcat-1"

7、tomcat連接數據庫

192.168.31.225作爲mysql數據庫服務器

1

2

3

4

5

6

7

8

9

10

11

12

[root@db ~]# mysql -uroot -p123.abc

mysql> grant all on *.* to javauser@'192.168.31.%' identified by 'javapasswd';

Query OK, 0 rows affected, 1 warning (0.02 sec)

  

mysql> create database javatest;

Query OK, 1 row affected (0.01 sec)

  

mysql> use javatest;

Database changed

  

mysql> create table testdata(id int not null auto_increment primary key,foo varchar(25),bar int);

Query OK, 0 rows affected (0.02 sec)

插入些數據

1

2

3

4

5

6

7

8

9

10

11

mysql> insert into testdata(foo,bar) values ('hello','123456'),('ok','654321');

Query OK, 2 rows affected (0.04 sec)

Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from testdata;

+----+-------+--------+

| id | foo   | bar    |

+----+-------+--------+

|  1 | hello | 123456 |

|  2 | ok    | 654321 |

+----+-------+--------+

2 rows in set (0.00 sec)

mysql防火牆配置

配置tomcat服務器連接mysql數據庫

下載mysql-connector-java-5.1.22-bin.jar並複製到$CATALINA_HOME/lib目錄下

1

2

3

4

5

6

7

8

9

10

11

12

13

[root@tomcat-1 ~]# cp mysql-connector-java-5.1.22-bin.jar /usr/local/tomcat7/lib/

[root@tomcat-1 ~]# ls /usr/local/tomcat7/lib/mysql-connector-java-5.1.22-bin.jar 

/usr/local/tomcat7/lib/mysql-connector-java-5.1.22-bin.jar

  

context configuration

configure the JNDI datasource in tomcat by adding a declaration for your resource to your

context

[root@tomcat-1 ~]# vi /usr/local/tomcat7/conf/context.xml

在<Context>中添加如下內容:

<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"

maxActive="100" maxIdle="30" maxWait="10000"

username="javauser" password="javapass" driverClassName="com.mysql.jdbc.Driver"

url="jdbc:mysql://192.168.31.225:3306/javatest"/>

保存修改並退出

1

2

3

web.xml configuration

[root@tomcat-1 ~]# mkdir /web/webapp1/WEB-INF

[root@tomcat-1 ~]# vi /web/webapp1/WEB-INF/web.xml

添加內容如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"

version="2.4">

<description>MySQL Test App</description>

<resource-ref>

<description>DB Connection</description>

<res-ref-name>jdbc/TestDB</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

<res-auth>Container</res-auth>

</resource-ref>

</web-app>

保存修改並退出,重啓tomcat服務

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

[root@tomcat-1 ~]# shutdown.sh 

Using CATALINA_BASE:   /usr/local/tomcat7

Using CATALINA_HOME:   /usr/local/tomcat7

Using CATALINA_TMPDIR: /usr/local/tomcat7/temp

Using JRE_HOME:        /usr/local/java

Using CLASSPATH:       /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar

[root@tomcat-1 ~]# startup.sh 

Using CATALINA_BASE:   /usr/local/tomcat7

Using CATALINA_HOME:   /usr/local/tomcat7

Using CATALINA_TMPDIR: /usr/local/tomcat7/temp

Using JRE_HOME:        /usr/local/java

Using CLASSPATH:       /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar

Tomcat started.

tomcat-2進行和tomcat-1相同的操用

Test code

Now create a simple test.jsp page,內容如下:

[root@tomcat-2 ~]# vi /web/webapp1/test.jsp

[root@tomcat-2 webapp1]# cattest.jsp

<%@ page language="java" import="java.sql.*" pageEncoding="GB2312"%>

<html>

<head>

<title>MySQL</title>

</head>

<body>

connect MySQL<br>

<%

String driverClass="com.mysql.jdbc.Driver";

String url="jdbc:mysql://192.168.31.225:3306/javatest";

String username = "javauser"; 

String password = "javapasswd"; 

Class.forName(driverClass); 

Connection conn=DriverManager.getConnection(url, username, password); 

Statement stmt=conn.createStatement();

ResultSetrs = stmt.executeQuery("select * from testdata"); 

while(rs.next()){

out.println("<br>foo:"+rs.getString(2)+"bar:"+rs.getString(3));

}

rs.close();

stmt.close();

conn.close();

%>

</body></html>

通過瀏覽器訪問http://192.168.31.141/test.jsp測試頁

wKioL1jXUt7xdiE6AAAKtQE4VYM635.png-wh_50

注:

以上配置可以參考tomcat docs

wKioL1jXUvGRP1MNAABepYAbdAs716.jpg-wh_50

wKiom1jXUv2SJJJ6AAA3Q6TSS7Y377.png-wh_50

wKioL1jXUw3hzTuvAAA-ck8Vwh0747.png-wh_50




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章