tiny-3.0
最后,让我们弥补前面tiny-2.0的缺憾, 我们要开发tiny-3.0. tiny-3.0中我们不会在重启application, 我们这次 让Application温和的不间断的完成升级.
这里我假设你已经看懂前面的部分,并且亲自动手做了很多尝试. 下面是快速,直白的代码罗列..
代码修改
新建tiny-3.0目录,添加一个module: mydict.erl 采用我们自定义的dict代替stdlib dict, 修改tiny_server, 使其加入一个last mofiy信息(最后修改时间), 添加一个supervisor child, 其定期的显示当前应用的状态.
代码可以从后面的附件下载。
tiny-3.0.erl
下面是我们的tiny-3.0.rel:
{release, {"tiny app release", "3.0"}, {erts, "5.6.5"},
[{kernel, "2.12.5"},
{stdlib, "1.15.5"},
{sasl, "2.1.5.4"},
{tiny_app, "3.0"}
]}.
tiny_app.appup
下面是我们的tiny_app.appup:
{"3.0",
[{"1.0", [{restart_application, tiny_app}]},
{"2.0", [{add_module, mydict},
{update, tiny_server, {advanced, []}},
{update, tiny, supervisor},
{apply, {supervisor, restart_child, [tiny_sup, tiny_reporter]}}
]}
],
[{"1.0", [{restart_application, tiny_app}]},
{"2.0", [{apply, {supervisor, terminate_child, [tiny_sup, tiny_reporter]}},
{apply, {supervisor, delete_child, [tiny_sup, tiny_reporter]}},
{update, tiny, supervisor},
{update, tiny_server, {advanced, []}},
{delete_module, mydict}
]}
]
}.
生成relup
代码:
[da6600a1@litaocheng ~/install/tiny-3.0]$ erl -boot start_sasl -pa ./ebin/
../tiny-1.0/ ../tiny- 1.0/ebin/ ../tiny-2.0/ ../tiny-2.0/ebin/
1> systools:make_relup("tiny-3.0", ["tiny-1.0", "tiny-2.0"], ["tiny-1.0", "tiny-2.0"]).
ok
2> systools:make_script("tiny-3.0").
ok
3> systools:make_tar("tiny-3.0").
ok
随后参照前面部分完成release
命令汇总
上面的章节,比较琐碎,凌乱。让我们最后来个汇总, 部署tiny及多个版本之间的upgrade和downgrade。
1) 在 /opt/local/tiny/ 目录安装 tiny-1.0
[da6600a1@litaocheng ~/install/tiny-1.0]$ erl -pa ../ ./ebin/
Erlang (BEAM) emulator version 5.6.5 [source] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.6.5 (abort with ^G)
1> target_system:create("tiny-1.0").
Reading file: "tiny-1.0.rel" ...
Creating file: "plain.rel" from "tiny-1.0.rel" ...
Making "plain.script" and "plain.boot" files ...
Making "tiny-1.0.script" and "tiny-1.0.boot" files ...
Creating tar file "tiny-1.0.tar.gz" ...
Creating directory "tmp" ...
Extracting "tiny-1.0.tar.gz" into directory "tmp" ...
Deleting "erl" and "start" in directory "tmp/erts-5.6.5/bin" ...
Creating temporary directory "tmp/bin" ...
Copying file "plain.boot" to "tmp/bin/start.boot" ...
Copying files "epmd", "run_erl" and "to_erl" from
"tmp/erts-5.6.5/bin" to "tmp/bin" ...
Creating "tmp/releases/start_erl.data" ...
Recreating tar file "tiny-1.0.tar.gz" from contents in directory "tmp" ...
Removing directory "tmp" ...
ok
2> target_system:install("tiny-1.0", "/opt/local/tiny").
Extracting tiny-1.0.tar.gz ...
Substituting in erl.src, start.src and start_erl.src to
form erl, start and start_erl ...
Creating the RELEASES file ...
ok
2) 运行 tiny-1.0 (假设为Shell A)
[da6600a1@litaocheng /opt/local/tiny]$ ./bin/erl -boot ./releases/1.0/start -sname tiny .... Eshell V5.6.5 (abort with ^G) (tiny@da6600a1)1> tiny_app 1.0 running... tiny_app 1.0 running...
3) 生成tiny-2.0的relup文件,及release tar
[da6600a1@litaocheng ~/install/tiny-2.0]$ erl -pa ../tiny-1.0/ ../tiny-1.0/ebin/ ./ebin/
Erlang (BEAM) emulator version 5.6.5 [source] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.6.5 (abort with ^G)
1> systools:make_relup("tiny-2.0", ["tiny-1.0"], ["tiny-1.0"]).
ok
2> systools:make_script("tiny-2.0").
ok
3> systools:make_tar("tiny-2.0").
ok
4) 生成tiny-3.0的relup文件,及release tar
[da6600a1@litaocheng ~/install/tiny-3.0]$ erl -boot start_sasl -pa ./ebin/ ../tiny-1.0/ ../tiny-1.0/ebin/
../tiny-2.0/ ../tiny-2.0/ebin/
...
Eshell V5.6.5 (abort with ^G)
1> systools:make_relup("tiny-3.0", ["tiny-1.0", "tiny-2.0"], ["tiny-1.0", "tiny-2.0"]).
ok
2> systools:make_script("tiny-3.0").
ok
3> systools:make_tar("tiny-3.0").
ok
5) 将 tiny-2.0.tar.gz 和 tiny-3.0.tar.gz拷贝到部署目标: /opt/local/tiny/ 的releases目录
[da6600a1@litaocheng ~/install]$ cp ./tiny-2.0/tiny-2.0.tar.gz /opt/local/tiny/releases/ [da6600a1@litaocheng ~/install]$ cp ./tiny-3.0/tiny-3.0.tar.gz /opt/local/tiny/releases/
6) tiny 1.0 升级到tiny 2.0
启动一个新的erl shell
[da6600a1@litaocheng ~]$ erl -sname install
Erlang (BEAM) emulator version 5.6.5 [source] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.6.5 (abort with ^G)
(install@da6600a1)1> % 按下 CTRL + G
User switch command
--> r tiny@da6600a1 shell % 在运行tiny-1.0的node中创建shell
--> j % 显示所有的可连接shell
1 {shell,start,[init]}
2* {tiny@da6600a1,shell,start,[]}
--> c 2 % 连接tiny@da6600a1
Eshell V5.6.5 (abort with ^G)
(tiny@da6600a1)1> application:which_applications().
[{tiny_app,"tiny app","1.0"},
{sasl,"SASL CXC 138 11","2.1.5.4"},
{stdlib,"ERTS CXC 138 10","1.15.5"},
{kernel,"ERTS CXC 138 10","2.12.5"}]
(tiny@da6600a1)2> release_handler:unpack_release("tiny-2.0"). % 解压 tiny-2.0.tar.gz
{ok,"2.0"}
(tiny@da6600a1)3> release_handler:install_release("2.0"). % 安装 tiny-2.0
{ok,"1.0",[]}
(tiny@da6600a1)4> release_handler:make_permanent("2.0"). % 使tiny-2.0成为默认版本
ok
此时看到Shell A中变化:
tiny_app 1.0 running...
=INFO REPORT==== 9-Mar-2009::13:56:58 ===
application: tiny_app
exited: stopped
type: permanent
=PROGRESS REPORT==== 9-Mar-2009::13:56:58 ===
supervisor: {local,tiny_sup}
started: [{pid,<0.68.0>},
{name,tiny_server},
{mfa,{tiny_server,start_link,[]}},
{restart_type,permanent},
{shutdown,10},
{child_type,worker}]
=PROGRESS REPORT==== 9-Mar-2009::13:56:58 ===
application: tiny_app
started_at: tiny@da6600a1
tiny_app 2.0 running ....
tiny_app 2.0 running ....
7) tiny 2.0升级到 tiny 3.0
(tiny@da6600a1)5> release_handler:unpack_release("tiny-3.0"). % 解压 tiny-3.0.tar.gz
{ok,"3.0"}
(tiny@da6600a1)6> release_handler:install_release("3.0"). % 安装 tiny-3.0
{ok,"2.0",[]}
(tiny@da6600a1)7> release_handler:make_permanent("3.0"). % 使tiny-3.0成为默认版本
ok
`此时看到Shell A中变化:`
tiny_app 2.0 running ....
tiny_server code change: from 1.0 to 2.0
=PROGRESS REPORT==== 9-Mar-2009::14:00:18 ===
supervisor: {local,tiny_sup}
started: [{pid,<0.78.0>},
{name,tiny_reporter},
{mfa,{tiny_reporter,start_link,[]}},
{restart_type,permanent},
{shutdown,10},
{child_type,worker}]
tiny_app 3.0 running ...
tiny_app 3.0 running ...
至此对于release hanlding相信,你应该比较熟悉了吧。 请参照附件中的代码,动手实验一下。如有错误,欢迎指正。
8) tiny 3.0回退到 tiny 2.0
(tiny@da6600a1)8> release_handler:install_release("2.0").
{ok,"2.0",[]}
此时看到Shell A中变化:
tiny_app 3.0 running ...
tiny_server code change: from 2.0 to 1.0
Tiny应用无缝升级实践
本文介绍了一个名为Tiny的应用如何从1.0版本平滑升级到3.0版本的过程,包括代码变更、模块替换以及状态报告的添加等细节,并演示了如何使用Erlang的systools模块生成relup文件实现版本的升级与回退。
4578

被折叠的 条评论
为什么被折叠?



