Introduction and use of Java Distributed Service Framework Dubbo

Source: Internet
Author: User
Tags zookeeper

1. What is Dubbo?

Dubbo is a distributed service framework dedicated to providing high-performance and transparent RPC remote service invocation scenarios, as well as SOA service governance scenarios. Simply put, Dubbo is a service framework, if there is no distributed demand, in fact, it is not needed, only in the distributed time, only Dubbo such a distributed service framework needs, and essentially a service invocation of the Dongdong, Frankly, it's a distributed framework for remote service invocation (say goodbye to the WSDL in Web service mode, register on Dubbo as a service provider and consumer)
The core part contains:
1. Remote communication: Provides an abstract encapsulation of various NIO frameworks based on long connections, including multiple threading models, serialization, and "request-response" mode of information exchange.
2. Cluster fault tolerance: Provide transparent remote procedure call based on interface method, including multi-protocol support, and cluster support such as soft load balancing, failure fault tolerance, address routing, dynamic configuration, etc.
3. Autodiscover: Based on the Registry directory service, the service consumer can dynamically find the service provider, make the address transparent, so that the service provider can increase or decrease the machine smoothly.

2. What can Dubbo do?

1. Transparent remote method calls, just like calling a local method, call the remote method, simply configure, without any API intrusion.
2. Soft load balancing and fault tolerant mechanism, can replace F5 and other hardware load balancer in intranet, reduce cost and reduce single point.
3. Service autoenrollment and discovery, no longer required to write dead service provider addresses, the registry queries the IP address of the service provider based on the interface name, and is able to smoothly add or remove service providers.

Dubbo uses a full spring configuration, transparent access to the application, no API intrusion to the application, just load the Dubbo configuration with spring, and Dubbo the spring-based schema extension.

3. Architecture of the DubboThe Dubbo frame composition is as follows:

Node role Description:

Provider: The service provider that exposes the service.

Consumer: Invokes the service consumer of the remote service.

Registry: Registration Center for service Registration and discovery.

Monitor: The monitoring center of the call times of the statistics service and the call time.

Container: The service runs the container.

I think it's a very good, well-defined character. Depending on the status of each node role, you can determine if the service is healthy.

Call Relationship Description:

0 The service container is responsible for starting, loading, and running the service provider.

1. Upon launch, the service provider registers its services with the registry. 2. When the service consumer starts, it subscribes to the registration center for the services it needs. 3. The registry returns the service provider address list to the consumer, and if there is a change, the registry will push the change data to the consumer based on a long connection. 4. Service consumers, from the provider address list, based on the soft load equalization algorithm, select a provider to make the call, if the call fails, then choose another call. 5. Service consumers and providers, the cumulative number of calls in memory and call time, timed to send statistics every minute to the monitoring center.

Dubbo's fault tolerance is obvious, performance has not yet been measured, we system a page needs to be 5 times the interface, would like to suggest a cache, but the business relationship can not be adopted, but also need to study the performance tuning problem of Dubbo ...

4. How to use Dubbo.

Dubbo uses a full spring configuration, transparent access to the application, no API intrusion to the application, just load the Dubbo configuration with spring, and Dubbo the spring-based schema extension. If you do not want to use the spring configuration and want to make calls through the API (not recommended)

Let's take a look at how spring is configured:

Service provider:

1. Download Zookeeper Registration Center,: http://www.apache.org/dyn/closer.cgi/zookeeper/after download, enter D:\apach-zookeeper-3.4.5\bin,

Double-click Zkserver.cmd to start the registry service.

2. Define the Service interface: (the interface needs to be packaged separately, shared between the service provider and the consumer) The following example is good, written in detail can be a model. [HTML]
    1. Package com.unj.dubbotest.provider;
    2. Import java.util.List;
    3. Public interface Demoservice {
    4. String SayHello (string name);
    5. Public List getusers ();
    6. }


implementing interfaces in the service provider: (Hidden implementations for service consumers)

[HTML]
  1. Package com.unj.dubbotest.provider;
  2. Import java.util.ArrayList;
  3. Import java.util.LinkedList;
  4. Import java.util.List;
  5. public class Demoserviceimpl implements demoservice{
  6. public string SayHello (string name) {
  7. Return "Hello" + name;
  8. }
  9. Public List getusers () {
  10. List list = new ArrayList ();
  11. User u1 = new User ();
  12. U1.setname ("Jack");
  13. U1.setage (20);
  14. U1.setsex ("male");
  15. User U2 = new User ();
  16. U2.setname ("Tom");
  17. U2.setage (21);
  18. U2.setsex ("female");
  19. User U3 = new User ();
  20. U3.setname ("Rose");
  21. U3.setage (19);
  22. U3.setsex ("female");
  23. List.add (U1);
  24. List.add (U2);
  25. List.add (U3);
  26. return list;
  27. }
  28. }

To declare an exposure service with spring configuration: [HTML]
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <beans xmlns="Http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:dubbo="Http://code.alibabatech.com/schema/dubbo"
  5. xsi:schemalocation= "Http://www.springframework.org/schema/beans
  6. Http://www.springframework.org/schema/beans/spring-beans.xsd
  7. Http://code.alibabatech.com/schema/dubbo
  8. Http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  9. ">
  10. <!--specific implementation bean--
  11. <Bean id= "demoservice" class="Com.unj.dubbotest.provider.DemoServiceImpl" />
  12. <!--provider application information for computing dependencies--
  13. <dubbo:application name="Xixi_provider" />
  14. <!--exposing service address using multicast broadcast registry
  15. <dubbo:registry address="multicast://224.5.6.7:1234" />-->
  16. <!--Use the Zookeeper registry to expose the service address --
  17. <dubbo:registry address="zookeeper://127.0.0.1:2181" />
  18. <!--exposed at 20880 ports with the Dubbo protocol --
  19. <dubbo:protocol name="Dubbo" port="20880" />
  20. <!--declaring a service interface that needs to be exposed--
  21. <dubbo:service interface="Com.unj.dubbotest.provider.DemoService" ref=" Demoservice " />
  22. </Beans>
Load the spring configuration and start the service: [HTML]
  1. Package com.unj.dubbotest.provider;
  2. Import Org.springframework.context.support.ClassPathXmlApplicationContext;
  3. public class Provider {
  4. public static void Main (string[] args) throws Exception {
  5. Classpathxmlapplicationcontext context = new Classpathxmlapplicationcontext (new string[] {"  Applicationcontext.xml "});
  6. Context.start ();
  7. System.in.read (); To ensure that the service is kept open, use the blocking of the input stream to simulate
  8. }
  9. }

Service Consumers:

Applicationcontext-dubbo.xml in the registration of their own need to call the interface, I just started to test the interface a lot, so the file is full of writing, and then familiar with the interface by business type, write n multiple  Applicationcontext-dubbo-***.xml more concise. "

1. Referencing remote services via spring configuration: [HTML]
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <beans xmlns="Http://www.springframework.org/schema/beans"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http// Code.alibabatech.com/schema/dubbo "
  4. xsi:schemalocation= "Http://www.springframework.org/schema/beans
  5. Http://www.springframework.org/schema/beans/spring-beans.xsd
  6. Http://code.alibabatech.com/schema/dubbo
  7. Http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  8. ">
  9. <!--consumer app name for calculating dependencies, not matching criteria, not the same as the provider--
  10. <dubbo:application name="Hehe_consumer" />
  11. <!--Use the Zookeeper registry to expose the service address --
  12. <!--<dubbo:registry address= "multicast://224.5.6.7:1234"/> --
  13. <dubbo:registry address="zookeeper://127.0.0.1:2181" />
  14. <!--generate a remote service proxy that can be used like a local bean demoservice--
  15. <dubbo:reference id="Demoservice"
  16. interface="Com.unj.dubbotest.provider.DemoService" />
  17. </Beans>

2. Load the spring configuration and invoke the remote service: [HTML]
  1. Package com.alibaba.dubbo.demo.pp;
  2. Import java.util.List;
  3. Import Org.springframework.context.support.ClassPathXmlApplicationContext;
  4. Import Com.unj.dubbotest.provider.DemoService;
  5. public class Consumer {
  6. public static void Main (string[] args) throws Exception {
  7. Classpathxmlapplicationcontext context = new Classpathxmlapplicationcontext (
  8. New string[] {"Applicationcontext.xml"});
  9. Context.start ();
  10. Demoservice Demoservice = (demoservice) context.getbean ("Demoservice");//
  11. String Hello = Demoservice.sayhello ("Tom");//?
  12. System.out.println (hello); //
  13. //
  14. List list = demoservice.getusers ();
  15. if (list = null && list.size () > 0) {
  16. for (int i = 0; I < list.size (); i++) {
  17. System.out.println (List.get (i));
  18. }
  19. }
  20. System.out.println (Demoservice.hehe ());
  21. System.in.read ();
  22. }
  23. }

The result of the call is:

Dubbo Administration Page:

This management page also need to deploy an environment, at first I thought it was Dubbo, found a half-day did not find ....

Application page:

Provider page:

Consumer page:

Service page:

The test is successful, I think as long as the status is normal, OK ....

Introduction and use of Java Distributed Service Framework Dubbo

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.