1. Distributed theory

1.1 Basic definition of Distributed

“Distributed System Principle and Paradigm” defines: “A distributed system is a collection of several independent computers, which are like a single related system for users.” A distributed system is a software system built on the network.

1.2 Evolution of the Architecture

Architecture evolved from the original single-application architecture, typically an ORM framework to facilitate database operations.

But as the system is more and more complex, a single application architecture will become difficult to maintain, so architecture evolved out of the vertical application architecture, the so-called vertical application architecture is installing business take apart the template, such as installing business an electricity system can be divided into order module, user information management module, commodity management module and so on, the MVC framework will come in handy at this time, The MVC framework can help systems to be better split by business, but the split is more maintainable than a single application architecture.

With the system to some complex, however, found it hard to reuse a lot of common modules, distributed service architecture appearance at this time, the distributed architecture is extracted some of the core business, as an independent service, gradually formed a stable service center, when the application needs, will go to the service center of the service can, and implement the service must be registered RPC framework.

When more and more services, capacity evaluation, waste of small Service resources and other problems gradually appear, at this time, it is necessary to add a scheduling center based on access pressure real-time management of cluster capacity, improve cluster utilization, at this time the need for mobile computing Architecture (SOA). Resource scheduling for machine utilization, SOA is a governance center. To sum up, software architecture has evolved so far: single application architecture -> vertical application architecture -> distributed application architecture -> mobile computing architecture, which can be best described in the following image on the Dubbo website

1.3 What is RPC?

Remote Procedure Call (RPC) refers to Remote Procedure Call, which is a method of communication between processes. It is a technical idea rather than a specification. It allows a program to call a procedure or function in another address space, usually on another machine on a shared network, without the programmer explicitly coding the details of this remote call.

RPC core module RPC has two core modules: communication and serialization

2. What is the Dubbo framework?

2.1 Apache Dubbo Definition

Apache Dubbo (incubating) | ˈ d ʌ b goes ʊ | is a high-performance, lightweight open source Java RPC framework, it provides three main core competencies: an interface of remote method invocation (rmi), intelligent fault tolerance and load balancing, and automatic registration and discovery of services.

Liverpoolfc.tv: dubbo.apache.org/

2.2 Dubbo role

  • Provider: exposes the service Provider

  • Container: The Container in which the service runs

  • Consumer: Consumer that invokes the remote service

  • Registry: A Registry where services are registered and discovered

  • Minitor: Monitoring center that counts the number and time of service invocations

2.3 Principles of Apache Dubbo

Dubbo’s service governance:

Dubbo principle picture from Dubbo official website:

Call procedure:

Here is an explanation based on my understanding

  • 0: The server container is responsible for starting, loading, and running the service provider

  • 1: Service providers can expose services to the registry after startup

  • 2: Service consumers can subscribe to the desired service from the registry after startup

  • 3: The registry returns a list of service invocations to the service consumer

  • 4: The service consumer invokes the service provider’s service based on the soft load balancing algorithm. The service provider may be a list of service providers, and the service provider is invoked based on the load balancing

  • 5: Service providers and service consumers regularly push the number and time of service invocation saved in memory to the monitoring center

Dubbo Spring Cloud

3.1 Concept Definition

Spring Cloud Alibaba Dubbo is one of the Spring Cloud Alibaba projects, which extends the distributed service invocation capability, not only enabling Apache Dubbo and OpenFeign to coexist, It also allows the Spring Cloud standard to call the underlying communication protocol over Dubbo

3.2 Comparison of Functions and Features

Function features directly quoted from the official website:

4. Preparation of experimental environment

  • Environment Preparation:

  • 64 – bit JDK 1.8

  • SpringBoot2.3.7. RELEASE

  • SpringCloud(Hoxton.SR9)

  • SpringCloudAlibaba2.2.2. RELEASE

  • Maven 3.2 +

  • The development tools

  • IntelliJ IDEA

  • smartGit

5. API project creation

Using the maven command

MVN archetype: generate-dgroupid =com.example. springcloud-dartifactid = dubo-sample-api-dversion =0.0.1-SNAPSHOT -DinteractiveMode=falseCopy the code

You can also create your own Maven project

Create a POM configuration file reference:

<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Example. Springcloud < / groupId > < artifactId > an artifact - dubbo - sample - API < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version > <name>artifact-dubbo-sample-api</name> <description>Demo project for Spring Boot</description> <packaging>jar</packaging> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>Copy the code

Write an API:

package com.example.springcloud.api.service; /** * <pre> * ApiService * </pre> ** <pre> * @author mazq * Modified record * * </pre> */ public interface ApiService { String echo(String message); }Copy the code

6. Start Nacos Service center

Nacos. IO /zh-cn/docs/… , you need to download the source code of the NACOS server first, and then compile and start the project:

Window +R Start CMD, CD go to the bin directory of nacos server, Linux directly run CD ${nacOS_SERVER_HOME}/bin

./startup.sh -m standalone
Copy the code

Run the startup. CMD -m standalone command on Windows

Start-up success, visit: http://127.0.0.1:8848/nacos, account passwords are nacos

Login successful, go to the home page:

7. Service provider project creation

To create a new project, use ali’s service URL:

Select JDK version and package method:

Select components:

Automatically generate projects: add @enableDiscoveryClient to enable services to register with NACOS

package com.example.springcloud.provider.nacosdiscovery;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration;

@EnableDiscoveryClient
@Configuration
public class NacosDiscoveryConfiguration {
}
Copy the code

Using @dubboService to provide dubbo service:

package com.example.springcloud.provider.service; import com.example.springcloud.api.service.ApiService; import org.apache.dubbo.config.annotation.DubboService; import org.apache.dubbo.config.annotation.Service; /** * <pre> * EchoServiceImpl * </pre> ** <pre> * @author mazq * Modified record * Modified by: 2021/01/19 15:13 Modified By: 2021/01/19 15:13 * </pre> */ @DubboService public class EchoServiceImpl implements ApiService { @Override public String echo(String message) { return String.format("echo:%s",message); }}Copy the code

Application. The properties configuration:

Name =dubbo-provider-sample # Dubbo dubbo. Protocol. Id =dubbo dubbo Dubbo protocol port (-1 indicates an incremented port starting from 20880) dubo.protocol. port=-1 # Dubbo consumer subscribes to the application name of the server, multiple service providers are separated by commas. Subscribed -services=dubbo-provider-sample # Dubbo service scan benchmark package Dubbo. Scan. Base - packages = com. Example. Springcloud. The provider # physical Web access port management server port = 8082 management.endpoints.jmx.exposure.include=* management.endpoints.web.exposure.include=* Management. The endpoint. Health. The show - the details = # always application service WEB server access port, the port = 8080 # Nacos documentation: https://nacos.io/zh-cn/docs/concepts.html # Nacos authentication information spring. Cloud. Nacos. Discovery. The username = Nacos Spring. Cloud. Nacos. Discovery. Password = # nacos nacos service discovery and register configuration, One child property server - addr Nacos server host and port specified spring. Cloud. Nacos. Discovery. The server - addr = 127.0.0.1:8848 # registered to Nacos specified namespace, The default to public spring. Cloud. Nacos. Discovery. The namespace = publicCopy the code

Start the project and go to the NacOS service to view the service

8. Service consumer project creation

Create a Dubbo-consumer-sample service consumer project. The process is the same as the service provider project, but the configuration needs to be modified to avoid port conflicts

Name = bbo-consumer-sample # Use the operating system to access the WEB server.port=9090 # Use the operating system to access the WEB management.server.port=9091 management.endpoints.jmx.exposure.include=* management.endpoints.web.exposure.include=* Management. The endpoint. Health. The show - the details = # always Nacos documentation: https://nacos.io/zh-cn/docs/concepts.html # Nacos authentication information spring. Cloud. Nacos. Discovery. The username = Nacos Spring. Cloud. Nacos. Discovery. Password = # nacos nacos service discovery and register configuration, One child property server - addr Nacos server host and port specified spring. Cloud. Nacos. Discovery. The server - addr = 127.0.0.1:8848 # registered to Nacos specified namespace, Default to public spring. Cloud. Nacos. Discovery. The namespace = public Dubbo. # # Dubbo service configuration Dubbo agreement protocol. The id = Dubbo Dubbo. Protocol. Name =dubbo # Dubbo protocol port (-1 indicates the self-added port, starting from 20880) Dubbo. Multiple service providers are separated by commas (,) to separate dubo.cloud. subscribed-services= dubo-provider-sampleCopy the code

Subscribe with @dubboReference:

package com.example.springcloud.consumer; import com.example.springcloud.api.service.ApiService; import org.apache.dubbo.config.annotation.DubboReference; import org.apache.dubbo.config.annotation.Reference; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class DubboConsumerSampleApplication { @DubboReference private ApiService echoService; @GetMapping("/echo") public String echo(String message) { return echoService.echo(message); } public static void main(String[] args) { SpringApplication.run(DubboConsumerSampleApplication.class, args); }}Copy the code

Use curl, window, or postman to test the interface:

The curl http://127.0.0.1:9090/echo? message=nacos echo:nacosCopy the code