Build a Dubbo+zookeeper platform from the start

Source: Internet
Author: User
Tags locale docker run

This article is mainly to share the process of building a dubbo+zookeeper platform from the beginning, which will briefly describe the role of the Dubbo service.

    • First of all, look at the general website architecture with the development of business, logic more and more complex, more and more data volume, more and more interaction after the conventional program evolution process.

    • Secondly, what service governance do we need to do when more and more services are available?

    • Finally, it's the Dubbo architecture diagram.


Registration Center Selection


Dubbo supports multiple types of registries:

    • Multicast Registration Center
    • Zookeeper Registration Center
    • Redis Registration Center
    • Simple Registration Center

Here we choose zookeeper, in fact, the advantages of the type of shortcomings can be viewed in detail documentation.


1:zookeeper installation, or use Docker as a fresh Run command to install the zookeeper

Docker run-dit--name zookeeper  --hostname  zookeeper-host  2181:2181


2: Install Zkui, can refer to Zkui project address to install, it provides a management interface, can be for the ZOOKEEPR node value of crud operation, but also provides security certification, follow the following steps to complete the installation.

    • MVN clean install, you need to install the Java environment before execution, the MAVEN environment, a jar file will be generated after successful execution.
    • Copy the config.cfg to the same directory as the jar file that you generated in the previous step, and then modify the zookeeper address in the configuration file.
    • Executes the jar. (Nohup Java-jar Zkui-2.0-snapshot-jar-with-dependencies.jar &), pay attention to the & in the back, which means no exit.
    • Test, http://localhost:9090, if you can see the following page on behalf of zookeeper installation is operating normally.


Here are the procedures for creating the Dubbo service and using the Dubbo service:

Dubbo provider, create a Java project with the following points in mind:

    • Package dependency, the introduction of the following three main packages on it, mainly Spring,dubbo and zkclient
<Dependency>            <groupId>Org.springframework</groupId>            <Artifactid>Spring-context</Artifactid>            <version>${spring-framework.version}</version>        </Dependency>        <Dependency>            <groupId>Com.alibaba</groupId>            <Artifactid>Dubbo</Artifactid>            <version>2.4.10</version>            <Exclusions>                <exclusion>                    <Artifactid>Spring</Artifactid>                    <groupId>Org.springframework</groupId>                </exclusion>            </Exclusions>        </Dependency>        <Dependency>            <groupId>Com.101tec</groupId>            <Artifactid>Zkclient</Artifactid>            <version>0.3</version>        </Dependency>
    • Define the interface, here for demonstration purposes, simply define an interface that returns the product name.
 Public Interface iproduct {    String getproductname ();}
    • Interface implementation
@Service  Public class Implements iproduct{    public  String getproductname () {               return "Jim";    } }
    • Service startup function
      • Load configuration file
      • Call Context.start ()
      • There are a number of ways to perform an operation that does not exit the program.
 Public classApp {Private Final StaticLogger Logger = Loggerfactory.getlogger (App.class);  Public Static voidMain (string[] args) {Classpathxmlapplicationcontext context=NewClasspathxmlapplicationcontext ("Classpath*:applicationcontext.xml");        Context.start (); Logger.info ("Dubbo service begin to start"); Try{System.in.read (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }}
    • The service profile, which needs to be noted here:
      • The definition of Dubbo:service with the dubbo:annotation,ref= "Productservice", is a specified ID, the actual implementation class through the annotation scan to complete, and does not specify the implementation class in the configuration file, This is reflected in the following consumer profiles.
      • Dubbo:application, you can specify the implementation interface of the logger.
      • Dubbo:protocol, you can specify whether to start an access log, which is useful for troubleshooting online problems.
 <?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"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:util= "Http://www.springframework.org/schema/util"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"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/HTTP Www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http ://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd "        >    <Context:property-placeholder Location= "Classpath*:config.properties"/>    <dubbo:applicationname= "Jim"Logger= "SLF4J" />    <Dubbo:registryProtocol= "Zookeeper"Address= "192.168.21.128:2181" />    <Dubbo:protocolAccesslog= "true"name= "Dubbo"Port= "20880" />    <dubbo:annotation Package= "Jim" />    <Dubbo:serviceInterface= "Jim." IProduct "ref= "Productservice"/>    <Context:component-scanBase-package= "Jim" />    <ImportResource= "Redis-context.xml"/></Beans>


Dubbo Consumer

    • Consumer profile, its configuration relative to the provider is much simpler:
      • Specify the name of the consumer, which can be arbitrary and does not require matching with the provider to do the tasks associated with.
      • Specifies the contract type, zookeeper address.
      • Specify the referenced service interface, and note that the ID here is the same as the ref value defined by 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://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:applicationname= "Consumer-of-jim-app"  />    <Dubbo:registryProtocol= "Zookeeper"Address= "192.168.21.128:2181"/>    <dubbo:referenceInterface= "Jim." IProduct "ID= "Productservice" /></Beans>
    • interface annotation definition and interface invocation
@Controller Public classHomeController {Private Static FinalLogger Logger = Loggerfactory.getlogger (HomeController.class); @AutowiredPrivateiproduct Productservice;@RequestMapping (Value= "/", method =requestmethod.get) PublicString Home (locale locale, model model) {Logger.info ("Welcome home! The client locale is {}. ", Locale); String ProductName= This. Productservice.getproductname (); Model.addattribute ("Name", productName); return"Home"; }}

Dubbo Admin

There is a UI tool that can be managed for the Dubbo service, but unfortunately I did not download the link in the official documentation, and then from elsewhere although the download completed, but in the installation of the deployment of temporary encountered some problems, need to process research to solve.

Normally you should see the following interface:

    • Management provider
    • Managing Consumers

Service governance



After the above steps, you can start the server and the client to verify. The above is simply a Dubbo environment and the implementation of a Hello World Service interface, to use good Dubbo there are a lot of best practices available, such as service governance:

    • Local stub
    • Local camouflage
    • Result Cache
    • Multiple Versions
    • Service downgrade
    • ......

This article refers to:

    1. http://dubbo.io/User+Guide-zh.htm#UserGuide-zh-%E5%A4%9A%E6%B3%A8%E5%86%8C%E4%B8%AD%E5%BF%83
    2. http://blog.csdn.net/yinwenjie/article/details/50113139
    3. Https://github.com/DeemOpen/zkui

Build a Dubbo+zookeeper platform from the beginning

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.