实战——session共享(Spring Boot + Spring Session + Redis)

来源:互联网 发布:配眼镜测得数据的意思 编辑:程序博客网 时间:2024/06/07 03:21

目的:实现服务器间session共享

环境:一台CentOS7的虚拟机,虚拟机安装了Redis数据库和Docker容器,Docker运行了1台Nginx容器和2台Tomcat容器

结构图

本地创建一个域名www.example.com指向ip地址192.168.40.128,通过www.example.com访问虚拟机,虚拟机映射Nginx服务器,Nginx服务器映射到两台tomcat服务器

实战准备:1.安装docker

  2.下载两个docker镜像(Nginx镜像,Tomcat镜像) 【注:由于spring boot项目自带tomcat,所以也可下载基础镜像如Ubuntu、CentOS来进行部署】

  3.安装Redis缓存数据库

步骤

1.运行容器,使用 docker  run-it  命令运行3各容器,1个Nginx容器,2个Tomcat容器

【注】

1.Nginx需要绑定端口号,本文将宿主机的8080端口绑定了Nginx的80端口;

2.Tomcat可不绑定端口;3.docker命令参见附录地址

2.进入Nginx容器,配置需代理的目标服务器。本文中,nginx指向172.17.0.3:8080、172.17.0.4:8080,权重都为1


反向代理测试:

1.分别打开两台Tomcat容器(以p地址为172.17.0.3为例),在webapps路径下创建目录 mkdir nginxTest

 2.在新建路径下创建文件 touch index.html 文件内容:

<head> <title>nginxTest</title></head><body>hello 172.17.0.3</body>
【注:ip地址为172.17.0.4的index.html 修改为 hello 172.17.0.4以示区分】

3.在宿主机进行反向代理测试(可以看出会访问nginx反向代理的两台tomcat)


【注】

1.使用 ifconfig -a  查询本机ip地址

2.tomcat/conf/server.xml中可配置所开启的端口

3.使用 tomcat/bin/startup.sh开启tomcat服务,并在tomcat本地先进行curl 测试,确保tomcat运行成功后再进行nginx反向代理测试

3.创建spring boot项目

(1)pom.xml文件

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.session</groupId><artifactId>spring-session-data-redis</artifactId></dependency>


(2)application.yml

server:  port: 8080  session-timeout: 60spring:    redis:         database: 0         host: 192.168.40.128       port: 6379         password:         timeout: 0         pool:             max-active: 8             max-wait: -1             max-idle: 8             min-idle: 0      session:        store-type: redis
【注】

1.server的port指定tomcat的端口号

2.sping.redis.host 是redis数据库的IP地址,端口号默认6379

3.spring.session.store-type需要指定redis进行session数据存储

4.确保spring boot项目所在的服务器能使用telnet命令 redis的ip和port,如果不通,请确保ip、port正确性,redis所在服务器的防火墙是否关闭或打开该端口

5.Redis的配置文件redis.conf需要更改两个地方(发布环境请自行增加password)

1)bind 127.0.0.1 改为 bind 0.0.0.0   //将redis暴露在公网

2)protected-mode yes   改为  protected-mode no  //取消保护模式

(3)spring.xml(主域名与子域名可进行跨域session共享的配置)

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">    <bean id="defaultCookieSerializer" class="org.springframework.session.web.http.DefaultCookieSerializer">        <property name="domainName" value="example.com"/>        <property name="cookieName" value="JSESSIONID"/>        <property name="cookiePath" value="/" />        <!-- <property name="domainNamePattern" value="^.+?\.(\w+\.[a-z]+)$"></property>-->    </bean>    <bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">        <property name="maxInactiveIntervalInSeconds" value="1800" />        <property name="cookieSerializer" ref="defaultCookieSerializer"/>    </bean></beans>

(4)创建SpringConfig类(读取spring.xml配置文件)

package com.cssweb.redis.config;@Configuration@ImportResource(locations={"spring.xml"})public class SpringConfig {}
(5)创建RedisConfig类(通过EnableRedisHttpSession注解进行自动配置)

package com.cssweb.redis.config@Configuration@EnableRedisHttpSession(maxInactiveIntervalInSeconds=60)//session 1分钟后过期public class RedisConfig {}

(6)创建QuickRunController类

package com.cssweb.login.controller;@RestController@RequestMapping(value = "/admin/")public class QuickRunController {    @RequestMapping(value = "/sessions", method = RequestMethod.GET)    public Object sessions (HttpServletRequest request){        Map<String, Object> map = new HashMap<>();        map.put("sessionId", request.getSession().getId());        return map;    }}

(7)SpringBoot启动类(应用入口)

package com.cssweb;@SpringBootApplication@EnableAutoConfigurationpublic class ShareSession1Application extends SpringBootServletInitializer{public static void main(String[] args) {SpringApplication.run(ShareSession1Application.class, args);}}

【注】

1.SpringConfig + spring.xml 是子域名间共享session

2.QuickRunController 提供/admin/sessions接口

3.将项目通过mvn package进行打包,放入宿主机中(jar包名为sharesession)

4.宿主机使用 docker cp 命令 ,将项目拷贝到172.17.0.3和172.17.0.4服务器上

5.使用 java -jar sharesession --server.port=8080进行启动服务(这里tomcat开启端口为8080)

4.在本机设置虚假域名

编辑C:\Windows\System32\drivers\etc\HOSTS文件,增加两个代码

192.168.40.128 www.example.com192.168.40.128 passport.example.com
5.测试

使用浏览器访问 www.example.com:8080/admin/sessions 以及 passport.example.com/admin/sessions



【注】

1.千万别使用curl 命令 在linux系统中进行测试,因为不管怎么测,你的Session ID都会一直变。

2.由于已经测试过反向代理模块,所以在spring boot项目中不再进行测试

【附录】

Docker命令,具体请参见:http://www.runoob.com/docker/docker-command-manual.html

SpringBoot项目地址:https://github.com/JIANGCHENZHOU/ShareSession

原创粉丝点击