Tomcat+Nginx+Redis+MySQL实现反向代理、负载均衡、session共享 - ITB运维部落—http://www.itbcn.cn—ITB运维技术交流之家平台-ITB运维部落—http://www.itbcn.cn—ITB运维技术交流之家平台
记录工作点滴
分享运维知识

Tomcat+Nginx+Redis+MySQL实现反向代理、负载均衡、session共享

一、环境准备

时间同步

关闭防火墙

联通网络,配置yum源

软件包链接:https://pan.baidu.com/s/1qYbtpnQ

二、安装nginx

1、解决依赖关系

[[email protected] ~]# yum install gcc openssl-devel pcre-devel zlib-devel  -y

2、添加用户nginx,实现以之运行nginx服务进程

[[email protected] ~]# groupadd -r nginx
[[email protected] ~]# useradd -r -g nginx -s /bin/false -M nginx

3.、下载nginx软件,并编译安装

[[email protected] ~]# wget http://nginx.org/download/nginx-1.6.3.tar.gz
[[email protected] ~]# tar xf nginx-1.6.3.tar.gz 
[[email protected] ~]# cd nginx-1.6.3
[[email protected] nginx-1.6.3]#./configure \
  --prefix=/usr/local/nginx \
  --sbin-path=/usr/local/nginx/sbin/nginx \
  --conf-path=/etc/nginx/nginx.conf \
  --error-log-path=/var/log/nginx/error.log \
  --http-log-path=/var/log/nginx/access.log \
  --pid-path=/var/run/nginx/nginx.pid  \
  --lock-path=/var/lock/nginx.lock \
  --user=nginx \
  --group=nginx \
  --with-http_ssl_module \
  --with-http_flv_module \
  --with-http_stub_status_module \
  --with-http_gzip_static_module \
  --http-client-body-temp-path=/var/tmp/nginx/client/ \
  --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
  --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
  --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
  --http-scgi-temp-path=/var/tmp/nginx/scgi \
  --with-pcre
[[email protected] nginx-1.6.3]# make && make install

4.为nginx提供SysV init脚本

[[email protected] ~]# vim /etc/rc.d/init.d/nginx

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15 
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid
 
# Source function library.
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
 
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
 
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
 
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
 
lockfile=/var/lock/subsys/nginx
 
make_dirs() {
   # make required directories
   user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   options=`$nginx -V 2>&1 | grep 'configure arguments:'`
   for opt in $options; do
       if [ `echo $opt | grep '.*-temp-path'` ]; then
           value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}
 
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
 
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
 
restart() {
    configtest || return $?
    stop
    sleep 1
    start
}
 
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}
 
force_reload() {
    restart
}
 
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
 
rh_status() {
    status $prog
}
 
rh_status_q() {
    rh_status >/dev/null 2>&1
}
 
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac
而后为此脚本赋予执行权限:
[[email protected] ~]# chmod +x /etc/rc.d/init.d/nginx

添加至服务管理列表,并让其开机自动启动:
[[email protected] ~]# chkconfig --add nginx
[[email protected] ~]# chkconfig nginx on

而后就可以启动服务并测试了:
[[email protected] ~]# service nginx start
正在启动 nginx: [确定]
[[email protected] ~]# netstat -tnlp|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 35524/nginx
[[email protected] ~]# curl -I http://192.168.0.11/
HTTP/1.1 200 OK
Server: nginx/1.6.3
Date: Fri, 29 Dec 2017 13:36:19 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 29 Dec 2017 13:20:55 GMT
Connection: keep-alive
ETag: "5a464137-264"
Accept-Ranges: bytes

三、安装tomcat服务器

1.安装JDK,配置Java环境

[[email protected] ~]# rpm -vih jdk-8u25-linux-x64.rpm 
Preparing...                ########################################### [100%]
   1:jdk1.8.0_25            ########################################### [100%]
Unpacking JAR files...
    rt.jar...
    jsse.jar...
    charsets.jar...
    tools.jar...
    localedata.jar...
    jfxrt.jar...
[[email protected] ~]# cat /etc/profile.d/java.sh  #设置java环境变量
export JAVA_HOME=/usr/java/latest
export CLASSPATH=$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$PATH 
[[email protected] ~]# . /etc/profile.d/java.sh
[[email protected] ~]# java -version #查看java变量是否配置成功
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

2.安装tomcat服务

[[email protected] ~]# wget http://mirrors.shuosc.org/apache/tomcat/tomcat-8/v8.0.48/bin/apache-tomcat-8.0.48.tar.gz 
[[email protected] ~]# tar xf apache-tomcat-8.0.48.tar.gz -C /usr/local/
[[email protected] ~]# cd /usr/local/
[[email protected] local]# ln -sv apache-tomcat-8.0.48 tomcat
"tomcat" -> "apache-tomcat-8.0.48"
[[email protected] local]# cat /etc/profile.d/tomcat.sh  #配tomcat环境变量
export CATALINA_HOME=/usr/local/tomcat
export PATH=$CATALINA_HOME/bin:$PATH 
[[email protected] local]# . /etc/profile.d/tomcat.sh
[[email protected] local]# catalina.sh start #启动服务
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/java/latest
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[[email protected] local]# netstat -tnlp #查看端口是否启动
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      947/sshd            
tcp        0      0 :::22                       :::*                        LISTEN      947/sshd            
tcp        0      0 ::ffff:127.0.0.1:8005       :::*                        LISTEN      8014/java           
tcp        0      0 :::8009                     :::*                        LISTEN      8014/java           
tcp        0      0 :::8080                     :::*                        LISTEN      8014/java           
[[email protected] local]# curl -I http://192.168.0.12:8080 #测试是否可以打开
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 29 Dec 2017 13:49:39 GMT

安装完成。tomcat-server-2和tomcat-server-1相同,在此忽略

 设置默认虚拟主机,并增加jvmRoute

[[email protected] local]# vim /usr/local/tomcat/conf/server.xml 
 <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat-1">  #jvmRoute是jvm标识,就是页面最顶部的标签,在实际生产环境中,所有的后台tomcat标识都要一样,这里为了实验的说明性,两台tomcat的标识改成不一样的,分别为tomcat-1h和tomcat-2
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <Context docBase="/Data/webapps1" path="" reloadable="true" /> #修改默认虚拟主机,并将网站文件路径指向/Data/webapps1

创建context目录和测试页面

[[email protected] local]# mkdir -pv /Data/webapps1
mkdir: 已创建目录 "/Data"
mkdir: 已创建目录 "/Data/webapps1"
[[email protected] local]# cat /Data/webapps1/index.jsp #创建测试页面,server-2中将tomcat-1改为tomcat-1即可
<%@ page language="java" %>
<html>
  <head><title>Tomcat-1</title></head>
  <body>
    <h1><font color="red">www.tomcat-1.com</font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("tomcat-1.com","tomcat-1.com"); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>

测试配置,并启动

[[email protected] local]# catalina.sh stop
[[email protected] local]# catalina.sh configtest
[[email protected] local]# catalina.sh start

四、配置nginx负载均衡tomcat

[[email protected] ~]# vim /etc/nginx/nginx.conf

user  nginx;
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.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"';
    sendfile        on;
    tcp_nopush     on;
    keepalive_timeout  65;
    gzip  on;
    upstream tomcat-web {
      server 192.168.0.12:8080;
      server 192.168.0.13:8080;
       }
    server {
        listen       80;
        server_name  www.tomcat.com;
        location / {
            root   html;
            index  index.html index.htm index.jsp;
        }
        location ~* \.(jsp|do)$ {
        proxy_pass http://tomcat-web;
        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.0.0/24;
        deny all;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

重新载入

[[email protected] ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[[email protected] ~]# service nginx reload
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
重新载入 nginx:                                           [确定]

测试(测试机配置hosts文件解析),访问http://www.tomcat.com/index.jsp URL

多刷新几次,从结果能看出,nginx把访问请求分别分发给了后端的tomcat-1和tomcat-2,客户端的访问请求实现了负载均衡,但session id不一样(即:没有实现session保持)

五、安装redis服务

下载源码,并编译

[[email protected] ~]# wget http://download.redis.io/releases/redis-3.2.3.tar.gz
[[email protected] ~]# tar xf redis-3.2.3.tar.gz 
[[email protected] ~]# cd redis-3.2.3
[[email protected] redis-3.2.3]# make
[[email protected] redis-3.2.3]# make PREFIX=/usr/local/redis install

配置redis

[[email protected] redis-3.2.3]# mkdir /usr/local/redis/etc/
[[email protected] redis-3.2.3]# cp redis.conf /usr/local/redis/etc/ 
[[email protected] redis-3.2.3]# cd /usr/local/redis/bin/
[[email protected] bin]# cp redis-benchmark redis-cli redis-server /usr/bin/

调整下内存分配使用方式并使其生效

#此参数可用的值为0,1,2 
#0表示当用户空间请求更多的内存时,内核尝试估算出可用的内存 
#1表示内核允许超量使用内存直到内存用完为止 
#2表示整个内存地址空间不能超过swap+(vm.overcommit_ratio)%的RAM值 
[[email protected] bin]# echo "vm.overcommit_memory=1">>/etc/sysctl.conf
[[email protected] bin]# sysctl -p

修改redis配置

[[email protected] bin]#vim /usr/local/redis/etc/redis.conf
# 修改一下配置
#设置redis监听的地址
bind 0.0.0.0

# redis以守护进程的方式运行
# no表示不以守护进程的方式运行(会占用一个终端)  
daemonize yes

# 客户端闲置多长时间后断开连接,默认为0关闭此功能                                      
timeout 300

# 设置redis日志级别,默认级别:notice                    
loglevel verbose

# 设置日志文件的输出方式,如果以守护进程的方式运行redis 默认:"" 
# 并且日志输出设置为stdout,那么日志信息就输出到/dev/null里面去了 
logfile "/usr/local/redis/log/redis-access.log"

#redis默认是空密码访问,这样很不安全。需要启用redis的密码验证功能
requirepass [email protected]

redis环境变量配置

[[email protected] bin]# echo "export PATH=/usr/local/redis/bin:$PATH" >  /etc/profile.d/redis.sh
[[email protected] bin]# . /etc/profile.d/redis.sh

创建Redis 系统启动脚本

[[email protected] bin]# cat /etc/init.d/redis

#!/bin/bash
  #chkconfig: 2345 80 90
  # Simple Redis init.d script conceived to work on Linux systems
  # as it does use of the /proc filesystem.

  PATH=/usr/local/bin:/sbin:/usr/bin:/bin
  REDISPORT=6379
  EXEC=/usr/local/redis/bin/redis-server
  REDIS_CLI=/usr/local/redis/bin/redis-cli
     
  PIDFILE=/var/run/redis_6379.pid
  CONF="/usr/local/redis/etc/redis.conf"
     
  case "$1" in
      start)
          if [ -f $PIDFILE ]
          then
                  echo "$PIDFILE exists, process is already running or crashed"
          else
                  echo "Starting Redis server..."
                  $EXEC $CONF
          fi
          if [ "$?"="0" ] 
          then
                echo "Redis is running..."
          fi
          ;;
      stop)
          if [ ! -f $PIDFILE ]
          then
                  echo "$PIDFILE does not exist, process is not running"
          else
                  PID=$(cat $PIDFILE)
                  echo "Stopping ..."
                  $REDIS_CLI -p $REDISPORT SHUTDOWN
                  while [ -x ${PIDFILE} ]
                 do
                      echo "Waiting for Redis to shutdown ..."
                      sleep 1
                  done
                  echo "Redis stopped"
          fi
          ;;
     restart|force-reload)
          ${0} stop
          ${0} start
          ;;
    *)
      echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
          exit 1
  esac
[[email protected] bin]# chmod +x /etc/init.d/redis
[[email protected] bin]# service redis start #启动

测试:

[[email protected] bin]# redis-cli -h 192.168.0.14 -p 6379 -a [email protected]
192.168.0.14:6379> keys *
(empty list or set)
192.168.0.14:6379> set name pwb
OK
192.168.0.14:6379> get name
"pwb"


redis源码安装完毕

六、配置tomcatsession redis同步(tomcat-server上)

Tomcat8连接Reids需要以下3个软件包:

1 commons-pool2-2.2.jar
2 jedis-2.5.2.jar
3 tomcat-redis-session-manager-2.0.0.jar #tomcat7这需要将这个包替换为tomcat-redis-session-manage-tomcat7.jar

将所需要的jar包复制到$CATALINA_HOME/lib/下,即tomcat安装目录的lib目录下

[[email protected] ~]# cp commons-pool2-2.2.jar jedis-2.5.2.jar tomcat-redis-session-manager-2.0.0.jar  /usr/local/tomcat/lib

在Tomcat的conf/context.xml文件中加入使用redis-session的配置

<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> 
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" 
host="192.168.0.14" 
password="[email protected]" 
port="6379" 
database="0" 
maxInactiveInterval="60"
 />
注意Valve必须配置在Manager之前

通过浏览器访问测试,结果如下:

可以看出,分别访问了不同的tomcat,但是得到的session却是相同的,说明达到了集群的目的。

注:从Tomcat6开始默认开启了Session持久化设置,测试时可以关闭本地Session持久化,在Tomcat的conf目录下的context.xml文件中,取消 <Manager pathname=”” /> 注释即可

七、配置tomcat连接数据库

安装mysql,创建认证用户(mysql-server上)

[[email protected] yum]# yum install mysql-server  -y
[[email protected] yum]# service mysqld start
[[email protected] yum]# mysql
mysql> grant all on *.* to tomcat_user@'192.168.0.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

下载mysql-connector-java-5.1.22-bin.jar并复制到$CATALINA_HOME/lib目录下(tomcat-serve上)

[[email protected] ]# cd /usr/local/tomcat/
[[email protected] tomcat]#wget https://cdn.mysql.com//Downloads/Connector-J/mysql-connector-java-5.1.22.tar.gz
[[email protected] tomcat]#unzip mysql-connector-java-5.1.22-bin.jar.zip 
[[email protected] tomcat]#cp mysql-connector-java-5.1.22-bin.jar lib/

配置JNDI数据源,保存后内容如下:

conf/context.xml
<?xml version='1.0' encoding='utf-8'?>
<!--
  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>
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
          <!--配置mysql数据库的连接池-->

    <!--配置mysql数据库的连接池, 
             需要做的额外步骤是将mysql的Java驱动类放到tomcat的lib目录下        
             maxIdle 连接池中最多可空闲maxIdle个连接 
             minIdle 连接池中最少空闲maxIdle个连接 
             initialSize 初始化连接数目 
             maxWait 连接池中连接用完时,新的请求等待时间,毫秒 
             username 数据库用户名
             password 数据库密码
                  -->
    <!-- Default set of monitored resources. If one of these changes, the    -->
    <!-- web application will be reloaded.                                   -->
    <!-- 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.0.14" 
        password="[email protected]" 
        port="6379" 
        database="0" 
        maxInactiveInterval="60"
      />
</Context>

在项目的目录下新建WEB-INF目录,用于存放网站xml配置文件,用于tomcat连接mysql数据库

[[email protected] tomcat]mkdir /Data/webapps1/WEB-INF 
[[email protected] tomcat]vim /Data/webapps1/WEB-INF/web.xml
WEB-INF/web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
  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.
-->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  version="3.1">

<!-- 数据源 -->
   <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>

重启服务

[[email protected] WEB-INF]# catalina.sh stop
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/java/latest
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[[email protected] WEB-INF]# catalina.sh start
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/java/latest
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.

现在创建一个简单的测试.jsp页面,测试tomcat和mysql的连通性

[[email protected] tomcat]# vim /Data/webapps1/test.jsp

<[email protected] import="javax.naming.InitialContext"%>
<[email protected] import="javax.sql.DataSource"%>
<[email protected] import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"  
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        out.print("MySQL 数据源测试开始..." + "<br/>");
        DataSource ds = null;
        try {
            InitialContext ctx = new InitialContext();
            ds = (DataSource) ctx.lookup("java:comp/env/jdbc/TestDB");
            Connection conn = ds.getConnection();
            conn.close();
            out.print("MySQL 数据源测试成功!");
        } catch (Exception ex) {
            out.print("出现意外,信息是:" + ex.getMessage());
            ex.printStackTrace();
        }
    %>
    %</body>
    %</html>

访问测试页:

 以看出来,现在tomcat可以连接到数据库了。

未经允许不得转载:ITB运维部落—http://www.itbcn.cn—ITB运维技术交流之家平台 » Tomcat+Nginx+Redis+MySQL实现反向代理、负载均衡、session共享

如果文章对你有帮助,欢迎点击上方按钮打赏作者

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址