006.Spring Boot Session Redis Sa
2017-03-14 本文已影响0人
airkisser
一、目录结构
目录结构二、pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.airkisser</groupId>
<artifactId>spring-boot-sample-session-redis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>spring-boot-sample-session</name>
<description>Spring boot session redis sample</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>${artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
三、application.properties
# 根据实际情况设置redis的host,port,password
# 此案例中redis无密码,host为localhost,port为6379
spring.redis.host=localhost
spring.redis.port=6379
四、java
Application.java
package com.airkisser;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
HttpSessionConfig.java
package com.airkisser.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
@Configuration
// 创建实现Filter的类,名为"springSessionRepositoryFilter"的Bean
@EnableRedisHttpSession
public class HttpSessionConfig {
}
HelloController.java
package com.airkisser.web;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
@Controller
public class HelloController {
@RequestMapping(value = "/", method = RequestMethod.GET)
@ResponseBody
public Map<String, Object> hello(HttpServletRequest request) {
String ip = getClientIp(request);
Map<String, Object> map = new HashMap<String, Object>();
map.put("sessionId", request.getSession().getId());
map.put("ip", ip);
System.out.println(ip + "\t" + request.getSession().getId());
return map;
}
/**
* 在很多应用下都可能有需要将用户的真实IP记录下来,这时就要获得用户的真实IP地址,在JSP里,获取客户端的IP地
* 址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的。但是在通过了Apache,Squid等
* 反向代理软件就不能获取到客户端的真实IP地址了。
* 但是在转发请求的HTTP头信息中,增加了X-FORWARDED-FOR信息。用以跟踪原有的客户端IP地址和原来客户端请求的服务器地址。
*/
public static String getClientIp(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
if (!StringUtils.isEmpty(ip)) {
ip = ip.split(",")[0];
}
return ip;
}
}
五、启动Redis
Redis启动界面六、分别部署该应用到端口为8080和8090的Tomcat服务器上,并启动这两个Tomcat
两个Tomcat服务器七、配置Nignix负载均衡并启动Nginx
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream nginx_tomcat{
#ip_hash;
# 可以通过weight给不同的服务器分配不同的访问量,
# 默认情况下是1:1,即nginx会将他拦截到的请求以2:1的比例分配给对应的服务器
server 127.0.0.1:8080 weight=2;
server 192.168.0.104:8090 weight=1;
}
server {
# 侦听80端口
listen 80;
server_name localhost;
location / {
# 设置主机头和客户端真实地址,以便服务器获取客户端真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 禁用缓存
proxy_buffering off;
# 反向代理的地址
proxy_pass http://nginx_tomcat;
}
}
}
八、结果
在浏览器上输入http://localhost
或http://127.0.0.1
或http://192.168.0.104
多次,发现两个服务器都接收到了请求。