Dubbo Getting Started to Actual Warfare

Frontier: Dubbo is a very popular technology in today's popular distributed architecture, allowing days of free time to learn and work on later projects to pave the way for later distributed projects.

Introduction to Dubbox

Dubbox is a distributed service framework, its predecessor is the Alibaba open source project Dubbo, which is used by domestic e-commerce and Internet projects. In the later period, Alibaba stopped the maintenance of the project. When Dangdang was optimized on the basis of Dubbo, and continued to maintain, it was named Dubbox in order to distinguish it from the original Dubbo.

Dubbox is committed to providing high-performance and transparent RPC remote service invocation schemes as well as SOA service governance schemes.Simply put, dubbox is a service framework. If there are no distributed requirements, it is not necessary. Only when there is a distributed demand, there will be a distributed service framework such as dubbox. It is essentially an east of service calls, or a distributed framework for remote service calls.

Node role description:

  • Provider: The service provider that exposes the service.
  • Consumer: The consumer of the service that invokes the remote service.
  • Registry: A registry for service registration and discovery.
  • Monitor: A monitoring center that counts call times and call times for services.
  • Container: Service Run Container.

Call relationship description:

  • The service container is responsible for starting, loading, and running the service provider.
  • Service providers register their services with the registry at startup.
  • When service consumers start up, they subscribe to the registry for the services they need.
  • The registry returns a list of service provider addresses to the consumer, and if there is a change, the registry will push based on a long connection
    Send change data to consumers.
  • Service consumers, from the list of provider addresses, select a provider to invoke based on the Soft Load Balancing algorithm.
    If the call fails, choose another call.
  • Service consumers and providers, cumulative calls in memory and call times, timed to send statistics every minute
    Data to the monitoring center.

Introduction to Zookeeper

The zookeeper registry is officially recommended.The registry is responsible for the registration and lookup of service addresses, which is equivalent to a directory service. Service providers and consumers only interact with the registry at startup. The registry does not forward requests and has less pressure.

Zookeeper, a subproject of Apacahe Hadoop, is a tree-based directory service that supports change pushing and is suitable as a registration center for Dubbox services. It is highly industrial and can be used in production environments.

Zookeeper Installation on Linux System

Installation steps:
Step 1: Install jdk
Step 2: Upload zookeeper's compressed package to the linux system.
Note: Alt+P enters SFTP, enter put d:\zookeeper-3.4.6.tar.gz to upload
Step 3: Decompress the package

tar -zxvf zookeeper-3.4.6.tar.gz

Step 4: Enter the zookeeper-3.4.6 directory and create the data folder.

mkdir data

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

cd conf
mv zoo_sample.cfg zoo.cfg

Step 6: Open zoo.cfg and modify the data property: dataDir=/root/zookeeper-3.4.6/data

Zookeeper service startup

Enter the bin directory and start the service input command

 ./zkServer.sh start

Output of the following indicates successful startup

Close Service Input Command

./zkServer.sh stop

Output the following tips

View status:

./zkServer.sh status

If boot status, prompt

If not, prompt:

Dubbo Local JAR Package Deployment and Installation

Dubbo's jar package was not deployed to Maven's central warehouse. You can find that the final version of Dubbo is 2.5.3 in Maven's central warehouse. After Alibaba disbanded the Dubbo team, Dangdang continued to maintain the project and renamed it as Dubbox. The coordinates were unchanged and the version changed, but it was not submitted to the central warehouse.
We now need to manually install the jar package for Dubbox into my local repository.
Put the dubbo-2.8.4.jar package in d:setup first, then enter the command

mvn install:install-file -Dfile=d:\setup\dubbo-2.8.4.jar -DgroupId=com.alibaba -DartifactId=dubbo -Dversion=2.8.4 -Dpackaging=jar

Download URL: https://github.com/dangdangdotcom/dubbox/tree/dubbox-2.8.4
After downloading, decompress directly. After decompression, direct cd enters dubbo to package maven, and then generates dubbox-2.8.4.jar package in the target directory

Getting Started Actual

  • Create a Maven Project (WAR) dubboxdemo-service to introduce dependencies in pom.xml
<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.itcast.dubboxdemo</groupId>
<artifactId>dubboxdemo-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<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>
                    <!-- Specify Port -->
                    <port>8081</port>
                    <!-- Request Path -->
                    <path>/</path>
                </configuration>
        </plugin>
    </plugins>
</build>
</project>
  • Create the WEB-INF folder and web.xml under the webapps of the project
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns: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>
  • Create business interfaces, create packages cn.ldc.org.dubbodemo.service, store business interfaces, create interfaces
package cn.ldc.org.dubbodemo.service;
/**
 * Business Interface
 * @author Administrator
 *
 */
publicinterface UserService {   
    public String getName();    
}
  • Create Business Implementation Class
    Create a package, cn.ldc.org.dubbodemo.service.impl, to hold business implementation classes.Create a business implementation class:
package cn.ldc.org.dubbodemo.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import cn.itcast.dubbodemo.service.UserService;
@Service
publicclass UserServiceImpl implements UserService {
    public String getName() {       
        return"itcast";
    }
}

Note: Service annotations are different from the original and need to be introduced under the com.alibaba package

  • Write a configuration file

Create applicationContext-service.xml under src/main/resources as follows:

<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="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">

<dubbo:application name="dubboxdemo-service"/>
<dubbo:registry address="zookeeper://192.168.179.128:2181"/>
<dubbo:annotation package="cn.ldc.org.dubboxdemo.service" />
</beans>

Note: dubbo:annotation is used to scan for @Service annotations.

  • test run
tomcat7:run

Service Consumer Development

Development steps:
(1) Create the Maven Project (WAR) dubboxdemo-web, introduce dependencies in pom.xml, and work with the "dubboxdemo-service" project.The difference is that the tomcat plug-in runs on port 8082.
(2) Create a WEB-INF directory under the webapps directory and create a web.xml

<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns: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 profile to load, by parameter contextConfigLocation Load-->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext-web.xml</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*</url-pattern>
</servlet-mapping>
</web-app>

(3) Copy Business Interface
Copy the cn.ldc.org.dubboxdemo.service package of the "dubboxdemo-service" project and the following interfaces to the project.
(4) Writing Controller

package cn.ldc.org.dubboxdemo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.itcast.dubbodemo.service.UserService;
@Controller
@RequestMapping("/user")
publicclass UserController {
    @Reference
    private UserService userService;    
    @RequestMapping("/showName")
    @ResponseBody
    public String showName(){
        returnuserService.getName();
    }       
}

(5) Write a spring configuration file
Create applicationContext-web.xml under src/main/resources

<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="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">

    <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>
    <!-- Quote dubbo service -->
    <dubbo:application name="dubboxdemo-web" />
    <dubbo:registry address="zookeeper://192.168.179.128:2181"/>
<dubbo:annotation package="cn.ldc.org.dubboxdemo.controller" />
</beans>

test run

tomcat7:run

Enter http://localhost:8082/user/showName in the browser to view the browser output

More tutorials to focus on: non-class classes

Keywords: Programming Dubbo Spring Zookeeper xml

Added by mepaco on Tue, 14 Jan 2020 19:30:16 +0200