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

What does java Dubbo do?

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "what does java Dubbo do?". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

What is RPC?

RPC,Remote Procedure Call is remote procedure call. The target of remote procedure call is local procedure call. Are you familiar with local procedure call?

Think about the green days, you were catching up with your final homework in college, you were conquering the library management system, and you hit the keyboard crazily, realizing the modules of book lending, book return, and so on. The calls between the methods you implement are called local procedure calls.

If you tell me that the system in the library has been serviced and called remotely, I can only tell you that you have something.

To put it simply, the internal method calls on the local machine can be called local procedure calls, while the remote procedure call actually means that you locally call a method on the remote machine, which is the remote procedure call.

Make a brief summary

Let's summarize that generally speaking, what a RPC framework needs to do is to agree on communication protocols, serialized formats, some fault-tolerant mechanisms, load balancing strategies, monitoring operations and a registry!

Simply implement a RPC framework

Yes, it is a simple implementation. We have thought a lot about how to design a RPC framework above, which can be regarded as the functional requirement at the usage level of the production environment. This is Demo, which aims to highlight the key functions of the RPC framework-the implementation of remote calls.

So there is nothing seven, seven, eight, eight, and I use pseudo code to show, in fact, I delete some protective and binding code, because it looks too much, it is not very intuitive, need a bunch of try-catch and so on, so I cut some, straight to the point.

Let's Do It!

First we define an interface and a simple implementation.

Public interface AobingService {String hello (String name);} public class AobingServiceImpl implements AobingService {public String hello (String name) {return "Yo man Hello,I am" + name;}}

Then we implement the function that the service provider exposes the service.

Public class AobingRpcFramework {public static void export (Object service, int port) throws Exception {ServerSocket server = new ServerSocket (port); while (true) {Socket socket = server.accept (); new Thread (new Runnable () {/ / deserialize ObjectInputStream input = new ObjectInputStream (socket.getInputStream ()) String methodName = input.read (); / / read method name Class [] parameterTypes = (Class []) input.readObject (); / / Parameter type Object [] arguments = (Object []) input.readObject (); / / Parameter Method method = service.getClass () .getMethod (methodName, parameterTypes) / / find the method Object result = method.invoke (service, arguments); / / call the method / / return the result ObjectOutputStream output = new ObjectOutputStream (socket.getOutputStream ()); output.writeObject (result);}. Start () }} public static T refer (Class interfaceClass, String host, int port) throws Exception {return (T) Proxy.newProxyInstance (interfaceClass.getClassLoader (), new Class [] {interfaceClass}, new InvocationHandler () {public Object invoke (Object proxy, Method method, Object [] arguments) throws Throwable {Socket socket = new Socket (host, port) / / specify ip and port ObjectOutputStream output of provider = new ObjectOutputStream (socket.getOutputStream ()); output.write (method.getName ()); / / pass method name output.writeObject (method.getParameterTypes ()); / / pass parameter type output.writeObject (arguments) / / pass parameter values ObjectInputStream input = new ObjectInputStream (socket.getInputStream ()); Object result = input.readObject (); / / read result return result;}});}}

OK, this RPC framework is so good, isn't it very simple? It is just that the caller passes the method name, parameter type, and parameter value, and the provider receives the parameter and then calls the method for it to return the result! This is the remote procedure call.

Let's see how to use it.

/ / the service provider only needs to expose the interface AobingService service = new AobingServiceImpl (); AobingRpcFramework.export (service, 2333); / / the service caller only needs to set the dependency AobingService service = AobingRpcFramework.refer (AobingService.class, "127.0.0.1", 2333); service.hello ()

It looks good, but it's very crude, and it's great to be used as a demo to help understand!

Next let's take a look at Dubbo! Serve the main course!

Introduction to Dubbo

Dubbo is a Java-based RPC framework that Alibaba opened up in 2011, but it has been quiet for some time, but some other enterprises are still using Dubbo and extending it themselves, such as Dangdang's Dubbox and NetEcola's Dubbok.

But Alibaba restarted Dubbo maintenance in 2017. In 2017, it was awarded the 2017 most popular open source software Top 3 in open source China.

It merged with Dubbox in 2018, joined the Apache incubator, and graduated in 2019 to become a top-level Apache program.

At present, the main maintenance of the Dubbo community is 2.6.x and 2.7.x, 2.6.x version is mainly bug repair and a small number of functional enhancements, is a stable version.

2.7.x is the main development version, updating and adding new feature and optimizations, and the release of 2.7.5 is considered by Dubbo to be a landmark release, which we will analyze later.

It implements interface-oriented proxy RPC invocation, and can cooperate with ZooKeeper and other components to achieve service registration and discovery functions, and has load balancing, fault-tolerant mechanism and so on.

Overall architecture of Dubbo

Let's first take a look at a picture on the official website.

The big three layers are Business (Business layer), RPC layer and Remoting, and they are also divided into API layer and SPI layer.

Divided into three layers actually means the same meaning as the network layering we know. Only with clear layers and clear boundaries of responsibilities can we better expand.

Divided into API layer and SPI layer, this is the success of Dubbo, using micro-kernel design + SPI extension, so that the access party with special needs can customize the extension and do customized secondary development.

Next let's take a look at what each floor does.

Service, the business layer, is the business logic layer we developed.

Config, the configuration layer, mainly around ServiceConfig and ReferenceConfig, initializes configuration information.

Proxy, the proxy layer, the service provider or the consumer all generate a proxy class that makes the service interface transparent, and the proxy layer makes remote calls and returns results.

Register, the registration layer, encapsulates service registration and discovery.

Cluster, the routing and cluster fault-tolerant layer, is responsible for selecting specific call nodes, handling special call requirements and fault-tolerant measures for remote call failures.

Monitor, the monitoring layer, is responsible for monitoring the time and number of calls.

Portocol, the remote invocation layer, is mainly responsible for encapsulating RPC calls, and is mainly responsible for managing Invoker,Invoker represents an abstract encapsulated executor, which will be explained in detail later.

Exchange, the information exchange layer, is used to encapsulate the request response model and change steps synchronously.

Transport, the network transport layer, abstracts the unified interface of network transmission, so that users can use Netty if they want to use Netty and Mina if they want to use Mina.

Serialize, the serialization layer, serializes data into binary streams and, of course, deserializes.

SPI

Let me mention SPI (Service Provider Interface), a built-in service discovery mechanism in JDK that completely decouples the interface from the concrete implementation. We only declare the interface, and the specific implementation class is selected in the configuration.

Specifically, you define an interface, and then place a text file with the same name as the interface in the META-INF/services directory. The content of the file is the implementation class of the interface, and multiple implementation classes are separated by newline characters.

This determines which implementation to use through configuration!

Dubbo SPI has also made some improvements, and the space is limited to talk about it later.

Dubbo calling procedure

I have already introduced what each layer is for. Now let's go through the process of calling again to deepen your understanding of Dubbo. Let's string together knowledge points and take a look at them again and again.

Service exposure process

First of all, Provider starts, and the interfaces that need to be exposed are encapsulated into Invoker,Invoker through Proxy components according to the specific protocol Protocol. Invoker,Invoker is a very core component of Dubbo, representing an executable.

Then wrap it through Exporter, in order to expose a layer of your suite in the registry, and then register the Exporter with the registry through Registry. This is the overall service exposure process.

Consumption process

Then let's take a look at the consumer invocation process (the process of exposing the server is also shown in the diagram, which is actually a quite complete flowchart).

First of all, the consumer startup will pull the meta-information of the service provider from the registry, and then the invocation process also starts with Proxy, which requires an agent to be unaware.

Proxy holds an Invoker object. After calling invoke, you need to obtain the Invoker list of all callable remote services from Directory through Cluster. If certain routing rules are configured, for example, if an interface can only call a node, then filter the Invoker list again.

Select one of the remaining Invoker through LoadBalance as a load balancer. Then do some statistics through Filter, and then do data transmission through Client, such as using Netty.

The transmission needs to be constructed through the Codec interface and then serialized. Finally, it is sent to the corresponding service provider.

After receiving it, the service provider also performs Codec protocol processing, and then deserializes and throws the request to the thread pool for processing. A thread will find the corresponding Exporter according to the request, and finding the Exporter will actually find the Invoker, but there will also be layers of Filter, which, after filtering the chain layer by layer, will finally call the implementation class and return the result.

This is the end of what java Dubbo does. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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