Introduction to Dubbox and Zookeeper and a small introductory case

Posted by drbigfresh on Sat, 11 May 2019 14:56:42 +0200

Dubbox

1: Introduction
Dubbox is a distributed service framework. Its predecessor is Dubbo, an open source project of Alibaba, which is used in domestic e-commerce and Internet projects. In the later period, Alibaba stopped the maintenance of the project. Dangdang Networks optimized and maintained on the basis of Dubbo. In order to distinguish it from the original Dubbo, it was named Dubbo.  
Dubbox is committed to providing high-performance and transparent RPC remote service invocation solutions and SOA service governance solutions. To put it bluntly, it is a distributed framework for remote service invocation.
 

 
Node role description:
Provider: A service provider that exposes services.
Consumer: A service consumer who calls a remote service.
Registry: Registry for service registration and discovery.
Monitor: The Monitoring Center for calling times and calling times of statistical services.
Container: Service Running Container.
 
Call relationship description:
0. The service container is responsible for starting, loading, and running the service provider.
1. When a service provider starts, it registers its services with the registry.
2. When service consumers start up, they subscribe to the registry for the services they need.
3. The registry returns a list of service provider addresses to consumers, and if there is a change, the registry will push based on long connections.
Send change data to consumers.
4. Service consumers, from the list of provider addresses, select a provider for invocation based on the soft load balancing algorithm.
If the call fails, select another call.
5. Service consumers and providers, accumulating calls and calls in memory, sending statistics regularly every minute
Data to the monitoring center.

Zookeeper

1: Introduction
Zookeeper is a sub-project of Apacahe Hadoop and a tree-like directory service. It is suitable for the registration center of Dubbox service. The registry is responsible for the registration and search of service addresses, which is equivalent to directory service. Service providers and consumers only interact with the registry at startup. The registry does not forward requests and has less pressure.
II: Zookeeper Installation in Linux System

  1. Installing jdk in Linux
  2. Upload zookeeper's compressed package to linux system.
  3. Decompression package:

    tar -zxvf zookeeper-3.4.6.tar.gz

  4. Go to the zookeeper-3.4.6 directory and create the data folder.

    mkdir data

  5. Enter the conf directory and rename zoo_sample.cfg to zoo.cfg

    mv zoo_sample.cfg zoo.cfg

  6. Open zoo.cfg and modify the data attribute:

    dataDir=/root/zookeeper-3.4.6/data

Three: Zookeeper service startup
Enter the bin directory and start the service input command

./zkServer.sh start

Demo

First: Maven Project to Create Service Providers
1:pom file

<properties>        
        <spring.version>4.2.4.RELEASE</spring.version>
   </properties>

    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>   

        <!-- dubbo Relevant -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.8.4</version>            
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>

        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.11.0.GA</version>
        </dependency>

    </dependencies>
   <build>  
      <plugins>
          <plugin>  
              <groupId>org.apache.maven.plugins</groupId>  
              <artifactId>maven-compiler-plugin</artifactId>  
              <version>2.3.2</version>  
              <configuration>  
                  <source>1.7</source>  
                  <target>1.7</target>  
              </configuration>  
          </plugin>  
          <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <configuration>
                    <!-- Designated port -->
                    <port>8081</port>
                    <!-- Request path -->
                    <path>/</path>
                </configuration>
          </plugin>
      </plugins>  
    </build>

2: web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">  

    <!-- Load spring container -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

3: Spring configuration file

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.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">

    <!-- Current application name -->
    <dubbo:application name="dubboxdemo-service" />
    <!-- Designated Registry Address -->
    <dubbo:registry address="zookeeper://192.168.25.128:2181"/>
    <!-- dubbox Packet scan -->
    <dubbo:annotation package="com.zgz.demo.serviceImpl" />

</beans>

4: Code and directory structure



Second: Maven Project to Create Service Consumers
1:pom file and consistency above
2: web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">  
   <!-- Solve post Random code -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
        <init-param>  
            <param-name>forceEncoding</param-name>  
            <param-value>true</param-value>  
        </init-param>  
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>   

  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- Specify the configuration file to be loaded through the parameters contextConfigLocation Load-->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:springmvc.xml</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

</web-app>

3: Spring Mvc configuration file

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.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">

    <!-- Configuration of Converter: Converting Return Value to String Output -->
    <mvc:annotation-driven >
        <mvc:message-converters register-defaults="false">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">  
                <constructor-arg value="UTF-8" />
            </bean>  
        </mvc:message-converters>   
    </mvc:annotation-driven>

    <!-- Current application name -->
    <dubbo:application name="dubboxdemo-web" />
    <!-- Designated Registry Address -->
    <dubbo:registry address="zookeeper://192.168.25.128:2181"/>
    <!-- dubbox Packet scan -->
    <dubbo:annotation package="com.zgz.demo.controller" />

</beans>

4: Code and directory structure



III: Deployment of Management Center
When we develop, we need to know which services are registered in the registry so that we can develop and test them. We can do this by deploying a management center. In fact, the management center is a web application, which can be deployed to tomcat.
(1) Compile the source code, get the war package, enter the dubbo-admin directory under the command, enter the maven command

mvn package -Dmaven.skip.test=true

(2) Enter the target folder, and you will see a dubbo-admin-2.8.4.war, which installs Tomcat on the linux server and uploads the war package to tomcat's webapps on the linux server. Auto decompression after starting tomcat.

(3) If you are deployed on the same host in zookeeper and the port is the default 2181, you do not need to modify any configuration. If it is not on a host or the port is modified, dubbo.properties under WEB-INF need to be modified. The following configuration needs to be modified and tomcat restarted after modification.

dubbo.registry.address=zookeeper://127.0.0.1:2181

IV: Use of Management Center

Open the browser, enter http://192.168.25.128:8080/dubbo-admin, login user name and password are root to enter the home page.

Five: Start demo
Start the service provider's Maven project first, and start the service consumer's maven project. (See figure below) Note that zookeeper's services need to be started in advance, enter the address of the service consumer directly, and see the results.

Topics: Web Development Dubbo Spring Zookeeper xml