玩轉spring boot——負載均衡與session共享

前言

 

當項目上線後,如果要修復bug或擴充功能,都需要重啓tomcat服務。此時,正在使用應用的用戶們就需要等待服務器的重啓,而這就會造成不好的用戶體驗。還有,當僅僅只有一臺tomcat服務時,如果CPU或內存達到極限,就會很難頂住壓力。而負載均衡就是解決這些問題的方案。

 項目的演化如下:

 

由一臺單tomcat服務器淨化到多臺服務器組成的集羣。

圖中的nginx作爲反向代理的負載均衡服務器,nginx把請求轉發到局域網內的tomcat服務器上。

 中間的session共享,需要使用redis來實現。

準備工作

pom.xml:

複製代碼
<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.github.carter659</groupId>
    <artifactId>spring11</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
    </parent>

    <name>spring11</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
複製代碼

 

MainController.java:

MainController.java

 

App.java:

App.java

 

index.html:

index.html

 

 

一、負載均衡


 

首先,進去src目錄後,maven打包:mvn package

啓動兩個spring boot mvc應用實例,分別配置8080和8081端口:

java -jar -Dserver.port=8080 target/spring11-0.0.1-SNAPSHOT.jar
java -jar -Dserver.port=8081 target/spring11-0.0.1-SNAPSHOT.jar

 

接着,下載nginx

修改nginx的配置文件“conf/nginx.conf”

加入如下代碼:

複製代碼
upstream tomcat {
		server localhost:8080;
		server localhost:8081;
	}

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
		proxy_redirect off;
		proxy_pass http://tomcat;
        }
}
複製代碼
upstream節點爲負載均衡配置
下面指定了兩個server實例,對於localhost的8080和8081端口
server節點監聽80端口,會跳轉到反向代理的名稱爲協議“http://tomcat”的upstream上
這裏的“http://tomcat”,要與upstream設置的名稱相同

運行nginx.exe:

輸入http://localhost網址

發現,在訪問80端口的網站時,會顯示8080和8081的本地端口。這樣,負載均衡就輕鬆實現了。不過此時,並未實現session共享

 

二、sesstion共享


 

 

spring boot的session共享相當簡單,只需要添加maven依賴即可,其他地方不需要改動

        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
        </dependency>

 

接下來,啓動redis-server.exe:

 

 

再次刷新http://localhost網址測試:

 

此時,sessionId保存不變,也能夠修改session的值了。

 

補充:由於我測試環境是自己的電腦,如果部署到其他環境,需要在“application.properties”中修改redis的host、端口和密碼。

 

總結


 

由此看出spring boot在管理session上非常方便。在日常運維中,只需要擴展mvc應用的實例便可以實現負載。通常在服務器中,會跑一個檢測cpu和內存的運維程序,當發現CPU或內存佔用率高的時候,自動調用雲服務商(如阿里雲)的sdk來創建若干個新的服務器;當cpu內存降下來後,再去把之前創建的服務器釋放掉,就能實現彈性計算。同理也可以結合docker,這樣實現的會更完美些。

 

代碼:https://github.com/carter659/spring-boot-11.git

 

如果你覺得我的博客對你有幫助,可以給我點兒打賞,左側微信,右側支付寶。

有可能就是你的一點打賞會讓我的博客寫的更好:)

 


 

作者:劉冬.NET博客地址:http://www.cnblogs.com/GoodHelper/

原文地址 : http://www.cnblogs.com/GoodHelper/p/6263240.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章