springboot+springsession+redis实现session共享

项目结构

1、springboot集成Redis以及springSession,需要在POM文件中增加依赖

<?xml version="1.0" encoding="UTF-8"?>
<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.it</groupId>
    <artifactId>springbootsession</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springbootsession</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>
        <!--spring boot 对redis的支持-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!--引入springsession-->
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

2.新建RedisSessionConfig类增加@EnableRedisHttpSession注解,开启spring boot对 springsession的支持并存储到redis中

package com.it.springbootsession.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

@Configuration
@EnableRedisHttpSession//开启spring session支持
public class RedisSessionConfig {
}

3、application.properties文件配置,我连接的为虚拟机中的redis,其中为了测试ngnix负载均衡功能,本工程配置为8080端口,另一个springboot工程可以配置为别的端口,比如8090,以便启动两个Server

spring.redis.host=192.168.0.147
server.port=8080

4、Controller的实现

package com.it.springbootsession.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "/spring/session")
public class SpringSessionController {

    @RequestMapping(value = "/")
    public ModelAndView test(HttpServletResponse response) throws IOException {
        return new ModelAndView("home");
    }

    @RequestMapping(value = "/setSession", method = RequestMethod.GET)
    public String setSession(HttpServletRequest request, HttpServletResponse response) {
        request.getSession().setAttribute("name", "tom");
        return "show";
    }

    @RequestMapping(value = "/getSession", method = RequestMethod.GET)
    public String getInterestPro(HttpServletRequest request, HttpServletResponse response) {
        return "show";
    }

    @RequestMapping(value = "/removeSession", method = RequestMethod.GET)
    public String removeSession(HttpServletRequest request, HttpServletResponse response) {
        request.getSession().removeAttribute("name");
        return "show";
    }

}

5、页面的实现home.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>tomcat8080</h1>
<p>This is the homepage!</p>
<a th:href="@{/spring/session/setSession}">设值</a>
<a th:href="@{/spring/session/getSession}">取值</a>
<a th:href="@{/pring/session/removeSession}">移除</a>
</body>
</html>

6、Show.html页面

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>tomcat8080</h1>
<p>This is the homepage!</p>
<label th:text="${#session.getId()}"></label><br/>
<label th:text="${session.name}"></label>

</body>
</html>

注意thymeleaf中如何取得session.setAttribute中的值,以后在thymeleaf中如何调用session中的相关方法,此时要接合SPEL,其中调用方法时前面#不能少。

7、通过redisClient查看数据

具体展示方式,可参考工的另一篇文章spring session+redis

猜你喜欢

转载自blog.csdn.net/fmwind/article/details/84532417
今日推荐