##If you are already proficient in using Dubbo, then this This article is not suitable for you, but if you want to understand Dubbo
and want to learn Dubbo
, then it is very suitable for you.
Dubbo was initially developed by Alibaba and later contributed to
Apache, so we will call it ## later. #Apache Dubbo
or just call Dubbo
.
is a high-performance, lightweight open source service framework.
Incorrect pronunciation: diubo, dubo
Correct pronunciation: |ˈdʌbəʊ|
RPC
Call During development, we all like to refer to Dubbo
as RPC
open source framework.
RPC
is the abbreviation of Remote Procedure Call
, which translates to: Remote Procedure Call
.
The simple understanding is that one node requests the services provided by another node.
In layman’s terms:
Two servers A and B, deploy an application serverA
on server A, and deploy an application serverB
on server B. At this time, serverA wants to call a certain method in serverB
. Since it is not in the same server, it cannot be called directly. It needs to express the semantics of the call and transmit the call data through the network.
Calling remote methods is just like calling local methods.
In our development, the calls between two services (services on different servers) are usually Use HTTP REST.
In fact, there are many RPC frameworks on the market, and Dubbo is just one of them. For example:
Let’s do it Take a look at the core roles in the Dubbo
architecture:
This picture comes from the official website. Let’s give a brief introduction to the picture. :
Registration Center. Responsible for the registration and search of service addresses. The Provider
and Consumer
of the service only interact with the registration center at startup. The registration center senses the existence of Provider
through long connections. When Provider
is down, the registration center will immediately push relevant event notifications to Consumer
.
Service provider. When it starts, it will register with the Registry, encapsulate the address of its own service and related configuration information into a URL and add it to ZooKeeper.
Service consumer. When it starts, it will subscribe to the Registry. The subscription operation will obtain the Provider registered URL from ZooKeeper
and add the corresponding listener in ZooKeeper
. After obtaining the Provider URL, Consumer
will select one Provider
from multiple Provider
based on the load balancing algorithm and establish a connection with it, and finally initiate a connection to the Provider
's RPC
call. If the Provider
URL changes, Consumer
will obtain the latest Provider URL information through the listener added in the registration center during the previous subscription process, and make corresponding adjustments, such as disconnection Connect to the downed Provider
and establish a connection to the new Provider
.Consumer establishes a long connection with Provider
, and Consumer
will cache Provider
information, so once the connection is established, even if the registration center is down, it will not affect the existing connections. Running Provider
and Consumer
.
Monitoring center. Used to count the number of service calls and call time. During the running process, Provider
and Consumer
will count the number of calls and the call time in the memory, and send statistical data to the monitoring center regularly every minute. The monitoring center does not play a necessary role in the above architecture diagram. The downtime of the monitoring center will not affect the functions of Provider
, Consumer
and Registry
, only the monitoring will be lost. Just data.
It develops obscenely and explodes in the later stage, (you may not pay much attention to it in the early stage, but it is particularly fragrant in the later stage)
Service running container. It is an independent container because services usually do not require the features of Web containers such as Tomcat
and JBoss
, so there is no need to use a Web container to load services. The service container is just a simple main method and loads a simple Spring container to expose the service.
In the picture above, there are several characters and many lines are drawn. Let’s give a brief explanation of this.
Dubbo
’s official website: https://dubbo .apache.org/
Since Dubbo
is developed by Alibaba technical team, so in terms of documentation For us Chinese, it is quite friendly. In one word: cool!
In addition, Dubbo
There are many things on the official website, so we won’t introduce them one by one here.
It is recommended that everyone go to the official website.
Without further ado, let’s have some fun first!
Let’s start with a case without a registration center.
We build a project and create three module
:
First is the pom
dependency:
<!--dubbo的依赖--> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>3.0.4</version> </dependency> <!-- provider和consumer共用类--> <dependency> <groupId>com.tian.dubbo</groupId> <artifactId>dubbo-demo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
Both the consumer and provider projects need to add these two dependencies.
api mainly defines service interfaces and some tool classes, which are mainly shared by consumers and providers.
In the api we only define one service interface: DemoService
package com.tian.dubbo.service; public interface DemoService { String sayHello(String msg); }
Then type it into a jar and add it to pom.xml in the consumer and provider projects
In dependencies, the last two times can be used.
Create a directory META-INF.spring
in the resources directory, and then create an application.xml
in the directory with the following content :
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!--Dubbo服务名称--> <dubbo:application name="dubbo-provider"/> <!--不需要注册到服务注册中心--> <dubbo:registry address="N/A"/> <!--端口和协议--> <dubbo:protocol name="dubbo" port="20880"/> <!--我们的服务--> <dubbo:service interface="com.tian.dubbo.service.DemoService" ref="demoService"/> <bean id="demoService" class="com.tian.dubbo.service.DemoServiceImpl"/> </beans>
Create a log printing configuration file in the resources directory: log4j.properties
###set log levels### log4j.rootLogger=debug, stdout ###output to the console### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy HH:mm:ss:SSS z}] %t %5p %c{2}: %m%n
Define a business implementation class: DemoServiceImpl
package com.tian.dubbo.service; public class DemoServiceImpl implements DemoService { public String sayHello(String msg) { System.out.println("msg= " + msg); return "SUCCESS"; } }
Then define a provider startup class: ProviderMain
package com.tian.dubbo; import org.apache.dubbo.container.Main; public class ProviderMain { public static void main(String[] args) { Main.main(args); } }
注意:这里的Main类是Dubbo
最后,我们启动ProviderMain
类,日志输出:
好了,已经启动成功了。
我们继续来看看consumer项目,在项目中,也就是调用我们服务的项目。
在consumer项目中application.xml
配置文件和provider有所区别。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 服务名称--> <dubbo:application name="dubbo-consumer"/> <!--不需要注册到服务注册中心--> <!-- 通过url直接调用--> <dubbo:reference id="demoService" interface="com.tian.dubbo.service.DemoService" url="dubbo://127.0.0.1:20880/com.tian.dubbo.service.DemoService"/> </beans>
这个url地址,我们在provider启动的时候,可以从日志中找到。
日志文件和provider一样,然后就是ConsumerMain
启动类了。
package com.tian.dubbo; import com.tian.dubbo.service.DemoService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ConsumerMain { public static void main(String[] args) { DemoService demoService = null; ApplicationContext context = new ClassPathXmlApplicationContext ("classpath:META-INF/spring/application.xml"); demoService = context.getBean(DemoService.class); //调用服务 System.out.println(demoService.sayHello("tian")); } }
前面,我们已经把provider成功启动了,下面我们就来启动ConsumerMain
。
从日志可以看出我们已经成功调用了provider,我们再来看看provider的日志输出:
也成功的输出了我们想要的。
到此,一个简单的入门无注册中心(通过url直接调用)的方式就完成了。
url is still very useful when developing joint debugging, because it gets rid of the dependence on the registration center.
We have demonstrated no registration center before, now we will demonstrate the registration center.
Dubbo
Currently supports almost all registration centers on the market:
Dubbo registration centers are used
Zookeeper and
Nacos.
Zookeeper (nacos is similar, which will be mentioned below).
pom
依赖application.xml
中添加注册中心pom
依赖我们需要在前面demo中consumer和provider的pom.xml
中添加Zookeeper
的依赖:
<dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-zookeeper</artifactId> <version>3.0.4</version> <type>pom</type> </dependency>
在provider项目中我们需要调整:
<dubbo:registry address="N/A"/>
改成:
<dubbo:registry address="zookeeper://127.0.0.1:2181" timeout="10000"/>
这个timeout建议配上,我这里其实没必要配,因为
dubbo
服务和Zookeeper
都在我本地。
然后我们启动provider项目:
看到我们的项目已经启动成功,并且已经注册到Zookeeper
上了。
我们可以使用Zookeeper
的可视化工具,看看注册上去的信息。
我们再看看consumer端的调整。
我们需要在application.xml
中添加
<dubbo:registry address="zookeeper://127.0.0.1:2181" timeout="10000"/>
同时,去掉reference
中的url:
<dubbo:reference id="demoService" interface="com.tian.dubbo.service.DemoService" url="dubbo://127.0.0.1:20880/com.tian.dubbo.service.DemoService"/>
因为是通过Zookeeper
注册中心拿到地址,所以这里的url就可以去掉了。
最后,启动ConsumerMain
类:
可以看到我们也成功调用服务,另外也有大量的Zookeeper
日志。
到此,说明,我们的Zookeeper
为注册中心的demo案例也成功了。
注意:provider和consumer项目都需要依赖相关jar包(api、zookeeper、dubbo)
关于Nacos
,我们这里就不演示了,因为太简单了,如果你把Nacos
搭建好了后,直接配置就好了。
<dubbo:registry address="nacos://127.0.0.1:8848" timeout="10000"/>
就是把address地址改一下就可以了。
Nacos 的演示,我们下一篇文章中见。
本文分享了Dubbo
入门案例的两个版本:无注册中心和Zookeeper
注册中心。
The above is the detailed content of Dubbo source code analysis: Beginner's guide. For more information, please follow other related articles on the PHP Chinese website!