This is the 20th day of my participation in the Genwen Challenge

Spring-boot integrates spring-session to implement session sharing in mysql

Springboot provides a simple and easy-to-use way to implement session sharing. Let’s take mysql relational database and Redis non-relational database as examples to explain how spring-Session can be used to implement session sharing. Other databases can also be quickly implemented by referring to this chapter

Mysql mode Session sharing

Rely on

<dependency>
	<groupId>org.springframework.session</groupId>
	<artifactId>spring-session-jdbc</artifactId>
</dependency>
Copy the code

And the mysql database connection library

configuration

  1. You need to import the two default tables provided by the official mysql database. Other databases also provide SQL files
  2. Set up theapplication.propetiesAdd configuration tospring.session.store-type=JDBC, the other configurations are as follows:
server.servlet.session.timeout =Session timed out. If no duration suffix is specified, seconds are used.
spring.session.jdbc.initialize-schema = Embedded# Database mode Initialization mode.
spring.session.jdbc.schema = Classpath: organization/springframework/session/JDBC/schema- @@platform @@.sql # path SQL file used to initialize the database schema.
spring.session.jdbc.table-name = SPRING_SESSION # Specifies the name of the database table used to store the session.
Copy the code

Table structure and SQL files

Spring-session-jdbc relies on two tables:

SPRING_SESSION
SPRING_SESSION_ATTRIBUTES
Copy the code

Import two tables into the database through the official provided SQL file:

DROP TABLE IF EXISTS SPRING_SESSION_ATTRIBUTES;
DROP TABLE IF EXISTS SPRING_SESSION;
CREATE TABLE SPRING_SESSION (
    PRIMARY_ID CHAR(36) NOT NULL,
    SESSION_ID CHAR(36) NOT NULL,
    CREATION_TIME BIGINT NOT NULL,
    LAST_ACCESS_TIME BIGINT NOT NULL,
    MAX_INACTIVE_INTERVAL INT NOT NULL,
    EXPIRY_TIME BIGINT NOT NULL,
    PRINCIPAL_NAME VARCHAR(100),
    CONSTRAINT SPRING_SESSION_PK PRIMARY KEY (PRIMARY_ID)
) ENGINE=INNODB ROW_FORMAT=DYNAMIC;

CREATE UNIQUE INDEX SPRING_SESSION_IX1 ON SPRING_SESSION (SESSION_ID);
CREATE INDEX SPRING_SESSION_IX2 ON SPRING_SESSION (EXPIRY_TIME);
CREATE INDEX SPRING_SESSION_IX3 ON SPRING_SESSION (PRINCIPAL_NAME);

CREATE TABLE SPRING_SESSION_ATTRIBUTES (
    SESSION_PRIMARY_ID CHAR(36) NOT NULL,
    ATTRIBUTE_NAME VARCHAR(200) NOT NULL,
    ATTRIBUTE_BYTES BLOB NOT NULL.CONSTRAINT SPRING_SESSION_ATTRIBUTES_PK PRIMARY KEY (SESSION_PRIMARY_ID, ATTRIBUTE_NAME),
    CONSTRAINT SPRING_SESSION_ATTRIBUTES_FK FOREIGN KEY (SESSION_PRIMARY_ID) REFERENCES SPRING_SESSION(PRIMARY_ID) ON DELETE CASCADE
) ENGINE=INNODB ROW_FORMAT=DYNAMIC;
Copy the code

According to the mysql database connection mode configured, spring-Session is running properly after the above steps

Redis mode Session sharing

Redis mode sharing is also very simple, the steps are as follows

Rely on

Adding a dependency library

<dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-redis</artifactId>  
</dependency>  
<dependency>  
        <groupId>org.springframework.session</groupId>  
        <artifactId>spring-session-data-redis</artifactId>  
</dependency>  
Copy the code

configuration

  1. Annotations need to be enabled in SpringBoot@EnableRedisHttpSession
  2. application.propertiesConfigure redis
    spring.redis.host=localhost  
    spring.redis.port=6379  
Copy the code

Spring-session is now running properly, isn’t it?

If you think the blogger wrote well, welcome to “like, collect, pay attention to” one key three even!