dubbo核心架构及实例
dubbo核心架构
参考官网 :http://dubbo.apache.org/zh/docs/v2.7/user/preface/architecture/
-
服务容器负责启动,加载,运行服务提供者
-
服务提供者再启动时,向注册中心注册自己提供的服务
-
服务消费者再启动时,向注册中心订阅自己所需的服务
-
注册中心返回服务提供者地址列表给消费者,如果有变更注册中心将基于长连接推送变更数据给消费者。
-
服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
-
服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
dubbo的配置中心
- 配置中心(2.7.0)在Dubbo中承担两个职责:
a. 外部化配置;启动配置的集中式存储(Apollo、Nacos等),Dubbo 所做的主要是保证能配合这些系统正常工作
- 可以简单理解为 dubbo.properties 的外部化存储,配置中心更适合将一些公共配置如注册中心、元数据中心配置等抽取以便做集中管理。
# 将注册中心地址、元数据中心地址等配置集中管理,可以做到统一环境、减少开发侧感知。
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.registry.simplified=true
dubbo.metadata-report.address=zookeeper://127.0.0.1:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
dubbo.application.qos.port=33333
# 优先级
# 作用域
全局和应用级别;全局是所有应用共享,应用级别各个应用自己维护且只对自身可见.(当前已支持的扩展实现有Zookeeper、Apollo)
- ZooKeeper
<dubbo:config-center address="zookeeper://127.0.0.1:2181"/>
或者
dubbo.config-center.address=zookeeper://127.0.0.1:2181
或者
ConfigCenterConfig configCenter = new ConfigCenterConfig();
configCenter.setAddress("zookeeper://127.0.0.1:2181");
默认所有配置都存储在 /dubbo/config节点
namespace,用于不同配置的环境隔离。
config,Dubbo约定的固定节点,不可更改,所有配置和服务治理规则都存储在此节点下。
dubbo/application,分别用来隔离全局配置、应用级别配置:dubbo是默认group值,application对应应用名
dubbo.properties,此节点的node value存储具体配置内容
- Apollo
<dubbo:config-center protocol="apollo" address="127.0.0.1:2181"/>
Apollo中的一个核心概念是命名空间 - namespace(和上面zookeeper的
默认情况下,Dubbo会从名叫dubbo(由于 Apollo 不支持特殊后缀 .properties )的命名空间中读取全局配置(<dubbo:config-center namespace="your namespace">)
<dubbo namespace="governance" group ="dubbo"/>
服务治理规则将存储在 governance namespace,而配置文件将存储在 dubbo namespace
-
自己加载外部化配置
Dubbo对配置中心得支持,本质上就是把 .properties 从远程拉取到本地,然后和本地的配置做一次融合。
Dubbo还提供了以下API,让用户将自己组织好的配置塞给 Dubbo 框架(配置加载的过程是用户要完成的),这样 Dubbo 框架就不再直接和 Apollo 或 Zookeeper 做读取配置交互
b. 服务治理。服务治理规则的存储与通知
-
ZooKeeper 默认节点结构
namespace:dubbo
固定节点:config
默认全局配置dubbo
configurators/tag-router/condition-router,不同的服务治理规则类型,node value 存储具体规则内容 -
Apollo
所有的服务治理规则都是全局性的,默认从公共命名空间 dubbo 读取和订阅:
dubbo和zookeeper整合实例
-
项目结构
-
pom依赖
- dubboDemo的pom
<?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>net.view.dubbo</groupId>
<artifactId>dubboDemo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>user-api</module>
<module>user-service</module>
<module>user-web</module>
</modules>
<properties>
<spring-boot.version>2.3.0.RELEASE</spring-boot.version>
<dubbo.version>2.7.8</dubbo.version>
<zookeeper.version>3.4.9</zookeeper.version>
<curator.version>2.12.0</curator.version>
</properties>
<repositories>
<repository>
<id>apache.snapshots.https</id>
<name>Apache Development Snapshot Repository</name>
<url>https://repository.apache.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>
- user-service的pom
<?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>dubboDemo</artifactId>
<groupId>net.view.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>net.view.dubbo.service</groupId>
<artifactId>user-service</artifactId>
<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>
<!-- Apache Dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>net.view.dubbo.api</groupId>
<artifactId>user-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- Dubbo Spring Boot Starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${curator.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>${curator.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>false</executable>
<finalName>${project.artifactId}-${spring-boot.version}</finalName>
<outputDirectory>${user.dir}/target</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
- user-web的pom
<?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>dubboDemo</artifactId>
<groupId>net.view.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>net.view.dubbo.web</groupId>
<artifactId>user-web</artifactId>
<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>
<!-- Apache Dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>net.view.dubbo.api</groupId>
<artifactId>user-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- Dubbo Spring Boot Starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${curator.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>${curator.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>false</executable>
<finalName>${project.artifactId}-${spring-boot.version}</finalName>
<outputDirectory>${user.dir}/target</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 配置文件
- user-service的配置文件
spring.application.name=user-service
dubbo.scan.base-packages=net.view.dubbo.service
dubbo.application.name = ${spring.application.name}
# Dubbo Protocol
dubbo.protocol.name=dubbo
dubbo.protocol.port=12345
## Dubbo Registry
dubbo.registry.address= zookeeper:\\192.168.249.209:12181
demo.service.version=1.0.0
- user-web的配置文件
spring:
application:
name: user-web
server:
port: 11111
dubbo:
registry:
address: zookeeper://127.0.0.1:12181
demo.service.version = 1.0.0
-
全代码
-
最终在provider和consumer都启动后,看zookeeper的节点的改变