HTTP Session Repository
Microservice http-session-repository
enables for you option to move your http session from your servlet (Spring Boot) microservice to external unit. This way for example when you restart your servlet microservice which is configured to use http-session-repository
your session will be sustained, as it is stored in memory in external microservice (http-session-repository
).
Configuration servlet microservice
To enable remote sessions put in your Maven configuration additional dependency:
<dependency>
<groupId>com.jlupin.microservice.partofjlupin</groupId>
<artifactId>http-session-repository-plugin</artifactId>
<version>1.1.2</version>
</dependency>
Then put @ComponentScan
annotation to your configuration class:
@ComponentScan("com.jlupin.impl.microservice.partofjlupin.httpsessionrepository.configuration")
And that's all. Your http sessions will be stored now with use of remote microservice.
How to use Http Session
When you define your controller and method to handle proper mapping you can use @Autowired
annotation to get access to created HTTP session, like shown below:
@GetMapping("/")
public String post(@Autowired HttpSession httpSession, @Autowired HttpServletRequest httpServletRequest) {
httpSession.setAttribute("ROOT_VISITED", Boolean.TRUE);
return "hello";
}
@GetMapping("/other")
public String post(@Autowired HttpSession httpSession, @Autowired HttpServletRequest httpServletRequest) {
final Boolean rootVisited = (Boolean) httpSession.get("ROOT_VISITED");
return rootVisited ? "visited" : "not_visited";
}
To see what happend first go to uri /other
. You should see that you have not visited root yet. Then go to /
and get back to /other
. Now you should see that you had visited root. And now the most important moment. Restart your microservice. In standard environment all sessions would be removed and when you visit uri /ohter
you should received information that you have not visited root yet. But because our sessions are stored in http-session-repository
microservice when you enter uri /ohter
you shuld see that you have already visited root uri. It gives opportunity to make for example small visual or UI changes without logging out users.