Table of contents
Dubbo concept_what is a distributed system
Dubbo configuration development environment_Zookeeper registration center
Dubbo configuration development environment_management console
Dubbo Getting Started Case_Requirements Introduction
Getting started with Dubbo_Configuring the development environment
Dubbo Getting Started Case_Service Producer Configuration
Dubbo Getting Started Case_Service Producer Code Writing
Dubbo concept_what is a distributed system
stand-alone architecture
When the business volume of a system is small, all the codes should be placed in one project, and then this project is deployed on a server, and all the services of the entire project are provided by this server.
shortcoming:
- There is a bottleneck in service performance
- The amount of code is huge, the system is bloated, and it affects the whole body
- single point of failure problem
cluster architecture
When the stand-alone processing reaches the bottleneck, you make several copies of the stand-alone, thus forming a cluster.
Problems with clusters:
When your business develops to a certain extent, you will find a problem that no matter how you add nodes, it seems that the performance improvement effect of the entire cluster is not obvious. At this time, you need to use a distributed architecture.
what is distributed
Distributed architecture is to split a complete system into independent subsystems according to business functions. In distributed architecture, each subsystem is called "service". These subsystems can run independently in the web container, and communicate with each other through RPC.
Distributed advantages:
1. The degree of coupling between systems is greatly reduced, independent development, independent deployment, and independent testing are possible. The boundaries between systems are very clear, troubleshooting becomes quite easy, and development efficiency is greatly improved.
2. The degree of coupling between systems is reduced, so that the system is easier to expand. We may extend certain services in a targeted manner.
3. Higher reusability of services. For example, when we use the user system as a separate service, all products of the company can use this system as the user system without repeated development.
The difference between the three
Summary: Split a system into different subsystems and deploy them on different servers (this is called distributed), and then deploy multiple identical subsystems on different servers (this is called clustering).
Cluster: Multiple people doing the same thing together.
Distributed: Multiple people work together to do different things.
real-time learning feedback
1. The following are distributed advantages that are ___.
The degree of coupling between A systems is greatly reduced
B services are more reusable
C Independent deployment, independent testing
All of the above are correct
2. Which of the following describes the distributed architecture is ____.
A Split a system into different subsystems and deploy them on different servers
B All code is placed in one project
C Multiple identical subsystems are deployed on different servers
All of the above are wrong
Dubbo concept_what is RPC
What is RPC
RPC (Remote Procedure Call) remote procedure call, which is a request service from a remote computer program through the network.
The vernacular understanding is: RPC allows you to use other people's things as if they were your own.
RPC has two functions:
- Shielding the difference between remote calls and local calls makes us feel like calling methods in the project
- Hiding the complexity of the underlying network communication allows us to focus more on business logic.
Commonly used RPC frameworks
RPC is a technical idea rather than a specification or protocol.
Common RPC technologies and frameworks:
Ali's Dubbo/Dubbox, Google gRPC, Spring Cloud.
Real-time effect feedback
1. RPC is a _____ technology.
A local call
B remote procedure call
C procedure call
D service call
2. The following __ frameworks are not RPC frameworks.
A Dubbo
B Spring Cloud
C GooglegRPC
D Spring MVC
Dubbo Concept_Introduction
What is Dubbo
Apache Dubbo is a high-performance, lightweight open source service framework dedicated to providing high-performance and transparent RPC remote service invocation solutions and SOA service governance solutions.
What can Dubbo do?
1. Transparent remote method invocation, invoking remote methods just like invoking local methods, only needs simple configuration, without any API intrusion.
2. The soft load balancing and fault tolerance mechanism can replace hardware load balancers such as F5 in the intranet, reducing costs and single points.
3. Automatic service registration and discovery, no need to hardcode 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
Notice:
Dubbo adopts the full Spring configuration method, transparently accessing the application, without any API intrusion into the application, just load the Dubbo configuration with Spring.
Protocols supported by Dubbo
The protocol is the basis for communication between two network entities. Data is transmitted from one entity to another on the network, and is delivered to the opposite end in the form of a byte stream. In this byte stream world, if there is no protocol, it is impossible to reshape this one-dimensional byte stream into a two-dimensional or multi-dimensional data structure and domain object.
Protocols supported by Dubbo
- Dubbo protocol
- Hessian agreement
- HTTP protocol
- RMI protocol
- WebService protocol
- Memcached protocol
- Redis protocol
Recommendation: Use the Dubbo protocol.
Real-time effect feedback
1. Apache Dubbo is a _____ framework.
A high-performance web service framework
B High performance web platform
C High Performance RPC Distributed Service Framework
D RPC framework
2. The main reason for using a distributed architecture is ___.
A to speed up the response
B Improve the high availability of the system
C can withstand larger concurrent traffic
D Improve the security of the system
Dubbo Concept_Core Components
Registry
In the Dubbo microservice system, the registration center is one of its core components. Dubbo realizes registration and discovery between services in a distributed environment through the registration center, which is the link between distributed nodes.
Its main functions are as follows:
Dynamic joining: A service provider can dynamically expose itself to other consumers through the registration center, without requiring consumers to update configuration files one by one.
Dynamic discovery: A consumer can dynamically perceive new configurations, routing rules, and new service providers without restarting the service to take effect.
Dynamic adjustment: The registration center supports dynamic adjustment of parameters, and new parameters are automatically updated to all relevant service nodes.
Unified configuration: Avoid the problem of inconsistency in the configuration of each service caused by local configuration.
Common Registry Discovery Service
Common registration centers include zookeeper, eureka, consul, etcd.
Service provider Provider
service provider
Service Consumer Consumer
The service consumer that invokes the remote service
Monitoring center Monitor
It is mainly responsible for monitoring and counting the number of calls and call time.
work process
Real-time effect feedback
1. The following ____ technologies in Apache Dubbo technology can be used for service registration and discovery services.
A Spring
B Nginx
C zookeeper
D Redis
Dubbo configuration development environment_Zookeeper registration center
Download the Zookeeper image
docker pull zookeeper:3.5.9
start running container
docker run --name zk -d -p 2181:2181
zookeeper:3.5.9
parameter:
-d: daemon running
-p: map port number
into the container
docker exec -it zk /bin/bash
parameter:
exec: Execute a command in a running container
-it: interactive
Dubbo configuration development environment_management console
introduce
Dubbo-admin management platform, graphical service management page, you need to specify the registration center address during installation, and you can obtain all providers/consumers from the registration center for configuration management.
Download the Dubbo-Admin image
docker pull docker.io/apache/dubbo-admin
start running container
docker run -d \
--name dubbo-admin \
-p 9600:8080 \
-e
admin.registry.address=zookeeper://192.168.66.102:2181 \
-e admin.config-center=zookeeper://192.168.66.102:2181 \
-e admin.metadata-report.address=zookeeper://192.168.66.102:2181 \
--restart=always \ docker.io/apache/dubbo-admin
parameter:
- --restart: always always restart when the container exits
- admin.registry.address: registry
- admin.config-center: configuration center
- admin.metadata-report.address: metadata center
visual interface
Browser input http://192.168.66.100:9600, user name root password root
Dubbo Getting Started Case_Requirements Introduction
monolithic architecture
project structure
order service
The function is as follows:
Create Order
Query order details based on user id
user service
The function is as follows:
Create Order
Query order details based on user id
Getting started with Dubbo_Configuring the development environment
Create a Maven project
create project
Set the JDK version
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
set item as parent type
<packaging>pom</packaging>
Modify character encoding
File->Settings->Editor->File Encodings
configure ignore file
File->Editor->File Types->Ignore Files and Folders
*.md;*.gitignore;.mvn;.idea;
Set aliyun Alibaba cloud maven local warehouse mirroring
<repositories>
<repository>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
Dubbo Getting Started Case_Service Producer Configuration
Create a service consumer SpringBoot project
Create a SpringBoot project
create submodule
select components
Modify the parent project configuration
Service producer modifies parent project configuration
<parent>
<groupId>com.tong</groupId>
<artifactId>dubbo-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
Parent project join configuration
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath/>
</parent>
The parent project joins the module
Add in the pom.xml file
<modules>
<module>dubbo-provider</module>
</modules>
Introduce dependencies
<!-- 整合dubbo -->
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
<!-- zookeeper客户端 -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.7</version>
</dependency>
Dubbo Getting Started Case_Service Producer Code Writing
Create an order entity class
/**
* 订单模型
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Order implements Serializable {
// 订单id
private Long id;
// 用户id
private Long userId;
// 订单总价格
private Double prict;
// 收货人手机号
private String mobile;
// 收货人地址
private String address;
// 支付类型 1:微信 2:支付宝
private Integer pay_method;
}
Write order interface
public interface IOrderService {
//创建订单
void createOrders(Orders orders);
//根据用户id查询订单详情
Orders findByuserId(Long userid);
}
Create a uniform return result set entity class
/**
* 统一返回结果集
* @param <T>
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> implements Serializable {
// 返回结果编码
private Integer code;
// 返回结果描述
private String message;
// 数据
private T data;
private CommonResult(Integer code,String message){
this(code,message,null);
}
}
Write order business implementation class
import com.alibaba.dubbo.config.annotation.Service;
import com.tong.entity.CommonResult;
import com.tong.entity.Order;
import com.tong.service.IOrderService;
/**
* 订单功能业务层
*/
@Service
public class OrderServiceImpl implements IOrderService {
/**
* 创建订单
* @param order
* @return
*/
@Override
public void create(Orders order) {
CommonResult commonResult = new CommonResult();
// 返回结果编码
commonResult.setCode(200);
// 返回结果描述信息
commonResult.setMessage("创建成功");
return commonResult;
}
/**
* 根据用户id查询订单详情
* @param userId
* @return
*/
@Override
public CommonResult<Orders> findByUserId(Long userId) {
//TODO 模拟数据库操作
CommonResult commonResult = new CommonResult();
// 返回结果编码
commonResult.setCode(200);
// 返回结果描述信息
commonResult.setMessage("查询成功");
// 返回结果集
Orders orders = new Orders();
orders.setId(1L);
orders.setUserId(1L);
orders.setPrict(121.1);
orders.setMobile("18588888888");
orders.setAddress("北京市海淀区中关村");
orders.setPay_method(1);
commonResult.setData(order);
return commonResult;
}
}
Service producers write configuration files
# 端口号
server.port=9090
# 1. 配置项目名称
spring.dubbo.application.name=user-service
# 2. 配置注册中心地址
spring.dubbo.registry.address=zookeeper://192.168.66.100
spring.dubbo.registry.port=2181
# 3. 指定dubbo使用的协议、端口
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
# 4. 指定注册到zk上超时时间,ms
spring.dubbo.registry.timeout=10000
# 5. 配置Dubbo包扫描
spring.dubbo.scan=com.tong.service
Write Dubbo annotations for interface implementation classes
import com.alibaba.dubbo.config.annotation.Service;
@service // 将这个类提供的方法(服务) 对外发布。将访问的地址 ip 端口路径 注册到注册中心
//@Service //将该类的对象创建出来放到spring的IOC容器中。 bean定义
public class OrderServiceImpl implements IOrderService {
}
Visualization platform