一.理论知识学习
视频学习推荐:https://www.bilibili.com/video/BV1ns411c7jV?p=4&vd_source=5d242c523ffcdeae19faea79735ffef6
1.什么是分布式系统
分布式系统是若干独立 计算机的集合,这些计算机对于用户来说就像单个相关系统。
2.什么是RPC
RPC协议是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。
3.什么是Apache Dubbo
Apache Dubbo 是一款微服务框架,为大规模微服务实践提供高性能 RPC 通信、流量治理、可观测性等解决方案,涵盖 Java、Golang 等多种语言 SDK 实现。
官网地址:https://dubbo.apache.org/zh/
4.Apache Dubbo部署架构(注册中心 配置中心 元数据中心)
官网学习地址:https://dubbo.apache.org/zh/docs/concepts/registry-configcenter-metadata/
服务提供者(Provider):
暴露服务的服务提供方,服务提供者在启动时,向注册中心注册自己提供的服务。
服务消费者(Consumer):
调用远程服务的服务消费方,服务消费者在启动时,向注册中心订阅自己所需的服务,服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
注册中心(Registry):
注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
监控中心(Monitor):
服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
5.什么是Zookeeper
Zookeeper 是 Apache Hadoop 的子项目,是一个树型的目录服务,支持变更推送,适合作为 Dubbo 服务的注册中心,工业强度较高,可用于生产环境,并推荐使用。
官网学习地址:https://dubbo.apache.org/zh/docs/references/registry/zookeeper/
二.快速开始
1.SpringBoot(注解方式)
项目参照官网: https://github.com/apache/dubbo-samples,对应dubbo-samples下的dubbo-samples-spring-boot项目
Spring Boot 快速开发 Dubbo 服务官方博文: https://dubbo.apache.org/zh/docs/quick-start/
项目中所使用的注解含义,可参照官方文档: Dubbo 中使用注解
(1) 项目搭建
创建一个名为dubbo-springboot-parent的maven项目作为父工程,在该项目下创建三个Module(都为Maven项目)分别为:
- dubbo-springboot-consumer 消费者
- dubbo-springboot-interface 服务接口
- dubbo-springboot-provider 生产者
(2) 项目的依赖配置与管理
dubbo-springboot-parent 的Maven配置如下:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>comdudu</groupId>
<artifactId>dubbo-springboot-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>dubbo-springboot-interface</module>
<module>dubbo-springboot-provider</module>
<module>dubbo-springboot-consumer</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring.version>4.3.16.RELEASE</spring.version>
<dubbo.version>3.0.7</dubbo.version>
<junit.version>4.12</junit.version>
<slf4j-log4j12.version>1.7.25</slf4j-log4j12.version>
<spring-boot.version>2.3.1.RELEASE</spring-boot.version>
<spring-boot-maven-plugin.version>2.1.4.RELEASE</spring-boot-maven-plugin.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Spring dubbo-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
</dependency>
<!--junit测试类-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j-log4j12.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
dubbo-springboot-interface的Maven配置如下:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo-springboot-parent</artifactId>
<groupId>comdudu</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.dudu</groupId>
<artifactId>dubbo-springboot-interface</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<type>pom</type>
</dependency>
<!-- dubbo starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<!-- spring starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
</project>
dubbo-springboot-consumer和dubbo-springboot-provider导入interface的依赖(实际开发中将依赖放在common中,其他模块导入common的依赖)
<dependencies>
<dependency>
<groupId>com.dudu</groupId>
<artifactId>dubbo-springboot-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-maven-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
(3) 在dubbo-springboot-interface模块中编写一个名为SayHelloService接口
(4) 在dubbo-springboot-provider模块中编写SayHelloService接口的实现类
SyaHelloServiceImpl接口实现类:
application.yaml配置文件:
dubbo:
application:
name: dubbo-springboot-demo-provider
protocol:
name: dubbo
port: 20887
registry:
id: zk-registry
address: zookeeper://127.0.0.1:2181
config-center:
address: zookeeper://127.0.0.1:2181
metadata-report:
address: zookeeper://127.0.0.1:2181
ProviderApplication:启动类
EmbeddedZooKeeper类可以通过代码方式启动ZooKeeper,启动代码如下:
new EmbeddedZooKeeper(2181, false).start();
(5) 在dubbo-springboot-consumer模块中编写相应代码调用sayHello()方法
ConsumerApplication启动类:
application.yaml配置文件:
dubbo:
application:
name: dubbo-springboot-demo-consumer
protocol:
name: dubbo
port: 20887
registry:
id: zk-registry
address: zookeeper://127.0.0.1:2181
config-center:
address: zookeeper://127.0.0.1:2181
metadata-report:
address: zookeeper://127.0.0.1:2181
(6) 测试(Windows环境下)
Zookeeper的安装可以参考这一篇博文: https://blog.csdn.net/ring300/article/details/80446918
Linux环境下可以参照官方博文: https://dubbo.apache.org/zh/docsv2.7/admin/install/zookeeper/
Windows环境下启动ZooKeeper:
启动ProviderApplication:
启动ConsumerApplication:
2.Spring(XML配置方式)
项目参照官网: https://github.com/apache/dubbo-samples,对应dubbo-samples下的dubbo-samples-basic项目
XML 配置方式官方博文: https://dubbo.apache.org/zh/docs/references/configuration/xml/
(1) 项目搭建
创建一个名为dubbo-parent的maven项目作为父工程,在该项目下创建三个Module(都为Maven项目)分别为:
- dubbo-consumer 消费者
- dubbo-api服务接口
- dubbo-producer生产者
(2) 项目的依赖配置与管理
dubbo-parent的Maven的依赖配置如下:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dudu</groupId>
<artifactId>dubbo-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>dubbo-api</module>
<module>dubbo-producer</module>
<module>dubbo-consumer</module>
</modules>
<properties>
<source.level>1.8</source.level>
<target.level>1.8</target.level>
<dubbo.version>3.0.7</dubbo.version>
<junit.version>4.12</junit.version>
<spring.version>4.3.16.RELEASE</spring.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
dubbo-api的Maven的依赖配置如下:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo-parent</artifactId>
<groupId>com.dudu</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-api</artifactId>
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<type>pom</type>
</dependency>
</dependencies>
</project>
dubbo-consumer和dubbo-producer导入dubbo-api的依赖(实际开发中将依赖放在common中,其他模块导入common的依赖):
<dependencies>
<dependency>
<groupId>com.dudu</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
(3) 在dubbo-api模块中编写一个名为IDemoService接口
(4) 在dubbo-producer模块中编写IDemoService接口的实现类
spring-dubbo-producer.xml配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="dubbo-demo-producer"/>
<!-- 注册中心地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 服务端口 -->
<dubbo:protocol name="dubbo" port="20887"/>
<!--bean对象-->
<bean id="demoService" class="com.dudu.dubbo.api.impl.DemoServiceImpl"/>
<!--暴露服务-->
<dubbo:service interface="com.dudu.dubbo.api.IDemoService" ref="demoService"/>
</beans>
启动类Main:
(5) 在dubbo-consumer模块中编写相应代码调用sayHello()方法
Main启动类:
package com.dudu.dubbo;
import com.dudu.dubbo.api.IDemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) throws InterruptedException {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext(new String[]{"spring-dubbo-consumer.xml"});
classPathXmlApplicationContext.start();
IDemoService iDemoService = (IDemoService) classPathXmlApplicationContext.getBean("demoService");
System.out.println("这里是消费者:");
while (true) {
Thread.sleep(1000);
System.out.println(iDemoService.sayHello());
}
}
}
spring-dubbo-consumer.xml配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="dubbo-demo-producer"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<dubbo:reference id="demoService" check="false" interface="com.dudu.dubbo.api.IDemoService"/>
</beans>
(6) 测试
(zookeeper在前面已经启动了,这里不在启动)
启动生产者Main:
启动消费者Main:
三.Dubbo Admin
1.什么是dubbo-admin
dubbo-admin(又叫管理控制台),作为dubbo的监控中心目前的管理控制台已经发布 0.1 版本,结构上采取了前后端分离的方式,前端使用 Vue 和 Vuetify 分别作为 Javascript 框架和UI框架,后端采用 Spring Boot 框架。既可以按照标准的 Maven 方式进行打包,部署,也可以采用前后端分离的部署方式,方便开发,功能上,目前具备了服务查询,服务治理(包括 Dubbo 2.7 中新增的治理规则)以及服务测试三部分内容。
部署方式,参照官方博文:https://dubbo.apache.org/zh/docs/v2.7/admin/ops/introduction/
dubbo-admin的github地址:https://github.com/apache/dubbo-admin/tree/master
2.安装
我采用的是Maven方式部署,dubbo admin的服务启动必须要先启动zookeeper之后才能够启动。
(1) 项目clone
git clone https://github.com/apache/dubbo-admin.git
(2) 修改项目的配置文件(修改端口号)
修改配置文件,在项目dubbo-admin\dubbo-admin-server\src\main\resources\application.properties中:
添加server.port=7001修改网页启动的端口号
(3) 项目clean
cd dubbo-admin
mvn clean package
(4) 启动项目
cd dubbo-admin-distribution/target
java -jar dubbo-admin-0.1.jar
其实启动的就是生成的dubbo-admin-0.4.0.jar:
(5) 访问
通过http://localhost:7001访问(账号和密码默认都为root)
3.启动XML方式项目的生产者和消费者Main
(zookeeper在前面已经启动了,这里不在启动)
启动生产者Main:
启动消费者Main:
网页访问http://localhost:7001访问(账号和密码默认都为root)