• 【Spring Session】和 Redis 结合实现 Session 共享


    【Spring Session】和 Redis 结合实现 Session 共享

    参考官方文档

    HttpSession with Redis Guide

    https://docs.spring.io/spring-session/docs/2.1.5.RELEASE/reference/html5/guides/boot-redis.html

    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>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.7.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>redis-service</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>redis-service</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.session</groupId>
                <artifactId>spring-session-data-redis</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.9.3</version>
            </dependency>
        </dependencies>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>

    application.properties

    server.port=8002
    spring.application.name=redis-service
    eureka.instance.prefer-ip-address=true
    eureka.instance.instance-id=${spring.application.name}:${random.uuid}
    eureka.client.service-url.default-zone=http://localhost:8761/eureka
    management.endpoints.web.exposure.include=routes
    spring.redis.host=192.168.226.128
    spring.redis.port=6379
    spring.redis.password=
    spring.redis.database=0
    spring.session.store-type=redis
    server.servlet.session.timeout= 7200
    spring.session.redis.flush-mode=on_save
    spring.session.redis.namespace=spring:session

    主类

    @EnableRedisHttpSession(redisFlushMode = RedisFlushMode.IMMEDIATE)
    @EnableDiscoveryClient
    @Slf4j
    @RestController
    @SpringBootApplication
    public class RedisServiceApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(RedisServiceApplication.class, args);
        }
    
        @Autowired
        RedisTemplate redisTemplate;
    
        @Autowired
        StringRedisTemplate stringRedisTemplate;
    
        @RequestMapping("/")
        public Map<String, Object> home(HttpServletRequest request, HttpServletResponse response) {
            HttpSession session = request.getSession();
            System.out.println(session.getId());
    
            SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss.SSS");
            String data = format.format(new Date());
    
            session.setAttribute("user", data);
    
            Map<String, Object> res = new HashMap<>();
            res.put("message", "redis");
            res.put("data", data);
            return res;
        }
    
        @RequestMapping("/set")
        public void set(String key, String value) {
            log.info("key = " + key + ", value = " + value);
            stringRedisTemplate.opsForValue().set(key, value);
        }
    
        @RequestMapping("/get")
        public String show(String key) {
            return stringRedisTemplate.opsForValue().get(key);
        }
    
    }
  • 相关阅读:
    计算机进制转换
    十进制二进制转换
    windows XP 下怎样查看系统运行时间总计?
    “编译器错误信息: CS0016: 未能写入输出文件”解决方法
    用ntsd关进程
    Hosts文件的位置
    开机显示client mac addr...... 错误的解决办法 .
    无法嵌入互操作类型“Microsoft.Office.Interop.Excel.ApplicationClass”。请改用适用的接口 .
    【转】XP远程桌面连接2008提示:远程计算机需要网络级别身份验证,而您的计算机不支持该验证 .
    关于Access的郁闷二三事。。。
  • 原文地址:https://www.cnblogs.com/yangchongxing/p/11395683.html
Copyright © 2020-2023  润新知