Skip to content

Commit

Permalink
Spring Session Redis test
Browse files Browse the repository at this point in the history
  • Loading branch information
easonjim committed Nov 10, 2017
1 parent 7d80732 commit 47fa98c
Showing 7 changed files with 212 additions and 0 deletions.
1 change: 1 addition & 0 deletions springboottest/springboottest7/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target/
68 changes: 68 additions & 0 deletions springboottest/springboottest7/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.jsoft.springboottest</groupId>
<artifactId>springboottest1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>springboottest1</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.7.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<!-- Add typical dependencies for a web application -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- 热部署模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional> <!-- 这个需要为 true 热部署才有效 -->
</dependency>

<!-- Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<!-- Session -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>

</dependencies>

<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.jsoft.springboottest.springboottest1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* Hello world!
*
*/
@SpringBootApplication
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.jsoft.springboottest.springboottest1;

import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 60 * 60 * 24)
public class SessionConfig {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.jsoft.springboottest.springboottest1.controller;

import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

private static final Logger logger = LoggerFactory.getLogger(TestController.class);

@Autowired
RedisTemplate redisTemplate;

@Autowired
StringRedisTemplate stringRedisTemplate;

@RequestMapping("/set")
public void set() {
redisTemplate.opsForValue().set("test", "4321");
}

@RequestMapping("/show")
public String show(){

logger.info(redisTemplate.opsForValue().get("test").toString());
return "Hello World";
}

@RequestMapping("/uid")
public String uid(HttpSession session) {
UUID uid = (UUID) session.getAttribute("uid");
if (uid == null) {
uid = UUID.randomUUID();
}
session.setAttribute("uid", uid);
return session.getId();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# REDIS\uff08RedisProperties\uff09
# \uff08\u666e\u901a\u96c6\u7fa4\uff0c\u4e0d\u4f7f\u7528\u5219\u4e0d\u7528\u5f00\u542f\uff09\u5728\u7fa4\u96c6\u4e2d\u6267\u884c\u547d\u4ee4\u65f6\u8981\u9075\u5faa\u7684\u6700\u5927\u91cd\u5b9a\u5411\u6570\u76ee\u3002
# spring.redis.cluster.max-redirects=
# \uff08\u666e\u901a\u96c6\u7fa4\uff0c\u4e0d\u4f7f\u7528\u5219\u4e0d\u7528\u5f00\u542f\uff09\u4ee5\u9017\u53f7\u5206\u9694\u7684\u201c\u4e3b\u673a\uff1a\u7aef\u53e3\u201d\u5bf9\u5217\u8868\u8fdb\u884c\u5f15\u5bfc\u3002
# spring.redis.cluster.nodes=
# \u8fde\u63a5\u5de5\u5382\u4f7f\u7528\u7684\u6570\u636e\u5e93\u7d22\u5f15\u3002
spring.redis.database=0
# \u8fde\u63a5URL\uff0c\u5c06\u8986\u76d6\u4e3b\u673a\uff0c\u7aef\u53e3\u548c\u5bc6\u7801\uff08\u7528\u6237\u5c06\u88ab\u5ffd\u7565\uff09\uff0c\u4f8b\u5982\uff1aredis://user:password@example.com:6379
spring.redis.url=
# Redis\u670d\u52a1\u5668\u4e3b\u673a\u3002
spring.redis.host=localhost
# \u767b\u5f55redis\u670d\u52a1\u5668\u7684\u5bc6\u7801\u3002
spring.redis.password=
# \u542f\u7528SSL\u652f\u6301\u3002
spring.redis.ssl=false
# \u6c60\u5728\u7ed9\u5b9a\u65f6\u95f4\u53ef\u4ee5\u5206\u914d\u7684\u6700\u5927\u8fde\u63a5\u6570\u3002\u4f7f\u7528\u8d1f\u503c\u65e0\u9650\u5236\u3002
spring.redis.pool.max-active=8
# \u6c60\u4e2d\u201c\u7a7a\u95f2\u201d\u8fde\u63a5\u7684\u6700\u5927\u6570\u91cf\u3002\u4f7f\u7528\u8d1f\u503c\u8868\u793a\u65e0\u9650\u6570\u91cf\u7684\u7a7a\u95f2\u8fde\u63a5\u3002
spring.redis.pool.max-idle=8
# \u8fde\u63a5\u5206\u914d\u5728\u6c60\u88ab\u8017\u5c3d\u65f6\u629b\u51fa\u5f02\u5e38\u4e4b\u524d\u5e94\u8be5\u963b\u585e\u7684\u6700\u957f\u65f6\u95f4\u91cf\uff08\u4ee5\u6beb\u79d2\u4e3a\u5355\u4f4d\uff09\u3002\u4f7f\u7528\u8d1f\u503c\u53ef\u4ee5\u65e0\u9650\u671f\u5730\u963b\u6b62\u3002
spring.redis.pool.max-wait=-1
# \u76ee\u6807\u4e3a\u4fdd\u6301\u5728\u6c60\u4e2d\u7684\u6700\u5c0f\u7a7a\u95f2\u8fde\u63a5\u6570\u3002\u8fd9\u4e2a\u8bbe\u7f6e\u53ea\u6709\u5728\u6b63\u9762\u7684\u60c5\u51b5\u4e0b\u624d\u6709\u6548\u679c\u3002
spring.redis.pool.min-idle=0
# Redis\u670d\u52a1\u5668\u7aef\u53e3\u3002
spring.redis.port=6379
# \uff08\u54e8\u5175\u6a21\u5f0f\uff0c\u4e0d\u4f7f\u7528\u5219\u4e0d\u7528\u5f00\u542f\uff09Redis\u670d\u52a1\u5668\u7684\u540d\u79f0\u3002
# spring.redis.sentinel.master=
# \uff08\u54e8\u5175\u6a21\u5f0f\uff0c\u4e0d\u4f7f\u7528\u5219\u4e0d\u7528\u5f00\u542f\uff09\u4e3b\u673a\uff1a\u7aef\u53e3\u5bf9\u7684\u9017\u53f7\u5206\u9694\u5217\u8868\u3002
# spring.redis.sentinel.nodes=
# \u4ee5\u6beb\u79d2\u4e3a\u5355\u4f4d\u7684\u8fde\u63a5\u8d85\u65f6\u3002
spring.redis.timeout=0
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.jsoft.springboottest.springboottest1;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}

/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}

/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}

0 comments on commit 47fa98c

Please sign in to comment.