当前位置:网站首页>Myrpc version 5
Myrpc version 5
2022-06-30 04:13:00 【Winter dream chaser】
RPC The concept of
Background knowledge
- RPC Basic concepts of , Core functions

common RPC frame
Duboo Basic function
- Telematics
- Transparent remote procedure call based on interface method
- Load balancing
- Service registry
RPC The process
client Call the remote method -> request serialize -> Protocol code -> Network transmission -> Server side -> Deserialization request -> Call the local method to get response -> serialize -> code ->……
Version iteration process
Catalog
from 0 At the beginning RPC Iterative process of :
- version0 edition : Complete a with less than 100 lines of code RPC Example
- version1 edition : Improve the common message format (request,response), The client's dynamic proxy completes the request Encapsulation of message formats
- version2 edition : Support the server to expose multiple service interfaces , Server program abstraction , Normalization
- version3 edition : Use a high-performance network framework netty The realization of network communication , And the refactoring of client code
- version4 edition : Custom message format , Support multiple serialization methods (java Native , json…)
- version5 edition : Implementation of server registration and discovery ,zookeeper As a registry
- version6 edition : Implementation of load balancing strategy
5.MyRPC edition 5
Background knowledge
- zookeeper install , Basic concepts
- understand curator Open source zookeeper Use in the client
Questions in this section
- How to design a registry
Registry Center ( Such as zookeeper) The address is fixed ( For high availability, it is usually a cluster , We can regard it as a black box ), When the server goes online , Register your service and corresponding address in the registry , When the client invokes the service , Go to the registry and find the corresponding server address according to the service name .
zookeeper We can approximate it as a tree directory file system , Is a distributed coordination application , Other registries are EureKa, Nacos etc.
Upgrade process
Premise
- Download decompression Zookeeper [ Address ](https://zookeeper.apache.org/releases.html)
- Learn one zookeeper Examples of startup The official example
- zoo.cfg modify dataDir For an existing directory
- windows Start command : bin/zkServer.cmd
- java Introduced in the project Curator client ,
<!-- This jar Packages should depend on log4j, Do not introduce log4j There will be consoles there will be warn, But it does not affect normal use -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.1.0</version>
</dependency>
to update 1 : introduce zookeeper As a registry
Start a local zookeeper Server side , Default port 2181.zookeeper Client tests are as follows :

First define the service registration interface
// Service registration interface , Two basic functions , register : Save service and address . Inquire about : Find the address according to the service name
public interface ServiceRegister {
void register(String serviceName, InetSocketAddress serverAddress);
InetSocketAddress serviceDiscovery(String serviceName);
}
zookeeper Implementation class of service registration interface
public class ZkServiceRegister implements ServiceRegister{
// curator Provided zookeeper client
private CuratorFramework client;
// zookeeper Root path node
private static final String ROOT_PATH = "MyRPC";
// This is responsible zookeeper Initialization of the client , And with zookeeper The server establishes a connection
public ZkServiceRegister(){
// Exponential time retry
RetryPolicy policy = new ExponentialBackoffRetry(1000, 3);
// zookeeper Your address is fixed , Whether it's a service provider or , Consumers have to connect with it
// sessionTimeoutMs And zoo.cfg Medium tickTime It matters ,
// zk And according to minSessionTimeout And maxSessionTimeout Two parameters readjust the last timeout value . The default values are tickTime Of 2 Times and 20 times
// Use heartbeat monitoring status
this.client = CuratorFrameworkFactory.builder().connectString("127.0.0.1:2181")
.sessionTimeoutMs(40000).retryPolicy(policy).namespace(ROOT_PATH).build();
this.client.start();
System.out.println("zookeeper Successful connection ");
}
@Override
public void register(String serviceName, InetSocketAddress serverAddress){
try {
// serviceName Create a permanent node , When the service provider goes offline , Do not delete the service name , Just delete the address
if(client.checkExists().forPath("/" + serviceName) == null){
client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/" + serviceName);
}
// Path address , One / Represents a node
String path = "/" + serviceName +"/"+ getServiceAddress(serverAddress);
// Temporary node , Delete the node when the server goes offline
client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(path);
} catch (Exception e) {
System.out.println(" This service already exists ");
}
}
// Return the address according to the service name
@Override
public InetSocketAddress serviceDiscovery(String serviceName) {
try {
List<String> strings = client.getChildren().forPath("/" + serviceName);
// The first one used here by default , Then add load balancing
String string = strings.get(0);
return parseAddress(string);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
// Address -> XXX.XXX.XXX.XXX:port character string
private String getServiceAddress(InetSocketAddress serverAddress) {
return serverAddress.getHostName() +
":" +
serverAddress.getPort();
}
// The string resolves to an address
private InetSocketAddress parseAddress(String address) {
String[] result = address.split(":");
return new InetSocketAddress(result[0], Integer.parseInt(result[1]));
}
}
to update 2: Update how the client gets the server , When the server exposes Services , Register with the registry
First new client There is no need to pass in host And name, While sending request when , From the registry
// No transmission required host,port
RPCClient rpcClient = new NettyRPCClient();
Client transformation
public class SimpleRPCClient implements RPCClient {
private String host;
private int port;
private ServiceRegister serviceRegister;
public SimpleRPCClient() {
// Initialize registry , Establishing a connection
this.serviceRegister = new ZkServiceRegister();
}
// The client initiates a request call ,Socket Establishing a connection , Initiate request Request, Get a response Response
// there request It's packaged , Different service Different packaging is required , The client only knows Service Interface , A layer of dynamic proxy is needed to encapsulate different objects according to reflection Service
public RPCResponse sendRequest(RPCRequest request) {
// Get... From the registry host,port
InetSocketAddress address = serviceRegister.serviceDiscovery(request.getInterfaceName());
host = address.getHostName();
port = address.getPort();
try {
Socket socket = new Socket(host, port);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
System.out.println(request);
objectOutputStream.writeObject(request);
objectOutputStream.flush();
RPCResponse response = (RPCResponse) objectInputStream.readObject();
//System.out.println(response.getData());
return response;
} catch (IOException | ClassNotFoundException e) {
System.out.println();
return null;
}
}
}
The transformation of the server : Instead, the server needs to put its own ip, Port to registry
ServiceProvider serviceProvider = new ServiceProvider("127.0.0.1", 8899);
Add the function of registration in the service exposure class
public class ServiceProvider {
/** * An implementation class may implement multiple service interfaces , */
private Map<String, Object> interfaceProvider;
private ServiceRegister serviceRegister;
private String host;
private int port;
public ServiceProvider(String host, int port){
// You need to pass in the network address of the server's own service
this.host = host;
this.port = port;
this.interfaceProvider = new HashMap<>();
this.serviceRegister = new ZkServiceRegister();
}
public void provideServiceInterface(Object service){
Class<?>[] interfaces = service.getClass().getInterfaces();
for(Class clazz : interfaces){
// Native mapping table
interfaceProvider.put(clazz.getName(),service);
// Register the service in the registry
serviceRegister.register(clazz.getName(),new InetSocketAddress(host,port));
}
}
public Object getService(String interfaceName){
return interfaceProvider.get(interfaceName);
}
}
result
Running successfully !

summary
In this version, we have added the registry , Finally a complete RPC The framework has three roles : Service providers , Serving consumers , Registry Center
The biggest pain point of this version
- When querying the address according to the service name , We always return first IP, This leads to great pressure on the provider , Other providers cannot call
边栏推荐
- Huawei cloud native - data development and datafactory
- Error Nova missingauthplugin: an auth plugin is required to determine endpoint URL
- The school training needs to make a registration page. It needs to open the database and save the contents entered on the registration page into the database
- Interface test tool postman
- Node red series (28): communication with Siemens PLC based on OPC UA node
- Es2018 key summary
- Technology sharing | broadcast function design in integrated dispatching
- SQL append field
- If you encounter problems when using spark for the first time, please ask for help
- JS reflect
猜你喜欢

Linear interpolation of spectral response function

Node red series (28): communication with Siemens PLC based on OPC UA node

AI落地的新范式,就“藏”在下一场软件基础设施的重大升级里
![[note] on May 27, 2022, MySQL is operated through pychart](/img/34/36a3765683b2af485ca7c3e366da59.png)
[note] on May 27, 2022, MySQL is operated through pychart

el-upload上传文件(手动上传,自动上传,上传进度)

Share an example of a simple MapReduce method using a virtual machine

RPC correction

Smart use of bitmap to achieve 100 million level massive data statistics

lego_loam 代码阅读与总结

【图像融合】基于交叉双边滤波器和加权平均实现多焦点和多光谱图像融合附matlab代码
随机推荐
Unity échappe à l'entrée de caractères lors de l'entrée de chaînes dans l'éditeur
base64.c
Share an example of a simple MapReduce method using a virtual machine
Solutions for project paths
解决navicat连接数据库遇到的问题
Idea grey screen problem
Titanic(POJ2361)
Jour 9 Gestion des scripts et des ressources
【WEBRTC】ADM: rtc_include_internal_audio_device 触发 RTC_DCHECK(adm) 断言
(03).NET MAUI实战 基础控件
MySQL updates JSON string in array form
工程安全和工程质量
找到接口在表单里加参数
The new paradigm of AI landing is "hidden" in the next major upgrade of software infrastructure
Error in conditional filter (if) syntax in sum function in SQL Server2005
Ananagrams(UVA156)
基于海康EhomeDemo工具排查公网部署出现的视频播放异常问题
Simple theoretical derivation of SVM (notes)
技术分享| 融合调度中的广播功能设计
[fuzzy neural network prediction] water quality prediction based on fuzzy neural network, including Matlab source code