spring session代理

因项目要使用分布式,所以需要实现session共享。

1.pom.xml添加

  1. <dependency>  
  2.             <groupId>org.springframework.session</groupId>  
  3.             <artifactId>spring-session-data-redis</artifactId>  
  4.             <version>1.0.2.RELEASE</version>  
  5.         </dependency>  
  6.   
  7.         <!-- 2、Redis -->  
  8.         <dependency>  
  9.             <groupId>org.springframework.data</groupId>  
  10.             <artifactId>spring-data-redis</artifactId>  
  11.             <version>1.4.2.RELEASE</version>  
  12.         </dependency>        
  13.         <dependency>  
  14.             <groupId>redis.clients</groupId>  
  15.             <artifactId>jedis</artifactId>  
  16.             <version>2.5.2</version>  
  17.         </dependency>  
  18.         <dependency>  
  19.                <groupId>org.springframework.session</groupId>  
  20.                <artifactId>spring-session</artifactId>  
  21.                <version>1.0.2.RELEASE</version>  
  22.         </dependency>  
  23.         <dependency>  
  24.               <groupId>org.apache.commons</groupId>  
  25.               <artifactId>commons-pool2</artifactId>  
  26.               <version>2.2</version>  
  27.         </dependency>  

2.web.xml添加(必须放在第一位)

<filter>
<filter-name>springSessionRepositoryFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
<filter-name>springSessionRepositoryFilter</filter-name>
<url-pattern>/*</url-pattern>

</filter-mapping>

3.配置spring.xml

<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
<property name="maxInactiveIntervalInSeconds" value="1800"></property>

</bean>

加上以下这些配置,即把session放入reids库了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中实现反向代理WebSocket的代码实现可以通过使用Spring的WebSocket支持和代理服务器来实现。下面是一个简单的示例代码: 1. 首先,需要在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> ``` 2. 创建一个WebSocket处理器类,用于处理WebSocket连接和消息: ```java import org.springframework.stereotype.Component; import org.springframework.web.socket.CloseStatus; import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.WebSocketHandler; import import org.springframework.web.socket.WebSocketSession; @Component public class MyWebSocketHandler implements WebSocketHandler { @Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { // 连接建立时的处理逻辑 } @Override public void handleMessage(WebSocketSession session, TextMessage message) throws Exception { // 处理收到的消息 } @Override public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception { // 连接关闭时的处理逻辑 } @Override public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception { // 处理传输错误 } } ``` 3. 创建一个WebSocket配置类,用于配置WebSocket相关的Bean: ```java import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.config.annotation.EnableWebSocket; import org.springframework.web.socket.config.annotation.WebSocketConfigurer; import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; @Configuration @EnableWebSocket public class WebSocketConfig implements WebSocketConfigurer { private final MyWebSocketHandler myWebSocketHandler; public WebSocketConfig(MyWebSocketHandler myWebSocketHandler) { this.myWebSocketHandler = myWebSocketHandler; } @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(myWebSocketHandler, "/websocket").setAllowedOrigins("*"); } } ``` 4. 创建一个Spring Boot应用程序的入口类: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 以上代码示例了如何在Spring Boot中实现反向代理WebSocket。你可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值