1. 程式人生 > >springboot+springsession+redis實現session共享

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