Writing in the front
In the last article, “The Ice Begins to Get Dubbo!” In this article, we focus on why we need to learn Dubbo, but also on understanding the principles and core source code of Dubbo. Dubbo source code: Dubbo source code: Dubbo source code: Dubbo source code: Dubbo source code: Dubbo However, in order to better understand Dubbo, I will focus on three parts: Dubbo in the core role, build Dubbo source environment, Dubbo core module description, run Dubbo sample program four parts. Just do it. Get to the point.
Note: For this series of projects, I have analyzed the source code based on Dubbo version 2.7.8.
The article has been included:
Github.com/sunshinelyz…
Gitee.com/binghe001/t…
The central character in Dubbo
To better illustrate the central role in Dubbo, I’ll directly refer to a dependency diagram for Dubbo.
Note: Image from Dubbo website.
From the dependency diagram for Dubbo, we can see that Dubbo consists of four main parts: Registry, Provider, Consumer, and Monitor. Next, we will give a brief introduction to these four parts respectively.
- It’s a Registry. In Dubbo, the registry is responsible for registering and discovering services, primarily for registering and looking up service addresses. It’s worth noting that in Dubbo, providers and consumers only interact with the registry when the service is started. After that, the registry realizes the existence of the Provider through a long connection. If the Provider is down or unavailable, the registry immediately pushes relevant events to inform consumers.
- Provider indicates the Provider of the service. When the Provider starts, it registers its related services with the registry. The Provider encapsulates its service address and related configuration information as urls and adds them to services such as Zookeeper.
- A Consumer b service C service D service When a Consumer starts up, it subscribes to the registry for the services it cares about. Obtain the REGISTERED URL of the Provider from services such as Zookeeper and add corresponding listeners to services such as Zookeeper. After obtaining the Provider registered URL, the Consumer uses a load balancing algorithm to select one of the acquired providers, establish a connection with it, and initiate an RPC call. If a Provider registered in a service such as Zookeeper changes, the Consumer gets the latest Provider information through a listener added to the registry. In addition, the Consumer caches information about the Provider. Once the Consumer and Provider are connected, even if the registry is down or unavailable, the interaction between the Consumer and Provider will not be affected.
- Monitor: indicates the monitoring center. It is used to count the number of calls to the Dubbo service and the call duration. In Dubbo’s core architecture, the monitoring center is not required, and the failure or unavailability of the monitoring center does not affect Dubbo’s overall service.
Well, that’s all we have to say about Dubbo’s core characters. For more information, please refer to Dubbo’s official documentation.
Build Dubbo source code environment
We can use the following command to download github source locally.
git clone https://github.com/apache/dubbo.git
Copy the code
Next, switch the source for Dubbo to 2.7.8
Git checkout -b dubo-2.7.8 dubo-2.7.8 git checkoutCopy the code
Compile using Maven
mvn clean install -Dmaven.test.skip=true
Copy the code
Conversion into IDEA project, here I use IDEA analysis Dubbo source code.
mvn idea:idea
Copy the code
Next, we can import the Dubbo source into IDEA.
Having said that, another option is to download Dubbo 2.7.8 directly from your browser.
Open the link in your browser: github.com/apache/dubb… Download the Dubbo source code.
You can download the zip and tar.gz packages to a local PC, decompress them, and import them to IDEA.
After the import is complete, we see the project structure as shown below.
Next, let’s take a brief look at the core modules in Dubbo’s source code.
Dubbo core module description
Dubbo – common modules
Dubbo public module, provides Dubbo SPI implementation, time wheel implementation, dynamic compilation and other general functions.
Dubbo – remoting module
Dubbo-remoting-api is the core abstraction of the entire module. Other submodules implement dubbo-Remoting-API based on other open source frameworks.
Dubbo xml-rpc module
The Dubbo RPC module depends on the Dubbo – Remoting module. Dubbo-remoting-api is the core abstraction of the entire Dubbo-RPC module, and other modules are implementations of dubbo-Remoting-API.
Dubbo – registry module
A module in Dubbo that interacts with the registry. Dubbo-registry – API is the core abstraction of the whole Dubbo-Registry, and other modules are the concrete implementation of Dubbo-Registry – API.
Dubbo – config module
A module in Dubbo that parses exposed configurations. The dubbo-config-API submodule is responsible for configuring dubbo in API mode, and the Dubbo-config-spring submodule is responsible for configuring dubbo in integrated use with Spring.
Dubbo – metadata module
Metadata module in Dubbo. Dubbo-metadata-api is the abstraction of the whole Dubbo-metadata, and other modules are the implementation of dubbo-metadata-API.
Dubbo – configcenter module
Dubbo configuration central module, which provides multiple service discovery methods and access to multiple service discovery components.
Dubbo – monitor module
The monitoring module of Dubbo is mainly used to count the number of service calls, the time of service calls and the services that realize the tracking of the call chain.
Dubbo – cluster module
Dubbo cluster management module, mainly provides load balancing, fault tolerance, routing and other functions.
Run the Dubbo sample program
In the Dubbo source code, there is a sample program module dubbo-demo. Before running the example in the Dubbo-demo module, we first start a Zookeeper locally as the registry.
Note: You can download Zookeeper from the Apache website.
Dubbo sample program structure
The overall structure of the sample program provided by Dubbo is shown below.
Let’s take a look at what modules are available under Dubo-Demo.
- Dubo-demo-interface: indicates the service interface defined by the Dubbo example.
- Dubo-demo-xml: Provides Spring XML-based usage examples.
- Dubo-demo-annotation: Provides an example of using Spring based annotations.
- Dubbo – Demo-API: Provides an example of using dubbo in API mode.
Among them, dubo-Demo-XML, Dubo-Demo-annotation and Dubo-Demo-API modules all rely on dubo-Demo-interface module.
Next, we will briefly introduce the core code of the Dubbo-Demo-Interface and Dubbo-Demo-Annotation modules, and run relevant sample programs. You can analyze and run the sample programs in Dubbo-Demo-xml and Dubbo-Demo-API and run the relevant code.
(1) Dubo-demo-interface: defines the service interface.
The core code of DemoService interface is shown as follows.
package org.apache.dubbo.demo;
import java.util.concurrent.CompletableFuture;
public interface DemoService {
// Synchronous call
String sayHello(String name);
// Asynchronous invocation
default CompletableFuture<String> sayHelloAsync(String name) {
returnCompletableFuture.completedFuture(sayHello(name)); }}Copy the code
(2) Dubo-Demo-annotation: provides a sample program based on Spring annotations.
The Provider code
Let’s start with the Dubo-Demo-Annotation-provider module, which is the service provider. The code for DemoServiceImpl is shown below.
@DubboService
public class DemoServiceImpl implements DemoService {
private static final Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class);
@Override
public String sayHello(String name) {
logger.info("Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
}
@Override
public CompletableFuture<String> sayHelloAsync(String name) {
return null; }}Copy the code
The code for the Application class is shown below.
public class Application {
public static void main(String[] args) throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProviderConfiguration.class);
context.start();
System.in.read();
}
@Configuration
@EnableDubbo(scanBasePackages = "org.apache.dubbo.demo.provider")
@PropertySource("classpath:/spring/dubbo-provider.properties")
static class ProviderConfiguration {
@Bean
public RegistryConfig registryConfig(a) {
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress(Zookeeper: / / "127.0.0.1:2181");
returnregistryConfig; }}}Copy the code
Consumer code
Next, let’s look at the code for the Dubbo-Demo-annotation-consumer module, which is the sample code for the service consumer. The code of the DemoServiceComponent class is shown below.
@Component("demoServiceComponent")
public class DemoServiceComponent implements DemoService {
@DubboReference
private DemoService demoService;
@Override
public String sayHello(String name) {
return demoService.sayHello(name);
}
@Override
public CompletableFuture<String> sayHelloAsync(String name) {
return null; }}Copy the code
The code for the Application class is shown below.
public class Application {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
context.start();
DemoService service = context.getBean("demoServiceComponent", DemoServiceComponent.class);
String hello = service.sayHello("world");
System.out.println("result :" + hello);
}
@Configuration
@EnableDubbo(scanBasePackages = "org.apache.dubbo.demo.consumer.comp")
@PropertySource("classpath:/spring/dubbo-consumer.properties")
@ComponentScan(value = {"org.apache.dubbo.demo.consumer.comp"})
static class ConsumerConfiguration {}}Copy the code
Run the Dubbo sample program
We started Zookeeper locally and then ran the Application class of the Dubbo-Demo-annotation-Provider module and the Application class of the Dubbo-Demo-annotation-consumer module respectively.
The following information is displayed on the CONSOLE of IDEA.
result :Hello world, response from provider: 192.168. 0. 5:20880
Copy the code
Dubbo summary
Here, we introduced the Dubbo in the core role, how to build Dubbo source environment, the Dubbo source code of the core module for a simple description, and a simple analysis of the Dubbo sample program and run the sample program. Among them, when introducing and running the sample program, we focused on the Dubo-Demo-Annotation sample module. Friends can analyze and run other sample modules by themselves. In subsequent articles, we will analyze the source code primarily by debugging Dubbo’s sample program.
Ok, that’s all for today, I’m Glacier, if you have any questions, you can leave a comment below, you can also add my wechat: sun_shine_LYz, exchange technology together, advance together, together awesome ~~