Format and remove 08.4.md spaces

This commit is contained in:
vCaesar
2017-06-10 12:16:14 +08:00
parent 6955edb1c4
commit be43d55cc1

View File

@@ -47,40 +47,40 @@ http的服务端代码实现如下
```Go
package main
package main
import (
import (
"errors"
"fmt"
"net/http"
"net/rpc"
)
)
type Args struct {
type Args struct {
A, B int
}
}
type Quotient struct {
type Quotient struct {
Quo, Rem int
}
}
type Arith int
type Arith int
func (t *Arith) Multiply(args *Args, reply *int) error {
func (t *Arith) Multiply(args *Args, reply *int) error {
*reply = args.A * args.B
return nil
}
}
func (t *Arith) Divide(args *Args, quo *Quotient) error {
func (t *Arith) Divide(args *Args, quo *Quotient) error {
if args.B == 0 {
return errors.New("divide by zero")
}
quo.Quo = args.A / args.B
quo.Rem = args.A % args.B
return nil
}
}
func main() {
func main() {
arith := new(Arith)
rpc.Register(arith)
@@ -90,7 +90,7 @@ http的服务端代码实现如下
if err != nil {
fmt.Println(err.Error())
}
}
}
```
通过上面的例子可以看到我们注册了一个Arith的RPC服务然后通过`rpc.HandleHTTP`函数把该服务注册到了HTTP协议上然后我们就可以利用http的方式来传递数据了。
@@ -99,24 +99,24 @@ http的服务端代码实现如下
```Go
package main
package main
import (
import (
"fmt"
"log"
"net/rpc"
"os"
)
)
type Args struct {
type Args struct {
A, B int
}
}
type Quotient struct {
type Quotient struct {
Quo, Rem int
}
}
func main() {
func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: ", os.Args[0], "server")
os.Exit(1)
@@ -143,15 +143,15 @@ http的服务端代码实现如下
}
fmt.Printf("Arith: %d/%d=%d remainder %d\n", args.A, args.B, quot.Quo, quot.Rem)
}
}
```
我们把上面的服务端和客户端的代码分别编译,然后先把服务端开启,然后开启客户端,输入代码,就会输出如下信息:
```Go
$ ./http_c localhost
Arith: 17*8=136
Arith: 17/8=2 remainder 1
$ ./http_c localhost
Arith: 17*8=136
Arith: 17/8=2 remainder 1
```
通过上面的调用可以看到参数和返回值是我们定义的struct类型在服务端我们把它们当做调用函数的参数的类型在客户端作为`client.Call`的第23两个参数的类型。客户端最重要的就是这个Call函数它有3个参数第1个要调用的函数的名字第2个是要传递的参数第3个要返回的参数(注意是指针类型)通过上面的代码例子我们可以发现使用Go的RPC实现相当的简单方便。
@@ -159,41 +159,41 @@ http的服务端代码实现如下
上面我们实现了基于HTTP协议的RPC接下来我们要实现基于TCP协议的RPC服务端的实现代码如下所示
```Go
package main
package main
import (
import (
"errors"
"fmt"
"net"
"net/rpc"
"os"
)
)
type Args struct {
type Args struct {
A, B int
}
}
type Quotient struct {
type Quotient struct {
Quo, Rem int
}
}
type Arith int
type Arith int
func (t *Arith) Multiply(args *Args, reply *int) error {
func (t *Arith) Multiply(args *Args, reply *int) error {
*reply = args.A * args.B
return nil
}
}
func (t *Arith) Divide(args *Args, quo *Quotient) error {
func (t *Arith) Divide(args *Args, quo *Quotient) error {
if args.B == 0 {
return errors.New("divide by zero")
}
quo.Quo = args.A / args.B
quo.Rem = args.A % args.B
return nil
}
}
func main() {
func main() {
arith := new(Arith)
rpc.Register(arith)
@@ -212,14 +212,14 @@ http的服务端代码实现如下
rpc.ServeConn(conn)
}
}
}
func checkError(err error) {
func checkError(err error) {
if err != nil {
fmt.Println("Fatal error ", err.Error())
os.Exit(1)
}
}
}
```
上面这个代码和http的服务器相比不同在于:在此处我们采用了TCP协议然后需要自己控制连接当有客户端连接上来后我们需要把这个连接交给rpc来处理。
@@ -229,24 +229,24 @@ http的服务端代码实现如下
```Go
package main
package main
import (
import (
"fmt"
"log"
"net/rpc"
"os"
)
)
type Args struct {
type Args struct {
A, B int
}
}
type Quotient struct {
type Quotient struct {
Quo, Rem int
}
}
func main() {
func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: ", os.Args[0], "server:port")
os.Exit(1)
@@ -273,7 +273,7 @@ http的服务端代码实现如下
}
fmt.Printf("Arith: %d/%d=%d remainder %d\n", args.A, args.B, quot.Quo, quot.Rem)
}
}
```
这个客户端代码和http的客户端代码对比唯一的区别一个是DialHTTP一个是Dial(tcp),其他处理一模一样。
@@ -283,42 +283,42 @@ JSON RPC是数据编码采用了JSON而不是gob编码其他和上面介
```Go
package main
package main
import (
import (
"errors"
"fmt"
"net"
"net/rpc"
"net/rpc/jsonrpc"
"os"
)
)
type Args struct {
type Args struct {
A, B int
}
}
type Quotient struct {
type Quotient struct {
Quo, Rem int
}
}
type Arith int
type Arith int
func (t *Arith) Multiply(args *Args, reply *int) error {
func (t *Arith) Multiply(args *Args, reply *int) error {
*reply = args.A * args.B
return nil
}
}
func (t *Arith) Divide(args *Args, quo *Quotient) error {
func (t *Arith) Divide(args *Args, quo *Quotient) error {
if args.B == 0 {
return errors.New("divide by zero")
}
quo.Quo = args.A / args.B
quo.Rem = args.A % args.B
return nil
}
}
func main() {
func main() {
arith := new(Arith)
rpc.Register(arith)
@@ -337,14 +337,14 @@ JSON RPC是数据编码采用了JSON而不是gob编码其他和上面介
jsonrpc.ServeConn(conn)
}
}
}
func checkError(err error) {
func checkError(err error) {
if err != nil {
fmt.Println("Fatal error ", err.Error())
os.Exit(1)
}
}
}
```
通过示例我们可以看出 json-rpc是基于TCP协议实现的目前它还不支持HTTP方式。
@@ -352,24 +352,24 @@ JSON RPC是数据编码采用了JSON而不是gob编码其他和上面介
请看客户端的实现代码:
```Go
package main
package main
import (
import (
"fmt"
"log"
"net/rpc/jsonrpc"
"os"
)
)
type Args struct {
type Args struct {
A, B int
}
}
type Quotient struct {
type Quotient struct {
Quo, Rem int
}
}
func main() {
func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: ", os.Args[0], "server:port")
log.Fatal(1)
@@ -396,7 +396,7 @@ JSON RPC是数据编码采用了JSON而不是gob编码其他和上面介
}
fmt.Printf("Arith: %d/%d=%d remainder %d\n", args.A, args.B, quot.Quo, quot.Rem)
}
}
```
## 总结