springboot集成springsession利用redis来实现session共享

在spring boot的文档中,告诉我们添加@EnableRedisHttpSession来开启spring session支持,配置如下:

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

@SpringBootApplication
@EnableRedisHttpSession
public class SpringbootRedisSessionApplication {

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

}

而@EnableRedisHttpSession这个注解是由spring-session-data-redis提供的,所以在pom.xml文件中添加:

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

接下来,则需要在application.properties中配置redis服务器的位置了,在这里,我们就用本机:

spring.redis.host=localhost
spring.redis.port=6379
spring.redis.database=0

这样以来,最简单的spring boot + redis实现session共享就完成了,下面进行下测试。

首先我们开启两个tomcat服务,端口分别为8080和9090,在application.properties中进行设置

server.port=8090

接下来定义一个Controller:

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

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: qh
 * @Date: 2019/3/5 10:10
 * @Description:
 */
@RestController
@RequestMapping(value = "/admin/v1/")
public class HomeController {

    @RequestMapping(value = "index", method = RequestMethod.GET)
    public Map<String, Object> index(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        request.getSession().setAttribute("request url", request.getRequestURL());
        map.put("request url", request.getRequestURL());
        return map;
    }

    @RequestMapping(value = "sessions", method = RequestMethod.GET)
    public Object sessions(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put("sessionId", request.getSession().getId());
        map.put("message", request.getSession().getAttribute("request url"));
        return map;
    }
}

启动之后进行访问测试,首先访问8090端口的tomcat,返回

{"request url":"http://localhost:8090/admin/v1/index"}

接着,我们访问8090端口的sessions,返回:

{"sessionId":"01452b8e-69d2-421e-b116-e77d3a191b12","message":"http://localhost:8090/admin/v1/index"}

最后,再访问8085端口的sessions,返回:

{"sessionId":"01452b8e-69d2-421e-b116-e77d3a191b12","message":"http://localhost:8090/admin/v1/index"}

可见,8090与8085两个服务器返回结果一样,实现了session的共享

如果此时再访问8085端口的first的话,首先返回:

{"request Url":"http://localhost:8085/admin/v1/index"}  

而两个服务器的sessions都是返回:

{"sessionId":"efcc85c0-9ad2-49a6-a38f-9004403776b5","message":"http://localhost:8085/admin/v1/index"} 

通过spring boot + redis来实现session的共享非常简单,而且用处也极大,配合nginx进行负载均衡,便能实现分布式的应用了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值