原文地址: https://www.jianshu.com/p/e7c50f87f64c
京东就是现实世界的CDN,京东的物流很快,这得益于京东发货的时候采用就近原则,从最近的仓库取出货物派发给用户。
修改Main.cc源码
在Main.cc中添加,这样在开发插件的时候就能以root的方式运行traffic_server,拥有绑定1-1023端口的权限
#if !defined(BIG_SECURITY_HOLE)
#define BIG_SECURITY_HOLE 0
#endif
- ATS与Nginx对比
速度方面,具有很强的并发能力,nginx采用的master+work的方式,多线程多进程下一般能开的work不多,而ats采用协程(continuations)的方式,理论上可以随便开,百万级别都不是问题,但是性能肯定会下降,凡是都是双刃剑,所以不说哪个好,适用才是王道。 - 插件的编程语言
该采用c还是c++编写呢?答案肯定是都可以,但是如果用C++的话,需要按C的编译器来编译,而不是C++。 - 开发一个plugin需要满足以下5个条件
1)、确保你的插件源码中包含TSPluginInit初始化函数;
2)、编译源码,创建链接库;
3)、在plugin.config中添加该插件入口;
4)、在record.config中声明插件对应的链接库所在的位置;
5)、重启ATS让配置生效。 - 编译
将源码编译成动态链接库。这里直接通过ATS提供的编译器tsxs进行编译,命令如下:
/usr/local/ats/bin/tsxs-o hello.so -c hello.cpp
7.初始化函数
初始化函数中,需要做的有四件事:创建continuations、绑定数据存放、绑定事件处理的主函数、绑定端口并监听
- 为了测试插件的过程,编写一个脚本,自动编译、自动重启、自动删除日志、自动启动测试客户端、自动关闭ATS,命令如下
/usr/local/ats/bin/tsxs -lpthread -o hello.so -c hello.cpp
sudo /usr/local/ats/bin/tsxs -o hello.so -i
sudo rm -f /usr/local/ats/var/log/trafficserver/*
sudo /usr/local/ats/bin/trafficserver restart
sleep 3s
python /home/carl/httpsend.py &
sleep 9s
sudo /usr/local/ats/bin/trafficserver stop
sleep 1s
TSRemapInit
The Traffic Server remap interface provides a simplified mechanism for plugins to manipulate HTTP transactions. A remap plugin is not global; it is configured on a per-remap rule basis, which enables you to customize how URLs are redirected based on individual rules in the remap.config file. Writing a remap plugin consists of implementing one or more of the remap entry points and configuring the remap.config configuration file to route the transaction through your plugin. Multiple remap plugins can be specified for a single remap rule, resulting in a remap plugin chain where each pligin is given an opportunity to examine the HTTP transaction.
TSRemapInit()
is a required entry point. This function will be called once when Traffic Server loads the plugin. If the optional TSRemapDone()
entry point is available, Traffic Server will call then when unloading the remap plugin.
A remap plugin may be invoked for different remap rules. Traffic Server will call the entry point each time a plugin is specified in a remap rule. When a remap plugin instance is no longer required, Traffic Server will call TSRemapDeleteInstance()
.
TSRemapDoRemap()
is called for each HTTP transaction. This is a mandatory entry point. In this function, the remap plugin may examine and modify the HTTP transaction.