nginx+tomcat8+redis实现session共享

一、配置nginx的负载均衡

  1.tomcat 改端口,一个为8180,一个为8280.

  2.下载nginx-1.12.2 修改conf/nginx.conf

  在#gzip  on;下添加
    
    upstream localhost {
        server localhost:8180 weight=1;
        server localhost:8280 weight=1;
    }

 在server { 的部分改为如下
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            proxy_set_header  Host $host:$server_port;
            proxy_redirect  http://localhost http://localhost:80;
            proxy_pass  http://localhost;
        }

 (直接启动nginx.exe,在cmd中nginx -s reload可以重启)

二、设置session共享

  1.新建maven项目helloword

      下载 https://github.com/jcoleman/tomcat-redis-session-manager 。取出其src放入自己的项目

    RedisSessionManager.java有一个修改:

//    if (getContainer() != null) {
//      loader = getContainer().getLoader();
//    }

改为:
    Context context = this.getContext();
    if(context!=null){
        loader = context.getLoader();
    }

       pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.bj</groupId>
  <artifactId>helloword</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  
  <dependencies>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-catalina</artifactId>
            <version>8.0.33</version>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.7.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin> <!-- 打jar包 -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <!-- 源代码编译版本 -->
                    <source>1.8</source>
                    <!-- 目标平台编译版本 -->
                    <target>1.8</target>
                    <!-- 字符集编码 -->
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

打包jar放入tomcat的lib下。(helloword-0.0.1-SNAPSHOT.jar、 jedis-2.9.0 .jar、commons-pool2-2.4.2.jar)

  2.新建web工程

  Hello_Word 

  index.jsp

<body>
	访问的Nginx IP:<%=request.getServerName()%>
	<BR> Tomcat SessionPort:<%=request.getServerPort()%>
	<%
		out.println("Tomcat Server Info=" + request.getLocalAddr() + " : " + request.getLocalPort() + "<br>");
		out.println("当前 Session ID=" + session.getId() + "<br>");
	%>
	<%
		String dataName = request.getParameter("dataName");
		if (dataName != null && dataName.length() > 0) {
			String dataValue = request.getParameter("dataValue");
			session.setAttribute(dataName, dataValue);
		}
		out.println("<b>Session列表</b><br>");
		System.out.println("Session列表");
		Enumeration e = session.getAttributeNames();
		while (e.hasMoreElements()) {
			String name = (String) e.nextElement();
			String value = session.getAttribute(name).toString();
			out.println(name + " = " + value + "<br>");
			System.out.println(name + " = " + value);
		}
	%>

	<form action="sessiontest.html" method="POST">
		名称:<input type=text size=20 name="dataName"> <br /> 
		值:<input type=text size=20 name="dataValue"> <br />
		  <input type=submit value="提交">
	</form>

</body>

  3.更改tomcat 8180和8280的context.xml

在<Context>下添加,即redis做session共享

    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
    <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
    <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
                 host="127.0.0.1"
                 port="6379"
                 database="0"
                 maxInactiveInterval="60" />

4.启动redis

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值