springboot+redis集群实现redis集群状态下的session共享

springboot+redis的案例很多,但是redis集群的案例很少,so~笔者在这里记录一下~


一、springboot依靠@EnableRedisHttpSession来开启spring session支持,该注解是由spring-session-data-redis提供的,所以在pom.xml文件中添加如下依赖:

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

二、配置@EnableRedisHttpSession来开启spring session支持

方法一:在springboot启动类上加上@EnableRedisHttpSession来开启spring session支持,其中maxInactiveIntervalInSeconds 的值是session过期时间

方法二:重建redisSession管理类,加上EnableRedisHttpSession配置

@Configuration  

@EnableRedisHttpSession  (maxInactiveIntervalInSeconds = 1800)

public class RedisSessionConfig { 


} 

三、在配置文件(application.properties文件或者yaml文件)中配置redis服务器的位置

spring.redis.cluster.nodes=Centos6701:6379,Centos6701:6380,Centos6702:6380,Centos6702:6379,Centos6703:6380,Centos6703:6379
yaml:
 spring:  
   redis:  
     cluster:  
       nodes:  
        - Centos6701:6379  
        - Centos6701:6380
        - Centos6702:6380
        - Centos6702:6379 
        - Centos6703:6379 
        - Centos6703:6380

四、测试

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

2、定义一个Controller

@RestController  

@RequestMapping(value = "/admin/v1")  

public class QuickRun {  


    @RequestMapping(value = "/first", method = RequestMethod.GET)  

    public Map<String, Object> firstResp (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("map"));  

        return map;  
    }  

} 

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

{"request Url":"http://localhost:8080/admin/v1/first"}

接着访问8080端口的sessions,返回

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

最后访问9090端口的sessions,返回

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

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

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

{"request Url":"http://localhost:9090/admin/v1/first"} 

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

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

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

本次的redis并没有进行主从、读写分离、使用zookeeper进行负载均衡等等配置,而且nginx的单点故障也是应用的障碍。

参考文档:

https://www.cnblogs.com/mengmeng89012/p/5519698.html

https://blog.csdn.net/qq_32143169/article/details/78183325

https://blog.csdn.net/qq_32143169/article/details/78183263

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值