SpringSeesion的学习使用

得意时要看淡,失意时要看开。不论得意失意,切莫大意;不论成功失败,切莫止步。志得意满时,需要的是淡然,给自己留一条退路;失意落魄时,需要的是泰然,给自己觅一条出路SpringSeesion的学习使用,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

一.新建项目

项目结构如下(创建出空项目即可):

SpringSeesion的学习使用

二. 添加依赖

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
            <version>2.1.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <!--Lettuce是 一 个 基 于 Netty的 NIO方 式 处 理 Redis的 技 术 -->
        <dependency>
            <groupId>io.lettuce</groupId>
            <artifactId>lettuce-core</artifactId>
        </dependency>

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

三.添加配置

在每个子模块的yml中添加如下配置:

spring.session.store-type=redis
server.port=8020
spring.redis.host=xxx.xx.xxx.x
spring.redis.port=6379
spring.redis.password=xxx
spring.redis.database=11

spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-wait=30
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.min-idle=0
spring.redis.jedis.timeout=10

四.启动类添加注解@EnableRedisHttpSession

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

@EnableRedisHttpSession
@SpringBootApplication
public class SessionService1Application {

    public static void main(String[] args) {
        SpringApplication.run(SessionService1Application.class, args);
    }

}

五.测试代码

1、在service1、service2分别新建 controller/WebController

        service1:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RequestMapping("/service1")
@RestController
public class WebController {

    @GetMapping("/setMsg")
    public String setMsg(HttpSession session){
        session.setAttribute("msg","hello,SpringSession!");
        return "OK";
    }
}

        service2:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RequestMapping("/service2")
@RestController
public class WebController {
    @GetMapping("/getMsg")
    public String getMsg(HttpSession session){
        String msg = (String)session.getAttribute("msg");
        return msg;
    }
}

六.测试

1、访问localhost:8020/service1/setMsgSpringSeesion的学习使用

 2、访问localhost:8081/service2/getMsg

SpringSeesion的学习使用

 3、在Redis Manager查看

SpringSeesion的学习使用

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/154450.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!