视频学习推荐:https://www.bilibili.com/video/BV1ns411c7jV?p=4&vd_source=5d242c523ffcdeae19faea79735ffef6
分布式系统是若干独立 计算机的集合,这些计算机对于用户来说就像单个相关系统。
RPC协议是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。
Apache Dubbo 是一款微服务框架,为大规模微服务实践提供高性能 RPC 通信、流量治理、可观测性等解决方案,涵盖 Java、Golang 等多种语言 SDK 实现。
官网地址:https://dubbo.apache.org/zh/
官网学习地址:https://dubbo.apache.org/zh/docs/concepts/registry-configcenter-metadata/
服务提供者(Provider):
暴露服务的服务提供方,服务提供者在启动时,向注册中心注册自己提供的服务。
服务消费者(Consumer):
调用远程服务的服务消费方,服务消费者在启动时,向注册中心订阅自己所需的服务,服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
注册中心(Registry):
注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
监控中心(Monitor):
服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
Zookeeper 是 Apache Hadoop 的子项目,是一个树型的目录服务,支持变更推送,适合作为 Dubbo 服务的注册中心,工业强度较高,可用于生产环境,并推荐使用。
官网学习地址:https://dubbo.apache.org/zh/docs/references/registry/zookeeper/
项目参照官网:https://github.com/apache/dubbo-samples,对应dubbo-samples下的dubbo-samples-spring-boot项目
Spring Boot 快速开发 Dubbo 服务官方博文:https://dubbo.apache.org/zh/docs/quick-start/
项目中所使用的注解含义,可参照官方文档:Dubbo 中使用注解
创建一个名为dubbo-springboot-parent的maven项目作为父工程,在该项目下创建三个Module(都为Maven项目)分别为:
dubbo-springboot-parent 的Maven配置如下:
<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.0modelVersion>
<groupId>comdudugroupId>
<artifactId>dubbo-springboot-parentartifactId>
<packaging>pompackaging>
<version>1.0-SNAPSHOTversion>
<modules>
<module>dubbo-springboot-interfacemodule>
<module>dubbo-springboot-providermodule>
<module>dubbo-springboot-consumermodule>
modules>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<spring.version>4.3.16.RELEASEspring.version>
<dubbo.version>3.0.7dubbo.version>
<junit.version>4.12junit.version>
<slf4j-log4j12.version>1.7.25slf4j-log4j12.version>
<spring-boot.version>2.3.1.RELEASEspring-boot.version>
<spring-boot-maven-plugin.version>2.1.4.RELEASEspring-boot-maven-plugin.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>${spring-boot.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-bomartifactId>
<version>${dubbo.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-dependencies-zookeeperartifactId>
<version>${dubbo.version}version>
<type>pomtype>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>${junit.version}version>
<scope>testscope>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>${slf4j-log4j12.version}version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.7.0version>
<configuration>
<source>1.8source>
<target>1.8target>
configuration>
plugin>
plugins>
build>
project>
dubbo-springboot-interface的Maven配置如下:
<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-parentartifactId>
<groupId>comdudugroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<groupId>com.dudugroupId>
<artifactId>dubbo-springboot-interfaceartifactId>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-dependencies-zookeeperartifactId>
<type>pomtype>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-autoconfigureartifactId>
dependency>
dependencies>
project>
dubbo-springboot-consumer和dubbo-springboot-provider导入interface的依赖(实际开发中将依赖放在common中,其他模块导入common的依赖)
<dependencies>
<dependency>
<groupId>com.dudugroupId>
<artifactId>dubbo-springboot-interfaceartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<version>${spring-boot-maven-plugin.version}version>
<executions>
<execution>
<goals>
<goal>repackagegoal>
goals>
execution>
executions>
plugin>
plugins>
build>
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();
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
Zookeeper的安装可以参考这一篇博文:https://blog.csdn.net/ring300/article/details/80446918
Linux环境下可以参照官方博文:https://dubbo.apache.org/zh/docsv2.7/admin/install/zookeeper/
Windows环境下启动ZooKeeper:
启动ProviderApplication:
启动ConsumerApplication:
项目参照官网:https://github.com/apache/dubbo-samples,对应dubbo-samples下的dubbo-samples-basic项目
XML 配置方式官方博文:https://dubbo.apache.org/zh/docs/references/configuration/xml/
创建一个名为dubbo-parent的maven项目作为父工程,在该项目下创建三个Module(都为Maven项目)分别为:
dubbo-parent的Maven的依赖配置如下:
<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.0modelVersion>
<groupId>com.dudugroupId>
<artifactId>dubbo-parentartifactId>
<packaging>pompackaging>
<version>1.0-SNAPSHOTversion>
<modules>
<module>dubbo-apimodule>
<module>dubbo-producermodule>
<module>dubbo-consumermodule>
modules>
<properties>
<source.level>1.8source.level>
<target.level>1.8target.level>
<dubbo.version>3.0.7dubbo.version>
<junit.version>4.12junit.version>
<spring.version>4.3.16.RELEASEspring.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-framework-bomartifactId>
<version>${spring.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-bomartifactId>
<version>${dubbo.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-dependencies-zookeeperartifactId>
<version>${dubbo.version}version>
<type>pomtype>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>${junit.version}version>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<scope>testscope>
dependency>
dependencies>
project>
dubbo-api的Maven的依赖配置如下:
<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-parentartifactId>
<groupId>com.dudugroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>dubbo-apiartifactId>
<dependencies>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-dependencies-zookeeperartifactId>
<type>pomtype>
dependency>
dependencies>
project>
dubbo-consumer和dubbo-producer导入dubbo-api的依赖(实际开发中将依赖放在common中,其他模块导入common的依赖):
<dependencies>
<dependency>
<groupId>com.dudugroupId>
<artifactId>dubbo-apiartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
dependencies>
spring-dubbo-producer.xml配置文件:
<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 id="demoService" class="com.dudu.dubbo.api.impl.DemoServiceImpl"/>
<dubbo:service interface="com.dudu.dubbo.api.IDemoService" ref="demoService"/>
beans>
启动类Main:
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配置文件:
<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>
(zookeeper在前面已经启动了,这里不在启动)
启动生产者Main:
启动消费者Main:
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
我采用的是Maven方式部署,dubbo admin的服务启动必须要先启动zookeeper之后才能够启动。
git clone https://github.com/apache/dubbo-admin.git
修改配置文件,在项目dubbo-admin\dubbo-admin-server\src\main\resources\application.properties中:
添加server.port=7001修改网页启动的端口号
cd dubbo-admin
mvn clean package
cd dubbo-admin-distribution/target
java -jar dubbo-admin-0.1.jar
其实启动的就是生成的dubbo-admin-0.4.0.jar:
通过http://localhost:7001访问(账号和密码默认都为root)
(zookeeper在前面已经启动了,这里不在启动)
启动生产者Main:
启动消费者Main:
网页访问http://localhost:7001访问(账号和密码默认都为root)