Dubbo is a high-performance, Java-based RPC framework, open-source by Alibaba. A distributed service framework. SOA (Service-oriented Architecture) architectures can be implemented. Dubbo uses: JD.com, Dangdang, Alibaba, China Telecom, etc.

The origin of distributed service architecture

The following architectural evolution process (the following example is purely illustrative and irrelevant to the business itself) :

In the early days, when telecommunications were landline only, the system had only one function for making calls and one function for billing. Because the business is single, there is only one system.

  • The monolithic architecture of a single business

Later, the telecommunications business became rich. Added “SMS”, “broadband”, “mobile phone traffic” and other service functions. According to the conventional practice, the original “call” single business system on the basis of a few more business function modules. All the business functions (” phone “, “SMS”, “broadband”, “mobile traffic”) are still within a project. The diagram below:

  • Multi-service singleton architecture

The single architecture in multi-service mode may cause the following problems when services expand and the system has more and more service function modules:

1. The coupling degree of business function modules is too high, which is not conducive to expansion, maintenance and promotion.

2, and there is a magic number in the program: 65535 (16bit maximum) limit, (because the instruction capacity of the call method is only 16bit, 65535 is the largest number that can hold 16bit). Too many repetitions will accelerate the approach to this upper limit. (For example, an Android app is easily capped at 65535).

For example, taobao, Tmall and Alibaba all need payment. It is assumed that the integration of Taobao, Tmall and Alibaba into three business function modules of one project will be messy. Therefore, there are three independent projects like Taobao, Tmall and Alibaba, which are similar to the following figure:

  • Vertical architecture

Through step by step evolution, the architecture has become the vertical architecture shown in the figure. But everyone noticed that the billing feature appeared four times. This is definitely not conducive to project maintenance and unified configuration. (And the billing shown above is just one of many possible duplicate modules). So the same modules used by multiple projects have to be isolated and shared with business functions. In this way, the following architecture is evolved:

As shown in the figure, billing is extracted separately into an independent app, which can be used by other apps. The “other” module is used to replace the thousands of modules that are similar to billing.

In this way, each square is a separate application. In this way, the complexity of services is solved, and services are modular and independent for easy sharing and expansion. This architecture gives us the following problems to solve:

1. How to solve the communication problem between independent Apps?

2. How to achieve unified scheduling and coordinated processing?

3. If the billing module is the module with the largest concurrency, but the concurrency of other modules is not large. How to implement load balancing for billing?

Architecture Evolution process

What is RPC?

Remote Procedure Call Protocol (RPC) Remote Procedure Call Protocol. A technique in which server A calls methods on server B. Dubbo is an RPC framework that implements remote procedure calls.

Schematic of Dubbo

The three main elements of Dubbo are: 1. Remote call of interface 2. Load balancing. 3. Automatic service registration and discovery

The use of Dubbo

1, description,

The Dubbo framework requires a registry, and in this case Redis is used as the registry for Dubbo. In addition to Redis, Zookeeper and others can also serve as registries for Dubbo.

2. Environmental requirements

JDK 1.6 or above.

3. Add dependencies

<dependencies> <! -- https://mvnrepository.com/artifact/com.alibaba/dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.5</version> </dependency> <dependency> <groupId>redis. Clients </groupId> < artifactId > jedis < / artifactId > < version > 2.9.0 < / version > < / dependency > < the dependency > < the groupId > org. Springframework. Data < / groupId > < artifactId > spring - data - redis < / artifactId > < version > 1.8.4. RELEASE < / version > </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.11.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId> < version > 4.12 < / version > < / dependency > < the dependency > < groupId > org. Springframework < / groupId > < artifactId > spring - test < / artifactId > < version > 4.3.11. RELEASE < / version > < / dependency > < / dependencies >Copy the code

4. Define Dubbo service interface

Create a separate module (and import the above dependencies in POM.xml) and create some interfaces and methods (also called services) in the Module. For example, create an interface IDubboService in dubbo_service_interface:

package com.javen.dubbo.service; import java.util.List; @author yangjw */ public interface IDubboService {List<String> getData(String data); }Copy the code

5. Define the service provider (interface implementer)

Create another module named dubbo_PROVIDer1 (and import the above dependencies in POM.xml). Add dubbo_service_interface as a dependency.

1. Create class DubboService and inherit IDubboService interface. The code is as follows:

package com.javen.dubbo.provider; import com.javen.dubbo.service.IDubboService; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; @author Yangjw */ @Component("dubboService") public class dubboService implements IDubboService{public List<String> getData(String data) { ArrayList<String> list = new ArrayList<String>(); List.add (" This is the data returned by the Provider in Dubbo: "+ data); return list; }}Copy the code

Configure spring-dubo. XML as follows:

<? The 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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <context:annotation-config/> <context:component-scan base-package="com.javen.dubbo.provider"/> <! Dubbo: Application name="dubbo_provider1"/> <! Register your service with Redis And configuration protocol and port 20880 - > < dubbo: registry address = "redis: / / 192.168.72.188:6379" / > < dubbo: protocol name = "dubbo" port = "20880" / > <! - configure the service interface, ref associated to the service implementation class - > < dubbo: service interface = "com. Javen. Dubbo. Service. IDubboService" ref = "dubboService" / > < / beans >Copy the code

3. Start the Provider with the following code:

package com.javen.dubbo.provider; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; public class StartProvider { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] {"spring-dubbo.xml"}); System.out.println(" service 1 started ~~~"); context.start(); System.in.read(); // Thread blocking: the service is guaranteed to exist, and if the thread terminates, the service terminates. // press any key to exit } }Copy the code

6. Define service consumers (specific callers of interfaces)

Create another module named dubbo_consumer (and import the above dependencies in pom.xml). Add dubbo_service_interface as a dependency.

Configure spring-dubo. XML as follows:

<? The 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="demo-consumer"/> <dubbo:registry Address = "redis: / / 192.168.72.188:6379" / > < dubbo: reference id = "demoService" interface="com.javen.dubbo.service.IDubboService"/> </beans>Copy the code

2, test,

package com.test; import com.javen.dubbo.service.IDubboService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; import java.util.List; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:spring-dubbo.xml") public class DubboTest { @Resource(name = "demoService") private IDubboService dubboService; @Test public void testDubbo(){ List<String> haha = dubboService.getData("haha"); System.out.println(haha.get(0)); }}Copy the code

Dubbo, MyCat, master-slave configuration, read-write separation, Redis distributed, JTA distributed transaction relationship.

By default, dubbo accesses only one server at a time.

<! Replicate writes to all servers simultaneously, but reads from a single server. Default is failover, reading and writing are single server - > < dubbo: registry cluster = "replicate" address = "redis: / / 192.168.72.188:6379" / >Copy the code