Getting Started with Dubbo---Building a Simplest Demo Framework

Dubbo Background and Introduction

Dubbo started with the e-commerce system, so let's start with the evolution of the e-commerce system .

  1. Single Application Framework (ORM)
    When the website traffic is very small, only one application is needed, and all functions such as order payment are deployed together to reduce deployment nodes and costs.
    Disadvantages : The single system architecture makes more and more resources occupied in the development process, and becomes more and more difficult to maintain as the traffic increases
    write picture description here

  2. Vertical Application Framework (MVC)
    The vertical application architecture solves the expansion problem faced by a single application architecture. The traffic can be dispersed among various subsystems, and the volume of the system is controllable, which reduces the cost of collaboration and maintenance between developers to a certain extent. , improve the development efficiency.
    Disadvantages : But in the vertical architecture, the same logic code needs to be copied continuously and cannot be reused.
    write picture description here

  3. Distributed application architecture (RPC)
    When there are more and more vertical applications, the interaction between applications is inevitable. The core business is extracted as an independent service, and a stable service center is gradually formed.
    write picture description here


  4. With the further development of service-oriented architecture (SOA), there are more and more services, and the invocation and dependencies between services are becoming more and more complex . A series of corresponding technologies, such as service frameworks that encapsulate behaviors such as service provision, service invocation, connection processing, communication protocols, serialization methods, service discovery, service routing, and log output

From the above is the evolution of the e-commerce system, we can see the process of architecture evolution:
write picture description here

  • single application architecture

    • When the website traffic is small, only one application is needed, and all functions are deployed together to reduce deployment nodes and costs.
    • At this point, a data access framework (ORM) that simplifies the CRUD workload is key.
  • Vertical Application Architecture

    • When the number of visits gradually increases, the acceleration brought by adding a single application to the machine is getting smaller and smaller, and the application is divided into several applications that are not related to each other to improve efficiency.
    • At this point, a web framework (MVC) for accelerating front-end page development is key.
  • Distributed Service Architecture
    • When there are more and more vertical applications, the interaction between applications is inevitable. The core business is extracted as an independent service, and a stable service center is gradually formed, so that front-end applications can respond more quickly to changing market demands.
    • At this time, the distributed service framework (RPC) for improving business reuse and integration is the key.
  • Mobile Computing Architecture
    • When there are more and more services, problems such as capacity evaluation and waste of small service resources gradually appear, a dispatch center needs to be added to manage the cluster capacity in real time based on the access pressure to improve the cluster utilization.
    • At this point, a Resource Scheduling and Governance Center (SOA) for improving machine utilization is key.

An introduction to RPC is inserted here:
RPC (Remote Procedure Call Protocol): Remote Procedure Call :
Two servers A and B deploy different applications a and b respectively. When server A wants to call a function or method provided by application b on server B, it cannot be called directly because it is not in a memory space. It needs to express the semantics of the call and convey the data of the call through the network.
To put it bluntly, you wrote a program on your machine, which I cannot call directly. At this time, the concept of a remote service call appears.

RPC is a protocol for requesting services from a remote computer program over a network without requiring knowledge of the underlying network technology. The RPC protocol assumes the existence of some transport protocol, such as TCP or UDP, to carry information data between communicating programs. In the OSI network communication model, RPC spans the transport layer and the application layer. RPC makes it easier to develop applications including network distributed multiprogramming.
RPC uses a client/server model. The requestor is a client, and the service provider is a server. First, the client calling process sends a call message with process parameters to the server process, and then waits for a reply message. On the server side, the process stays asleep until the call message arrives. When a call message arrives, the server obtains the process parameters, calculates the result, sends a reply message, and then waits for the next call message. Finally, the client calls the process to receive the reply message, obtain the process result, and then the call execution continues.

Problems that RPC needs to solve:
(you can learn more about it, and see other blog posts for details)

  • Communication problems : Mainly by establishing a TCP connection between the client and the server, all the exchanged data of the remote procedure call are transmitted in this connection. The connection can be an on-demand connection that is disconnected after the call ends, or it can be a persistent connection, where multiple remote procedure calls share the same connection.
  • Addressing problem : How does the application on the A server tell the underlying RPC framework how to connect to the B server (such as a host or IP address) and a specific port, what is the name of the method, so that the call can be completed. For example, for RPC based on the Web service protocol stack, it is necessary to provide an endpoint URI, or to find it from the UDDI service. If it is called by RMI, an RMI Registry is also required to register the address of the service.
  • Serialization and deserialization : When the application on the A server initiates a remote procedure call, the parameters of the method need to be passed to the B server through the underlying network protocol such as TCP. Since the network protocol is based on binary, the value of the parameters in the memory must be Serialize into binary form, that is, serialize (Serialize) or marshal (marshal), and send the serialized binary to the B server through addressing and transmission.
    In the same way, the B server needs to deserialize the parameters to receive the parameters. The result returned by the B server application after calling its own method for processing should also be serialized to the A server, and the reception by the A server should also go through the process of deserialization.

What is Dubbo

Dubbo is:

  • A distributed service framework
  • High-performance and transparent RPC remote service invocation scheme
  • SOA Service Governance Solution

It provides support for more than 3 billion visits per day for more than 2,000 services, and is widely used in Alibaba Group's member sites and other companies' businesses.

Dubbo Architecture

write picture description here

Provider : The service provider that exposes the service.
Consumer : The service consumer that invokes the remote service.
Registry : A registry for service registration and discovery.
Monitor : A monitoring center that counts service calls and call times.

Call process
0. The service container is responsible for starting, loading, and running the service provider.
1. When the service provider starts, it registers the service it provides with the registration center.
2. When the service consumer starts, it subscribes to the registration center for the services it needs.
3. The registry returns the service provider address list to the consumer. If there is a change, the registry will push the change data to the consumer based on the persistent connection.
4. The service consumer, from the provider address list, selects a provider to call based on the soft load balancing algorithm, and if the call fails, selects another provider to call.
5. 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

Dubbo Registration Center

For the service provider, it needs to publish services, and due to the complexity of the application system, the number and types of services are also expanding;
for the service consumer, it is most concerned about how to obtain the services it needs, and faces complex applications. system that needs to manage a large number of service calls.
Moreover, for service providers and service consumers, they may also have both roles, that is, they need to provide services and consume services.

Through unified management of services, the process and management of service publishing/use by internal applications can be effectively optimized. The service registry can complete the external unification of services through specific protocols.

The registration center provided by Dubbo has the following types to choose from :

  • Multicast Registry
  • Zookeeper Registry
  • Redis registry
  • Simple Registry

Advantages and disadvantages of Dubbo

advantage:

  1. Transparent remote method invocation
    - Invoke remote methods like local methods; simple configuration without any API intrusion.
  2. Soft load balancing and fault tolerance mechanism
    • It can replace hardware load balancers such as nginx lvs in the intranet.
  3. Service registry automatic registration & configuration management
    - no need to write the service provider address, the registry automatically queries the provider ip based on the interface name.
    Using distributed coordination services such as zookeeper as the service registry, most project configurations can be moved into the zookeeper cluster.
  4. Service interface monitoring and management
    - Dubbo-admin and Dubbo-monitor provide complete service interface management and monitoring functions. For different interfaces of different applications, multi-version, multi-protocol, and multi-registration management can be performed.

shortcoming:

  • Only supports JAVA language

Dubbo Entry Demo

After understanding Dubbo, it is natural to build a simple Demo implementation. This article adopts the integration of Dubbo with Zookeeper and Spring framework.

The main steps are as follows:
1. Install Zookeeper and start it;
2. Create a MAVEN project and build a simple Demo implemented by Dubbo+Zookeeper+Spring;
3. Install Dubbo-admin for monitoring.

1 Zookeeper introduction and installation

The Dubbo registry in this demo uses Zookeeper. Why use Zookeeper?

Zookeeper is a distributed service framework. It is a tree-type directory service data store. It can manage data in clusters. It can be used as a registration center for Dubbo services.

Dubbo can be deployed in clusters with Zookeeper. When the provider experiences abnormal shutdowns such as power failure, the Zookeeper registry can automatically delete provider information. When the provider restarts, it can automatically restore registration data and subscription requests.

The specific installation methods are not described here, please refer to the blog post:
http://blog.csdn.net/tlk20071/article/details/52028945

After the installation is complete, go to the bin directory and start zkServer.cmd. This script will start a java process:
(Note: You need to start zookeeper first, and then run the dubbo demo code to use the function of the zookeeper registration center)
write picture description here

2 Create a MAVEN project

Project structure:
It is mainly divided into three modules:
dubbo-api : stores public interfaces;
dubbo-consumer : calls remote services;
dubbo-provider : provides remote services.
write picture description here

The code building process will be described in detail below.
1) First build the MAVEN project and import the required jar package dependencies.
There are jar packages such as spring, dubbo, zookeeper, etc. that need to be imported.
(For details, please refer to the project code provided later)

2) Create a MAVEN project of dubbo-api (with a separate pom.xml for packaging for provider and consumer use).
Define the service interface in the project: The interface needs to be packaged separately and shared between the service provider and the consumer.
write picture description here

package com.alibaba.dubbo.demo;
import java.util.List;

public interface DemoService {
    List<String> getPermissions(Long id);
}

3) Create a MAVEN project of dubbo-provider (with a separate pom.xml for packaging for consumers).
Implement the public interface, this implementation is hidden from consumers:

package com.alibaba.dubbo.demo.impl;

import com.alibaba.dubbo.demo.DemoService;

import java.util.ArrayList;
import java.util.List;
public class DemoServiceImpl implements DemoService {
    public List<String> getPermissions(Long id) {
        List<String> demo = new ArrayList<String>();
        demo.add(String.format("Permission_%d", id - 1));
        demo.add(String.format("Permission_%d", id));
        demo.add(String.format("Permission_%d", id + 1));
        return demo;
    }
}

Need to add dependencies where the public interface is located
write picture description here

Expose services with Spring configuration declarations

<?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: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://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!--定义了提供方应用信息,用于计算依赖关系;在 dubbo-admin 或 dubbo-monitor 会显示这个名字,方便辨识-->
    <dubbo:application name="demotest-provider" owner="programmer" organization="dubbox"/>
    <!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper-->
    <dubbo:registry address="zookeeper://localhost:2181"/>
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <!--使用 dubbo 协议实现定义好的 api.PermissionService 接口-->
    <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" protocol="dubbo" />
    <!--具体实现该接口的 bean-->
    <bean id="demoService" class="com.alibaba.dubbo.demo.impl.DemoServiceImpl"/>
</beans>

Start the remote service:

package com.alibaba.dubbo.demo.impl;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class Provider {
        public static void main(String[] args) throws IOException {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
            System.out.println(context.getDisplayName() + ": here");
            context.start();
            System.out.println("服务已经启动...");
            System.in.read();
        }
    }

4) Create a MAVEN project of dubbo-consumer (there can be multiple consumers, but they need to be configured).
Call the desired remote service:

Reference the remote service via Spring configuration:

<?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: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://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <dubbo:application name="demotest-consumer" owner="programmer" organization="dubbox"/>
    <!--向 zookeeper 订阅 provider 的地址,由 zookeeper 定时推送-->
    <dubbo:registry address="zookeeper://localhost:2181"/>
    <!--使用 dubbo 协议调用定义好的 api.PermissionService 接口-->
    <dubbo:reference id="permissionService" interface="com.alibaba.dubbo.demo.DemoService"/>
</beans>

Start the Consumer and call the remote service:

package com.alibaba.dubbo.consumer;
import com.alibaba.dubbo.demo.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Consumer {
    public static void main(String[] args) {
        //测试常规服务
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("consumer.xml");
        context.start();
        System.out.println("consumer start");
        DemoService demoService = context.getBean(DemoService.class);
        System.out.println("consumer");
        System.out.println(demoService.getPermissions(1L));
    }
}

5) Run the project, first ensure that the provider has been run and then start the consumer module:
run the provider:
write picture description here
the consumer successfully calls the remote service provided by the provider:
write picture description here

Of course, this is just a simulated project. In reality, there are multiple providers and multiple consumers, which is much more complicated than this. Of course, only in this way can the characteristics of dubbo be reflected.

Introduction of Dubbo Management Console

Admin Console Features

Routing rules, dynamic configuration, service degradation,
access control, weight adjustment,
load balancing
write picture description here

Download dubbo-admin and install it according to the online introduction. The general approach is to replace the contents of a folder in dubbo-admin with the conf of tomcat, and then run tomcat. But I found that JDK8 could not run in actual operation, and later found a dubbo-admin version that JDK8 can implement. Download address: http://www.itmayun.com/it/files/226631678709806/resource/901920001882583/1.html .

After successfully entering the user name and password root, you can enter the console home page to view the consumer provider status:
View provider:
write picture description here
View consumer:
write picture description here

Unfortunately, the official Dubbo website is no longer maintained, and there are many updated Dubbo, such as Dangdang's Dubbox, which you can learn about yourself.

The code of the entire project has been uploaded to my github https://github.com/nomico271/DatatablesDemo.git , welcome to check it out.
(The pictures in the project on github are the content of the blog, which can be deleted).

Reference: https://www.zhihu.com/question/25536695

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325295222&siteId=291194637