Senior Java interview questions: Distributed Session implementations What?

Contents
I. interviewer psychological analysis
Second, the face questions analysis
2.1 completely without the session
2.2 Tomcat + Redis
2.3 the Spring the session + Redis

Senior Java interview questions: Distributed Session implementations What?
interviewer psychoanalysis

interviewer asks you a bunch dubbo is how to play, you can play on it dubbo the monolithic system ended up distributed system, then the ensuing pile of problems, the biggest problem is the distributed transaction, such as power interfaces, distributed lock after distributed, and the last one is distributed session.
Of course, the problem is far more than a distributed system so that, very much, very high complexity, here only talk about several common problem, but also often ask when interviewing a few.

Face questions profiling
session is what? Browser has a cookie, the cookie exists a period of time, and then are put over each time a retransmission request JSESSIONID special cookie, according to this thing, the server can maintain a session corresponding field, which can put the point data.
In general, then as long as you do not close your browser, cookie is still, so that the corresponding session in, but if not the cookie, session also gone. What is common in the cart or something like that, and save the login state of the class.
This much said, to understand Java all know this.
When monolithic systems play session so no problem, but if you do a distributed system, so many services, maintaining session state where ah?
In fact, many ways, but commonly used are the following:

Session is no need to
use JWT Token storage user, and then obtain additional information from a database or cache. Whether such a request is assigned to which server it does not matter.

tomcat + redis
this fact quite handy, is to use the session code, as before, or session-based support to tomcat native, then it is to use something called TomcatRedisSessionManager, so that all our tomcat deployment will store session data to redis can be.
In the tomcat configuration file is:

<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />

<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
 host="{redis.host}"
 port="{redis.port}"
 database="{redis.dbnum}"
 maxInactiveInterval="60"/>

Redis then specify the host and port ok.

<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
     sentinelMaster="mymaster"
     sentinels="<sentinel1-ip>:26379,<sentinel2-ip>:26379,<sentinel3-ip>:26379"
     maxInactiveInterval="60"/>

The above can also be used in this way to save session data based on redis redis Sentinel supports high availability cluster, is of ok.

spring session + redis
above mentioned second way will be re-coupled with the tomcat container, if I want to migrate to the web container jetty, the jetty is it still possible to re-configure all over again?
Because that way above tomcat + redis easy to use, but will rely heavily on web container, the code will not be portable to other web container up, especially if you changed the technology stack zezheng? For example, replaced by a spring cloud or spring boot like it?
So now is a better one-stop solutions based on Java, which is the spring. People contract the spring basically most of the framework we need to use, spirng cloud services do micro, spring boot made of scaffolding, so use sping session is a good choice.
In pom.xml configuration:

<dependency>
 <groupId>org.springframework.session</groupId>
 <artifactId>spring-session-data-redis</artifactId>
 <version>1.2.1.RELEASE</version>
</dependency>
<dependency>
 <groupId>redis.clients</groupId>
 <artifactId>jedis</artifactId>
 <version>2.8.1</version>
</dependency>

In the spring configuration file is:

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

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
 <property name="maxTotal" value="100" />
 <property name="maxIdle" value="10" />
</bean>
<bean id="jedisConnectionFactory"
 class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
 <property name="hostName" value="${redis_hostname}"/>
 <property name="port" value="${redis_port}"/>
 <property name="password" value="${redis_pwd}" />
 <property name="timeout" value="3000"/>
 <property name="usePool" value="true"/>
 <property name="poolConfig" ref="jedisPoolConfig"/>
</bean>

In web.xml configuration:

<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>

Sample code:

@RestController
@RequestMapping("/test")
public class TestController {

 @RequestMapping("/putIntoSession")
 public String putIntoSession(HttpServletRequest request, String username) {
        request.getSession().setAttribute("name", "leo");
 return "ok";
 }

 @RequestMapping("/getFromSession")
 public String getFromSession(HttpServletRequest request, Model model){
 String name = request.getSession().getAttribute("name");
 return name;
 }
}

The above code is ok, a session configuration based Sping redis to store session data, and then configure the Spring session of a filter, this is the case, the operation will be related to the Spring session to manage the session. Next, in the code, to use native operating session, it is based directly on the spring sesion in the acquired data from redis.
Implementation of Distributed sessions There are many ways, I'm talking about just the more common ways, tomcat + redis early more commonly used, but will be re-coupled to the tomcat; In recent years, achieved by spring session.

Guess you like

Origin blog.51cto.com/14442094/2422168