• Stars
    star
    107
  • Rank 314,493 (Top 7 %)
  • Language
    C++
  • Created over 5 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

《Linux 高性能服务器》附带的项目程序springsnil详细解读,一个负载均衡服务器

springsnail

《Linux 高性能服务器》附带的项目程序详细解读

安装及使用

下载源码:

git clone https://github.com/liu-jianhao/springsnail.git

然后进入springsnil目录直接make即可生成可执行文件

填写配置文件,我测试的是网站是网易云音乐,首先我先看看网易云音乐服务器的ip有哪几个:

$ nslookup music.163.com
Server:         8.8.8.8
Address:        8.8.8.8#53

Non-authoritative answer:
Name:   music.163.com
Address: 103.65.41.125
Name:   music.163.com
Address: 103.65.41.126

可以看出网易云音乐的网站有两个ip地址,我配置文件中的逻辑地址就用这两个地址

config.xml:

Listen 127.0.0.1:8080

<logical_host>
  <name>103.65.41.126</name>
  <port>80</port>
  <conns>2</conns>
</logical_host>
<logical_host>
  <name>103.65.41.125</name>
  <port>80</port>
  <conns>2</conns>
</logical_host>

第一行是负载均衡服务器的地址,下面两个则是真正的服务器,spingsnil只是起到一个中转站的作用,将客户端的连接转发给比较“闲”的服务器

接下来开始运行程序:

$ ./springsnail -f config.xml
[ 09/13/18 21:49:56 ] mgr.cpp:0050 info: logcial srv host info: (103.65.41.126, 80)
[ 09/13/18 21:49:56 ] mgr.cpp:0050 info: logcial srv host info: (103.65.41.125, 80)
[ 09/13/18 21:49:58 ] mgr.cpp:0062 info: build connection 0 to server success
[ 09/13/18 21:49:59 ] mgr.cpp:0062 info: build connection 0 to server success
[ 09/13/18 21:49:59 ] mgr.cpp:0062 info: build connection 1 to server success
[ 09/13/18 21:50:00 ] mgr.cpp:0062 info: build connection 1 to server success

没有出错服务器就已经在运行了,然后我尝试连接该负载均衡服务器:

$ nc localhost 8080
GET / HTTP/1.1
Host: music.163.com

HTTP/1.1 302 Found
Server: nginx
Date: Thu, 13 Sep 2018 13:52:34 GMT
Content-Length: 0
Connection: keep-alive
Cache-Control: no-store
Pragrma: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Cache-Control: no-cache
Location: https://music.163.com/
X-Via: MusicEdgeServer
X-From-Src: 218.17.40.86

而另一端服务器则会有类似以下的输出:

[ 09/13/18 21:52:24 ] processpool.h:0378 info: send request to child 0
[ 09/13/18 21:52:24 ] mgr.cpp:0109 info: bind client sock 11 with server sock 9

我采用模拟HTTP报文发送给负载均衡服务器,服务器也确实有返回数据回来,我也用浏览器直接访问,但是会出现403编码,表示禁止访问,可能服务器出于安全的考虑,禁止这样的中转站的存在,所以不能让客户端访问(纯属猜测)

大家也可以试试其他的网站,每个网站的结果都有些不一样

各个文件的作用

  1. main.cpp:

    1. 设置命令行参数的处理
    2. 读取配置文件
    3. 解析配置文件
    4. 开始线程池的循环
  2. processpool: 线程池,是整个项目的发动机

  3. fdwrapper:操作fd的包裹函数

  4. log:日志

  5. conn:客户端

  6. mgr:处理网络连接和负载均衡的框架

More Repositories

1

Cpp-Design-Patterns

C++设计模式
C++
2,187
star
2

CMU-15-445

CMU的数据库系统课程
C
236
star
3

chatRoom

从零开始实现一个聊天室客户端(用qt实现,在Windows和Linux环境下都行),和在Linux下实现服务器后台
C++
218
star
4

Interview

程序员代码面试指南C++版,更新中
Java
116
star
5

yfsCpp11

yfs是一个分布式文件系统(mit6.824)
C++
69
star
6

chitchat

《Go Web 编程》中chitchat博客项目的学习(添加详细中文注释)
Go
48
star
7

Raft-Paxos-Sample

MIT6.824实现分布式一致性算法——Raft&Paxos
Go
44
star
8

simpleFS

基于内存的简易文件系统实现
C++
24
star
9

TinyWebServer

CMU 15-441 项目一 Liso Web服务器
C
18
star
10

linux_learing

unix/Linux系统编程手册
C
15
star
11

Cracking-the-Coding-Interview

程序员面试金典(第6版)Go、C++、Rust题解
C++
12
star
12

HoneyBadgerBFT

一个Byzantine容错的分布式共识协议研究与实现
Go
12
star
13

learn_system_programming_in_rust

通过系统编程学习Rust
Rust
11
star
14

Rust_learning

Rust入门
Rust
6
star
15

high-performance-server

学习高性能服务器及muduo库的解析
C++
6
star
16

Shell-Scripting-Learning

Linux Shell 脚本攻略第三版学习笔记
Shell
3
star
17

cos461

普利斯顿大学的计算机网络课程
Go
3
star
18

talent-plan

my solution for PingCAP training courses
Go
3
star
19

CS144

我的CS144课程学习记录及解决
3
star
20

gopl_answers

《Go程序设计语言》练习答案
Go
3
star
21

note

学习笔记
C++
3
star
22

MIT6.828-2014

MIT6.828-2014实验笔记
C
3
star
23

ProgrammingErlang

Erlang程序设计(第2版)读书笔记,包含源代码和课后练习答案
Erlang
2
star
24

go-leveldb

Go语言实现leveldb学习版
Go
1
star
25

CS110L

CS 110L: Safety in Systems Programming
C
1
star
26

mini_distributed_system

实现一个简单的分布式系统,不用任何框架
Go
1
star
27

geek_time

极客时间学习笔记
1
star
28

leetcode

my leetcode solution, C++,Go,python,Rust
C++
1
star
29

crawl_funds

用Go语言实现基金的爬取
Go
1
star
30

CandCPP_programing

leetcode
C
1
star
31

naivechain

区块链的简单实现
C++
1
star
32

token_bucket

几十行代码实现令牌桶算法
Go
1
star