├── README.md ├── pom.xml └── src └── main ├── java └── com │ └── fei │ └── springboot │ ├── Application.java │ ├── config │ └── RedisSessionConfig.java │ └── controller │ └── TestController.java └── resources ├── application.yml └── logback.xml /README.md: -------------------------------------------------------------------------------- 1 | # spring-boot-redis-session 2 | spring boot学习redis做session缓存的整合 3 | aaaaaabbbbcccc -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | com.fei.springboot 4 | springboot-redis-session 5 | 0.0.1-SNAPSHOT 6 | war 7 | 8 | 9 | 10 | alimaven 11 | http://maven.aliyun.com/nexus/content/repositories/central/ 12 | 13 | true 14 | 15 | 16 | true 17 | 18 | 19 | 20 | 21 | 22 | 23 | alimaven 24 | http://maven.aliyun.com/nexus/content/repositories/central/ 25 | 26 | true 27 | 28 | 29 | true 30 | 31 | 32 | 33 | 34 | 35 | UTF-8 36 | UTF-8 37 | 38 | UTF-8 39 | 40 | 1.5.2.RELEASE 41 | 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-starter-parent 46 | 1.5.2.RELEASE 47 | 48 | 49 | 50 | 51 | org.springframework.boot 52 | spring-boot-starter-web 53 | 54 | 55 | 56 | 57 | org.springframework.boot 58 | spring-boot-starter-tomcat 59 | 60 | 61 | 62 | 63 | 64 | org.springframework.boot 65 | spring-boot-starter-tomcat 66 | 67 | provided 68 | 69 | 70 | 71 | org.springframework.boot 72 | spring-boot-starter-redis 73 | 1.4.6.RELEASE 74 | 75 | 76 | 77 | 78 | org.springframework.session 79 | spring-session-data-redis 80 | 81 | 82 | 83 | 84 | 85 | 86 | src 87 | 88 | 89 | maven-compiler-plugin 90 | 91 | 92 | 1.7 93 | 1.7 94 | UTF-8 95 | 96 | 97 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /src/main/java/com/fei/springboot/Application.java: -------------------------------------------------------------------------------- 1 | package com.fei.springboot; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.boot.builder.SpringApplicationBuilder; 7 | import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; 8 | import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; 9 | import org.springframework.boot.web.support.SpringBootServletInitializer; 10 | import org.springframework.context.annotation.ComponentScan; 11 | 12 | @EnableAutoConfiguration 13 | @ComponentScan(basePackages={"com.fei.springboot"}) 14 | @SpringBootApplication 15 | public class Application extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer{ 16 | 17 | @Override 18 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 19 | //aaa 20 | return application.sources(Application.class); 21 | } 22 | 23 | 24 | public static void main(String[] args) throws Exception { 25 | SpringApplication.run(Application.class, args); 26 | } 27 | 28 | public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) { 29 | // configurableEmbeddedServletContainer.setPort(9090); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/fei/springboot/config/RedisSessionConfig.java: -------------------------------------------------------------------------------- 1 | package com.fei.springboot.config; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; 5 | 6 | @Configuration 7 | //maxInactiveIntervalInSeconds 默认是1800秒过期,这里测试修改为60秒 8 | @EnableRedisHttpSession(maxInactiveIntervalInSeconds=60) 9 | public class RedisSessionConfig{ 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/fei/springboot/controller/TestController.java: -------------------------------------------------------------------------------- 1 | package com.fei.springboot.controller; 2 | 3 | import javax.servlet.http.HttpServletRequest; 4 | 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.ResponseBody; 8 | 9 | @Controller 10 | @RequestMapping("/") 11 | public class TestController { 12 | 13 | @RequestMapping(value="/getSessionId") 14 | @ResponseBody 15 | public String getSessionId(HttpServletRequest request){ 16 | 17 | Object o = request.getSession().getAttribute("springboot"); 18 | if(o == null){ 19 | o = "spring boot 牛逼了!!!有端口"+request.getLocalPort()+"生成"; 20 | request.getSession().setAttribute("springboot", o); 21 | } 22 | 23 | return "端口=" + request.getLocalPort() + " sessionId=" + request.getSession().getId() +"
"+o; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | logging: 2 | config: classpath:logback.xml 3 | path: d:/logs 4 | server: 5 | port: 8080 6 | session-timeout: 60 7 | 8 | spring: 9 | redis: 10 | database: 0 11 | host: 127.0.0.1 12 | port: 6379 13 | password: 14 | timeout: 0 15 | pool: 16 | max-active: 8 17 | max-wait: -1 18 | max-idle: 8 19 | min-idle: 0 20 | session: 21 | store-type: none -------------------------------------------------------------------------------- /src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 |     8 |     9 |       %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n 10 |     11 |   12 | 13 | 14 | ${LOG_PATH}/info.log 15 | 16 | ${LOG_PATH}/info-%d{yyyyMMdd}.log.%i 17 | 18 | 500MB 19 | 20 | 2 21 | 22 | 23 | %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n 24 | 25 | 26 | 27 | 28 | 29 | 30 | ERROR 31 | 32 | ${LOG_PATH}/error.log 33 | 34 | ${LOG_PATH}/error-%d{yyyyMMdd}.log.%i 35 | 36 | 37 | 500MB 38 | 39 | 2 40 | 41 | 42 | %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 53 | 54 | 55 | --------------------------------------------------------------------------------