springboot2.0.2 + redis + spring-session sharing problem solving session

Ready to work

New versions of two springboot2.0.2 service configuration file to add:

#在默认设置下,Eureka服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

Profiles need to add more than two configurations, or will report the following error:

com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server

Integrated redis

Add Maven dependence:

<!--spring boot 与redis应用基本环境配置 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--spring session 与redis应用基本环境配置,需要开启redis后才可以使用,不然启动Spring boot会报错 -->
<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-data-redis</artifactId>
</dependency>
<!--jedis -->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>

We must add a third rely on, or will report the following error:

Caused by: java.lang.NoClassDefFoundError: redis/clients/jedis/Tuple

Add redis configuration information:

# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=localhost
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=500

** Note: For each version of springboot redis integration, reference springboot in various versions of the configuration issues redis

New sessionConfig class

//这个类用配置redis服务器的连接
//maxInactiveIntervalInSeconds为SpringSession的过期时间(单位:秒)
@EnableRedisHttpSession(maxInactiveIntervalInSeconds= 1800)
public class SessionConfig {

    //冒号后的值为没有配置文件时,制动装载的默认值
    @Value("${spring.redis.host:localhost}")
    String HostName;
    @Value("${spring.redis.port:6379}")
    int Port;
    @Bean
    public JedisConnectionFactory connectionFactory() {
        JedisConnectionFactory connection = new JedisConnectionFactory();
        connection.setPort(Port);
        connection.setHostName(HostName);
        return connection;
    }
}

Main test class by adding a test interface (port number: 8763):

@SpringBootApplication
@EnableEurekaClient
@RestController
public class ServerTestApplication {

   public static void main(String[] args) {
       SpringApplication.run(ServerTestApplication.class, args);
   }

   @RequestMapping(value="/",method = RequestMethod.GET)
   public String setSession(HttpServletRequest request){
       Map<String,Object> map = new HashMap();
       map.put("name","超级管理员");
       map.put("account","admin");
       request.getSession().setAttribute("userSession",map);
       String sessionId = request.getSession().getId();
       return sessionId;
   }
}

Return result:

4c0fe3e3-87cc-4991-acae-8bca2645705a

adding a test test1 main class (port number: 8764)

@SpringBootApplication
@EnableEurekaClient
@RestController
public class ServerTest1Application {

   public static void main(String[] args) {
       SpringApplication.run(ServerTest1Application.class, args);
   }

   @RequestMapping(value="/",method = RequestMethod.GET)
   public Map<String,Object> getSession(HttpServletRequest request){
       String sessionId = request.getSession().getId();
       Object obj = request.getSession().getAttribute("userSession");
       Map<String,Object> map = new HashMap();
       map.put("sessionId",sessionId);
       map.put("user",obj);
       return map;
   }
}

Return result:

<Map>
  <sessionId>4c0fe3e3-87cc-4991-acae-8bca2645705a</sessionId>
  <user>
    <name>超级管理员</name>
    <account>admin</account>
  </user>
</Map>

As can be seen from the above results, access to the port services sessionId 8763 information and information access sessionId 8764 port services agreement

Original Address: https: //www.jianshu.com/p/f0a069d2a85b

Guess you like

Origin www.cnblogs.com/jpfss/p/11016400.html