11 Ocak 2023 Çarşamba

SpringSession Redis

Giriş
Açıklaması şöyle
How does this work?
1. We inform Spring that sessions will now be cached in Redis.
2. Spring receives a request.
3. Spring Security kicks in and user is authenticated.
4. Spring Session object is serialized and saved in the cache.
5. Client gets a cookie with the Session ID.
6. Client then sends the session id for further requests.
7. Any instance of the UI Service will check in the cache for a session object against the Session ID provided by the client.
8. Session object is de-serialized and reused.
Gradle
Şöyle yaparız
dependencies {
  compile 'org.springframework.boot:spring-boot-starter-data-redis'
  compile("org.springframework.boot:spring-boot-starter-cache")
  implementation('org.springframework.session:spring-session-data-redis')
}
1. @EnableRedisHttpSession Anotasyonu
Açıklaması şöyle
This annotation when parsed, creates a Spring Bean with the name of springSessionRepositoryFilter that implements Filter. The filter is in charge of replacing the HttpSession implementation to be backed by Spring Session. In this instance, Spring Session is backed by Redis.
Örnek
Şöyle yaparız
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

@EnableRedisHttpSession
@EnableEurekaClient
@SpringBootApplication
public class UIApplication {

  public static void main(String[] args) {
    SpringApplication.run(UIApplication.class, args);
  }
}
2. application.properties
Şöyle yaparız
spring.cache.type=redis
spring.redis.host=<ip-address>
spring.redis.port=<Redis port>



Hiç yorum yok:

Yorum Gönder