Dubbo
分布式系统中的相关概念
大型互联网项目架构框架目标
传统项目和互联网项目
传统项目的用户群体是企业员工,互联网项目的用户群体是网民;互联网项目面向的群体相当庞大且用户对项目要求较高。
用户体验:美观,功能,速度,稳定性。
- 互联网项目的特点:
- 用户多
- 流量大
- 海量数据
- 易受攻击·
- 功能繁琐
- 变更快
- 目标:
- 高性能:提供快速的访问体验
衡量网站的性能指标- 响应时间
- 并发数
- 吞吐量
- 高可用:网站服务一直可以正常访问
- 可伸缩:通过硬件增加/减少,提高/降低处理能力
- 高可扩展:系统间的耦合第,方便地通过新增、移除方式,增加/减少新的功能/模块
- 安全性:提供网站安全访问和数据加密,安全存储等策略
- 敏捷性:随需应变,快速响应
- 高性能:提供快速的访问体验
集群和分布式
- 集群:很多“人”一起,干一样的事
- 分布式:很多“人”一起,干不一样的事,合起来是一件大事。
架构的演进
Dubbo概述
Dubbo概念
- Dubbo是阿里巴巴公司开源的一个高性能,轻量级的Java RPC框架(远程调用)
Dubbo架构
快速入门
Zookeeper安装(注册中心)
自行查找
Dubbo快速入门
- 创建服务提供者Provider模块
- 创建消费者Consumer模块
- 在服务提供者模块编写UserServiceImpl提供服务
- 在服务消费者中的UserController远程调用UserServiceImpl提供的服务
- 分别启动两个服务,测试
- dubbo-interface
package com.wilihelmi.service;
public interface UserService {
public String sayHello();
}
- dubbo-service
package com.wilihelmi.service.impl;
import com.wilihelmi.service.UserService;
import org.apache.dubbo.config.annotation.Service;
//@Service//将该类的对象创造出来并放到Spring的IOC容器中
@Service//将这个类提供的方法(服务),对外发布,将访问的地址(ip,端口,路径等)注册到注册中心
public class UserServiceImpl implements UserService {
public String sayHello() {
return "hello dubbo!";
}
}
application.xml
<?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:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo
http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 包扫描-->
<!-- <context:component-scan base-package="com.wilihelmi.service"/>-->
<!-- dubbo的配置-->
<!-- 1.配置项目的名称-->
<dubbo:application name="dubbo-service"/>
<!-- 2.配置注册中心的地址(zookeeper)-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!-- 3. dubbo包扫描-->
<dubbo:annotation package="com.wilihelmi.service.impl"/>
</beans>
web.xml
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
id="WebApp_ID" version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wilihelmi</groupId>
<artifactId>dubbo-service</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<properties>
<spring.version>5.1.9.RELEASE</spring.version>
<dubbo.version>2.7.1</dubbo.version>
<zookeeper.version>2.12.0</zookeeper.version>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--spring的坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!--spring mvc的坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
<!-- dubbo的起步依赖-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!-- zookeeper客户端实现-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${zookeeper.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>${zookeeper.version}</version>
</dependency>
<dependency>
<groupId>com.wilihelmi</groupId>
<artifactId>dubbo-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>9000</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
- dubbo-web
import com.wilihelmi.service.UserService;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
// @Autowired//本地注入
/**
* 1. 从zookeeper注册中心货物userService的访问url
* 2. 进行远程调用RPC
* 3. 将结果封装为一个代理对象。给变量赋值
*/
@Reference//远程注入
private UserService userService;
@RequestMapping("/sayHello")
public String sayHello(){
return userService.sayHello();
}
}
springmvc.xml
<?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:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns:context="http://www.springframework.org/schema/context"
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://dubbo.apache.org/schema/dubbo
http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.wilihelmi.controller"/>
<!-- dubbo的配置-->
<!-- 1.配置项目的名称-->
<dubbo:application name="dubbo-web">
<dubbo:parameter key="qos.port" value="33333"/>
</dubbo:application>
<!-- 2.配置注册中心的地址(zookeeper)-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!-- 3. dubbo包扫描-->
<dubbo:annotation package="com.wilihelmi.controller"/>
</beans>
web.xml
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
id="WebApp_ID" version="4.0">
<!-- SpringMVC-->
<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:spring/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wilihelmi</groupId>
<artifactId>dubbo-web</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<properties>
<spring.version>5.1.9.RELEASE</spring.version>
<dubbo.version>2.7.1</dubbo.version>
<zookeeper.version>2.12.0</zookeeper.version>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--spring的坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!--spring mvc的坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
<!-- dubbo的起步依赖-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!-- zookeeper客户端实现-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${zookeeper.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>${zookeeper.version}</version>
</dependency>
<dependency>
<groupId>com.wilihelmi</groupId>
<artifactId>dubbo-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>8000</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
高级特性
dubbo-admin管理平台
- 安装dubbo-admin
- 分别运行其前后端(前端是vue项目,通过node.js运行;后端是SpringBoot项目,通过java运行)
默认账号密码均为root
dubbo常用高级配置
序列化
地址缓存
- 注册中心挂了,服务是否可以正常访问呢?
- 可以,因为Dubbo服务消费者在第一次调用时,会将服务提供方地址缓存到本地,以后再调用则不会访问注册中心
- 当服务提供者地址发生变化时,注册中心会通知服务消费者
超时与重试
问题:服务消费者在调用服务提供者的时候发生了阻塞,等待等情形,这个时候服务消费者会一直等待下去;在某个峰值时刻,大量的请求都在同时请求服务消费者,会造成大量线程堆积,势必会造成雪崩
- dubbo利用超时机制,设置一个超时时间,在这个时间段内,无法完成服务访问,则自动断开连接。
- 使用timeout属性设置超时时间,默认值1000,单位毫秒
配置超时,可以选择在提供方的·@Service注解配置,或者在调用方的远程调用注解@Reference注解配置
- 建议配置在提供方
问题:设置了超时时间,在这个时间段内,无法完成服务访问,则自动断开连接;如果出现了网络抖动,则这一次的请求就会失败。
- Dubbo提供重试机制来避免类似的问题发生
- 通过retries属性来设置重试次数,默认次数为两次
多版本
- 灰度发布:当出现新功能时,会让一部分用户先使用新功能,用户反馈没问题时,再将所有用户迁移到新功能
- 在生产模块有多个版本,可通过@Service(version=" 版本信息")区分相同接口的不同实现;在消费模块注入时@Reference(version=“版本信息”)确定注入哪个版本的实体
- dubbo使用version属性来设置和调用同一个接口的不同版本
负载均衡
- 四种策略:
- Radom:按权重随机,默认值。按权重设置随机概率。
- RoundRobin:按权重轮训
- LeastActive:最少活跃调用数,相同活跃数的随机调用
- ConsistenHash:一致性Hash,相同参数的请求总是发送到同一提供者
集群容错
- 集群容错模式:
- Failover Cluster:失败重试,默认值。当出现失败,重试其他服务器,默认重试2次,使用retries配置,一般用于读操作。
。。。