1. Distributed theory
1.1 basic definition of distributed
Definition of distributed system principle and paradigm:
"A distributed system is a collection of several independent computers that are like a single related system to users."
distributed system is a software system based on network.
1.2 architecture development and evolution
The development of architecture is built by the original single application architecture, which is generally ORM framework to facilitate database operation.
However, as the system becomes more and more complex, a single application architecture will become difficult to maintain, so the architecture has gradually evolved into a vertical application architecture. The so-called vertical application architecture is actually the installation of business templates for splitting. For example, an e-commerce system can be divided into order module, user information management module, commodity management module, etc, At this time, the MVC framework comes in handy. The MVC framework can help the system better split according to business. However, after business splitting, it is better maintained than a single application architecture.
However, as the system becomes more and more complex, it is found that many common modules are difficult to reuse. At this time, the distributed service architecture comes on stage. The distributed architecture extracts some core businesses as independent services and gradually forms a stable service center. When the application needs, it can go to the service center to adjust services, The RPC framework must implement this service registration.
When there are more and more services, problems such as capacity evaluation and waste of small service resources gradually appear. At this time, it is necessary to add a scheduling center to manage the cluster capacity in real time based on the access pressure and improve the cluster utilization. At this time, it is necessary to use the service oriented architecture to improve the resource scheduling of machine utilization. SOA is a governance center, To sum up, so far, the evolution of software system architecture has experienced: single application architecture - > vertical application architecture - > distributed application architecture - > mobile computing architecture. The following pictures on Dubbo's official website can be well described
1.3. What is RPC?
RPC concept RPC [Remote Procedure Call] refers to Remote Procedure Call. It is a way of inter process communication. It is a technical idea, not a specification. It allows a program to call a procedure or function in another address space (usually on another machine sharing a network) without the programmer explicitly encoding the details of the remote call.
RPC core module RPC has two core modules: communication and serialization
2. What is the Dubbo framework?
2.1 definition of Apache Dubbo
Apache Dubbo (incubating) | ˈ d ʌ b əʊ| It is a high-performance and lightweight open source Java RPC framework. It provides three core capabilities: interface oriented remote method invocation, intelligent fault tolerance and load balancing, and automatic service registration and discovery.
Official website: http://dubbo.apache.org/
2.2. Dubbo's role
- Provider: the service provider that exposes the service
- Container: the container where the service runs
- Consumer: consumer who calls the remote service
- Registry: the registry for service registration and discovery
- Minitor: a monitoring center that counts the number and time of service calls
2.3 Apache Dubbo principle
Dubbo's service governance:
Dubbo principle picture, from Dubbo official website:
Call procedure:
Let's explain it according to my understanding
- 0: the server container is responsible for starting, loading and running the service provider
- 1: After the service provider is started, it can expose the service to the registry
- 2: After service consumers start, they can subscribe to the desired service from the registry
- 3: The registry returns a list of service calls to the service consumer
- 4: The service consumer invokes the service of the service provider based on the soft load balancing algorithm. The service provider may be a list of service providers, and the service provider is invoked according to load balancing
- 5: Service providers and service consumers regularly push the service call times and service call times saved in memory to the monitoring center
3,Dubbo Spring Cloud
3.1 concept definition
Spring Cloud Alibaba Dubbo is a part of the Spring Cloud Alibaba project, which extends the ability of distributed service invocation. It not only enables Apache Dubbo and OpenFeign to coexist, but also allows the Spring Cloud standard to invoke the underlying communication protocol supported by Dubbo
3.2 comparison of functional characteristics
Functional features are summarized directly from the official website:
4. Experimental environment preparation
- Environment preparation: 64bit JDK 1.8 springboot 2.3.7.releasespringcloud (Hoxton. SR9) springcloudalibaba 2.2.2.releasesave 3.2+
- Development tool IntelliJ ideasmart Git
5. API project creation
Using the maven command
mvn archetype:generate -DgroupId=com.example.springcloud -DartifactId=dubbo-sample-api -Dversion=0.0.1-SNAPSHOT -DinteractiveMode=false
You can also create a maven project yourself
Created pom profile reference:
<?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>artifact-dubbo-sample-api</artifactId> <version>0.0.1-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>
Write an api interface:
package com.example.springcloud.api.service; /** * <pre> * ApiService * </pre> * * <pre> * @author mazq * Modify record * Modified version: Modified by: Modification date: 2021/01/19 14:57 Modified content: * </pre> */ public interface ApiService { String echo(String message); }
6. Start Nacos Service Center
For details, please refer to the official website: https://nacos.io/zh-cn/docs/quick-start.html , you need to download the source code of the Nacos server first, and then compile the startup project after downloading the source code:
window+R starts the cmd window, cd to the bin directory of the nacos server, and the linux system directly uses cd ${nacos_server_home}/bin
./startup.sh -m standalone
The window system uses the command startup.cmd -m standalone
Start successfully, access: http://127.0.0.1:8848/nacos The account and password are all Nacos
Log in successfully and go to the home page:
7. Service provider project creation
To create a new project, use Alibaba's service url:
Select the jdk version and packaging method:
Select components:
Auto generate project: add @ EnableDiscoveryClient support service 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 { }
Use @ DubboService to provide dubbo services:
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 * Modify record * Modified version: Modified by: Modification date: 2021/01/19 15:13 Modified content: * </pre> */ @DubboService public class EchoServiceImpl implements ApiService { @Override public String echo(String message) { return String.format("echo:%s",message); } }
application.properties configuration:
# apply name spring.application.name=dubbo-provider-sample # dubbo agreement dubbo.protocol.id=dubbo dubbo.protocol.name=dubbo # dubbo Protocol port( - one Indicates the self increment port, from twenty thousand eight hundred and eighty (start) dubbo.protocol.port=-1 # Dubbo The consumer subscribes to the application name of the server, and multiple service providers are separated by commas # The "self" subscription here will be ignored. Please add it according to the actual situation dubbo.cloud.subscribed-services=dubbo-provider-sample # dubbo Service scan benchmark package dubbo.scan.base-packages=com.example.springcloud.provider # Actuator Web Access port management.server.port=8082 management.endpoints.jmx.exposure.include=* management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always # application service WEB Access port server.port=8080 # Nacos help documentation: https://nacos.io/zh-cn/docs/concepts.html # Nacos certification information spring.cloud.nacos.discovery.username=nacos spring.cloud.nacos.discovery.password=nacos # Nacos Service discovery and registration configuration, where sub attributes server-addr appoint Nacos Server host and port spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 # Register to nacos Designation of namespace, the default is public spring.cloud.nacos.discovery.namespace=public
Start the project and go to the nacos service to view the service
8. Service consumer engineering creation
Create a Dubbo consumer sample service consumer project. The creation process is the same as that of the service provider project, but the configuration needs to be modified to avoid port conflict
# apply name spring.application.name=dubbo-consumer-sample # application service WEB Access port server.port=9090 # Actuator Web Access port management.server.port=9091 management.endpoints.jmx.exposure.include=* management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always # Nacos help documentation: https://nacos.io/zh-cn/docs/concepts.html # Nacos certification information spring.cloud.nacos.discovery.username=nacos spring.cloud.nacos.discovery.password=nacos # Nacos Service discovery and registration configuration, where sub attributes server-addr appoint Nacos Server host and port spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 # Register to nacos Designation of namespace, the default is public spring.cloud.nacos.discovery.namespace=public # Dubbo service configuration # dubbo agreement dubbo.protocol.id=dubbo dubbo.protocol.name=dubbo # dubbo Protocol port( - one Indicates the self increment port, from twenty thousand eight hundred and eighty (start) dubbo.protocol.port=-1 # Dubbo The consumer subscribes to the application name of the server, and multiple service providers are separated by commas dubbo.cloud.subscribed-services=dubbo-provider-sample
Use @ DubboReference to subscribe to the service:
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); } }
The linux test interface uses the command curl, window directly to the browser or postman to test:
curl http://127.0.0.1:9090/echo?message=nacos echo:nacos