go rpc

阅读更多
go rpc_第1张图片


package hello

/***
Go语言的RPC包的路径为net/rp
中Hello方法必须满足Go语言的RPC规则:方法只能有两个可序列化的参数,其
中第二个参数是指针类型,并且返回一个error类型,同时必须是公开的方法。
 */
type HelloService struct {}

func (p *HelloService) Hello(request string,reply *string) error {
	*reply = "hello:" + request
	return nil
}


package main

import (
	"log"
	"net"
	"net/rpc"
	"./services/hello"
)
func main(){
	rpc.RegisterName("HelloService",new(HelloService))
    listener ,err := net.Listen("tcp",":1234")
    if err != nil {
    	log.Fatal("listen tcp error : ",err)
	}

    conn,err := listener.Accept()
    if err!=nil{
		log.Fatal("listen accept error : ",err)
	}

    rpc.ServeConn(conn)
}



package main

import (
	"fmt"
	"log"
	"net/rpc"
)

func main(){
	client,err := rpc.Dial("tcp","127.0.0.7:1234")
	if err!=nil {
		log.Fatal("Erro :",err)
	}

	var reply string
	err = client.Call("HelloService.Hello","hello",&reply)
    if err!=nil {
		log.Fatal("Erro :",err)
	}

	fmt.Printf("reply :%s",reply)
	fmt.Println(reply)
}


protoc 

https://github.com/protocolbuffers/protobuf/releases

命令
hello.proto

syntax = "proto3";
package main;
message String{
string value =1;
}


拷贝 protoc-gen-go.exe protoc.exe到 C:\Windows\System32

protoc -I=源地址 --java_out=目标地址  源地址/xxx.proto

protoc  -I=D:\gopro\src\protobuf  --go_out=D:\gopro\src\protobuf\go  D:\gopro\src\protobuf\hello.proto
protoc-gen-go  -I=D:\gopro\src\protobuf  --go_out=D:\gopro\src\protobuf\go  D:\gopro\src\protobuf\hello.proto




在protoc-gen-go内部已经集成了一个名字为 grpc 的插件,可以针对gRPC生
成代码
protoc  -I=D:\gopro\src\protobuf  --go_out=plugins=grpc:. D:\gopro\src\protobuf\hello.proto

cd D:\gopromod\z.book\protocol
protoc --go_out=plugins=grpc:. hello.proto


rpc 资料
https://www.bookstack.cn/read/go-rpc-programming-guide/part1-thrift.md



  • go rpc_第2张图片
  • 大小: 7.6 KB
  • 查看图片附件

你可能感兴趣的:(go,windows)