spring boot + redis + spring session + Nginx achieve load balancing session sharing

About Nginx presentation I do not understand can look my previous blog using Nginx load balancing under Linux which explain what is and how to use Nginx.

Problem Description: In the current micro + Distributed service most of us will face a problem: for example, the client sends a request to Nginx, Nginx distributed to tomcatA above according to their load balancing strategy, and then to save session data on a tomcatA . After the client has issued a request, Nginx given to tomcatB, in this case the tomcatB to read data before session data is not found. To solve this problem we need to do session sharing. That no matter under which tomcat have access to our public session in order to read the data.

The solution: It is simply the first store session data stored directly into redis in each subsequent read data are read directly from the redis, so even the different services can share the session. This solution can be achieved manually by the developer, that exist manual, read the manual, without a doubt, slightly larger workload.

Simplified scheme is implemented by spring session. Spring session will operate all of the session intercepted, the data is automatically synchronized to redis, or read from the redis. The actual operation is as follows.

Premise: install redis, install Nginx

First introduced depend on (among some of the code I was testing directly in the Demo before a spring boot is not necessary here so I just posted some code about this test)

        <!--redis 配置-->
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>
		<!--redis session 共享-->
		<dependency>
		    <groupId>org.springframework.session</groupId>
		    <artifactId>spring-session-data-redis</artifactId>
		</dependency>

application.yml add redis configuration

spring:
  redis:
       database: 0
 # Redis服务器地址
       host: 127.0.0.1
 # Redis服务器连接端口
       port: 6379  
 # Redis服务器连接密码(默认为空)
       password:  
 # 连接池最大连接数(使用负值表示没有限制)
       pool: 
         max-active: 8  
 # 连接池最大阻塞等待时间(使用负值表示没有限制)
         max-wait: -1  
 # 连接池中的最大空闲连接
         max-idle: 8  
 # 连接池中的最小空闲连接
         min-idle: 0  
       cluster:
         max-redirects: 10
         nodes: 127.0.0.1:6080
 # 连接超时时间(毫秒)
       timeout: 2000

For example, my project called SpringBootDemo, a direct copy version SpringBootDemo2 change my port number, I was a 8080 to a 8081, specifically as

 

controller1 the test code

	/**
	 * http://localhost:8080/login/setSession 
	 * @param request
	 * @return
	 */
	@RequestMapping("/setSession")
	@ResponseBody
	public String login(HttpServletRequest request) {
		request.getSession().setAttribute("zhoukaishun", request.getSession().getId());
		return request.getSession().getId()+"---端口8080下set Session";
	}
	/**
	 * http://localhost:8080/login/getSession 
	 * @param request
	 * @return
	 */
	@RequestMapping("/getSession")
	@ResponseBody
	public String getSession(HttpServletRequest request) {
		return request.getSession().getAttribute("zhoukaishun")+"---端口8080下get Session";
	}

controller2 the test code (controller1 controller2 and belong to two services, Edition 2 is a copy of minor modifications to facilitate testing)

/**
	 * http://localhost:8081/login/setSession 
	 * @param request
	 * @return
	 */
	@RequestMapping("/setSession")
	@ResponseBody
	public String login(HttpServletRequest request) {
		request.getSession().setAttribute("zhoukaishun", request.getSession().getId());
		return request.getSession().getId()+"---端口8081下set Session";
	}
	/**
	 * http://localhost:8081/login/getSession 
	 * @param request
	 * @return
	 */
	@RequestMapping("/getSession")
	@ResponseBody
	public String getSession(HttpServletRequest request) {
		return request.getSession().getAttribute("zhoukaishun")+"---端口8081下get Session";
	}

Nginx.conf Nginx configuration under the install directory under the conf

upstream showing the configuration of the upstream server
mydata represents the name of the server cluster (name free to take, and the following correspondence can)
upstream inside configuration is one of the separate service
weight represents the weight of the service weight, which means will have on what percentage of requests from Nginx forwarded to the service
location in proxy_pass represented request forwarding address, / represents intercept all requests immediately forwarded to the configured forwarding service cluster
proxy_redirect response header data representing the setting time occurs when the redirection request, automatic correction Nginx ( the default is to return a redirect Tomcat, then Tomcat redirection address is an address, we need to modify it to make it address the Nginx, if you use "default" parameter settings will be determined according to location and proxy_pass parameters)

Start Nginx, start redis, started two projects

First enter localhost: 80 / login / setSession

Then enter localhost: 80 / login / getSession

Repeatedly refresh the page can be drawn from 8080 port services and port services alternately cut 8081 session values ​​appear the same, redis in

Reference blog: https://blog.csdn.net/Websphere_zxf/article/details/97764860

发布了20 篇原创文章 · 获赞 58 · 访问量 6万+

Guess you like

Origin blog.csdn.net/zks_4826/article/details/98050863