Application and principle of Dubbo

Here are the Dubbo used in the project and some knowledge about Dubbo you know.
Plus assistant v: xiehuangbao1123 Get java learning materials and the latest interview materials



What is Dubbo?

Dubbo [] is a distributed service framework, which is committed to providing high-performance and transparent RPC remote service invocation scheme and SOA Service governance scheme.

Its core part includes:

  • Remote communication: it provides Abstract encapsulation of a variety of NIO frameworks based on long connection, including a variety of thread models, serialization, and information exchange mode of "request response" mode.
  • Cluster fault tolerance: provide transparent remote procedure calls based on interface methods, including multi protocol support, soft load balancing, fault tolerance, address routing, dynamic configuration and other cluster support.
  • Automatic discovery: Based on the registry directory service, the service consumer can dynamically find the service provider, make the address transparent, and enable the service provider to smoothly increase or decrease the number of machines.

What can Dubbo do?

  • Transparent remote method calls, like calling local methods, call remote methods with simple configuration and no API intrusion.
  • Soft load balancing and fault tolerance mechanism can replace F5 and other hardware load balancers in the intranet to reduce costs and single points.
  • Automatic service registration and discovery eliminates the need to write the address of the service provider. The registration center queries the IP address of the service provider based on the interface name, and can smoothly add or delete the service provider.

Spring Integration

Dubbo adopts the full Spring configuration mode, transparently accesses the application, and has no API intrusion on the application. It only needs Spring to load Dubbo's configuration, which is loaded based on the Schema Extension of Spring. [receiving data]

The above briefly introduces some concepts of Dubbo. Here is another picture to describe it vividly:

We use this figure to describe it, and then map it to the project:
When we deploy different systems in multiple tomcat, for example, system A (Tomcat A) wants to call the services in system B (Tomcat b), Dubbo has A place to play First, we need system B to register its own Url in the registration center, and then the registration center returns the Url to system A, so system A can call it Of course, what I'm talking about here is just A usage of Dubbo, and the remote call function of Dubbo is also used in this project (it feels A bit like web service)

Now let's get to the point and take a look at the application examples of Dubbo in the project:
1. Install Zookeeper under linux

[receiving data]
The installation of Zookeeper is not described in detail here. The previous blog on Linux has talked about the installation of software under Linux. After installation, start Zookeeper directly

2. Use requirements

Here, when we have a requirement that we need to do some operations on the product in the background (console), and here we can only use the public service method, how to call the implementation of the service in the product?

Project structure:

Public service method:

TestTbService.java:

1 package cn.itcast.core.service;
2 
3 import cn.itcast.core.bean.TestTb;
4 
5 public interface TestTbService {
6     public void insertTestTb(TestTb testTb);
7 }


service implementation class in product:
TestTbService.java:

 1 package cn.itcast.core.service;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Service;
 5 import org.springframework.transaction.annotation.Transactional;
 6 
 7 import cn.itcast.core.bean.TestTb;
 8 import cn.itcast.core.dao.TestTbDao;
 9 
10 @Service("testTbService")
11 @Transactional
12 public class TestTbServiceImpl implements TestTbService {
13 
14     @Autowired
15     private TestTbDao testTbDao;
16     
17     //preservation
18     public void insertTestTb(TestTb testTb){
19         testTbDao.insertTestTb(testTb);
20     }
21 }


Use the service implementation class in product in console:
CenterController.java:
[receiving data]

1 package cn.itcast.core.controller;
 2 
 3 import java.util.Date;
 4 
 5 import org.junit.runners.model.TestTimedOutException;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.stereotype.Controller;
 8 import org.springframework.ui.Model;
 9 import org.springframework.web.bind.annotation.RequestMapping;
10 
11 import cn.itcast.core.bean.TestTb;
12 import cn.itcast.core.service.TestTbService;
13 
14 @Controller
15 public class CenterController {
16     
17     @Autowired
18     private TestTbService testTbService;
19     
20     //test
21     @RequestMapping(value = "/test/index.do")
22     public void index(Model model){
23         
24         TestTb testTb = new TestTb();
25         testTb.setName("Fan Bingbing");
26         testTb.setBirthday(new Date());
27         
28         testTbService.insertTestTb(testTb);
29         
30     }
31 }


Can this direct call work? Of course not. Only the service method is defined in the console, so how can we call the service implementation class in product directly?

Of course, some configuration files are needed here:
First, you need to register services in product:
dubbo-provider.xml:

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 3     xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:aop="http://www.springframework.org/schema/aop" 
 5     xmlns:tx="http://www.springframework.org/schema/tx"
 6     xmlns:task="http://www.springframework.org/schema/task"
 7     xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
 8     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 9         http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
10         http://www.springframework.org/schema/mvc 
11         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
12         http://www.springframework.org/schema/context 
13         http://www.springframework.org/schema/context/spring-context-4.0.xsd 
14         http://www.springframework.org/schema/aop 
15         http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
16         http://www.springframework.org/schema/tx 
17         http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
18         http://www.springframework.org/schema/task
19            http://www.springframework.org/schema/task/spring-task-4.0.xsd
20         http://code.alibabatech.com/schema/dubbo        
21         http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
22         
23         
24         <!-- integration Dubbo -->
25         <!-- Step 1: Dubbo give a name    Calculations are distinguished by this name   -->
26         <dubbo:application name="babasport-service-product"/>
27         <!-- Step 2: intermediary registration center: zookeeper  redis ... -->
28         <!-- <dubbo:registry address="one hundred and ninety-two.168.200.128:2181,192.168.200.129:2181,192.168.200.130:2181" protocol="zookeeper"/> -->
29         <dubbo:registry address="192.168.200.128:2181" protocol="zookeeper"/>
30         <!-- Step 3: setting dubbo Port number of     192.168.40.88:20880/Interface  -->
31         <dubbo:protocol name="dubbo" port="20880"/>
32         <!-- Step 4: set the interface provided by the service provider -->
33         <dubbo:service interface="cn.itcast.core.service.TestTbService" ref="testTbService"/>
34         
35 </beans>


The following is used in the console:
Service consumer:
dubbo-cusmer.xml:

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 3     xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:aop="http://www.springframework.org/schema/aop" 
 5     xmlns:tx="http://www.springframework.org/schema/tx"
 6     xmlns:task="http://www.springframework.org/schema/task"
 7     xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
 8     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 9         http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
10         http://www.springframework.org/schema/mvc 
11         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
12         http://www.springframework.org/schema/context 
13         http://www.springframework.org/schema/context/spring-context-4.0.xsd 
14         http://www.springframework.org/schema/aop 
15         http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
16         http://www.springframework.org/schema/tx 
17         http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
18         http://www.springframework.org/schema/task
19            http://www.springframework.org/schema/task/spring-task-4.0.xsd
20         http://code.alibabatech.com/schema/dubbo        
21         http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
22         
23         <!-- integration Dubbo -->
24         <!-- Step 1: Dubbo give a name    Calculations are distinguished by this name   -->
25         <dubbo:application name="babasport-console"/>
26         <!-- Step 2: intermediary registration center    zookeeper  redis ... -->
27         <!--<dubbo:registry address="192.168.200.128:2181,192.168.200.129:2181,192.168.200.130:2181" protocol="zookeeper"/> -->
28         <dubbo:registry address="192.168.200.128:2181" protocol="zookeeper"/>
29         <!-- Step 3: call the interface provided by the service provider -->
30         <dubbo:reference interface="cn.itcast.core.service.TestTbService" id="testTbService"/>
31         
32 </beans>


The rest is to start the service:
Pay attention to starting the service provider first and then the service consumer

If there are mistakes in the article, please correct them and communicate with each other. Students who are used to reading technical articles on wechat and want to get more Java learning materials and interview materials can add v: xiehuangbao1123


Keywords: Java Linux Dubbo Spring Spring Boot

Added by joix on Fri, 28 Jan 2022 23:32:03 +0200