标签:jar work 服务器 color 目录 tar https 启动 ips
1.nginx或者haproxy做的负载均衡,用nginx做的负载均衡可以添加ip_hash这个配置;用haproxy做的负载均衡可以用balance source这个配置,从而使用一个IP的请求发到同一个服务器;
2.利用数据库同步session;
3.利用cookie同步session数据,但是安全性差,http请求都需要带参增加了带宽消耗;
4.Tomcat配置session共享;
5利用session集群存放Redis;
package com.zn.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/SessionIPServlet") public class SessionIPServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("当前请求端口:"+request.getLocalPort()); String action=request.getParameter("action"); //向Session中存放一个数据 if(action.equals("setSession")){ request.getSession().setAttribute("username","zhangsan"); }else if(action.equals("getSession")){ response.getWriter().write((String)request.getSession().getAttribute("username")); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } }
分别访问8080和8081
访问:http://www.znzn.com/SessionIPServlet?action=setSession
获取:http://www.znzn.com/SessionIPServlet?action=getSession
<dependency> <!-- spring-boot-starter-web是为我们提供了包括mvc,aop等需要的一些jar --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- 因为我们已经配置了 parent 中的version 所以这里不需要指定version了 --> </dependency> <!--spring boot 与redis应用基本环境配置 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency> <!--spring session 与redis应用基本环境配置,需要开启redis后才可以使用,不然启动Spring boot会报错 --> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> </dependency>
package com.zn.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
@RestController
public class MyController {
//存放Session值
@RequestMapping("/setSession")
public String setSession(HttpServletRequest request){
request.getSession().setAttribute("username","zhangsan");
return "success";
}
//获取Session值
@RequestMapping("/getSession")
public String getSession(HttpServletRequest request){
return (String)request.getSession().getAttribute("username");
}
}
package com.zn;
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);
}
}
存入数据是已将数据存放到redis缓存中
标签:jar work 服务器 color 目录 tar https 启动 ips
原文地址:https://www.cnblogs.com/Zzzzn/p/12299333.html