session一致性-將session儲存到redis中
阿新 • • 發佈:2019-02-13
分散式專案通常部署在不同的伺服器中,而傳統session只儲存在本地伺服器,並不能實現session的跨域使用。因此實現session一致性通常將sessio儲存到redis中,其他伺服器獲取回話資訊直接去redis中獲取。spring-session原理就是講獲取session的方式,從tomcate容器獲取改為了從redis中獲取。
現在spring-session整合redis:
pom.xml:在spring+springmvc基礎上:
<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> <groupId>cn.todd.spring</groupId> <artifactId>webmvc</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.0.RELEASE</version> </dependency> <!--spring-web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.0.0.RELEASE</version> </dependency> <!-- spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.0.0.RELEASE</version> </dependency> <!-- javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.0</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- spring整合spring-session實現快取一致性 --> <!-- spring 整合redis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.session/spring-session-data-redis --> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> <version>1.3.1.RELEASE</version> </dependency> <!--單元測試 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- logback日誌列印--> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <!-- 設定編譯版本為1.8 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
在srpingmvc.xml:新增
<!-- redis配置 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="10" /> <property name="maxTotal" value="5000" /> <property name="testOnBorrow" value="true" /> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="localhost"></property> <property name="port" value="6379"></property> <property name="database" value="2"></property> <property name="usePool" value="true"></property> <property name="poolConfig" ref="poolConfig"></property> </bean> <!-- httpsession配置 --> <bean id="redisHttpSession" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"> <property name="maxInactiveIntervalInSeconds" value="1600"></property> </bean>
web.xml:配置
<!-- springsession配置 --> <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> <servlet> <servlet-name>dispather</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc1.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispather</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
基本完成,測試是不是將session資訊存到redis了:
@Controller
public class ItemsController3 {
@RequestMapping("/query")
public ModelAndView query(HttpServletRequest request){
request.getSession().setAttribute("name", "zhangsan");
ModelAndView mv=new ModelAndView();
mv.setViewName("item");
return mv;
}
}
結果:
我運行了3次,這樣回話就儲存到redis了。取的時候也是直接從redis中取的。
簡單原理: