负载均衡(上)
来源:互联网 发布:xap格式软件下载 编辑:程序博客网 时间:2024/06/06 03:46
第一:什么是负载均衡呢?
在互联网高速发展的时代,大数据量、高并发等是互联网网站提及最多的。如何处理高并发带来的系统性能问题,最终大家都会使用负载均衡机制。它是根据某种负载策略把请求分发到集群中的每一台服务器上,让整个服务器群来处理网站的请求。
公司比较有钱的,可以购买专门负责负载均衡的硬件(如:F5),效果肯定会很好。对于大部分公司,会选择廉价有效的方法扩展整个系统的架构,来增加服务器的吞吐量和处理能力,以及承载能力。
第二:我今天想说的是Tomcat+Nginx搭建集群,和SpringSession和Redis组合实现Session共享
这篇文章需要亲爱的读者知道:
Nginx的配置和安装
SpringSession的配置和安装
Redis的配置和安装(主要是用java来操作Redis)
1)首先我们要搭建一个环境,SpringMVC+Spring
我们都很清楚,搭建环境有两种,一种maven,一种普通导jar包
我今天用第二种,第一步导jar包
第二步:配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--注意,filter的name必须为springSessionRepositoryFilter--> <filter> <filter-name>springSessionRepositoryFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSessionRepositoryFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping></web-app>
第三步:配置spring-servlet.xml和applicationContext.xml
spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="org.peter" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <mvc:annotation-driven/></beans>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="org.peter" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/> <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/> </context:component-scan> <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/> <bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="192.168.42.128"/> <property name="port" value="6379"/> <property name="database" value="0"/> </bean></beans>
第三步:我们创建一个Controller来做测试
package org.peter.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;/** * Created by sang on 2017/6/14. */@RestControllerpublic class HelloController { @RequestMapping("/hello") public String hello() { return "hello"; } @RequestMapping("/reg") public void reg(HttpSession session, String name) { session.setAttribute("name", name); } @RequestMapping(value = "/login", produces = "text/html;charset=utf-8") public String login(HttpSession session, HttpServletRequest req) { return session.getAttribute("name").toString()+"-------------"+req.getServletContext().getRealPath("/"); }}
第四步:写个jsp的页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>Title</title></head><body><form action="/reg"> <input type="text" name="name"><input type="submit" value="提交"></form><form action="/login"> <input type="submit" value="获取"></form></body></html>
当我向httpsession中写数据时,这些数据会自动存储到redis缓存/数据库中。在返回session数据时我加上了项目部署路径,是为了在部署到集群上之后区分这个请求是由哪台服务器处理的。启动项目,页面如下:
点击提交,会将数据保存到redis 中,如下:
然后再回到起始页面,点击获取按钮,即可以拿到Session中的值,如下:
结果是ok的,到这里我的环境就搭建好。
注意:一定要将你的Redis缓存打开。如果连接出现了错误
在我的博客中有一篇关于redis的安装
- 负载均衡(上)
- 负载均衡技术全攻略(上)
- 负载均衡1.5--案例(上)
- 负载均衡详解第二十九篇:高速缓存负载均衡-部署高速缓存(上)
- 负载均衡—大型网站架构系列:负载均衡详解(上)
- win上模拟负载均衡
- Apache +Jetty的负载均衡与集群配置(上)
- Apache +Jetty的负载均衡与集群配置(上)
- Windows平台分布式架构实践 - 负载均衡(上)
- 大型网站架构系列:负载均衡详解(上)
- 在win2008R2上使用(NLB)网络负载均衡
- Apache +Jetty的负载均衡与集群配置(上)
- 负载均衡(功能)
- 负载均衡(一)
- 负载均衡(简单)
- 负载均衡(算法)
- 负载均衡(一)
- (二)负载均衡
- RecyclerView自定义分割线
- UVALive
- OpenSSL命令详解
- 4招教你“洗白”简历上的致命“污点”!
- Echarts象形图简单示例
- 负载均衡(上)
- [SDUT](3311)数据结构实验之串三:KMP应用 ---KMP(串)
- NYOJ129树的判定
- mui 如何设置只有年份选择器
- IT单词积累
- artDiaLog弹出插件
- Linux第三方库的调用
- mysql优化步骤
- NFS安装实战