Distributed RPC framework Apache Dubbo

Posted by pro on Mon, 20 Dec 2021 21:41:15 +0100

1. Evolution process of software architecture

The development of software architecture has experienced the evolution process from single architecture, vertical architecture, SOA architecture to micro service architecture. Let's learn about these architectures respectively.

1.1 single structure

1.2 Vertical Architecture

1.3 SOA Architecture

1.4 microservice architecture

 

 2. Apache Dubbo overview

2.1 introduction to Dubbo

Apache Dubbo is a high-performance Java RPC framework. Its predecessor is Alibaba's open source and lightweight open source Java RPC framework, which can be seamlessly integrated with the Spring framework. In 2018, Alibaba donated this framework to the apache foundation

What is RPC?

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 methods provided by the application on server B. because the two applications are not in the same memory space and cannot be called directly, it is necessary to express the calling semantics and convey the calling data through the network. It should be noted that RPC is not A specific technology, but refers to the whole 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, such as RMI, Hessian, Dubbo, etc.

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

The dashed lines are asynchronous access, and the solid lines are synchronous access. The blue dashed lines: functions completed at startup. The red dashed lines (solid lines) are functions executed during program operation

Description of calling relationship:

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

1. When the service provider starts, it registers its own services with the registry.

2. Service consumers subscribe to the services they need from the registry when they start.

3. The registry returns the service provider address list to the consumer. If there is any change, the registry will push the change data to the consumer based on the long connection.

4. From the provider address list, the service consumer selects one provider to call based on the soft load balancing algorithm. If the call fails, select another provider to call.

5. Service consumers and providers accumulate call times and call times in memory, and regularly send statistical data to the monitoring center every minute.

3. Service registry Zookeeper

3.1 introduction to zookeeper

Zookeeper is a sub project of Apache Hadoop. It is a tree type directory service that supports change push. It is suitable as the registry of Dubbo service. It has high industrial intensity and can be used in production environment. It is recommended

3.2 installation of Zookeeper

Download address: http://archive.apache.org/dist/zookeeper/

Step 1: install jdk (omitted) step 2: compress zookeeper (zookeeper-3.4.6. Tar. GZ) upload to the linux system step 3: decompress the compressed package tar -zxvf zookeeper-3.4.6.tar.gz -C /usr step 4: enter the zookeeper-3.4.6 directory and create the data directory mkdir data step 5: enter the conf directory and change the name of zoo_sample.cfg to zoo.cfg cd conf mv zoo_sample.cfg zoo.cfg step 6: open the zoo.cfg file and modify the data attribute: D ataDir=/usr/zookeeper3. 4.6/data

3.3 start and stop Zookeeper

Enter the bin directory of Zookeeper and start the service command/ zkServer.sh start

Stop service command/ zkServer.sh stop

View service status:/ zkServer.sh status

Client connection/ zkCli.sh

4. Dubbo quick start

As an RPC framework, Dubbo's core function is to realize cross network remote call. This section is to create two applications, one as a service provider and the other as a service consumer. Dubbo enables the service consumer to remotely call the method of the service provider.

4.1 service provider development

Development steps: (1) create a maven project (packaged as war) dubcodemo_provider, and import the following coordinates into 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.5.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 relevant -->
<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>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</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>

(2) Configure the web.xml file

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<listener>
<listenerclass>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

(3) Create service interface

package com.lxs.service;
public interface HelloService {
public String sayHello(String name);
}

(4) Create service implementation class

package com.lxs.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.lxs.service.HelloService;
@Service
public class HelloServiceImpl implements HelloService {
public String sayHello(String name) {
return "hello " + name;
}
}

Note: the Service annotation used on the Service implementation class is provided by Dubbo. It is used to publish the Service (5) create applicationContext-service.xml under src/main/resources

<?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">
<!-- The current application name is used to calculate the dependency between applications in the registry. Note: the application names of consumers and providers are different -->
<dubbo:application name="dubbodemo_provider" />
<!-- Connect service registry zookeeper ip by zookeeper Server ip address-->
<dubbo:registry address="zookeeper://192.168.134.129:2181"/>
<!-- Registration agreement and port -->
<dubbo:protocol name="dubbo" port="20881"></dubbo:protocol>
<!-- Scan the specified package and join@Service Annotated classes are published as services -->
<dubbo:annotation package="com.lxs.service.impl" />
</beans>

(6) Start service tomcat7:run

4.2 service consumer development

Development steps: (1) create a maven project (packaged as war) dubbodemo_consumer. The pom.xml configuration is the same as the above service provider. You only need to change the port number of Tomcat plug-in to 8082. (2) configure the web.xml file

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servletclass>
<!-- Specify the configuration file to load through parameters contextConfigLocation load -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-web.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

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

package com.lxs.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.lxs.service.HelloService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/demo")
public class HelloController {
@Reference
private HelloService helloService;
@RequestMapping("/hello")
@ResponseBody
public String getName(String name){
//Remote call
String result = helloService.sayHello(name);
System.out.println(result);
return result;
}
}

Note: the @ Reference annotation provided by Dubbo is used to inject HelloService into the Controller

(5) Create applicationContext-web.xml under src/main/resources

<?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">
<!-- The current application name is used to calculate the dependency between applications in the registry. Note: the application names of consumers and providers are different -->
<dubbo:application name="dubbodemo-consumer" />
<!-- Connect service registry zookeeper ip by zookeeper Server ip address-->
<dubbo:registry address="zookeeper://192.168.134.129:2181"/>
<!-- Expose the interface by scanning -->
<dubbo:annotation package="com.lxs.controller" />
</beans>

(6) Run the test tomcat7:run to start typing in the browser http://localhost:8082/demo/hello.do?name=Jack , view the browser output

5. Dubbo management console

During development, we need to know which services are registered in the Zookeeper registry and which consumers consume these services. We can do this by deploying a management center. In fact, the management center is a web application, which can be deployed to tomcat.

5.1 installation

Installation steps:

(1) Copy the dubbo-admin-2.6.0.war file in the data to the webapps directory of tomcat(

2) Start tomcat and the war file will be decompressed automatically(

3) Modify Dubbo. Under WEB-INF Properties file, note Dubbo registry. The value corresponding to address needs to correspond to the ip address and port number of the Zookeeper currently used

dubbo.registry.address=zookeeper://192.168.134.129:2181 dubbo.admin.root.password=root dubbo.admin.guest.password=guest

(4) Restart tomcat

5.2 use

Operation steps:

(1) Visit http://localhost:8080/dubbo-admin-2.6.0 /, enter the user name (root) and password (root)

(2) start the service provider project and the service consumer project to view the corresponding information

Topics: Dubbo