软件架构的演进过程 软件架构的发展经历了由单体架构、垂直架构、SOA架构到微服务架构的演进过程,下面我们分别了解 一下这几个架构。
单体架构 架构说明: 全部功能集中在一个项目内(All in one)。
架构优点: 架构简单,前期开发成本低、开发周期短,适合小型项目。
架构缺点: 全部功能集成在一个工程中,对于大型项目不易开发、扩展和维护。 技术栈受限,只能使用一种语言开发。 系统性能扩展只能通过扩展集群节点,成本高。
垂直架构 架构说明: 按照业务进行切割,形成小的单体项目。
架构优点: 技术栈可扩展(不同的系统可以用不同的编程语言编写)。
架构缺点: 功能集中在一个项目中,不利于开发、扩展、维护。 系统扩张只能通过集群的方式。 项目之间功能冗余、数据冗余、耦合性强。
SOA 架构 SOA 全称为 Service-Oriented Architecture,即面向服务的架构。它可以根据需求通过网络对松散耦合的粗粒度应用组件 (服务) 进行分布式部署、组合和使用。一个服务通常以独立的形式存在于操作系统进程中。 站在功能的角度,把业务逻辑抽象成可复用的服务,通过服务的编排实现业务的快速再生,目的:把原先固有的业务功能转变为通用的业务服务,实现业务逻辑的快速复用。
架构说明:将重复功能或模块抽取成组件的形式,对外提供服务,在项目与服务之间使用 ESB(企业服务总线)的形式作为通信的桥梁。
架构优点:重复功能或模块抽取为服务,提高开发效率。可重用性高。可维护性高。
架构缺点:各系统之间业务不同,很难确认功能或模块是重复的。抽取服务的粒度大。系统和服务之间耦合度高。
微服务架构 架构说明:将系统服务层完全独立出来,抽取为一个一个的微服务。抽取的粒度更细,遵循单一原则。 采用轻量级框架协议传输。
架构优点:服务拆分粒度更细,有利于提高开发效率。可以针对不同服务制定对应的优化方案。 适用于互联网时代,产品迭代周期更短。
架构缺点:粒度太细导致服务太多,维护成本高。分布式系统开发的技术成本高,对团队的挑战大。
Apache Dubbo 概述 Dubbo 简介 Apache Dubbo 是一款高性能的 Java RPC 框架。其前身是阿里巴巴公司开源的、轻量级的开源 Java RPC 框架,可以和 Spring 框架无缝集成,2018 年阿里巴巴把这个框架捐献给了 apache 基金会。
Dubbo 官网地址:https://cn.dubbo.apache.org/zh-cn/index.html Dubbo 提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。
什么是 RPC? RPC 全称为remote procedure call
,即远程过程调用 。比如两台服务器 A 和 B,A 服务器上部署一个应 用,B 服务器上部署一个应用,A 服务器上的应用想调用 B 服务器上的应用提供的方法,由于两个应用不在一个内存空间,不能直接调用,所以需要通过网络来表达调用的语义和传达调用的数据。
需要注意的是 RPC 并不是一个具体的技术,而是指整个网络远程调用过程。
RPC 是一个泛化的概念,严格来说一切远程过程调用手段都属于 RPC 范畴。各种开发语言都有自己的 RPC 框架。Java 中的 RPC 框架比较多,广泛使用的有 RMI、Hessian、Dubbo 等。
Dubbo 架构
Provider 暴露服务的服务提供方
Consumer 调用远程服务的服务消费方
Registry 服务注册与发现的注册中心
Monitor 统计服务的调用次数和调用时间的监控中心
Container 服务运行容器
![](Apache Dubbo/ada49bd310b8562e5c37c66ba608150e.png)
调用关系说明:
服务容器负责启动,加载,运行服务提供者。
服务提供者在启动时,向注册中心注册自己提供的服务。
服务消费者在启动时,向注册中心订阅自己所需的服务。
注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
服务注册中心 通过前面的 Dubbo 架构图可以看到,Registry(服务注册中心)在其中起着至关重要的作用。Dubbo 官方推荐使用 Zookeeper 作为服务注册中心。
Zookeeper 是 Apache Hadoop 的子项目,是一个树型的目录服务,支持变更推送,适合作为 Dubbo 服务的注册中心,工业强度较高,可用于生产环境,并推荐使用。
流程说明:
服务提供者 (Provider) 启动时:向 /dubbo/com.foo.BarService/providers 目录下写入自己的 URL 地址
服务消费者 (Consumer) 启动时:订阅 /dubbo/com.foo.BarService/providers 目录下的提供者 URL 地址。并向 /dubbo/com.foo.BarService/consumers 目录下写入自己的 URL 地址
监控中心 (Monitor) 启动时:订阅 /dubbo/com.foo.BarService 目录下的所有提供者和消费者 URL 地址
Dubbo 快速入门 Dubbo 作为一个 RPC 框架,其最核心的功能就是要实现跨网络的远程调用。这里是要创建两个应用,一个作为服务的提供方,一个作为服务的消费方。通过 Dubbo 来实现服务消费方远程调用服务提供方的方法。
服务提供方开发 创建 maven 工程(打包方式为 war)dubbodemo_provider,在 pom.xml 文件中导入如下坐标
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 <modelVersion > 4.0.0</modelVersion > <packaging > war</packaging > <properties > <project.build.sourceEncoding > UTF-8</project.build.sourceEncoding > <maven.compiler.source > 1.8</maven.compiler.source > <maven.compiler.target > 1.8</maven.compiler.target > <spring.version > 5.0.5.RELEASE</spring.version > </properties > <dependencies > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-context</artifactId > <version > ${spring.version}</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-beans</artifactId > <version > ${spring.version}</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-webmvc</artifactId > <version > ${spring.version}</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-jdbc</artifactId > <version > ${spring.version}</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-aspects</artifactId > <version > ${spring.version}</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-jms</artifactId > <version > ${spring.version}</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-context-support</artifactId > <version > ${spring.version}</version > </dependency > <dependency > <groupId > com.alibaba</groupId > <artifactId > dubbo</artifactId > <version > 2.6.0</version > </dependency > <dependency > <groupId > org.apache.zookeeper</groupId > <artifactId > zookeeper</artifactId > <version > 3.4.7</version > </dependency > <dependency > <groupId > com.github.sgroschupf</groupId > <artifactId > zkclient</artifactId > <version > 0.1</version > </dependency > <dependency > <groupId > javassist</groupId > <artifactId > javassist</artifactId > <version > 3.12.1.GA</version > </dependency > <dependency > <groupId > com.alibaba</groupId > <artifactId > fastjson</artifactId > <version > 1.2.47</version > </dependency > </dependencies > <build > <plugins > <plugin > <groupId > org.apache.maven.plugins</groupId > <artifactId > maven-compiler-plugin</artifactId > <version > 2.3.2</version > <configuration > <source > 1.8</source > <target > 1.8</target > </configuration > </plugin > <plugin > <groupId > org.apache.tomcat.maven</groupId > <artifactId > tomcat7-maven-plugin</artifactId > <version > 2.2</version > <configuration > <port > 8081</port > <path > /</path > </configuration > </plugin > </plugins > </build >
配置 web.xml 文件: 右键项目add framework support(添加框架支持)
,选择WEB Application
, 文件目录IdeaProjects\dubbodemo_provider\web\WEB-INF\web.xml
1 2 3 4 5 6 7 8 <context-param > <param-name > contextConfigLocation</param-name > <param-value > classpath*:applicationContext*.xml</param-value > </context-param > <listener > <listener-class > org.springframework.web.context.ContextLoaderListener</listener-class > </listener >
创建服务接口
1 2 3 public interface HelloService { public String sayHello (String name) ; }
创建服务实现类, 类上使用的 Service 注解是 Dubbo 提供的,用于对外发布服务
1 2 3 4 5 6 7 8 import com.alibaba.dubbo.config.annotation.Service;@Service public class HelloServiceImpl implements HelloService { public String sayHello (String name) { return "hello " + name; } }
在 src/main/resources 下创建 applicationContext-service.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns ="http://www.springframework.org/schema/beans" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:p ="http://www.springframework.org/schema/p" xmlns:context ="http://www.springframework.org/schema/context" xmlns:dubbo ="http://code.alibabatech.com/schema/dubbo" xmlns:mvc ="http://www.springframework.org/schema/mvc" xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" > <dubbo:application name ="dubbodemo_provider" /> <dubbo:registry address ="zookeeper://127.0.0.1:2181" /> <dubbo:protocol name ="dubbo" port ="20881" > </dubbo:protocol > <dubbo:annotation package ="pers.fulsun.service.impl" /> </beans >
插件选择 tomcat7:run
启动服务, 在 Zookeeper 查看
1 2 127.0.0.1:2181 $ ls /dubbo/pers.fulsun.service.HelloService/providers [dubbo%3A%2F%2F192.168.1.5%3A20881%2Fpers.fulsun.service.HelloService%3Fanyhost%3Dtrue%26application%3Ddubbodemo_provider%26dubbo%3D2.6.0%26generic%3Dfalse%26interface%3Dpers.fulsun.service.HelloService%26methods%3DsayHello%26pid%3D1768%26side%3Dprovider%26timestamp%3D1686155530214]
![](Apache Dubbo/205a6dcdb2e6b662e80fe05fafb74e1e.png)
服务消费方开发 创建 maven 工程(打包方式为 war)dubbodemo_consumer
,pom.xml 配置和上面服务提供者相同,只需要将 Tomcat 插件的端口号改为 8082 即可。
配置 web.xml 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 <?xml version="1.0" encoding="UTF-8" ?> <web-app xmlns ="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version ="4.0" > <context-param > <param-name > contextConfigLocation</param-name > <param-value > classpath*:applicationContext*.xml</param-value > </context-param > <listener > <listener-class > org.springframework.web.context.ContextLoaderListener</listener-class > </listener > <servlet > <servlet-name > springmvc</servlet-name > <servlet-class > org.springframework.web.servlet.DispatcherServlet</servlet-class > <init-param > <param-name > contextConfigLocation</param-name > <param-value > classpath:applicationContext-web.xml</param-value > </init-param > </servlet > <servlet-mapping > <servlet-name > springmvc</servlet-name > <url-pattern > /</url-pattern > </servlet-mapping > </web-app >
将服务提供者工程中的 HelloService 接口复制到当前工程, 编写 Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import com.alibaba.dubbo.config.annotation.Reference;@RestController @RequestMapping("/demo") public class HelloController { @Reference private HelloService helloService; @RequestMapping("/hello") public String sayHello (String name) { String result = helloService.sayHello(name); System.out.println(result); return result; } }
在 src/main/resources 下创建 applicationContext-web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns ="http://www.springframework.org/schema/beans" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:p ="http://www.springframework.org/schema/p" xmlns:context ="http://www.springframework.org/schema/context" xmlns:dubbo ="http://code.alibabatech.com/schema/dubbo" xmlns:mvc ="http://www.springframework.org/schema/mvc" xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" > <dubbo:application name ="dubbodemo_consumer" /> <dubbo:registry address ="zookeeper://10.0.0.102:2181" /> <dubbo:annotation package ="com.lxs.controller" /> </beans >
测试运行 运行测试 tomcat7:run 启动,先启动 provider 再启动 consumer 在浏览器输入 http://localhost:8082/demo/hello?name=huh , 查看浏览器输出结果
![](Apache Dubbo/c975ced72e28dd9fbb483b7a004e8ca5.png)
在 zookeeper 中查看
![](Apache Dubbo/7cc6029aeff9b3a4e1b615e8bf6989de.png)
Dubbo 相关配置说明 包扫描 服务提供者和服务消费者都需要配置,表示包扫描,作用是扫描指定包 (包括子包) 下的类。
1 <dubbo:annotation package ="com.lxs.service.impl" />
如果不使用包扫描,也可以通过如下配置的方式来发布服务:
服务提供者和服务消费者都需要配置,表示包扫描,作用是扫描指定包 (包括子包) 下的类。 如果不使用包扫描,也可以通过如下配置的方式来发布服务:
1 2 <bean id ="helloService" class ="pers.fulsun.service.impl.HelloServiceImpl" /> <dubbo:service interface ="pers.fulsun.api.HelloService" ref ="helloService" />
作为服务消费者,可以通过如下配置来引用服务:
1 2 <dubbo:reference id ="helloService" interface =pers.fulsun.api.HelloService " />
上面这种方式发布和引用服务,一个配置项 (dubbo:service
、dubbo:reference
) 只能发布或者引用一个 服务,如果有多个服务,这种方式就比较繁琐了。推荐使用包扫描方式。
协议 一般在服务提供者一方配置,可以指定使用的协议名称和端口号。 其中 Dubbo 支持的协议有:dubbo、rmi、hessian、http、webservice、rest、redis 等。推荐使用的是 dubbo 协议。
1 <dubbo:protocol name ="dubbo" port ="20881" > </dubbo:protocol >
dubbo 协议采用单一长连接和 NIO 异步通讯,适合于小数据量大并发的服务调用,以及服务消费者机器数远大于服务提供者机器数的情况。不适合传送大数据量的服务,比如传文件,传视频等,除非请求量很低。也可以在同一个工程中配置多个协议,不同服务可以使用不同的协议,例如:
1 2 3 4 5 6 7 8 9 <dubbo:protocol name ="dubbo" port ="20880" /> <dubbo:protocol name ="rmi" port ="1099" /> <dubbo:service interface ="pers.fulsun.api.HelloService" ref ="helloService" protocol ="dubbo" /><dubbo:service interface ="pers.fulsun.api.DemoService" ref ="demoService" protocol ="rmi" />
负载均衡 负载均衡(Load Balance):其实就是将请求分摊到多个操作单元上进行执行,从而共同完成工作任务。
在集群负载均衡时,Dubbo 提供了多种均衡策略(包括随机、轮询、最少活跃调用数、一致性 Hash),缺省为 random 随机调用。
算法
特性
备注
配置值
Weighted Random LoadBalance
加权随机
默认算法,默认权重相同
random (默认)
RoundRobin LoadBalance
加权轮询
借鉴于 Nginx 的平滑加权轮询算法,默认权重相同,
roundrobin
LeastActive LoadBalance
最少活跃优先 + 加权随机
背后是能者多劳的思想
leastactive
Shortest-Response LoadBalance
最短响应优先 + 加权随机
更加关注响应速度
shortestresponse
ConsistentHash LoadBalance
一致性哈希
确定的入参,确定的提供者,适用于有状态请求
consistenthash
P2C LoadBalance
Power of Two Choice
随机选择两个节点后,继续选择 “连接数” 较小的那个节点。
p2c
Adaptive LoadBalance
自适应负载均衡
在 P2C 算法基础上,选择二者中 load 最小的那个节点
adaptive
配置负载均衡策略,既可以在服务提供者一方配置,也可以在服务消费者一方配置,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import com.alibaba.dubbo.config.annotation.Reference;@RestController @RequestMapping("/demo") public class HelloController { @Reference(check = false, loadbalance = "roundrobin") private HelloService helloService; @RequestMapping("/hello") public String sayHello (String name) { String result = helloService.sayHello(name); System.out.println(result); return result; } }
启动多个服务提供者来观察 Dubbo 负载均衡效果, 在一台机器上启动多个服务提供者,所以需要修改 tomcat 的端口号和 Dubbo 服务的 端口号来防止端口冲突。
服务端负载均衡
1 2 3 4 5 6 @Service(loadbalance = "random") public class HelloServiceImpl implements HelloService { public String sayHello (String name) { return "hello " + name; } }