Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How rpc communication is conducted

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of how rpc communication is carried out, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how rpc communication is carried out. Let's take a look at it.

RPC (Remote Procedure Call Protocol)-remote procedure call protocol, which is a protocol that requests services from remote computer programs over a network without knowing the underlying network technology. The protocol allows programs running on one computer to call subroutines of another computer, and programmers do not need to program for this interaction. The RPC protocol assumes the existence of certain transport protocols, such as TCP or UDP, to carry information data between communication programs. In the OSI network communication model, RPC spans the transport layer and the application layer. RPC makes it easier to develop applications, including web-based distributed multiprograms.

Basic concept

RPC (Remote Procedure Call) remote procedure call, simply understood as one node requesting services provided by another node

Local procedure call: if you need to age+1 the local student object, you can implement an addAge () method, pass in the student object, update the age and return it. The function body of the local method call is specified by the function pointer.

Remote procedure call: during the above operation, if the addAge () method is on the server side and the function body of the function executing the function is on the remote machine, how can you tell the machine that this method needs to be called?

Today, we will demonstrate through an example code, step by step to see how the communication of rpc is carried out. Interested friends can implement the code, debug themselves, and check the parameters of each step.

Package com.mashibing.rpc.common

Import java.io.Serializable

Public class User implements Serializable {

Private static final long serialVersionUID = 1L

Private Integer id

Private String name

Public User (Integer id, String name) {

This.id = id

This.name = name

}

Public Integer getId () {

Return id

}

Public String getName () {

Return name

}

Public void setId (Integer id) {

This.id = id

}

Public void setName (String name) {

This.name = name

}

@ Override

Public String toString () {

Return "User {" +

"id=" + id +

", name='" + name +'\'+

'}'

}

}

Package com.mashibing.rpc.common

Public interface IUserService {

Public User findUserById (Integer id)

} package com.mashibing.rpc01

Import com.mashibing.rpc.common.IUserService

Import com.mashibing.rpc.common.User

Public class UserServiceImpl implements IUserService {

@ Override

Public User findUserById (Integer id) {

Return new User (id, "Alice")

}

}

Package com.mashibing.rpc01

Import com.mashibing.rpc.common.User

Import java.io.ByteArrayOutputStream

Import java.io.DataInputStream

Import java.io.DataOutputStream

Import java.net.Socket

Public class Client {

Public static void main (String [] args) throws Exception {

Socket s = new Socket ("127.0.0.1", 8888)

ByteArrayOutputStream baos = new ByteArrayOutputStream ()

DataOutputStream dos = new DataOutputStream (baos)

Dos.writeInt (123)

S.getOutputStream () .write (baos.toByteArray ()

S.getOutputStream () .flush ()

DataInputStream dis = new DataInputStream (s.getInputStream ())

Int id = dis.readInt ()

String name = dis.readUTF ()

User user = new User (id, name)

System.out.println (user)

Dos.close ()

S.close ()

}

}

Package com.mashibing.rpc02

Import com.mashibing.rpc.common.User

Import java.io.ByteArrayOutputStream

Import java.io.DataInputStream

Import java.io.DataOutputStream

Import java.net.Socket

Public class Stub {

Public User findUserById (Integer id) throws Exception {

Socket s = new Socket ("127.0.0.1", 8888)

ByteArrayOutputStream baos = new ByteArrayOutputStream ()

DataOutputStream dos = new DataOutputStream (baos)

Dos.writeInt (123)

S.getOutputStream () .write (baos.toByteArray ()

S.getOutputStream () .flush ()

DataInputStream dis = new DataInputStream (s.getInputStream ())

Int receivedId = dis.readInt ()

String name = dis.readUTF ()

User user = new User (id, name)

Dos.close ()

S.close ()

Return user

}

}

Package com.mashibing.rpc03

Import com.mashibing.rpc.common.IUserService

Import com.mashibing.rpc.common.User

Import java.io.ByteArrayOutputStream

Import java.io.DataInputStream

Import java.io.DataOutputStream

Import java.lang.reflect.InvocationHandler

Import java.lang.reflect.Method

Import java.lang.reflect.Proxy

Import java.net.Socket

/ * *

* and the call to Client does not seem very reasonable (there is only findById code in Stub). If there is a new method of findByName, it will have to be improved again.

* the following way of writing solves the problem of adding methods.

, /

Public class Stub {

Public static IUserService getStub () {

InvocationHandler h = new InvocationHandler () {

@ Override

Public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {

Socket s = new Socket ("127.0.0.1", 8888)

ByteArrayOutputStream baos = new ByteArrayOutputStream ()

DataOutputStream dos = new DataOutputStream (baos)

Dos.writeInt (123)

S.getOutputStream () .write (baos.toByteArray ()

S.getOutputStream () .flush ()

DataInputStream dis = new DataInputStream (s.getInputStream ())

Int id = dis.readInt ()

String name = dis.readUTF ()

User user = new User (id, name)

Dos.close ()

S.close ()

Return user

}

}

Object o = Proxy.newProxyInstance (IUserService.class.getClassLoader (), new Class [] {IUserService.class}, h)

Return (IUserService) o

}

}

Package com.mashibing.rpc04

Import com.mashibing.rpc.common.IUserService

Import com.mashibing.rpc.common.User

Import java.io.ByteArrayOutputStream

Import java.io.DataInputStream

Import java.io.DataOutputStream

Import java.io.ObjectOutputStream

Import java.lang.reflect.InvocationHandler

Import java.lang.reflect.Method

Import java.lang.reflect.Proxy

Import java.net.Socket

/ * *

* but only the method proxy of findByUserId is implemented here. What should I do if I want to implement the proxy of other methods?

* here we need to make improvements from the protocol layer

*

* the server side should also make corresponding processing.

, /

Public class Stub {

Public static IUserService getStub () {

InvocationHandler h = new InvocationHandler () {

@ Override

Public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {

Socket s = new Socket ("127.0.0.1", 8888)

ObjectOutputStream oos = new ObjectOutputStream (s.getOutputStream ())

String methodName = method.getName ()

Class [] parametersTypes = method.getParameterTypes ()

Oos.writeUTF (methodName)

Oos.writeObject (parametersTypes)

Oos.writeObject (args)

Oos.flush ()

DataInputStream dis = new DataInputStream (s.getInputStream ())

Int id = dis.readInt ()

String name = dis.readUTF ()

User user = new User (id, name)

Oos.close ()

S.close ()

Return user

}

}

Object o = Proxy.newProxyInstance (IUserService.class.getClassLoader (), new Class [] {IUserService.class}, h)

Return (IUserService) o

}

}

Package com.mashibing.rpc04

Import com.mashibing.rpc.common.IUserService

Import com.mashibing.rpc.common.User

Import java.io.*

Import java.lang.reflect.Method

Import java.net.ServerSocket

Import java.net.Socket

Public class Server {

Private static boolean running = true

Public static void main (String [] args) throws Exception {

ServerSocket ss = new ServerSocket (8888)

While (running) {

Socket s = ss.accept ()

Process (s)

S.close ()

}

Ss.close ()

}

Private static void process (Socket s) throws Exception {

InputStream in = s.getInputStream ()

OutputStream out = s.getOutputStream ()

ObjectInputStream oos = new ObjectInputStream (in)

DataOutputStream dos = new DataOutputStream (out)

String methodName = oos.readUTF ()

Class [] parameterTypes = (Class []) oos.readObject ()

Object [] args = (Object []) oos.readObject ()

IUserService service = new UserServiceImpl ()

Method method = service.getClass () .getMethod (methodName, parameterTypes)

User user = (User) method.invoke (service, args)

Dos.writeInt (user.getId ())

Dos.writeUTF (user.getName ())

Dos.flush ()

}

}

The returned value is encapsulated in Object, and any type is supported.

This is the end of the article on "how rpc Communications are carried out". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how rpc communication is carried out". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report