Commit 541d7f71 authored by Dobrosław Żybort's avatar Dobrosław Żybort

Update: clean and fix docs markdown

parent fdb94aeb
##What is hot update?
## What is hot update?
If you have used nginx, you may know that nginx supports hot update, which means you can update your nginx without stopping and restarting it. It serves old connections with old version, and accepts new connections with new version. Notice that hot compiling is different from hot update, where hot compiling is monitoring your source files and recompile them when the content changes, it requires stop and restart your applications, `bee start` is a tool for hot compiling.
##Is hot update necessary?
## Is hot update necessary?
Some people says that hot update is not as useful as its cool name. In my opinion, this is absolutely necessary because zero-down server is our goal for our services. Even though sometimes some errors or hardware problems may occur, but it belongs to design of high availability, don't mix them up. Service update is a known issue, so we need to fix this problem.
##How Beego support hot update?
## How Beego support hot update?
The basic principle of hot update: main process fork a process, and child process execute corresponding programs. So what happens? We know that after forked a process, main process will have all handles, data and stack, etc, but all handles are saved in `CloseOnExec`, so all copied handles will be closed when you execute it unless you clarify this, and we need child process to reuse the handle of `net.Listener`. Once a process calls exec functions, it is "dead", system replaces it with new code. The only thing it left is the process ID, which is the same number but it is a new program after executed.
Therefore, the first thing we need to do is that let child process fork main process and through `os.StartProcess` to append files that contains handle that is going to be inherited.
......@@ -15,7 +20,8 @@ The final step is that we want to serve old connections with old version of appl
Above are three problems that we need to solve, you can see my code logic for specific implementation.
##Show time
## Show time
1. Write code in your Get method:
......@@ -26,15 +32,15 @@ Above are three problems that we need to solve, you can see my code logic for sp
}
2. Open two terminals:
One execute: ` ps -ef|grep <application name>`
Another one execute:`curl "http://127.0.0.1:8080/?sleep=20"`
Another one execute: `curl "http://127.0.0.1:8080/?sleep=20"`
3. Hot update
`kill -HUP <PID>`
4. Open a terminal to request connection: `curl "http://127.0.0.1:8080/?sleep=0"`
As you will see, the first request will wait for 20 seconds, but it's served by old process; after hot update, the first request will print old process ID, but the second request will print new process ID.
\ No newline at end of file
4. Open a terminal to request connection: `curl "http://127.0.0.1:8080/?sleep=0"`
As you will see, the first request will wait for 20 seconds, but it's served by old process; after hot update, the first request will print old process ID, but the second request will print new process ID.
#Installation
# Installation
Beego is a simple web framework, but it uses many third-party packages, so you have to install all dependency packages also.
- Before anything you do, you have to check that you installed Go in your computer, see more detail about Go installation in my book: [Chapter 1](https://github.com/Unknwon/build-web-application-with-golang_EN/blob/master/eBook/01.1.md)
- Use `go get ` to install Beego:
- Use `go get ` to install Beego:
go get github.com/astaxie/beego
- Install bee tools for fast-develop Beego applications:
go get github.com/astaxie/bee
- Install bee tools for fast-develop Beego applications:
go get github.com/astaxie/bee
Good job, you're ready to Beego with powerful bee tools!
Good job, you're ready to Beego with powerful bee tools!
![](images/bee.png)
......@@ -22,4 +23,4 @@ Beego has following dependency packages:
- To support markdown as template function: [github.com/russross/blackfriday](https://github.com/russross/blackfriday)
- [Introduction](README.md)
- [Quick start](Quickstart.md)
\ No newline at end of file
- [Quick start](Quickstart.md)
This diff is collapsed.
#Beego
# Beego
Beego is a lightweight, open source, non-blocking and scalable web framework for the Go programming language. It's like tornado in Python. This web framework has already been using for building web server and tools in SNDA's CDN system. Documentation and downloads available at [http://astaxie.github.com/beego](http://astaxie.github.com/beego)
It has following main features:
......@@ -19,29 +20,33 @@ The working principles of Beego as follows:
Beego is licensed under the Apache Licence, Version 2.0
(http://www.apache.org/licenses/LICENSE-2.0.html).
#Simple example
# Simple example
The following example prints string "Hello world" to your browser, it shows how easy to build a web application with Beego.
package main
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego"
)
type MainController struct {
beego.Controller
beego.Controller
}
func (this *MainController) Get() {
this.Ctx.WriteString("hello world")
this.Ctx.WriteString("hello world")
}
func main() {
beego.Router("/", &MainController{})
beego.Run()
beego.Router("/", &MainController{})
beego.Run()
}
#Handbook
# Handbook
- [Purposes](Why.md)
- [Installation](Install.md)
- [Quick start](Quickstart.md)
......@@ -49,5 +54,7 @@ The following example prints string "Hello world" to your browser, it shows how
- [Real world usage](Application.md)
- [Hot update](HotUpdate.md)
#Documentation
[Go Walker](http://gowalker.org/github.com/astaxie/beego)
\ No newline at end of file
# Documentation
[Go Walker](http://gowalker.org/github.com/astaxie/beego)
##supervisord
## supervisord
1. Installation
wget http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg
sh setuptools-0.6c11-py2.7.egg
sh setuptools-0.6c11-py2.7.egg
easy_install supervisor
echo_supervisord_conf >/etc/supervisord.conf
mkdir /etc/supervisord.conf.d
2. Configure /etc/supervisord.conf
2. Configure /etc/supervisord.conf
[include]
files = /etc/supervisord.conf.d/*.conf
3. Add new application
cd /etc/supervisord.conf.d
......@@ -31,4 +31,4 @@
startsecs = 5
user = root
redirect_stderr = true
stdout_logfile = /var/log/supervisord/beepkg.log
\ No newline at end of file
stdout_logfile = /var/log/supervisord/beepkg.log
# 一步一步跟我写博客
## 创建项目
## 数据库结构设计
## 控制器设计
## 模板设计
## 用户登陆退出
## 数据库操作
\ No newline at end of file
## 数据库操作
# Design purposes and ideas
People may ask me why I want to build a new web framework rather than use other good ones. I know there are many excellent web frameworks on the internet and almost all of them are open source, and I have my reasons to do this.
Remember when I was writing the book about how to build web applications with Go, I just wanted to tell people what were my valuable experiences with Go in web development, especially I have been working with PHP and Python for almost ten years. At first, I didn't realize that a small web framework can give great help to web developers when they are learning to build web applications in a new programming language, and it also helps people more by studying its source code. Finally, I decided to write a open source web framework called Beego as supporting materiel for my book.
......@@ -9,11 +10,11 @@ I used to use CI in PHP and tornado in Python, there are both lightweight, so th
2. Learn more about languages by studying their source code, it's not hard to read and understand them because they are both lightweight frameworks.
3. It's quite easy to make secondary development of these frameworks for specific purposes.
Those reasons are my original intention of implementing Beego, and used two chapters in my book to introduce and design this lightweight web framework in GO.
Those reasons are my original intention of implementing Beego, and used two chapters in my book to introduce and design this lightweight web framework in Go.
Then I started to design logic execution of Beego. Because Go and Python have somewhat similar, I referenced some ideas from tornado to design Beego. As you can see, there is no different between Beego and tornado in RESTful processing; they both use GET, POST or some other methods to implement RESTful. I took some ideas from [https://github.com/drone/routes](https://github.com/drone/routes) at the beginning of designing routes. It uses regular expression in route rules processing, which is an excellent idea that to make up for the default Mux router function in Go. However, I have to design my own interface in order to implement RESTful and use inherited ideas in Python.
The controller is the most important part of whole MVC model, and Beego uses the interface and ideas I said above for the controller. Although I haven't decided to have to design the model part, everyone is welcome to implement data management by referencing Beedb, my another open source project. I simply adopt Go built-in template engine for the view part, but add more commonly used functions as template functions. This is how a simple web framework looks like, but I'll keep working on form processing, session handling, log recording, configuration, automated operation, etc, to build a simple but complete web framework.
- [Introduction](README.md)
- [Installation](Install.md)
\ No newline at end of file
- [Installation](Install.md)
......@@ -2,10 +2,12 @@
热升级是什么呢?了解nginx的同学都知道,nginx是支持热升级的,可以用老进程服务先前链接的链接,使用新进程服务新的链接,即在不停止服务的情况下完成系统的升级与运行参数修改。那么热升级和热编译是不同的概念,热编译是通过监控文件的变化重新编译,然后重启进程,例如bee start就是这样的工具
## 热升级有必要吗?
很多人认为HTTP的应用有必要支持热升级吗?那么我可以很负责的说非常有必要,不中断服务始终是我们所追求的目标,虽然很多人说可能服务器会坏掉等等,这个是属于高可用的设计范畴,不要搞混了,这个是可预知的问题,所以我们需要避免这样的升级带来的用户不可用。你还在为以前升级搞到凌晨升级而烦恼嘛?那么现在就赶紧拥抱热升级吧。
## beego如何支持热升级
热升级的原理基本上就是:主进程fork一个进程,然后子进程exec相应的程序。那么这个过程中发生了什么呢?我们知道进程fork之后会把主进程的所有句柄、数据和堆栈继承过来、但是里面所有的句柄存在一个叫做CloseOnExec,也就是执行exec的时候,copy的所有的句柄都被关闭了,除非特别申明,而我们期望的是子进程能够复用主进程的net.Listener的句柄。一个进程一旦调用exec类函数,它本身就"死亡"了,系统把代码段替换成新的程序的代码,废弃原有的数据段和堆栈段,并为新程序分配新的数据段与堆栈段,唯一留下的,就是进程号,也就是说,对系统而言,还是同一个进程,不过已经是另一个程序了。
......@@ -17,6 +19,7 @@
上面是我们需要解决的三个方面的问题,具体的实现大家可以看我实现的代码逻辑。
## 如何演示热升级
1. 编写代码,在beego应用的控制器中Get方法实现大概如下:
......@@ -28,15 +31,15 @@
}
2. 打开两个终端
一个终端输入:` ps -ef|grep 应用名`
一个终端输入请求:`curl "http://127.0.0.1:8080/?sleep=20"`
3. 热升级
3. 热升级
`kill -HUP 进程ID`
4. 打开一个终端输入请求:`curl "http://127.0.0.1:8080/?sleep=0"`
我们可以看到这样的结果,第一个请求等待20s,但是处理他的是老的进程,热升级之后,第一个请求还在执行,最后会输出老的进程ID,而第二次请求,输出的是新的进程ID
\ No newline at end of file
4. 打开一个终端输入请求:`curl "http://127.0.0.1:8080/?sleep=0"`
我们可以看到这样的结果,第一个请求等待20s,但是处理他的是老的进程,热升级之后,第一个请求还在执行,最后会输出老的进程ID,而第二次请求,输出的是新的进程ID
# 安装入门
beego虽然是一个简单的框架,但是其中用到了很多第三方的包,所以在你安装beego的过程中Go会自动安装其他关联的包。
- 当然第一步你需要安装Go,如何安装Go请参考我的书[第一章](https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/01.1.md)
......@@ -6,10 +7,10 @@ beego虽然是一个简单的框架,但是其中用到了很多第三方的包
- 安装beego
go get github.com/astaxie/beego
- 安装bee工具,这个工具可以用来快速的建立beego的应用
go get github.com/astaxie/bee
go get github.com/astaxie/bee
这样就完成了beego的安装,你就可以开始开发了,可以通过bee工具来创建beego项目
......@@ -19,6 +20,7 @@ beego虽然是一个简单的框架,但是其中用到了很多第三方的包
> - session模块:github.com/astaxie/beego/session
> - session模块中支持redis引擎:github.com/garyburd/redigo/redis
> - session模块中支持mysql引擎:github.com/go-sql-driver/mysql
......@@ -27,4 +29,4 @@ beego虽然是一个简单的框架,但是其中用到了很多第三方的包
- [beego介绍](README.md)
- [快速入门](Quickstart.md)
\ No newline at end of file
- [快速入门](Quickstart.md)
This diff is collapsed.
......@@ -18,22 +18,23 @@ beego是一个类似tornado的Go应用框架,采用了RESTFul的方式来实
package main
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego"
)
type MainController struct {
beego.Controller
beego.Controller
}
func (this *MainController) Get() {
this.Ctx.WriteString("hello world")
this.Ctx.WriteString("hello world")
}
func main() {
beego.Router("/", &MainController{})
beego.Run()
beego.Router("/", &MainController{})
beego.Run()
}
# beego 指南
* [为什么设计beego](Why.md)
......@@ -43,8 +44,9 @@ beego是一个类似tornado的Go应用框架,采用了RESTFul的方式来实
* [beego案例](Application.md)
* [热升级](HotUpdate.md)
# API接口
API对于我们平时开发应用非常有用,用于查询一些开发的函数,godoc做的非常好了
[Go Walker](http://gowalker.org/github.com/astaxie/beego)
\ No newline at end of file
[Go Walker](http://gowalker.org/github.com/astaxie/beego)
......@@ -3,20 +3,20 @@
1. setuptools安装
wget http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg
sh setuptools-0.6c11-py2.7.egg
easy_install supervisor
echo_supervisord_conf >/etc/supervisord.conf
mkdir /etc/supervisord.conf.d
2. 修改配置/etc/supervisord.conf
[include]
files = /etc/supervisord.conf.d/*.conf
3. 新建管理的应用
cd /etc/supervisord.conf.d
......@@ -31,4 +31,4 @@
startsecs = 5
user = root
redirect_stderr = true
stdout_logfile = /var/log/supervisord/beepkg.log
\ No newline at end of file
stdout_logfile = /var/log/supervisord/beepkg.log
# 一步一步跟我写博客
## 创建项目
## 数据库结构设计
## 控制器设计
## 模板设计
## 用户登陆退出
## 数据库操作
\ No newline at end of file
## 数据库操作
......@@ -17,4 +17,4 @@
整个的MVC逻辑中C是最重要的部分,这一块采用了我上面说的接口方式,M模块目前我还没想好怎么做,但是大家可以参考我的另一个开源项目beedb来实现数据的管理,V这一块目前采用了Go语言自带的模板引擎,但是实现了很多方便的模板函数。这样一个简易的框架就完成了,然后我就不断的完善周边的功能,包括表单处理、session处理、日志处理、配置处理、自动化运行等功能。
- [beego介绍](README.md)
- [安装入门](Install.md)
\ No newline at end of file
- [安装入门](Install.md)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment