Getting to combat Dubbo

Frontier: the current popular distributed architecture Dubbo is a very popular technology available these days by learning learning, and real behind the project, laying the groundwork for the later distributed projects.

Dubbox Profile

Dubbox is a distributed service framework, its predecessor is an open source project Alibaba Dubbo, is the use of domestic electricity providers and Internet projects, the late Ali Baba stopped the maintenance of the project, Dangdang will be optimized on the basis of Dubbo, and continues to maintain in order to distinguish the original Dubbo, it will be named Dubbox.

Dubbox is committed to providing high performance and transparency of RPC remote service call programs, services and SOA governance program. Simply put, dubbox is a service framework, if not distributed demand, in fact, we do not need, and only when distributed, have dubbox demand for such a distributed service framework, and is called a service essentially stuff, it means a distributed framework for remote service invocation.
Here Insert Picture Description
Node Role Description:

  • Provider: Service Provider exposure.
  • Consumer: the service consumer calls the remote service.
  • Registry: Registry service registration and discovery.
  • Monitor: statistical services of call times to reconcile the monitoring center call time.
  • Container: run container services.

Call relationship Description:

  • Responsible for starting the service container, load, run service provider.
  • Service provider when you start, registration services they provided to the registry.
  • Consumer services at startup, you need to subscribe to the service registry.
  • Registry return address list service providers to consumers, if there is a change, the registry will be based on the long connection push
    to send changed data to consumers.
  • Service consumers, providers from the list of addresses, soft load balancing algorithm, choose a provider call,
    if the call fails, then select another call.
  • Service consumers and providers, in memory of the cumulative number of calls and call time, time sent once per minute statistical
    data to the monitoring center.

Zookeeper Introduction

The official recommended zookeeper registry. Find the registry responsible for registration and address of the service, the equivalent of directory services, service providers and consumers interact with only registry, the registry does not forward the request at startup, less pressure.

Zookeeper is Apacahe Hadoop subprojects, is a tree of directory services to support change push for as Dubbox services registry, higher industrial strength, can be used in a production environment.

Zookeeper installed Linux systems

Installation steps:
Step 1: Install jdk
Step: The compressed package zookeeper uploaded to linux system.
Note: Alt + P into the SFTP, enter put d: \ zookeeper-3.4.6.tar.gz upload
third step: extracting compressed packet

tar -zxvf zookeeper-3.4.6.tar.gz

Step 4: Enter zookeeper-3.4.6 directory, create a data folder.

mkdir data

Step 5: Enter conf directory, zoo_sample.cfg renamed zoo.cfg

cd conf
mv zoo_sample.cfg zoo.cfg

Step Six: Open zoo.cfg, modify the data attributes: dataDir = / root / zookeeper-3.4.6 / data

Zookeeper service starts

Go to the bin directory, enter the command to start the service

 ./zkServer.sh start

The following output represents a successful start
Here Insert Picture Description
shutting down services enter the command

./zkServer.sh stop

Outputs the following message
Here Insert Picture Description
View status:

./zkServer.sh status

If you start state, suggesting that
Here Insert Picture Description
if the state does not start, suggesting:
Here Insert Picture Description

Dubbo Local JAR package deployment and installation

Dubbo jar package is not deployed to the Maven's central repository, you can look in the Maven central repository in Dubbo to the final version is 2.5.3, Alibaba disbanded Dubbo team continues to maintain this project consists of Dangdang, and renamed as Dubbox, coordinates unchanged, version changes, but did not submit to the central repository.
We now need to manually install Dubbox jar package to my local warehouse.
First dubbo-2.8.4.jar package into d: \ setup, and 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 directly extracted, unpacked, packed directly into the cd were maven of dubbo, and then it will generate dubbox-2.8 in the target directory .4.jar package

Getting real

  • Creating the Maven project (WAR) dubboxdemo-service, introduced in pom.xml dependence
<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相关 -->
        <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>
                    <!-- 指定端口 -->
                    <port>8081</port>
                    <!-- 请求路径 -->
                    <path>/</path>
                </configuration>
        </plugin>
    </plugins>
</build>
</project>
  • Creating WEB-INF folder under the project webapps, create 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">  
    <!-- 加载spring容器 -->
    <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 a service interface, create a package cn.ldc.org.dubbodemo.service, used to store business interface, create an interface
package cn.ldc.org.dubbodemo.service;
/**
 * 业务接口
 * @author Administrator
 *
 */
publicinterface UserService {   
    public String getName();    
}
  • Create a business class that implements
    create a package cn.ldc.org.dubbodemo.service.impl, used to store business implementation class. Create a business class that implements:
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 Notes different from the original, the package needs to be introduced under com.alibaba

  • Write configuration file

Creating applicationContext-service.xml in 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 for scanning @Service comment.

  • Test Run
tomcat7:run

Consumer Services Development

Development steps:
(1) create a Maven project (WAR) dubboxdemo-web, introduction of dependence in pom.xml, with "dubboxdemo-service" project. The difference is to run tomcat plug-in port to 8082.
(2) create the WEB-INF directory in the webapps directory, and create 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">  
<!-- 解决post乱码 -->
    <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>
    <!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载-->
    <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) service interface copies
the "dubboxdemo-service" works cn.ldc.org.dubboxdemo.service packet copy and the following interface to this project.
(4) written 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) the preparation of the spring configuration file
is created applicationContext-web.xml in 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>
    <!-- 引用dubbo 服务 -->
    <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 your browser to http: // localhost: 8082 / user / showName, see the browser output

More tutorials please pay attention: non-Coban Coban

Guess you like

Origin blog.51cto.com/14481935/2466759