<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>yakjuly&#039;s blog</title>
	<atom:link href="http://www.yakjuly.com/feed" rel="self" type="application/rss+xml" />
	<link>http://www.yakjuly.com</link>
	<description>关注Ruby on Rails、电子商务、技术创新及应用</description>
	<lastBuildDate>Thu, 07 Jul 2011 02:28:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>修改默认shell的bash</title>
		<link>http://www.yakjuly.com/2011/06/modify-user-default-shell-bash-path.html</link>
		<comments>http://www.yakjuly.com/2011/06/modify-user-default-shell-bash-path.html#comments</comments>
		<pubDate>Wed, 01 Jun 2011 14:27:07 +0000</pubDate>
		<dc:creator>yakjuly</dc:creator>
				<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://www.yakjuly.com/?p=160</guid>
		<description><![CDATA[sudo vi /etc/passwd 会看到所有用户的权限以及使用的bash root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/bin/sh bin:x:2:2:bin:/bin:/bin/sh sys:x:3:3:sys:/dev:/bin/sh sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60:games:/usr/games:/bin/sh man:x:6:12:man:/var/cache/man:/bin/sh lp:x:7:7:lp:/var/spool/lpd:/bin/sh mail:x:8:8:mail:/var/mail:/bin/sh news:x:9:9:news:/var/spool/news:/bin/sh uucp:x:10:10:uucp:/var/spool/uucp:/bin/sh proxy:x:13:13:proxy:/bin:/bin/sh www-data:x:33:33:www-data:/var/www:/bin/sh backup:x:34:34:backup:/var/backups:/bin/sh list:x:38:38:Mailing List Manager:/var/list:/bin/sh irc:x:39:39:ircd:/var/run/ircd:/bin/sh gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/bin/sh nobody:x:65534:65534:nobody:/nonexistent:/bin/sh libuuid:x:100:101::/var/lib/libuuid:/bin/sh syslog:x:101:103::/home/syslog:/bin/false sshd:x:102:65534::/var/run/sshd:/usr/sbin/nologin ntpd:x:103:108::/var/run/openntpd:/bin/false mysql:x:104:109:MySQL Server,,,:/var/lib/mysql:/bin/false yakjuly:x:1000:1000::/home/yakjuly:/bin/sh 把最后yakjuly的移行的sh改成bash 即可 sh 和 bash 是两个不同的shell，linux下有很多种shell。 sh比较小，但是功能没有bash多，bash用得比较普遍 Subscribe to the comments for this post? Share this on Facebook Share this on [...]]]></description>
			<content:encoded><![CDATA[<p>sudo vi /etc/passwd</p>
<p>会看到所有用户的权限以及使用的bash</p>
<pre class="brush:shell">root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh
uucp:x:10:10:uucp:/var/spool/uucp:/bin/sh
proxy:x:13:13:proxy:/bin:/bin/sh
www-data:x:33:33:www-data:/var/www:/bin/sh
backup:x:34:34:backup:/var/backups:/bin/sh
list:x:38:38:Mailing List Manager:/var/list:/bin/sh
irc:x:39:39:ircd:/var/run/ircd:/bin/sh
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/bin/sh
nobody:x:65534:65534:nobody:/nonexistent:/bin/sh
libuuid:x:100:101::/var/lib/libuuid:/bin/sh
syslog:x:101:103::/home/syslog:/bin/false
sshd:x:102:65534::/var/run/sshd:/usr/sbin/nologin
ntpd:x:103:108::/var/run/openntpd:/bin/false
mysql:x:104:109:MySQL Server,,,:/var/lib/mysql:/bin/false
yakjuly:x:1000:1000::/home/yakjuly:/bin/sh</pre>
<p>把最后yakjuly的移行的sh改成bash 即可</p>
<p>sh 和 bash 是两个不同的shell，linux下有很多种shell。<br />
sh比较小，但是功能没有bash多，bash用得比较普遍</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-wealth">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://www.yakjuly.com/2011/06/modify-user-default-shell-bash-path.html/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.yakjuly.com/2011/06/modify-user-default-shell-bash-path.html&amp;t=%E4%BF%AE%E6%94%B9%E9%BB%98%E8%AE%A4shell%E7%9A%84bash" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-friendfeed">
			<a href="http://www.friendfeed.com/share?title=%E4%BF%AE%E6%94%B9%E9%BB%98%E8%AE%A4shell%E7%9A%84bash&amp;link=http://www.yakjuly.com/2011/06/modify-user-default-shell-bash-path.html" rel="nofollow" class="external" title="Share this on FriendFeed">Share this on FriendFeed</a>
		</li>
		<li class="shr-googlebookmarks">
			<a href="http://www.google.com/bookmarks/mark?op=add&amp;bkmk=http://www.yakjuly.com/2011/06/modify-user-default-shell-bash-path.html&amp;title=%E4%BF%AE%E6%94%B9%E9%BB%98%E8%AE%A4shell%E7%9A%84bash" rel="nofollow" class="external" title="Add this to Google Bookmarks">Add this to Google Bookmarks</a>
		</li>
		<li class="shr-googlereader">
			<a href="http://www.google.com/reader/link?url=http://www.yakjuly.com/2011/06/modify-user-default-shell-bash-path.html&amp;title=%E4%BF%AE%E6%94%B9%E9%BB%98%E8%AE%A4shell%E7%9A%84bash&amp;srcUrl=http://www.yakjuly.com/2011/06/modify-user-default-shell-bash-path.html&amp;srcTitle=%E4%BF%AE%E6%94%B9%E9%BB%98%E8%AE%A4shell%E7%9A%84bash&amp;snippet=sudo%20vi%20%2Fetc%2Fpasswd%0D%0A%0D%0A%E4%BC%9A%E7%9C%8B%E5%88%B0%E6%89%80%E6%9C%89%E7%94%A8%E6%88%B7%E7%9A%84%E6%9D%83%E9%99%90%E4%BB%A5%E5%8F%8A%E4%BD%BF%E7%94%A8%E7%9A%84bash%0D%0Aroot%3Ax%3A0%3A0%3Aroot%3A%2Froot%3A%2Fbin%2Fbash%0D%0Adaemon%3Ax%3A1%3A1%3Adaemon%3A%2Fusr%2Fsbin%3A%2Fbin%2Fsh%0D%0Abin%3Ax%3A2%3A2%3Abin%3A%2Fbin%3A%2Fbin%2Fsh%0D%0Asys%3Ax%3A3%3A3%3Asys%3A%2Fdev%3A%2Fbin%2Fsh%0D%0Async%3Ax%3A4%3A65534%3Async%3A%2Fbin%3A%2Fbin%2Fsync%0D%0Agames%3Ax%3A5%3A60%3Agames%3A%2Fusr%2Fgames%3A%2Fbin%2Fsh%0D%0Aman%3Ax%3A6%3A12%3Aman%3A%2Fvar%2Fcac" rel="nofollow" class="external" title="Add this to Google Reader">Add this to Google Reader</a>
		</li>
		<li class="shr-mail">
			<a href="mailto:?subject=%22%E4%BF%AE%E6%94%B9%E9%BB%98%E8%AE%A4shell%E7%9A%84bash%22&amp;body=Link: http://www.yakjuly.com/2011/06/modify-user-default-shell-bash-path.html (sent via shareaholic)%0D%0A%0D%0A----%0D%0A sudo%20vi%20%2Fetc%2Fpasswd%0D%0A%0D%0A%E4%BC%9A%E7%9C%8B%E5%88%B0%E6%89%80%E6%9C%89%E7%94%A8%E6%88%B7%E7%9A%84%E6%9D%83%E9%99%90%E4%BB%A5%E5%8F%8A%E4%BD%BF%E7%94%A8%E7%9A%84bash%0D%0Aroot%3Ax%3A0%3A0%3Aroot%3A%2Froot%3A%2Fbin%2Fbash%0D%0Adaemon%3Ax%3A1%3A1%3Adaemon%3A%2Fusr%2Fsbin%3A%2Fbin%2Fsh%0D%0Abin%3Ax%3A2%3A2%3Abin%3A%2Fbin%3A%2Fbin%2Fsh%0D%0Asys%3Ax%3A3%3A3%3Asys%3A%2Fdev%3A%2Fbin%2Fsh%0D%0Async%3Ax%3A4%3A65534%3Async%3A%2Fbin%3A%2Fbin%2Fsync%0D%0Agames%3Ax%3A5%3A60%3Agames%3A%2Fusr%2Fgames%3A%2Fbin%2Fsh%0D%0Aman%3Ax%3A6%3A12%3Aman%3A%2Fvar%2Fcac" rel="nofollow" class="external" title="Email this to a friend?">Email this to a friend?</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=%E4%BF%AE%E6%94%B9%E9%BB%98%E8%AE%A4shell%E7%9A%84bash+-+http://www.yakjuly.com/2011/06/modify-user-default-shell-bash-path.html&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

 <img src="http://www.yakjuly.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=160" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://www.yakjuly.com/2011/06/modify-user-default-shell-bash-path.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>authorized_keys立即生效</title>
		<link>http://www.yakjuly.com/2011/06/authorized_keys-effect-soon.html</link>
		<comments>http://www.yakjuly.com/2011/06/authorized_keys-effect-soon.html#comments</comments>
		<pubDate>Wed, 01 Jun 2011 14:19:11 +0000</pubDate>
		<dc:creator>yakjuly</dc:creator>
				<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://www.yakjuly.com/?p=159</guid>
		<description><![CDATA[我将公钥拷贝成authorized_keys之后，为什么公钥认证没有生效？ 解决方法: 通常这是由于$HOME，$HOME/.ssh的文件权限造成的，或者是$HOME/.ssh/authorized_keys的权限超过了sshd默认允许的权限。 如果是这样，可以执行下面的命令去解决这个问题： $ chmod go-w $HOME $HOME/.ssh $ chmod 600 $HOME/.ssh/authorized_keys &#160; Subscribe to the comments for this post? Share this on Facebook Share this on FriendFeed Add this to Google Bookmarks Add this to Google Reader Email this to a friend? Tweet This!]]></description>
			<content:encoded><![CDATA[<div>我将公钥拷贝成authorized_keys之后，为什么公钥认证没有生效？</div>
<div>解决方法:</div>
<p>通常这是由于$HOME，$HOME/.ssh的文件权限造成的，或者是$HOME/.ssh/authorized_keys的权限超过了sshd默认允许的权限。</p>
<p>如果是这样，可以执行下面的命令去解决这个问题：</p>
<pre>$ chmod go-w $HOME $HOME/.ssh
$ chmod 600 $HOME/.ssh/authorized_keys</pre>
<p>&nbsp;</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-wealth">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://www.yakjuly.com/2011/06/authorized_keys-effect-soon.html/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.yakjuly.com/2011/06/authorized_keys-effect-soon.html&amp;t=authorized_keys%E7%AB%8B%E5%8D%B3%E7%94%9F%E6%95%88" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-friendfeed">
			<a href="http://www.friendfeed.com/share?title=authorized_keys%E7%AB%8B%E5%8D%B3%E7%94%9F%E6%95%88&amp;link=http://www.yakjuly.com/2011/06/authorized_keys-effect-soon.html" rel="nofollow" class="external" title="Share this on FriendFeed">Share this on FriendFeed</a>
		</li>
		<li class="shr-googlebookmarks">
			<a href="http://www.google.com/bookmarks/mark?op=add&amp;bkmk=http://www.yakjuly.com/2011/06/authorized_keys-effect-soon.html&amp;title=authorized_keys%E7%AB%8B%E5%8D%B3%E7%94%9F%E6%95%88" rel="nofollow" class="external" title="Add this to Google Bookmarks">Add this to Google Bookmarks</a>
		</li>
		<li class="shr-googlereader">
			<a href="http://www.google.com/reader/link?url=http://www.yakjuly.com/2011/06/authorized_keys-effect-soon.html&amp;title=authorized_keys%E7%AB%8B%E5%8D%B3%E7%94%9F%E6%95%88&amp;srcUrl=http://www.yakjuly.com/2011/06/authorized_keys-effect-soon.html&amp;srcTitle=authorized_keys%E7%AB%8B%E5%8D%B3%E7%94%9F%E6%95%88&amp;snippet=%E6%88%91%E5%B0%86%E5%85%AC%E9%92%A5%E6%8B%B7%E8%B4%9D%E6%88%90authorized_keys%E4%B9%8B%E5%90%8E%EF%BC%8C%E4%B8%BA%E4%BB%80%E4%B9%88%E5%85%AC%E9%92%A5%E8%AE%A4%E8%AF%81%E6%B2%A1%E6%9C%89%E7%94%9F%E6%95%88%EF%BC%9F%0D%0A%E8%A7%A3%E5%86%B3%E6%96%B9%E6%B3%95%3A%0D%0A%E9%80%9A%E5%B8%B8%E8%BF%99%E6%98%AF%E7%94%B1%E4%BA%8E%24HOME%EF%BC%8C%24HOME%2F.ssh%E7%9A%84%E6%96%87%E4%BB%B6%E6%9D%83%E9%99%90%E9%80%A0%E6%88%90%E7%9A%84%EF%BC%8C%E6%88%96%E8%80%85%E6%98%AF%24HOME%2F.ssh%2Fauthorized_keys%E7%9A%84%E6%9D%83%E9%99%90%E8%B6%85%E8%BF%87%E4%BA%86sshd%E9%BB%98%E8%AE%A4%E5%85%81%E8%AE%B8%E7%9A%84%E6%9D%83%E9%99%90%E3%80%82%0D%0A%0D%0A%E5%A6%82%E6%9E%9C%E6%98%AF%E8%BF%99%E6%A0%B7%EF%BC%8C%E5%8F%AF%E4%BB%A5%E6%89%A7%E8%A1%8C%E4%B8%8B%E9%9D%A2%E7%9A%84%E5%91%BD%E4%BB%A4%E5%8E%BB%E8%A7%A3%E5%86%B3" rel="nofollow" class="external" title="Add this to Google Reader">Add this to Google Reader</a>
		</li>
		<li class="shr-mail">
			<a href="mailto:?subject=%22authorized_keys%E7%AB%8B%E5%8D%B3%E7%94%9F%E6%95%88%22&amp;body=Link: http://www.yakjuly.com/2011/06/authorized_keys-effect-soon.html (sent via shareaholic)%0D%0A%0D%0A----%0D%0A %E6%88%91%E5%B0%86%E5%85%AC%E9%92%A5%E6%8B%B7%E8%B4%9D%E6%88%90authorized_keys%E4%B9%8B%E5%90%8E%EF%BC%8C%E4%B8%BA%E4%BB%80%E4%B9%88%E5%85%AC%E9%92%A5%E8%AE%A4%E8%AF%81%E6%B2%A1%E6%9C%89%E7%94%9F%E6%95%88%EF%BC%9F%0D%0A%E8%A7%A3%E5%86%B3%E6%96%B9%E6%B3%95%3A%0D%0A%E9%80%9A%E5%B8%B8%E8%BF%99%E6%98%AF%E7%94%B1%E4%BA%8E%24HOME%EF%BC%8C%24HOME%2F.ssh%E7%9A%84%E6%96%87%E4%BB%B6%E6%9D%83%E9%99%90%E9%80%A0%E6%88%90%E7%9A%84%EF%BC%8C%E6%88%96%E8%80%85%E6%98%AF%24HOME%2F.ssh%2Fauthorized_keys%E7%9A%84%E6%9D%83%E9%99%90%E8%B6%85%E8%BF%87%E4%BA%86sshd%E9%BB%98%E8%AE%A4%E5%85%81%E8%AE%B8%E7%9A%84%E6%9D%83%E9%99%90%E3%80%82%0D%0A%0D%0A%E5%A6%82%E6%9E%9C%E6%98%AF%E8%BF%99%E6%A0%B7%EF%BC%8C%E5%8F%AF%E4%BB%A5%E6%89%A7%E8%A1%8C%E4%B8%8B%E9%9D%A2%E7%9A%84%E5%91%BD%E4%BB%A4%E5%8E%BB%E8%A7%A3%E5%86%B3" rel="nofollow" class="external" title="Email this to a friend?">Email this to a friend?</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=authorized_keys%E7%AB%8B%E5%8D%B3%E7%94%9F%E6%95%88+-+http://www.yakjuly.com/2011/06/authorized_keys-effect-soon.html&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

 <img src="http://www.yakjuly.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=159" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://www.yakjuly.com/2011/06/authorized_keys-effect-soon.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>wordpress迁移点滴</title>
		<link>http://www.yakjuly.com/2011/06/wordpress-migrate-tips.html</link>
		<comments>http://www.yakjuly.com/2011/06/wordpress-migrate-tips.html#comments</comments>
		<pubDate>Wed, 01 Jun 2011 14:16:36 +0000</pubDate>
		<dc:creator>yakjuly</dc:creator>
				<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[杂七杂八]]></category>

		<guid isPermaLink="false">http://www.yakjuly.com/?p=158</guid>
		<description><![CDATA[昨天把主机从dreamhost换到了linode，虽然觉得dreamhost很不错 但是没有root权限始终有点不快。 今年打算把服务器，运维等知识好好补充一下，于是买了口碑一直不错的linode的VPS。 第一天的任务就是 迁移博客。 1. scp把wordpress文件 都拷贝至新主机上 2.apt-get 安装mysql php5 php5-mysql php5-cgi libfcgi-dev spawn-fcgi nginx 3.使用spawn-fcgi 脚本 启动php5-cgi进程 sudo spawn-fcgi -f /usr/bin/php5-cgi -a 127.0.0.1 -p 9000 -C 3 -P /var/run/php5-cgi.pid 4.配置nginx 在/etc/nginx/sites-enabled下创建 yakjuly.com文件 server { listen 80; server_name yakjuly.com; access_log /home/yakjuly/applications/yakjuly.com/log/access.log; error_log /home/yakjuly/applications/yakjuly.com/log/error.log; rewrite /wp-admin$ $scheme://$host$uri/ permanent; location / { root /home/yakjuly/applications/yakjuly.com; index index.php [...]]]></description>
			<content:encoded><![CDATA[<p>昨天把主机从dreamhost换到了linode，虽然觉得dreamhost很不错 但是没有root权限始终有点不快。</p>
<p>今年打算把服务器，运维等知识好好补充一下，于是买了口碑一直不错的linode的VPS。</p>
<p>第一天的任务就是 迁移博客。</p>
<p>1. scp把wordpress文件 都拷贝至新主机上</p>
<p>2.apt-get 安装mysql php5 php5-mysql php5-cgi libfcgi-dev spawn-fcgi nginx</p>
<p>3.使用spawn-fcgi 脚本 启动php5-cgi进程</p>
<pre class="brush:shell">sudo spawn-fcgi -f /usr/bin/php5-cgi -a 127.0.0.1 -p 9000 -C 3 -P /var/run/php5-cgi.pid</pre>
<p>4.配置nginx</p>
<p>在/etc/nginx/sites-enabled下创建 yakjuly.com文件</p>
<pre class="brush:shell">server {
        listen   80;
        server_name yakjuly.com;

        access_log  /home/yakjuly/applications/yakjuly.com/log/access.log;
        error_log /home/yakjuly/applications/yakjuly.com/log/error.log;
        rewrite /wp-admin$ $scheme://$host$uri/ permanent;

        location / {
                root   /home/yakjuly/applications/yakjuly.com;
                index  index.php index.html index.htm;

                if (-f $request_filename/index.html){
                        rewrite (.*) $1/index.html break;
                }
                if (-f $request_filename/index.php){
                        rewrite (.*) $1/index.php;
                }
                if (!-e $request_filename){
                         rewrite . /index.php;
                }
        }

        location ~ .*\.php$ {
                include /etc/nginx/fastcgi_params;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param   SCRIPT_FILENAME /home/yakjuly/applications/yakjuly.com$fastcgi_script_name;
        }
}</pre>
<div></div>
<blockquote>
<div>rewrite /wp-admin$ $scheme://$host$uri/ permanent;</div>
</blockquote>
<div>解决了 /wp-admin 和/wp-admin/ 产生的url地址不一致的错误。</div>
<div></div>
<div>5.wordpress中文显示乱码。</div>
<div></div>
<div>在dreamhost时数据库是latin1，在导入linode时已经转化为了utf8 而且在mysql console中显示正常，说明是php的设置问题。</div>
<div>添加以下内容到wp-config.php</div>
<div>
<pre class="brush:php">define('DB_CHARSET', 'utf8');
define('DB_COLLATE', 'uft8_general_ci');</pre>
</div>
<div>6.域名指向</div>
<div>登陆godaddy设置域名A记录指向新主机ip地址</div>
<div></div>
<div>10分钟后就生效了 感叹godaddy的神速 V5</div>
<div></div>
<div>新blog的访问速度明显比原来快得多。</div>
<div></div>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-wealth">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://www.yakjuly.com/2011/06/wordpress-migrate-tips.html/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.yakjuly.com/2011/06/wordpress-migrate-tips.html&amp;t=wordpress%E8%BF%81%E7%A7%BB%E7%82%B9%E6%BB%B4" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-friendfeed">
			<a href="http://www.friendfeed.com/share?title=wordpress%E8%BF%81%E7%A7%BB%E7%82%B9%E6%BB%B4&amp;link=http://www.yakjuly.com/2011/06/wordpress-migrate-tips.html" rel="nofollow" class="external" title="Share this on FriendFeed">Share this on FriendFeed</a>
		</li>
		<li class="shr-googlebookmarks">
			<a href="http://www.google.com/bookmarks/mark?op=add&amp;bkmk=http://www.yakjuly.com/2011/06/wordpress-migrate-tips.html&amp;title=wordpress%E8%BF%81%E7%A7%BB%E7%82%B9%E6%BB%B4" rel="nofollow" class="external" title="Add this to Google Bookmarks">Add this to Google Bookmarks</a>
		</li>
		<li class="shr-googlereader">
			<a href="http://www.google.com/reader/link?url=http://www.yakjuly.com/2011/06/wordpress-migrate-tips.html&amp;title=wordpress%E8%BF%81%E7%A7%BB%E7%82%B9%E6%BB%B4&amp;srcUrl=http://www.yakjuly.com/2011/06/wordpress-migrate-tips.html&amp;srcTitle=wordpress%E8%BF%81%E7%A7%BB%E7%82%B9%E6%BB%B4&amp;snippet=%E6%98%A8%E5%A4%A9%E6%8A%8A%E4%B8%BB%E6%9C%BA%E4%BB%8Edreamhost%E6%8D%A2%E5%88%B0%E4%BA%86linode%EF%BC%8C%E8%99%BD%E7%84%B6%E8%A7%89%E5%BE%97dreamhost%E5%BE%88%E4%B8%8D%E9%94%99%20%E4%BD%86%E6%98%AF%E6%B2%A1%E6%9C%89root%E6%9D%83%E9%99%90%E5%A7%8B%E7%BB%88%E6%9C%89%E7%82%B9%E4%B8%8D%E5%BF%AB%E3%80%82%0D%0A%0D%0A%E4%BB%8A%E5%B9%B4%E6%89%93%E7%AE%97%E6%8A%8A%E6%9C%8D%E5%8A%A1%E5%99%A8%EF%BC%8C%E8%BF%90%E7%BB%B4%E7%AD%89%E7%9F%A5%E8%AF%86%E5%A5%BD%E5%A5%BD%E8%A1%A5%E5%85%85%E4%B8%80%E4%B8%8B%EF%BC%8C%E4%BA%8E%E6%98%AF%E4%B9%B0%E4%BA%86%E5%8F%A3%E7%A2%91%E4%B8%80%E7%9B%B4%E4%B8%8D%E9%94%99%E7%9A%84linode%E7%9A%84VPS%E3%80%82%0D%0A%0D%0A%E7%AC%AC%E4%B8%80%E5%A4%A9%E7%9A%84%E4%BB%BB%E5%8A%A1%E5%B0%B1%E6%98%AF%20%E8%BF%81%E7%A7%BB%E5%8D%9A%E5%AE%A2%E3%80%82%0D%0A%0D%0A1.%20scp%E6%8A%8Awordpress" rel="nofollow" class="external" title="Add this to Google Reader">Add this to Google Reader</a>
		</li>
		<li class="shr-mail">
			<a href="mailto:?subject=%22wordpress%E8%BF%81%E7%A7%BB%E7%82%B9%E6%BB%B4%22&amp;body=Link: http://www.yakjuly.com/2011/06/wordpress-migrate-tips.html (sent via shareaholic)%0D%0A%0D%0A----%0D%0A %E6%98%A8%E5%A4%A9%E6%8A%8A%E4%B8%BB%E6%9C%BA%E4%BB%8Edreamhost%E6%8D%A2%E5%88%B0%E4%BA%86linode%EF%BC%8C%E8%99%BD%E7%84%B6%E8%A7%89%E5%BE%97dreamhost%E5%BE%88%E4%B8%8D%E9%94%99%20%E4%BD%86%E6%98%AF%E6%B2%A1%E6%9C%89root%E6%9D%83%E9%99%90%E5%A7%8B%E7%BB%88%E6%9C%89%E7%82%B9%E4%B8%8D%E5%BF%AB%E3%80%82%0D%0A%0D%0A%E4%BB%8A%E5%B9%B4%E6%89%93%E7%AE%97%E6%8A%8A%E6%9C%8D%E5%8A%A1%E5%99%A8%EF%BC%8C%E8%BF%90%E7%BB%B4%E7%AD%89%E7%9F%A5%E8%AF%86%E5%A5%BD%E5%A5%BD%E8%A1%A5%E5%85%85%E4%B8%80%E4%B8%8B%EF%BC%8C%E4%BA%8E%E6%98%AF%E4%B9%B0%E4%BA%86%E5%8F%A3%E7%A2%91%E4%B8%80%E7%9B%B4%E4%B8%8D%E9%94%99%E7%9A%84linode%E7%9A%84VPS%E3%80%82%0D%0A%0D%0A%E7%AC%AC%E4%B8%80%E5%A4%A9%E7%9A%84%E4%BB%BB%E5%8A%A1%E5%B0%B1%E6%98%AF%20%E8%BF%81%E7%A7%BB%E5%8D%9A%E5%AE%A2%E3%80%82%0D%0A%0D%0A1.%20scp%E6%8A%8Awordpress" rel="nofollow" class="external" title="Email this to a friend?">Email this to a friend?</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=wordpress%E8%BF%81%E7%A7%BB%E7%82%B9%E6%BB%B4+-+http://www.yakjuly.com/2011/06/wordpress-migrate-tips.html&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

 <img src="http://www.yakjuly.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=158" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://www.yakjuly.com/2011/06/wordpress-migrate-tips.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>数据库备份导入乱码问题</title>
		<link>http://www.yakjuly.com/2011/05/mysql-dump-import-chinese-problem-solution.html</link>
		<comments>http://www.yakjuly.com/2011/05/mysql-dump-import-chinese-problem-solution.html#comments</comments>
		<pubDate>Mon, 30 May 2011 03:10:26 +0000</pubDate>
		<dc:creator>yakjuly</dc:creator>
				<category><![CDATA[database]]></category>
		<category><![CDATA[杂七杂八]]></category>

		<guid isPermaLink="false">http://www.yakjuly.com/?p=157</guid>
		<description><![CDATA[备份数据库时需要知道原来数据库的文件编码格式是什么。 show variables like &#8220;%character%&#8221; 看到的结果可能是latin1可能是gbk可能是utf8 代表着你在原来数据存储文字编码的格式，在备份时需要设定&#8211;default-character-set。不设定则默认时utf8 mysql执行导出 mysqldump -uyakjulycom -pxxxxx -h mysql.yakjuly.com &#8211;default-character-set=latin1 yakjuly_com &#62; yakjuly_com.sql 这样在yakjuly_com.sql文件中 的中文内容就能显示正确。 那么再导入另外一个数据库 本地的数据库设定了 character-set是uf8 如果直接source则在数据库中仍然显示乱码 原因是在yakjuly_com.sql内容的开头有定义 /*!40101 SET NAMES latin1 */; 把latin1替换为utf8 保存，然后再创建数据库source该文件或 执行 mysql -uroot -p &#8211;default-character-set=utf8 -f yakjuly_com &#60; yakjuly_com.sql 这样数据库中的中文就显示正确了 &#160; Subscribe to the comments for this post? Share this on Facebook Share this [...]]]></description>
			<content:encoded><![CDATA[<p>备份数据库时需要知道原来数据库的文件编码格式是什么。</p>
<p>show variables like &#8220;%character%&#8221;</p>
<p>看到的结果可能是latin1可能是gbk可能是utf8 代表着你在原来数据存储文字编码的格式，在备份时需要设定&#8211;default-character-set。不设定则默认时utf8</p>
<p>mysql执行导出</p>
<p>mysqldump -uyakjulycom -pxxxxx -h mysql.yakjuly.com &#8211;default-character-set=latin1 yakjuly_com &gt; yakjuly_com.sql</p>
<p>这样在yakjuly_com.sql文件中 的中文内容就能显示正确。</p>
<p>那么再导入另外一个数据库</p>
<p>本地的数据库设定了 character-set是uf8 如果直接source则在数据库中仍然显示乱码</p>
<p>原因是在yakjuly_com.sql内容的开头有定义</p>
<p>/*!40101 SET NAMES latin1 */;</p>
<p>把latin1替换为utf8 保存，然后再创建数据库source该文件或 执行</p>
<p>mysql -uroot -p &#8211;default-character-set=utf8 -f yakjuly_com &lt; yakjuly_com.sql</p>
<p>这样数据库中的中文就显示正确了</p>
<p>&nbsp;</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-wealth">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://www.yakjuly.com/2011/05/mysql-dump-import-chinese-problem-solution.html/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.yakjuly.com/2011/05/mysql-dump-import-chinese-problem-solution.html&amp;t=%E6%95%B0%E6%8D%AE%E5%BA%93%E5%A4%87%E4%BB%BD%E5%AF%BC%E5%85%A5%E4%B9%B1%E7%A0%81%E9%97%AE%E9%A2%98" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-friendfeed">
			<a href="http://www.friendfeed.com/share?title=%E6%95%B0%E6%8D%AE%E5%BA%93%E5%A4%87%E4%BB%BD%E5%AF%BC%E5%85%A5%E4%B9%B1%E7%A0%81%E9%97%AE%E9%A2%98&amp;link=http://www.yakjuly.com/2011/05/mysql-dump-import-chinese-problem-solution.html" rel="nofollow" class="external" title="Share this on FriendFeed">Share this on FriendFeed</a>
		</li>
		<li class="shr-googlebookmarks">
			<a href="http://www.google.com/bookmarks/mark?op=add&amp;bkmk=http://www.yakjuly.com/2011/05/mysql-dump-import-chinese-problem-solution.html&amp;title=%E6%95%B0%E6%8D%AE%E5%BA%93%E5%A4%87%E4%BB%BD%E5%AF%BC%E5%85%A5%E4%B9%B1%E7%A0%81%E9%97%AE%E9%A2%98" rel="nofollow" class="external" title="Add this to Google Bookmarks">Add this to Google Bookmarks</a>
		</li>
		<li class="shr-googlereader">
			<a href="http://www.google.com/reader/link?url=http://www.yakjuly.com/2011/05/mysql-dump-import-chinese-problem-solution.html&amp;title=%E6%95%B0%E6%8D%AE%E5%BA%93%E5%A4%87%E4%BB%BD%E5%AF%BC%E5%85%A5%E4%B9%B1%E7%A0%81%E9%97%AE%E9%A2%98&amp;srcUrl=http://www.yakjuly.com/2011/05/mysql-dump-import-chinese-problem-solution.html&amp;srcTitle=%E6%95%B0%E6%8D%AE%E5%BA%93%E5%A4%87%E4%BB%BD%E5%AF%BC%E5%85%A5%E4%B9%B1%E7%A0%81%E9%97%AE%E9%A2%98&amp;snippet=%E5%A4%87%E4%BB%BD%E6%95%B0%E6%8D%AE%E5%BA%93%E6%97%B6%E9%9C%80%E8%A6%81%E7%9F%A5%E9%81%93%E5%8E%9F%E6%9D%A5%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A%84%E6%96%87%E4%BB%B6%E7%BC%96%E7%A0%81%E6%A0%BC%E5%BC%8F%E6%98%AF%E4%BB%80%E4%B9%88%E3%80%82%0D%0A%0D%0Ashow%20variables%20like%20%22%25character%25%22%0D%0A%0D%0A%E7%9C%8B%E5%88%B0%E7%9A%84%E7%BB%93%E6%9E%9C%E5%8F%AF%E8%83%BD%E6%98%AFlatin1%E5%8F%AF%E8%83%BD%E6%98%AFgbk%E5%8F%AF%E8%83%BD%E6%98%AFutf8%20%E4%BB%A3%E8%A1%A8%E7%9D%80%E4%BD%A0%E5%9C%A8%E5%8E%9F%E6%9D%A5%E6%95%B0%E6%8D%AE%E5%AD%98%E5%82%A8%E6%96%87%E5%AD%97%E7%BC%96%E7%A0%81%E7%9A%84%E6%A0%BC%E5%BC%8F%EF%BC%8C%E5%9C%A8%E5%A4%87%E4%BB%BD%E6%97%B6%E9%9C%80%E8%A6%81%E8%AE%BE%E5%AE%9A--default-character-set%E3%80%82%E4%B8%8D%E8%AE%BE%E5%AE%9A%E5%88%99%E9%BB%98%E8%AE%A4" rel="nofollow" class="external" title="Add this to Google Reader">Add this to Google Reader</a>
		</li>
		<li class="shr-mail">
			<a href="mailto:?subject=%22%E6%95%B0%E6%8D%AE%E5%BA%93%E5%A4%87%E4%BB%BD%E5%AF%BC%E5%85%A5%E4%B9%B1%E7%A0%81%E9%97%AE%E9%A2%98%22&amp;body=Link: http://www.yakjuly.com/2011/05/mysql-dump-import-chinese-problem-solution.html (sent via shareaholic)%0D%0A%0D%0A----%0D%0A %E5%A4%87%E4%BB%BD%E6%95%B0%E6%8D%AE%E5%BA%93%E6%97%B6%E9%9C%80%E8%A6%81%E7%9F%A5%E9%81%93%E5%8E%9F%E6%9D%A5%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A%84%E6%96%87%E4%BB%B6%E7%BC%96%E7%A0%81%E6%A0%BC%E5%BC%8F%E6%98%AF%E4%BB%80%E4%B9%88%E3%80%82%0D%0A%0D%0Ashow%20variables%20like%20%22%25character%25%22%0D%0A%0D%0A%E7%9C%8B%E5%88%B0%E7%9A%84%E7%BB%93%E6%9E%9C%E5%8F%AF%E8%83%BD%E6%98%AFlatin1%E5%8F%AF%E8%83%BD%E6%98%AFgbk%E5%8F%AF%E8%83%BD%E6%98%AFutf8%20%E4%BB%A3%E8%A1%A8%E7%9D%80%E4%BD%A0%E5%9C%A8%E5%8E%9F%E6%9D%A5%E6%95%B0%E6%8D%AE%E5%AD%98%E5%82%A8%E6%96%87%E5%AD%97%E7%BC%96%E7%A0%81%E7%9A%84%E6%A0%BC%E5%BC%8F%EF%BC%8C%E5%9C%A8%E5%A4%87%E4%BB%BD%E6%97%B6%E9%9C%80%E8%A6%81%E8%AE%BE%E5%AE%9A--default-character-set%E3%80%82%E4%B8%8D%E8%AE%BE%E5%AE%9A%E5%88%99%E9%BB%98%E8%AE%A4" rel="nofollow" class="external" title="Email this to a friend?">Email this to a friend?</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=%E6%95%B0%E6%8D%AE%E5%BA%93%E5%A4%87%E4%BB%BD%E5%AF%BC%E5%85%A5%E4%B9%B1%E7%A0%81%E9%97%AE%E9%A2%98+-+http://www.yakjuly.com/2011/05/mysql-dump-import-chinese-problem-solution.html&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

 <img src="http://www.yakjuly.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=157" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://www.yakjuly.com/2011/05/mysql-dump-import-chinese-problem-solution.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>系统接口设计</title>
		<link>http://www.yakjuly.com/2011/05/multi-system-interface-desgin.html</link>
		<comments>http://www.yakjuly.com/2011/05/multi-system-interface-desgin.html#comments</comments>
		<pubDate>Tue, 24 May 2011 12:05:02 +0000</pubDate>
		<dc:creator>yakjuly</dc:creator>
				<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.yakjuly.com/?p=155</guid>
		<description><![CDATA[最近的工作redmine的二次开发，需要频繁的与别的系统打交道，在写接口这方面的代码 有了点心得写下来 分享一下。 首先 redmine作为一个前端展示给客户的系统，需要和 翻译系统，代码管理系统，SDK管理系统交互。 这三个系统 通过普通的Net::HTTP请求，返回内容。 返回内容格式不尽相同， 1.翻译系统直接返回 xml字符串， 2.sdk系统返回 {&#8220;code&#8221;: &#8220;200&#8243;, &#8220;data&#8221;: &#8220;xxxx&#8221;} 或 {&#8220;code&#8221;: &#8220;400&#8243;, &#8220;data&#8221;: &#8221; xxxx&#8221;, &#8220;message&#8221;: &#8220;xx wrong&#8221;} 3.代码管理系统返回 {&#8220;status&#8221;: 0, &#8220;val&#8221;: &#8220;xxxxx&#8221;, } 或  {&#8220;status&#8221; : -1, &#8220;err&#8221;: {&#8220;message&#8221;: &#8220;xxxx&#8221;, &#8220;event&#8221;: &#8220;xxx&#8221;}} 我希望无论请求哪个接口都能在日志种记录下来，发生错误时能有合适的客户提示，代码不要重复同时也要便于修改。 定义加强版Net::HTTP require "net/http" require "tempfile" require "base64" require "digest" class EnhancedNetHttp class Error &#60; [...]]]></description>
			<content:encoded><![CDATA[<p>最近的工作redmine的二次开发，需要频繁的与别的系统打交道，在写接口这方面的代码 有了点心得写下来 分享一下。</p>
<p>首先 redmine作为一个前端展示给客户的系统，需要和 翻译系统，代码管理系统，SDK管理系统交互。</p>
<p>这三个系统 通过普通的Net::HTTP请求，返回内容。</p>
<p>返回内容格式不尽相同，</p>
<p>1.翻译系统直接返回 xml字符串，</p>
<p>2.sdk系统返回 {&#8220;code&#8221;: &#8220;200&#8243;, &#8220;data&#8221;: &#8220;xxxx&#8221;} 或 {&#8220;code&#8221;: &#8220;400&#8243;, &#8220;data&#8221;: &#8221; xxxx&#8221;, &#8220;message&#8221;: &#8220;xx wrong&#8221;}</p>
<p>3.代码管理系统返回 {&#8220;status&#8221;: 0, &#8220;val&#8221;: &#8220;xxxxx&#8221;, } 或  {&#8220;status&#8221; : -1, &#8220;err&#8221;: {&#8220;message&#8221;: &#8220;xxxx&#8221;, &#8220;event&#8221;: &#8220;xxx&#8221;}}</p>
<p>我希望无论请求哪个接口都能在日志种记录下来，发生错误时能有合适的客户提示，代码不要重复同时也要便于修改。</p>
<p>定义加强版Net::HTTP</p>
<pre class="brush:ruby">require "net/http"
require "tempfile"
require "base64"
require "digest"

class EnhancedNetHttp

  class Error &lt; StandardError; end

  @@default_logger = Logger.new(Rails.root.join("log/net_http.log"))
  def self.default_logger
    @@default_logger
  end

  USER_AGENT    = "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/523.10.6 (KHTML, like Gecko) Version/3.0.4 Safari/523.10.6"
  BOUNDARY      = "---xxxxxxxxxxxxxxxxxx"
  CONTENT_TYPE  = "multipart/form-data; boundary=#{BOUNDARY};"

  attr_accessor :error_klass

  def initialize(*args)
    options = args.extract_options!
    self.logger = options[:logger]
    self.error_klass = options[:error_klass]
  end

  def logger
    @logger || EnhancedNetHttp.default_logger
  end

  def logger=(new_logger)
    @logger = new_logger
  end

  def error_klass
    @error_klass || EnhancedNetHttp::Error
  end

  def error_klass=(new_error_klass)
    @error_klass = new_error_klass
  end

  def post_multipart_form(url, params = {})
    uri = URI.parse(url)
    http = Net::HTTP.new(uri.host, uri.port)

    http.request(multipart_request(uri, params))
  end

  def post_form(url, params = {})
    uri = URI.parse(url)

    response = Net::HTTP.post_form(uri, params)

    if response.is_a?(Net::HTTPSuccess)
      logger.info format_message(uri, "POST", response, params)
    else
      raise error_klass.new(format_message(uri, "POST", response, params))
    end
    response
  end

  def get_response(url)
    uri = URI.parse(url)
    response = Net::HTTP.get_response(URI.parse(url))

    if response.is_a?(Net::HTTPSuccess)
      logger.info format_message(uri, "GET", response)
    else
      raise error_klass.new(format_message(uri, "GET", response))
    end
    response
  end

  def oauth_post(url, params = {})
    uri           = URI.parse(url)
    auth_header   = oauth_header(url, params)
    data          = ActiveSupport::JSON.encode(params)

    response = Net::HTTP.start(uri.host, uri.port) {|http|
      http.request_post(uri.path, data, auth_header)
    }

    if response.is_a?(Net::HTTPSuccess)
      logger.info format_message(uri, "OAUTH POST", response, params)
    else
      raise error_klass.new(format_message(uri, "OAUTH POST", response, params))
    end
    response
  end

  private

  def oauth_header(url, params= {})
    oauth = {
      "oauth_consumer_key" =&gt; "xxxxxxxxxxxx",
      "oauth_signature_method" =&gt; "HMAC-SHA1",
      "oauth_timestamp" =&gt; Time.now.to_i,
      "oauth_nonce" =&gt; Time.now.to_i,
      "oauth_version" =&gt; "1.0"
    }
    data = ActiveSupport::JSON.encode(params)
    param_string  = oauth.keys.sort.collect{ |item| "#{item}=#{oauth[item]}"}.join('&amp;')
    base          = 'POST&amp;' &lt;&lt; CGI.escape(url) &lt;&lt; '&amp;' &lt;&lt; CGI.escape(param_string)
    base          &lt;&lt; "&amp;#{CGI.escape(data)}" unless params.blank?
    signature = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), "xxxxxxxxxxxxxxxxxxxxxx", base))
    oauth["oauth_signature"] = signature.strip
    header_info = oauth.keys.sort.collect{ |item| "#{item}=\"#{oauth[item]}\""}.join(',')
    {"Authorization" =&gt; "OAuth:" + header_info}
  end

  def multipart_request(uri, params = {})
    request = Net::HTTP::Post.new(uri.request_uri)
    post_body = []
    params.stringify_keys.each do |key, value|
      post_body &lt;&lt; "--#{BOUNDARY}\r\n"
      post_body &lt;&lt; to_multipart(key, value)
    end
    post_body &lt;&lt; "--#{BOUNDARY}--\r\n" unless post_body.empty?

    request.body = post_body.join
    request["Content-Type"] = CONTENT_TYPE
    request["User-Agent"] = USER_AGENT
    request
  end

  def to_multipart(key, value)
    if value.class == Tempfile
      "Content-Disposition: form-data; name=\"#{CGI::escape(key)}\"; filename=\"#{value.original_filename}\"\r\n" &lt;&lt;
        "Content-Type: \"application/octet-stream\"\r\n\r\n#{value.read}\r\n"
    elsif value.respond_to?(:path) and value.respond_to?(:read)
      "Content-Disposition: form-data; name=\"#{CGI::escape(key)}\"; filename=\"#{value.path}\"\r\n" &lt;&lt;
        "Content-Type: \"application/octet-stream\"\r\n\r\n#{value.read}\r\n"
    else
      "Content-Disposition: form-data; name=\"#{CGI::escape(key)}\"\r\n\r\n#{value.to_s}\r\n"
    end
  end

  def format_message(uri, method, response, params = {})
    "#{Time.now} [#{response.class.name}] #{method} #{uri} \n #{params.inspect if params.present?}"
  end

end</pre>
<p>这里的EnhancedNetHttp就是相当于一个NetHttp请求的代理，这个是读<a title="张哲 Gary" href="http://zhangzhe.heroku.com" target="_blank">Gary</a>的blog想到的。</p>
<p>以前在javascript中 经常写回调函数，用于在多个方法中交互，在这里我就想 为何不传入错误类型 来细化请求错误的原因呢？</p>
<p>于是在EnhancedNetHttp里就有了 logger 与 error_klass</p>
<p>post_multipart_form 和 oauth_post 则是在Net::HTTP的基础上增加的2个方法。</p>
<p>调用接口时则使用EnhancedNetHttp类来负责发送请求。</p>
<p>接下来就非常简单了 对三个不同系统的接口写三个Service，配置不通的错误类型和日志。这样方便反映错误。</p>
<p>实际上调用接口出错 我认为发让系统邮件给开发人员会比较好，第一时间发现错误总比过了很久去日志里查要好。</p>
<pre class="brush:ruby">class Sdk::Service
  class Error &lt; StandardError; end

  @@logger    = Logger.new(Sdk.config[:log_path])
  cattr_reader :logger

  def self.net_http
    @net_http ||= EnhancedNetHttp.new(:logger =&gt; logger, :error_klass =&gt; Sdk::Service::Error)
  end

  attr_accessor :host, :platform

  def initialize(*args)
    options = args.extract_options!
    options.assert_valid_keys(:host, :platform)

    self.host = options[:host] =~ /\/$/ ? options[:host].chop : options[:host]
    self.platform = options[:platform]
  end

  def admin_user_query(user_id)
    url = "/xxxxxxx/xxxxx"
    params = {:abc =&gt; user_id, :efg =&gt; 456}
    get_json(url, params)
  end

  def admin_user_xml(version_id)
    url = "/bbbbb/cccccl"
    params = {:version =&gt; version_id}
    get_xml(url, params)
  end

  def admin_owned_item_xml(version_id)
    url = "/ttttttttt/uuuuu"
    params = {:version =&gt; version_id}
    get_xml(url, params)
  end

  private

  def get_response(full_url, params)
    url = "#{full_url}?#{params.to_query}"
    Sdk::Service.net_http.get_response(url)
  end

  def oauth_post(url, params = {})
    Sdk::Service.net_http.oauth_post(url, params)
  end

  def get_xml(url_path, params = {})
    response = oauth_post("#{host}#{url_path}", params)
    Zlib::GzipReader.new(StringIO.new(response.body)).read
  end

  def get_json(url_path, params = {})
    response = oauth_post("#{host}#{url_path}", params)
    json     = ActiveSupport::JSON.decode response.body
    if json["code"] == 200
      HashWithIndifferentAccess.new(json["data"])
    else
      logger.error("Response [#{json['code']}]: #{url_path} - #{json.inspect}")
      raise Sdk::Service::Error.new(json["message"])
    end
  end

end</pre>
<p>在重构代码的时候还用到一个有趣又简单的代理</p>
<pre class="brush:ruby">class Translate::Proxy

  attr_accessor :service

  def initialize(service)
    self.service = service
  end

  def cache_exception(&amp;block)
    yield
  rescue Exception =&gt; e
    raise Translate::Service::Error.new(e.message)
  end

  def method_missing(method_sym, *arguments, &amp;block)
    if self.service.respond_to? method_sym
      cache_exception { self.service.send(method_sym, *arguments, &amp;block) }
    else
      super
    end
  end

end</pre>
<p>这个Proxy负责统一抛出的错误类型，如果是Net::HTTP请求发生错误 在这里则也会进行一次转换类型。</p>
<p>总结：优秀的程序员，总是能把代码组织的非常漂亮。多学习 多思考 多运用 会让生活变的更美好。</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-wealth">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://www.yakjuly.com/2011/05/multi-system-interface-desgin.html/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.yakjuly.com/2011/05/multi-system-interface-desgin.html&amp;t=%E7%B3%BB%E7%BB%9F%E6%8E%A5%E5%8F%A3%E8%AE%BE%E8%AE%A1" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-friendfeed">
			<a href="http://www.friendfeed.com/share?title=%E7%B3%BB%E7%BB%9F%E6%8E%A5%E5%8F%A3%E8%AE%BE%E8%AE%A1&amp;link=http://www.yakjuly.com/2011/05/multi-system-interface-desgin.html" rel="nofollow" class="external" title="Share this on FriendFeed">Share this on FriendFeed</a>
		</li>
		<li class="shr-googlebookmarks">
			<a href="http://www.google.com/bookmarks/mark?op=add&amp;bkmk=http://www.yakjuly.com/2011/05/multi-system-interface-desgin.html&amp;title=%E7%B3%BB%E7%BB%9F%E6%8E%A5%E5%8F%A3%E8%AE%BE%E8%AE%A1" rel="nofollow" class="external" title="Add this to Google Bookmarks">Add this to Google Bookmarks</a>
		</li>
		<li class="shr-googlereader">
			<a href="http://www.google.com/reader/link?url=http://www.yakjuly.com/2011/05/multi-system-interface-desgin.html&amp;title=%E7%B3%BB%E7%BB%9F%E6%8E%A5%E5%8F%A3%E8%AE%BE%E8%AE%A1&amp;srcUrl=http://www.yakjuly.com/2011/05/multi-system-interface-desgin.html&amp;srcTitle=%E7%B3%BB%E7%BB%9F%E6%8E%A5%E5%8F%A3%E8%AE%BE%E8%AE%A1&amp;snippet=%E6%9C%80%E8%BF%91%E7%9A%84%E5%B7%A5%E4%BD%9Credmine%E7%9A%84%E4%BA%8C%E6%AC%A1%E5%BC%80%E5%8F%91%EF%BC%8C%E9%9C%80%E8%A6%81%E9%A2%91%E7%B9%81%E7%9A%84%E4%B8%8E%E5%88%AB%E7%9A%84%E7%B3%BB%E7%BB%9F%E6%89%93%E4%BA%A4%E9%81%93%EF%BC%8C%E5%9C%A8%E5%86%99%E6%8E%A5%E5%8F%A3%E8%BF%99%E6%96%B9%E9%9D%A2%E7%9A%84%E4%BB%A3%E7%A0%81%20%E6%9C%89%E4%BA%86%E7%82%B9%E5%BF%83%E5%BE%97%E5%86%99%E4%B8%8B%E6%9D%A5%20%E5%88%86%E4%BA%AB%E4%B8%80%E4%B8%8B%E3%80%82%0D%0A%0D%0A%E9%A6%96%E5%85%88%20redmine%E4%BD%9C%E4%B8%BA%E4%B8%80%E4%B8%AA%E5%89%8D%E7%AB%AF%E5%B1%95%E7%A4%BA%E7%BB%99%E5%AE%A2%E6%88%B7%E7%9A%84%E7%B3%BB%E7%BB%9F%EF%BC%8C%E9%9C%80%E8%A6%81%E5%92%8C%20%E7%BF%BB%E8%AF%91%E7%B3%BB%E7%BB%9F%EF%BC%8C%E4%BB%A3%E7%A0%81%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F%EF%BC%8CSDK%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F%E4%BA%A4%E4%BA%92%E3%80%82%0D%0A%0D%0A%E8%BF%99%E4%B8%89%E4%B8%AA%E7" rel="nofollow" class="external" title="Add this to Google Reader">Add this to Google Reader</a>
		</li>
		<li class="shr-mail">
			<a href="mailto:?subject=%22%E7%B3%BB%E7%BB%9F%E6%8E%A5%E5%8F%A3%E8%AE%BE%E8%AE%A1%22&amp;body=Link: http://www.yakjuly.com/2011/05/multi-system-interface-desgin.html (sent via shareaholic)%0D%0A%0D%0A----%0D%0A %E6%9C%80%E8%BF%91%E7%9A%84%E5%B7%A5%E4%BD%9Credmine%E7%9A%84%E4%BA%8C%E6%AC%A1%E5%BC%80%E5%8F%91%EF%BC%8C%E9%9C%80%E8%A6%81%E9%A2%91%E7%B9%81%E7%9A%84%E4%B8%8E%E5%88%AB%E7%9A%84%E7%B3%BB%E7%BB%9F%E6%89%93%E4%BA%A4%E9%81%93%EF%BC%8C%E5%9C%A8%E5%86%99%E6%8E%A5%E5%8F%A3%E8%BF%99%E6%96%B9%E9%9D%A2%E7%9A%84%E4%BB%A3%E7%A0%81%20%E6%9C%89%E4%BA%86%E7%82%B9%E5%BF%83%E5%BE%97%E5%86%99%E4%B8%8B%E6%9D%A5%20%E5%88%86%E4%BA%AB%E4%B8%80%E4%B8%8B%E3%80%82%0D%0A%0D%0A%E9%A6%96%E5%85%88%20redmine%E4%BD%9C%E4%B8%BA%E4%B8%80%E4%B8%AA%E5%89%8D%E7%AB%AF%E5%B1%95%E7%A4%BA%E7%BB%99%E5%AE%A2%E6%88%B7%E7%9A%84%E7%B3%BB%E7%BB%9F%EF%BC%8C%E9%9C%80%E8%A6%81%E5%92%8C%20%E7%BF%BB%E8%AF%91%E7%B3%BB%E7%BB%9F%EF%BC%8C%E4%BB%A3%E7%A0%81%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F%EF%BC%8CSDK%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F%E4%BA%A4%E4%BA%92%E3%80%82%0D%0A%0D%0A%E8%BF%99%E4%B8%89%E4%B8%AA%E7" rel="nofollow" class="external" title="Email this to a friend?">Email this to a friend?</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=%E7%B3%BB%E7%BB%9F%E6%8E%A5%E5%8F%A3%E8%AE%BE%E8%AE%A1+-+http://www.yakjuly.com/2011/05/multi-system-interface-desgin.html&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

 <img src="http://www.yakjuly.com/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=155" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://www.yakjuly.com/2011/05/multi-system-interface-desgin.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
