Dubbo is a high-performance, lightweight, open source Java RPC Framework that provides three core competencies: interface-oriented remote method invocation, intelligent fault tolerance and load balancing, and service autoenrollment and discovery.
It features the following
- High-performance RPC calls for interface agents
- Intelligent Load Balancing
- Automatic registration and discovery of services
- Highly scalable capability
- Run-time Traffic scheduling
- Visualization of service governance and operations
Talk is cheap, Show me the code. Now let's start building a Dubbo project.
Build an XML configuration for the Dubbo project
Create three items
Shared interfaces
Define a common interface in SERVICE-API first
public interface DemoService { String sayHello(String name);}
Service Provider
The service provider Service-provider provides a Demoservice implementation class
public class DemoServiceImpl implements DemoService { @Override public String sayHello(String name) { System.out.println("[" + LocalDate.now() + "] Hello " + name ); return "Hello " + name; }}
Write an XML configuration file configuration app name
<dubbo:application name="demo-provider"/>
Configure the Registry
You can use the four multicast, Redis, Zookeeper, simple as a registry.
<dubbo:registry address="multicast://224.5.6.7:1234"/>
Configuration Protocol
Default is Dubbo
<dubbo:protocol name="dubbo" port="20880"/>
Defining beans
Then define the bean and expose the bean as a service
<bean id="demoService" class="com.learnDubbo.demo.provider.DemoServiceImpl"/><dubbo:service interface="com.learnDubbo.demo.DemoService" ref="demoService"/>
Main function
Write a main function to start the service provider
public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-provider.xml"); context.start(); System.in.read();//用于阻塞函数,使其一直运行}
Service consumers
The service consumer needs to invoke the Demoservice implementation class provided by the Service-provider service provider
It is also necessary to write an XML file, similar to the configuration file and service provider, and the difference is that the configuration of the exposed service needs to be modified to refer to the service's configuration, as follows
Reference Service
<dubbo:reference id="demoService" interface="com.learnDubbo.demo.DemoService"/>
Main function
Write a main function to start the service consumer, and then loop through the services provided by the service provider
public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-consumer.xml"); context.start(); DemoService demoService = (DemoService) context.getBean("demoService"); // 调用服务提供者提供的服务 while (true) { try { Thread.sleep(1000); String hello = demoService.sayHello("Dubbo"); // call remote method System.out.println(hello); // get result } catch (Throwable throwable) { throwable.printStackTrace(); } }}
Test
Start the Service-provider project first, then start Service-consumer, and the output is as follows
Build an annotated Dubbo project
Whether the service provider and the service consumer share the interface or use SERVICE-API, create the following two items
Annotation-based service providers
Create a new Springboot project
It is also necessary to provide a Demoservice implementation class, and add annotations to the class @Service
Note: it is Com.alibaba.dubbo.config.annotation.Service
Not org.springframework.stereotype.Service.
Don't get the wrong lead.
Next you need to add the Dubbo configuration class
@Configurationpublic class DubboConfiguration { /** * 对应xml配置:<dubbo:application name="demo-provider"/> * @return */ @Bean public ApplicationConfig applicationConfig() { ApplicationConfig applicationConfig = new ApplicationConfig(); applicationConfig.setName("demo-provider--annotation"); return applicationConfig; } /** * 对应xml配置:<dubbo:registry address="multicast://224.5.6.7:1234"/> * @return */ @Bean public RegistryConfig registryConfig() { RegistryConfig registryConfig = new RegistryConfig(); registryConfig.setAddress("multicast://224.5.6.7:1234"); return registryConfig; }}
The last step adds annotations on the startup class @DubboComponentScan(basePackages = "com.learnDubbo.demo.provider.service")
Basepackages self-modifying to provide the package path where the service class resides
Annotation-based service consumers
There is also a need for a configuration class, similar to the service provider, where the code is not posted
Next, create a controller for the test results, the code is as follows
@RestControllerpublic class DemoController { @Reference private DemoService demoService; @GetMapping("sayHello") public String sayHello(){ return demoService.sayHello("Dubbo"); }}
@Reference
Annotations indicate that a reference service is similar to an XML configuration
<dubbo:reference id="demoService" interface="com.learnDubbo.demo.DemoService"/>
Annotations are also required on the project startup class to @DubboComponentScan
specify the Dubbo scan path
Test
As with the XML configuration, the service provider needs to be started first
Configuration information
Configuration information mainly includes 3 chunks, registry, protocol, and schema configuration
Registration Center
There are a total of 4 registered centres, as follows
Registration Center |
Brief Description |
Dubbo Document Introduction |
Multicast |
You do not need to start any central node, as long as the broadcast address, you can find each other. |
Link |
Zookeeper |
Apacahe Hadoop Sub-project, is a tree-type directory service, support for change push, suitable as a Dubbo service registration center, high industrial strength, can be used in production environment, and recommended to use. |
Link |
Redis |
An efficient KV storage server, supported from the 2.1.0 release. |
Link |
Simple |
A common Dubbo service can reduce the reliance of third parties and make the whole communication way consistent. |
Link |
Using the Redis Registration center
When using the Redis registry, change the registry XML configuration of the service provider and consumer to the below
<dubbo:registry address="redis://localhost:6379"/>
The corresponding Redis will produce the following data
You can see that there are two keys, which correspond to service providers and consumers respectively. Two key corresponding data type is hash, you can see the service consumer key data as follows
1) "consumer://192.168.79.170/com.learnDubbo.demo.DemoService?application=demo-consumer&category=consumers&check=false&dubbo=2.6.2&interface=com.learnDubbo.demo.DemoService&methods=sayHello&pid=14208&side=consumer×tamp=1534389723130"2) "1534389813690"
The first is the URL address, and the second is the expiration time.
The official website gives the following figures
Using the Zookeeper Registration center
When using the Zookeeper registry, change the registry XML configuration of the service provider and consumer to the below
<dubbo:registry address="zookeeper://localhost:2181"/>
The corresponding zookeeper will produce the following data
It's a data map from the official website.
Some in and out, but roughly the same, more configurators and routers.
When the service provider starts: the corresponding directory structure is created, such as the shared interface name in my code above, the directory is created, and then the directory is created, and then the com.learnDubbo.demo.DemoService
directory is /dubbo/com.learnDubbo.demo.DemoService
written to providers
providers
its own URL address.
When the service consumer starts: The directory is created in the directory /dubbo/com.learnDubbo.demo.DemoService
consumers
and the directory is written to consumers
its own URL address.
When monitoring Center is started: /dubbo/com.learnDubbo.demo.DemoService
all provider and consumer URL addresses under the subscription directory.
Agreement
Protocol |
Brief Description |
Dubbo Document Introduction |
Dubbo |
The Dubbo default protocol uses a single long connection and NIO asynchronous communication, which is suitable for service invocation with large amount of small data volume, and the number of service consumer machines is much larger than the number of service provider machines. |
Link |
Rmi |
The RMI protocol is implemented with the JDK standard java.rmi.* , with blocking short joins and JDK standard serialization. |
Link |
Hessian |
The Hessian protocol is used to integrate Hessian services, Hessian the underlying Http communication, using Servlet exposure services, Dubbo the default inline Jetty as a server implementation. |
Link |
http |
The remote invocation protocol based on HTTP forms is implemented using Spring's httpinvoker. |
Link |
WebService |
WebService-based remote invocation protocol, based on Apache CXF Frontend-simple and Transports-http implementations. |
Link |
Thrift |
The current Dubbo supported thrift protocol is an extension of the Thrift native protocol, adding additional header information, such as service name,magic number, based on the native protocol. |
Link |
Memcached |
RPC protocol based on the memcached implementation. |
Link |
Redis |
A Redis-implemented RPC protocol. |
Link |
Rest |
Standard-based Java Rest Api--jax-rs 2.0 (Java API for RESTful WEB services shorthand) Implementation of REST invocation support |
Link |
There's a lot of stuff involved in the schema configuration, and it's not listed here.
Reference: Dubbo official website
Official website of the introduction are very detailed, from the official website can generally get to the information you think of.
There is a need to see the source of this blog post can point here: GitHub address