负载均衡(上)

第一:什么是负载均衡呢?

在互联网高速发展的时代,大数据量、高并发等是互联网网站提及最多的。如何处理高并发带来的系统性能问题,最终大家都会使用负载均衡机制。它是根据某种负载策略把请求分发到集群中的每一台服务器上,让整个服务器群来处理网站的请求。
公司比较有钱的,可以购买专门负责负载均衡的硬件(如: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.
 */
@RestController
public 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的安装


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值