Dubbo Getting Started

Source: Internet
Author: User

I have heard of the benefits of Dubbo, but have not used the opportunity in the project, so I never know how to use it. Be free this evening, learn a little bit.

When entering a door, after the project encountered, then use it is relatively simple, as for the introduction, I will not summarize, in fact, is a good solution to the distributed

Management of the problem, and the operation is very convenient.

The following picture is from the official website

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.

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.

(1) Connectivity:

    • Registries are responsible for registering and locating service addresses, equivalent to directory services, where service providers and consumers interact with the registry only at startup, and the registry does not forward requests and is less stressful
    • Monitoring Center is responsible for statistics of the number of service calls, call time, etc., statistics are first sent to the Monitoring center server every minute after the memory summary, and the report shows
    • The service provider registers its services with the registry and reports the call time to the Monitoring center, which does not include network overhead
    • The service consumer obtains the service provider address list from the registry and invokes the provider directly based on the load algorithm, reporting the call time to the Monitoring center, which includes the network overhead
    • Registration Center, service provider, service consumers are long connections between the three, except the monitoring center
    • The registry will immediately push events to inform consumers of the presence of a long-connected-aware service provider, service provider downtime
    • The Registry and Monitoring Center are all down, without impacting the already running providers and consumers, who cache the provider list locally
    • Registries and monitoring centers are optional and service consumers can connect directly to service providers

(2) Jian-like sex:

    • Monitoring Center outage does not affect use, just lost part of the sampled data
    • After the database is down, the registry can still provide a list of services through the cache, but cannot register for new services
    • Registration center peer cluster, after any one outage, will automatically switch to another
    • After the registry is all down, service providers and service consumers can still communicate via local cache
    • Service provider stateless, any one outage, does not affect the use of
    • After the service provider is all down, the service consumer app will not work and unlimited re-connect waits for the service provider to recover

(3) Flexibility:

    • The registry is a peer cluster that dynamically increases the machine deployment instance, and all clients will automatically discover the new registry
    • Service provider stateless, can dynamically increase the machine deployment instance, the registry will push the new service provider information to the consumer

(4) Upgrade:

    • When the scale of service cluster expands further, it needs to realize dynamic deployment and flow calculation, and the existing distributed service architecture will not bring resistance:

The above basic concepts are read, and then we begin to go directly to the code.

Let's write the service-side code first:

This is the code structure

Here's the code:

Demoservice.java
Package Com.hotusm.dubbo.provider;import java.util.List; Public Interface Demoservice { String SayHello (); List<?> getalluser ();}
Demoserviceimpl.java

Package com.hotusm.dubbo.provider;

Import java.util.ArrayList;
Import java.util.List;
Import Java.util.Random;

Import Com.hotusm.dubbo.model.entity.User;

public class Demoserviceimpl implements Demoservice {

Public String SayHello () {

Return "Hello Dubbo";
}

Public list<?> Getalluser () {
List<user> list=new arraylist<user> ();
Random r=new random ();
for (int i=0;i<10;i++) {
List.add (New User ("" +r.nextint (100));
}

return list;
}

}

Configuration file for the service provider:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd/http Www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">         <!--the specific implementation bean -      <BeanID= "Demoservice"class= "Com.hotusm.dubbo.provider.DemoServiceImpl" />            <!--provider app information for calculating dependencies -      <dubbo:applicationname= "Xixi_provider"  />         <!--use multicast broadcast registry to expose the service address <dubbo:registry address= "multicast://224.5.6.7:1234"/> -          <!--exposing a service address using the Zookeeper Registration center -      <Dubbo:registryAddress= "zookeeper://127.0.0.1:2182" />           <!--exposing services on 20880 ports with the Dubbo protocol -      <Dubbo:protocolname= "Dubbo"Port= "20880" />         <!--declaring a service interface that needs to be exposed -      <Dubbo:serviceInterface= "Com.hotusm.dubbo.provider.DemoService"ref= "Demoservice" /> </Beans>

Then we can write a test class that will publish the service provider.

 PackageJUnit;Importjava.io.IOException;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classProvider { Public Static voidMain (string[] args) {Classpathxmlapplicationcontext atx=NewClasspathxmlapplicationcontext ("Spring-context.xml");                Atx.start (); Try{System.out.println ("Server start ... press any key to quit");        System.in.read (); } Catch(IOException e) {e.printstacktrace (); }            }}

Dubbo has a client where we can see the provider and the consumer in the client, but this client needs to install

We can see that we can see the information of the service provider here, and click to see more detailed information, which is not demonstrated here.

And then we'll do a consumer example,

Note that the package name, the entity name, and the interface name are the same, and this is the same as webservice.

The most important is the configuration file

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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 "><!--consumer app name, used to calculate dependencies, not match criteria, not as provider -      <dubbo:applicationname= "Hehe_consumer" />        <!--exposing a service address using the Zookeeper Registration center -      <!--<dubbo:registry address= "multicast://224.5.6.7:1234"/> -      <Dubbo:registryAddress= "zookeeper://127.0.0.1:2182" />        <!--Build a remote service proxy that can be used like a local bean demoservice -      <dubbo:referenceID= "Demoservice"Interface= "Com.hotusm.dubbo.provider.DemoService" />      </Beans>

Test code:

Importjava.io.IOException;Importjava.util.List;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportCom.hotusm.dubbo.provider.DemoService; Public classConsumer { Public Static voidMain (string[] args) {Classpathxmlapplicationcontext context=NewClasspathxmlapplicationcontext (NewString[] {"Spring-context-consumer.xml" });                    Context.start (); Demoservice Demo= (Demoservice) context.getbean ("Demoservice");//  System.out.println (Demo.sayhello ()); List<?> list =Demo.getalluser ();            SYSTEM.OUT.PRINTLN (list); Try{System.out.println ("Connection ... press any key to quit");            System.in.read (); } Catch(IOException e) {e.printstacktrace (); }                }}

We can see in the console:

Hello Dubbo[user [name=37], user [name=95], user [name=68], user [name=59], user [name=14], user [name=51], user [name=44], user [name=62], user [name=71], user [name=94

So our example will be finished, Dubbo is also an introduction to it.

Let's talk about how to configure the Dubbo administration page.

The open source section of the Dubbo Management Console includes: Provider routing rules dynamically configure access control weights to regulate load balancer principals, and other management functions.

1. Download Dubbo

I was testing the installation on Windows, first remove the contents of the ROOT folder under Tomcat/webapps (replace Tomcat's startup home page), extract the downloaded war package to Webapps/root (you can let Tomcat automatically unzip Copy the contents to root), if it is Linux, the command is as follows:


2. Configuration:

(The Windows environment extracted from the war package with the Dubbo.properties file, directly modify it, the words on Linux will be dubbo.properties in the current user directory,) modify the zookeeper address

    1. Vim Webapps/root/web-inf/dubbo.properties
3、配置信息如下:
dubbo.registry.address=zookeeper://localhost:2181dubbo.admin.root.password=rootdubbo.admin.guest.password= Guest

这边使用的是zookeeper的注册中心。

To start Tomcat:

Description: Make sure the zookeeper is started before you start Tomcat.

Access:

Http://localhost:8080/dubbo-admin-2.5.4-SNAPSHOT (This is the access path for my tomcat)

The following interface shows that the installation configuration is successful and the login password is root/root

War pack: Http://pan.baidu.com/s/1jHQiesY Password: 5EOQ

Dubbo Getting Started

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.