What is Dubbo? What is Dubbo doing? How to use Dubbo?

What is Dubbo?

Dubbo official website address: http://dubbo.apache.org

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 is RPC?

The full name of RPC is remote procedure call, that is, remote procedure call . For example, two servers A and B, one application is deployed on server A, one application is deployed on server B, and 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 the widely used ones are RMI, Hessian, Dubbo, etc.

What is Dubbo doing?

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

Dubbo architecture diagram (officially provided by Dubbo) is as follows:

Node role description:

node Role Name
Provider  Service provider of exposed service
Consumer  Service consumer calling remote service
Registry Registry for service registration and discovery
Monitor Monitoring center that counts the number of service calls and call time
Container Service running container

Call relationship description:

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

1. When the service provider starts, register the service it provides with the registration center.

2. When the service consumer starts, he subscribes to the registration center for the service he needs.

3. The registry returns a list of service provider addresses to consumers. If there are changes, the registry will push the changed data to consumers based on the long connection.

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

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

Registry Service Registry Zookeeper

 As you can see 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 registration center.

Zookeeper is a sub-project of Apache Hadoop. It is a tree-type directory service that supports change push . It is suitable as a registration center for Dubbo services. It has high industrial strength and can be used in production environments and is recommended.

Zookeeper tree directory service:

Flow Description:

When the service provider (Provider) starts: write your own URL address to the `/dubbo/com.foo.BarService/providers` directory

When the service consumer (Consumer) starts: subscribe to the provider URL address in the `/dubbo/com.foo.BarService/providers` directory. And write your own URL address to the `/dubbo/com.foo.BarService/consumers` directory

When the monitoring center (Monitor) starts: subscribe to all provider and consumer URL addresses in the `/dubbo/com.foo.BarService` directory

Here is not a detailed description, the installation and startup process of Zookeeper.

How to use Dubbo?

 As an RPC framework, Dubbo's core function is to implement remote calls across networks . One is the provider of the service and the other is the consumer of the service. Through Dubbo, the service consumer can remotely call the service provider.

Service provider development

1. Create a maven project 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.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相关 -->
  <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>
        <!-- 指定端口 -->
        <port>8081</port>
        <!-- 请求路径 -->
        <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>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

3. Create a service interface

public interface HelloService {
    public String sayHello(String name);
}

4. Create a service implementation class

@Service
public class HelloServiceImpl implements HelloService {
    public String sayHello(String name) {
        return "hello " + name;
    }
}

5. Create applicationContext-service.xml under src/main/resources 

Note: This configuration needs to write the IP address where zookeeper is located

<?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">
    <!-- 当前应用名称,用于注册中心计算应用间依赖关系,注意:消费者和提供者应用名不要一样 -->
    <dubbo:application name="dubbodemo_provider" />
    <!-- 连接服务注册中心zookeeper ip为zookeeper所在服务器的ip地址-->
    <dubbo:registry address="zookeeper://ip地址:2181"/>
    <!-- 注册  协议和port   端口默认是20880 -->
    <dubbo:protocol name="dubbo" port="20881"></dubbo:protocol>
    <!-- 扫描指定包,加入@Service注解的类会被发布为服务  -->
    <dubbo:annotation package="com.itheima.service.impl" />
</beans>

7. Start the service

Service consumer development

1. Create a maven project, the pom.xml configuration is the same as the service provider above, just change the port number of the 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</servlet-class>
    <!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载 -->
    <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. Write Controller

Note: The HelloService injected into the Controller uses the @Reference annotation provided by Dubbo

@Controller
@RequestMapping("/demo")
public class HelloController {
    @Reference
    private HelloService helloService;

    @RequestMapping("/hello")
    @ResponseBody
    public String getName(String name){
        //远程调用
        String result = helloService.sayHello(name);
        System.out.println(result);
        return result;
    }
}

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">

    <!-- 当前应用名称,用于注册中心计算应用间依赖关系,注意:消费者和提供者应用名不要一样 -->
    <dubbo:application name="dubbodemo-consumer" />
    <!-- 连接服务注册中心zookeeper ip为zookeeper所在服务器的ip地址-->
    <dubbo:registry address="zookeeper://192.168.134.129:2181"/>
    <!-- 扫描的方式暴露接口  -->
    <dubbo:annotation package="com.itheima.controller" />
</beans>

6. Run the test

Enter http://localhost:8082/demo/hello.do?name=Jack in the browser to view the browser output

 

Guess you like

Origin blog.csdn.net/weixin_44126152/article/details/108615091