Dubbo (distributed RPC framework) - getting started

Table of contents

1. Evolution process of software architecture

1.1. Monolithic architecture

1.2. Vertical Architecture

1.3. SOA architecture

1.4. Microservice architecture

2. Overview of Apache Dubbo

2.1 Introduction to Dubbo

2.2, Dubbo architecture diagram

3. Service registration center Zookeeper

3.1 Introduction to Zookeeper

3.2. Zookeeper installation

4. Dubbo quick start

4.1. Development of the service provider

4.2. Configure the web.xml file

4.3. Create service interface

4.4, applicationContext.xml configuration file

4.5, service discoverer development


 

1. Evolution process of software architecture

1.1. Monolithic architecture

Architecture description:

All functions are concentrated in one project (All in one).

Architecture advantages:

The structure is simple, the initial development cost is low, the development cycle is short, and it is suitable for small projects.

Architecture disadvantages:

All functions are integrated in one project, which is not easy to develop, expand and maintain for large-scale projects.

The technology stack is limited and only one language can be used for development.

System performance expansion can only be done by expanding cluster nodes, which is costly.

1.2. Vertical Architecture

Architecture description:

Cut according to the business to form small single projects.

Architecture advantages:

The technology stack is scalable (different systems can be written in different programming languages).

Architecture disadvantages:

Functions are concentrated in one project, which is not conducive to development, expansion, and maintenance.

System expansion can only be done through clusters.

Functional redundancy, data redundancy, and strong coupling between projects.

1.3. SOA architecture

The full name of SOA is Service-Oriented Architecture, which is a service-oriented architecture. It can distribute, combine and use loosely coupled coarse-grained application components (services) through the network according to requirements. A service usually exists in an operating system process in an independent form.

From a functional point of view, business logic is abstracted into reusable services, and rapid business regeneration is realized through service orchestration. The purpose is to transform the original inherent business functions into general business services and realize rapid reuse of business logic.

Architecture description:

Extract repeated functions or modules into components, provide external services, and use ESB (Enterprise Service Bus) as a communication bridge between projects and services.

Architecture advantages:

Repeated functions or modules are extracted as services to improve development efficiency.

High reusability.

High maintainability.

Architecture disadvantages:

The business of each system is different, and it is difficult to confirm that the functions or modules are duplicated.

The granularity of extracted services is large.

There is a high degree of coupling between systems and services.

1.4. Microservice architecture

Architecture description:

The system service layer is completely isolated and extracted as microservices one by one.

The granularity of extraction is finer and follows a single principle.

It is transmitted using a lightweight framework protocol.

Architecture advantages:

The granularity of service splitting is finer, which is conducive to improving development efficiency.

Corresponding optimization schemes can be formulated for different services.

Applicable to the Internet era, the product iteration cycle is shorter.

Architecture disadvantages:

Too fine granularity leads to too many services and high maintenance costs.

The technical cost of distributed system development is high, which poses great challenges to the team.

2. Overview of Apache Dubbo

2.1 Introduction to Dubbo

Apache Dubbo is a high-performance Java RPC framework. Its predecessor is a high-performance, lightweight open source Java RPC framework open sourced by Alibaba, which can be seamlessly integrated with the Spring framework.

What are RPCs?

The full name of RPC is remote procedure call, that is, remote procedure call . For example, there are two servers A and B. An application is deployed on server A and an application is deployed on server B. The application on server A wants to call the method provided by the application on server B. Since the two applications are not in the same memory space, they cannot be called directly. , so it is necessary to express the semantics of the call and convey the data of the call through the network.

It should be noted that RPC is not a specific technology, but refers to the entire network remote call process.

RPC is a generalized concept. Strictly speaking, all remote procedure call methods belong to the category of RPC. Various development languages ​​have their own RPC framework. There are many RPC frameworks in Java, and RMI, Hessian, Dubbo, etc. are widely used.

Dubbo provides three core capabilities: interface-oriented remote method invocation, intelligent fault tolerance and load balancing, and automatic service registration and discovery.

2.2, Dubbo architecture diagram

node Role Name
Provider The service provider of the exposed service
Consumer The service consumer that invokes the remote service
Registry Registry for service registration and discovery
Monitor          A monitoring center that counts service calls and call times
Container service running container

Dotted lines are asynchronous access, solid lines are synchronous access blue dotted line: functions completed at startup, red dotted lines (solid lines) are functions executed during program running

Call relationship description:

  1. The service container is responsible for starting, loading, and running the service provider.

  2. When the service provider starts, it registers the services it provides with the registration center.

  3. When a service consumer starts, it subscribes to the registration center for the services it needs.

  4. The registration center returns the service provider address list to the consumer. If there is a change, the registration center will push the change data to the consumer based on the long connection.

  5. Service consumers, from the provider address list, select a provider to call based on the soft load balancing algorithm, and if the call fails, choose another provider to call.

  6. Service consumers and providers accumulate the number of calls and call time in memory, and regularly send statistical data to the monitoring center every minute.

3. Service registration center Zookeeper

3.1 Introduction to Zookeeper

As can be seen from the previous Dubbo architecture diagram, the Registry (service registration center) plays a vital role in it. Dubbo officially recommends using Zookeeper as the service registry.

Flow Description:

  • When the service provider (Provider) starts: write its own URL address to /dubbo/com.foo.BarService/providersthe directory

  • When the service consumer (Consumer) starts: Subscribe to the provider URL address under /dubbo/com.foo.BarService/providersthe directory . And write your own URL address to /dubbo/com.foo.BarService/consumersthe directory

  • When the Monitoring Center (Monitor) starts: Subscribe to all provider and consumer URL addresses under /dubbo/com.foo.BarServicethe directory

3.2. Zookeeper installation

Link: https://pan.baidu.com/s/1ewbnN4PN99cL0oeqeOONMg 
Extraction code: py22

Unzip: Open zookeeper\apache-zookeeper-3.5.5-bin\conf, copy zoo_sample.cfg and paste it into the current directory, and name it zoo.cfg.

Edit zoo.cfg. Modify the following configuration:

# Specify the directory where the data files are stored

dataDir= E:/zookeeper/data    

#Specify the directory where the logs are stored

dataLogDir= E:/zookeeper/log

Enter zookeeper\apache-zookeeper-3.5.5-bin\bin and double-click zkServer.cmd to run zookeeper

4. Dubbo quick start

4.1. Development of the service provider

(1) Create a maven-web project, the project name is dubbo_provider, and import the following coordinates in the pom.xml file

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <maven.compiler.source>1.8</maven.compiler.source>
  <maven.compiler.target>1.8</maven.compiler.target>
  <spring.version>5.0.8.RELEASE</spring.version>
</properties>
<dependencies>
  <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.6.0</version>
  </dependency>
  <dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.7</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.12.1.GA</version>
  </dependency>
  <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.47</version>
  </dependency>
</dependencies>

4.2. Configure the web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
		  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
           version="3.0">
    <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>

4.3. Create service interface

public interface HelloService {
    public String hello(String name);
}
@Service//dubbo的注解  服务发布  基于的接口类对象
public class HelloServiceImpl implements HelloService {
    @Override
    public String hello(String name) {
        System.out.println("端口号为8082的provider被调用了");
        return "hello " + name;
    }
}

Note: @Service must be imported under the dubbo package

import com.alibaba.dubbo.config.annotation.Service;

4.4, applicationContext.xml 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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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://www.springframework.org/schema/aop
 	http://www.springframework.org/schema/aop/spring-aop.xsd
 	http://www.springframework.org/schema/context
 	http://www.springframework.org/schema/context/spring-context.xsd
 	http://www.springframework.org/schema/tx
 	http://www.springframework.org/schema/tx/spring-tx.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">

    <!--配置应用名-->
    <dubbo:application name="dubbo_provider"/>
    <!--配置注册中心地址-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <!--配置协议和端口-->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!--包扫描:让dubbo注解起作用-->
    <dubbo:annotation package="cn.itssl"/>
    
</beans>

4.5, service discoverer development

(1) Create a maven project (the packaging method is war) dubbo_consumer, the pom.xml configuration is the same as the above service provider, only need to change the port number of the Tomcat plug-in to 8082

(2) Configure the web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
		  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
           version="3.0">
    <servlet>
        <servlet-name>s1</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>s1</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

(3) Copy the HelloService interface in the service provider project to the current project

(4) Write Controller

Note: @Reference must also be imported under the dubbo package

@RestController
public class HelloController {
    @Reference//服务发现  
    private HelloService helloService;

    //解决中文乱码
    @GetMapping(value = "/hello/{name}", produces = "text/html;charset=utf-8")
    public String hello(@PathVariable String name) {
        return helloService.hello(name);
    }
}

Create configuration file springmvc.xml 

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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://www.springframework.org/schema/aop
 	http://www.springframework.org/schema/aop/spring-aop.xsd
 	http://www.springframework.org/schema/context
 	http://www.springframework.org/schema/context/spring-context.xsd
 	http://www.springframework.org/schema/tx
 	http://www.springframework.org/schema/tx/spring-tx.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">

    <context:component-scan base-package="cn.itssl"/>
    <mvc:annotation-driven/>
    <dubbo:application name="dubbo_consumer"/>
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <dubbo:annotation package="cn.itssl"/>
  
</beans>

Guess you like

Origin blog.csdn.net/select_myname/article/details/128336063