gin framework RPC service

RPC architecture:

After a brief understanding of the definition and advantages and disadvantages of microservices, before we formally learn the microservice framework, we need to first understand the RPC architecture. Through RPC, we can visualize the workflow of microservices.

3.1 The concept of RPC

RPC (Remote Procedure Call Protocol), is the abbreviation of remote procedure call, in layman's terms, it is to call a function in the distance. Corresponding to it is the local function call, let's take a look at it first - the local function call. When we write the following code: rule

result := Add(1,2)

We know that we passed in two parameters of 1 and 2, called an Add function in the local code, and got the return value of result. At this time, the parameters, return value, and code segment are all in the process space, which is a local function call. Is there a way we can call a function across processes (so called "remote", in a typical case, this process is deployed on another server)?

RPC: remote communication-an application layer protocol (same layer as http protocol). The bottom layer is implemented using TCP. It is very simple to implement RPC in golang, and there are packaged official libraries and some third-party libraries to provide support. Go RPC can use tcp or http to transfer data, and can use various types of codecs for the data to be transferred. Golang's official net/rpc library uses encoding/gob for encoding and decoding, and supports tcp or http data transmission methods. Since other languages ​​​​do not support gob encoding and decoding methods, the RPC method implemented using the net/rpc library cannot be used for cross-language calls.

Golang also officially provides the net/rpc/jsonrpc library to implement the RPC method. JSON RPC uses JSON for data encoding and decoding, thus supporting cross-language calls. However, the current jsonrpc library is implemented based on the tcp protocol, and does not support the use of http for data transmission for the time being. In addition to the official rpc library provided by golang, there are many third-party libraries that provide support for the implementation of RPC in golang. Most third-party rpc libraries use protobuf for data encoding and decoding, and automatically generate rpc method definitions and Service registration code, it is very convenient to call RPC service in golang.

The following is a Helloworld:

1. Create a project to continue building the rpc server:

package main

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

type Hello struct {
}

/*
1、方法只能有两个可序列化的参数,其中第二个参数是指针类型
	req表示获取客户端传过来的数据
	res表示给客户端返回数据
2、方法要返回一个error类型,同时必须是公开的方法。
3、req和res的类型不能是: channel (通道)、func (函数)均不能进行序列化
*/

func (this Hello) SayHello(req string, res *string) error {
	fmt.Println(req, "=======================.")
	*res = req + "你好"
	return nil
}

func main() {

	//注册RPC服务
	err := rpc.RegisterName("hello", new(Hello))  //注册服务 进行创建 一个Hello结构题对象

	if err != nil {
		fmt.Print(err)
	}

	//监听端口
	listen, err := net.Listen("tcp", "127.0.0.1:8080")
	if err != nil {
		fmt.Println(err)
	}

	//3、应用退出的时候关闭监听端口
	defer listen.Close()

	//放入for循环中使值一直处于建立连接中
	for {

		fmt.Println("开始建立连接")
		//4、建立连接
		accept, err := listen.Accept()

		if err != nil {
			fmt.Println(err)
		}
		//5、绑定服务
		rpc.ServeConn(accept)
	}
    
}

2. Then create a project to build the client for access:

package main

import (
	"fmt"
	"net/rpc"
)

func main() {

	//1、使用rpc.Dial 和rpc微服务建立远程连接
	dial, err := rpc.Dial("tcp", "127.0.0.1:8080")
	if err != nil {
		fmt.Print(err)
	}

	//2、当客户端退出的时候关闭连接
	defer dial.Close()

	//调用远程函数
	var reply string
	/**
	第一个参数表示 我们的远程服务,名称加上要调用的方法名,
	第二个参数表示的就是我们传递过去的数据,
	第三个参数表示我们接收到的值
	*/
    //注意: hello 是我们rpc也买进行注册的服务名称
	err = dial.Call("hello.SayHello", "我是客户端", &reply)
	if err != nil {
		fmt.Println(err)
	}

	//获取返回连接
	fmt.Println(reply)

}

Guess you like

Origin blog.csdn.net/qq_63946922/article/details/127643363