传统项目:学校的选课系统,图书借阅系统,OA自动化办公系统,HR人力资源管理系统等
互联网项目对用户体验要求更高,从以下几个方面来衡量:
两大区别:
从微信,支付宝来看:
用户多
流量大,并发高
海量数据
易受攻击
功能繁琐
需求变更快
六大目标:
高性能:提供快速的访问体验。
衡量网站的几个性能指标:
**响应时间:**指执行一个请求从开始到最后收到响应数据所花费的总体时间。
**并发数:**指系统同时能处理的请求数量。
并发连接数: 指的是客户端向服务器发起请求,并建立了TCP连接。每秒钟服务器连接的总TCP数量
**请求数:**也称为QPS(Query Per Second)指每秒多少请求。浏览器F12查看请求数
请求数>=并发连接数
**并发用户数:**单位时间内有多少用户
**吞吐量:**指单位时间内系统能处理的请求数量。
高可用:网站服务一直可以正常访问。
可伸缩:通过硬件增加/减少,提高/降低处理能力。
高可扩展:系统间耦合低,方便的通过新增/移除方式,增加/减少新的功能/模块。
安全性:提供网站安全访问和数据加密,安全存储等策略。
敏捷性:随需应变,快速响应。
集群:很多“人”一起 ,干一样的事。
软件领域:一个软件,部署在多台服务器上。
软件领域:一个大的业务系统,拆分为小的业务模块,分别部署在不同的机器上
1. 原始项目
2. 集群项目
负载均衡是高可用网络基础架构的关键组件,通常用于将工作负载分布到多个服务器来提高网站、应用、数据库或其他服务的性能和可靠性。
访问淘宝时,根据用户所在地区跳转到不同的机房,提高响应速度,降低单个地区服务器的压力。
3. 集群分布式项目
淘宝购物:浏览商品(商品模块)是高频操作,相对而言下订单(订单模块)是低频操作
架构说明:全部功能集中在一个项目内(All in one)。
优点: 简单,开发部署都很方便,小型项目首选
缺点:
垂直架构是指将单体架构中的多个模块拆分为多个独立的项目,形成多个独立的单体架构。
架构说明:按照业务进行切割,形成小的单体项目。
架构优点:解决了单体架构的缺点,并且技术栈可扩展(不同的系统可以用不同的编程语言编写)。
架构缺点:项目之间功能冗余、数据冗余、耦合性强。
分布式架构是指在垂直架构的基础上,将公共业务模块抽取出来,作为独立的服务供其他调用者消费,以实现服务的共享和重用。
什么是RPC?
RPC全称为remote procedure call,即远程过程调用。比如两台服务器A和B,A服务器上部署一个应用,B服务器上部署一个应用,A服务器上的应用想调用B服务器上的应用提供的方法,由于两个应用不在同一台服务器,所以需要使用网络进行访问。
需要注意的是RPC并不是一个具体的技术,而是指整个网络远程调用过程。
分布式架构存在的问题: 服务提供方的IP或者端口一旦产生变更,所有调用他的模块都需要变更。
SOA: (Service- Oriented Architecture,面向服务的架构):是一个组件模型,它将应用程序的不同功能单元(称为服务)进行拆分,并通过这些服务之间定义良好的接口和契约联系起来。举例:课表
ESB: (Enterparise Servce Bus):企业服务总线,服务中介。主要是提供了一个服务于服务之间的交互。ESB包含的功能如:负载均衡,流量控制,加密处理,服务的监控,异常处理,监控告急等等。
架构说明:将重复功能或模块抽取成组件的形式,对外提供服务,在项目与服务之间使用ESB(企业服务总线)的形式作为通信的桥梁。
架构说明:
特点:
RPC全称为remote procedure call,即远程过程调用。比如两台服务器A和B,A服务器上部署一个应用,B服务器上部署一个应用,A服务器上的应用想调用B服务器上的应用提供的方法,由于两个应用不在同一台服务器,所以需要使用网络进行访问。
需要注意的是RPC并不是一个具体的技术,而是指整个网络远程调用过程。
●Dubbo是阿里巴巴公司开源的一个高性能、轻量级的Java RPC框架。
●致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。
●官网: http://dubbo.apache.org/zh-cn/
节点角色说明:
●Container: 服务运行容器;学校
●Provider: 暴露服务的服务提供方;自习室
●Consumer: 调用远程服务的服务消费方;学员
●Registry: 服务注册与发现的注册中心;电子黑板
●Monitor:统计服务的调用次数和调用时间的监控中心;统计每天自习室人数
Dubbo不能单独使用,需要配套的服务注册与发现的注册中心,常用的注册中心是Zookeeper,Nacos(阿里)
Dubbo作为一个RPC框架,其最核心的功能就是要实现跨网络的远程调用。
本小节就是要创建两个应用,一个作为服务的提供方,一个作为服务的消费方。
其中表现层(Controller)需要调用业务层(Service)完成功能:
服务提供者:Service被Controller调用,因此被调用者Service称为服务提供者
服务消费者:Controller发起调用(userService.save()),因此调用者Controller称为服务消费者
创建maven工程dubbo-service,在pom.xml文件中导入如下坐标
<properties>
<spring.version>5.1.9.RELEASEspring.version>
<dubbo.version>2.7.4.1dubbo.version>
<zookeeper.version>4.0.0zookeeper.version>
properties>
<dependencies>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
dependencies>
新建UserService和UserServiceImpl
public interface UserService {
String sayHello();
}
@Service//将该类的对象创建出来,放到Spring的IOC容器中 bean定义
public class UserServiceImpl implements UserService {
public String sayHello() {
return "hello dubbo hello!~";
}
}
3. 新建Spring配置文件:resources\spring\applicationContext.xml
```xml
新建dubbo-web模块,打包方式为war
<packaging>warpackaging>
<packaging>warpackaging>
<properties>
<spring.version>5.1.9.RELEASEspring.version>
<dubbo.version>2.7.4.1dubbo.version>
<zookeeper.version>4.0.0zookeeper.version>
properties>
<dependencies>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.1version>
<configuration>
<port>8000port>
<path>/path>
configuration>
plugin>
plugins>
build>
配置SpringMVC:resources\spring\springmvc.xml
<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:mvc="http://www.springframework.org/schema/mvc"
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://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.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">
<mvc:annotation-driven/>
<context:component-scan base-package="com.ithe.controller"/>
beans>
配置web.xml:webapp\WEB-INF\web.xml
注意:配置监听器保证Tomcat启动时加载Spring配置文件:classpath*
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath*:spring/applicationContext*.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring/springmvc.xmlparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>*.dourl-pattern>
servlet-mapping>
web-app>
注意:DispatcherServlet的第二种拦截配置方式:*.do
表示只拦截URL以.do结尾的,比如http://localhost:8000/user/sayHello.do
表现层需要调用业务层,因此添加dubbo-service的依赖
<dependency>
<groupId>com.ithegroupId>
<artifactId>dubbo-serviceartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
编写UserController
@RestController
@RequestMapping("/user")
public class UserController {
//注入Service
@Autowired//本地注入
private UserService userService;
@RequestMapping("/sayHello")
public String sayHello(){
return userService.sayHello();
}
}
安装dubbo-service到本地仓库
启动dubbo-web:使用tomcat7-maven插件
访问http://localhost:8000/user/sayHello.do
在dubbo-service工程中完成如下改动:
添加Dubbo、注册中心Zookeeper和log4j的坐标
在Maven中自定义属性:
<properties>
<spring.version>5.1.9.RELEASEspring.version>
<dubbo.version>2.7.4.1dubbo.version>
<zookeeper.version>4.0.0zookeeper.version>
properties>
添加依赖
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
<version>${zookeeper.version}version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
<version>${zookeeper.version}version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.21version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.7.21version>
dependency>
新建日志配置文件:resources\log4j.properties
# DEBUG < INFO < WARN < ERROR < FATAL
# Global logging configuration
log4j.rootLogger=info, stdout,file
# My logging configuration...
#log4j.logger.com.tocersoft.school=DEBUG
#log4j.logger.net.sf.hibernate.cache=debug
## Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=../logs/iask.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %l %m%n
修改UserServiceImpl中的注解
注意:服务实现类上使用的Service注解是Dubbo提供的,用于对外发布服务
import org.apache.dubbo.config.annotation.Service;
@Service//将这个类提供的方法(服务)对外发布。将访问的地址 ip,端口,路径注册到注册中心中
public class UserServiceImpl implements UserService {
public String sayHello() {
return "hello dubbo hello!~";
}
}
在dubbo-service工程中配置dubbo信息:src/main/resources/applicationContext.xml
确保引入dubbo命名空间
<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">
beans>
配置应用名称,注册中心zk地址,dubbo包扫描
<dubbo:application name="dubbo-service"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<dubbo:annotation package="com.ithe.service.impl" />
将模块改为web工程 -> 修改dubbo-service的pom.xml:打包为war,添加tomcat7插件
注意:改造之后UserController通过网络访问UserService,因此需要保证dubbo-service项目一直启动
<packaging>warpackaging>
配置tomcat7-maven插件
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.1version>
<configuration>
<port>8002port>
<path>/path>
configuration>
plugin>
plugins>
build>
在src\main目录下添加webapp\WEB-INF\web.xml,保留Spring提供的监听器
作用:负责Tomcat启动时加载dubbo相关的配置
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath*:spring/applicationContext*.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
web-app>
启动测试:tomcat7:run,启动后自动注册UserService服务到Zookeeper中
在dubbo-web工程中完成如下改动:
将UserService接口提取到dubbo-interface模块中
注意:dubbo-service和dubbo-web中都需要使用UserService类,因此单独将UserService接口拆分到dubbo-interface模块
dubbo-service和dubbo-web的pom.xml中引入dubbo-interface,并对dubbo-interface执行install
<dependency>
<groupId>com.ithegroupId>
<artifactId>dubbo-interfaceartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
添加Dubbo、Zookeeper和日志的引用和日志配置文件log4j.properties
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
<version>${zookeeper.version}version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
<version>${zookeeper.version}version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.21version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.7.21version>
dependency>
在dubbo-web工程中添加Dubbo配置:src/main/resources/spring/springmvc.xml
<dubbo:application name="dubbo-web" >
<dubbo:parameter key="qos.port" value="33333"/>
dubbo:application>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<dubbo:annotation package="com.ithe.controller" />
修改UserController
注意:Controller中注入HelloService使用的是Dubbo提供的@Reference注解
import org.apache.dubbo.config.annotation.Reference;
@RestController
@RequestMapping("/user")
public class UserController {
/*
1. 从zookeeper注册中心获取userService的访问url
2. 进行远程调用RPC
3. 将结果封装为一个代理对象。给变量赋值
*/
@Reference//远程注入
private UserService userService;
@RequestMapping("/sayHello")
public String sayHello(){
return userService.sayHello();
}
}
启动dubbo-service和dubbo-web测试:tomcat7:run http://localhost:8000/user/sayHello.do
最终执行过程如下
●dubbo-admin管理平台,是图形化的服务管理页面
●从注册中心中获取到所有的提供者 /消费者进行配置管理
●路由规则、动态配置、服务降级、访问控制、权重调整、负载均衡等管理功能
●dubbo- admin是一个前后端分离的项目。前端使用vue,后端使用springboot
●安装dubbo-admin其实就是部署该项目
具体安装参见:dubbo-admin.md
具体使用参见:dubbo-admin.md
如果要在Dubbo Admin中进行测试,需要dubbo-service中添加元数据配置
<dubbo:metadata-report address="zookeeper://127.0.0.1:2181" />
dubbo 内部已经封装序列化和反序列化的过程
我们只需要在定义pojo类时实现Serializable接口即可
一般会定义一 个公共的pojo模块,让生产者和消费者都依赖该模块。
新建dubbo-pojo模块,添加User实体类
/**
* 注意!!!
* 将来所有的pojo类都需要实现Serializable接口
*/
public class User implements Serializable {
private int id;
private String username;
private String password;
public User() {
}
public User(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
在dubbo-interface模块的UserService接口中添加findUserById方法,并添加dubbo-pojo依赖
<dependency>
<groupId>com.ithe</groupId>
<artifactId>dubbo-pojo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
public interface UserService {
public String sayHello();
/**
* 查询用户
*/
public User findUserById(int id);
}
对dubbo-pojo, dubbo-interface执行instal,将最新jar包安装到本地Maven仓库
在dubbo-service模块的UserServiceImpl中添加findUserById方法实现
public User findUserById(int id) {
//查询User对象
User user = new User(1,"zhangsan","123"); //模拟从数据库查询用户信息
return user;
}
在dubbo-web模块的UserController中远程调用findUserById方法
/**
* 根据id查询用户信息
* @param id
* @return
*/
//http://localhost:8000/user/find.do?id=3
@RequestMapping("/find")
public User find(int id){
return userService.findUserById(id);
}
启动dubbo-service和dubbo-web
易错点:User对象未实现serializable接口
解决办法:实现Serializable接口
public User implements Serializable {
private String name;
private int age;
}
注册中心挂了,服务是否可以正常访问?
服务消费者在调用服务提供者的时候发生了阻塞、等待的情形,这个时候,服务消费者会直等待下去。
在某个峰值时刻,大量的请求都在同时请求服务消费者,会造成线程的大量堆积,势必会造成雪崩。
dubbo利用超时机制来解决这个问题:设置一个超时时间, 在这个时间段内,无法完成服务则自动断开连接。
使用timeout属性配置超时时间,默认值1000,单位毫秒
//timeout 超时时间 单位毫秒 retries 重试次数
@Service(timeout = 3000,retries=0)
注意:超时时间建议配置在服务提供方
演示效果:
模拟查询用户超时:UserServiceImpl
@Service(timeout = 3000, retries = 0)//当前服务3秒超时
public class UserServiceImpl implements UserService {
public String sayHello() {
return "hello dubbo hello!~";
}
public User findUserById(int id) {
//查询User对象
User user = new User(1, "zhangsan", "123");
try {
//数据库查询很慢,查了5秒
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return user;
}
}
查看是否3秒超时抛出异常:UserController
@RestController
@RequestMapping("/user")
public class UserController {
@Reference //远程注入
private UserService userService;
@RequestMapping("/sayHello")
public String sayHello() {
return userService.sayHello();
}
/**
* 根据id查询用户信息
*
* @param id
* @return
*/
int i = 1;
@RequestMapping("/find")
public User find(int id) {
new Thread(new Runnable() {
public void run() {
while (true) {
System.out.println(i++);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
return userService.findUserById(id);
}
}
访问http://localhost:8000/user/find.do?id=1
可以看出请求发出三秒左右后才提示调用超时
设置了超时时间,在这个时间段内,无法完成服务访问,则自动断开连接。
如果出现网络抖动,则这一次请求就会失败。
Dubbo提供重试机制来避免类似问题的发生。
通过retries属性来设置重试次数。默认为2次
//timeout:超时时间(单位毫秒), retries:重试次数
@Service(timeout = 3000, retries=3)
**灰度发布:**当出现新功能时,会让一部分用户先使用新功能,用户反馈没问题时,再将所有用户迁移到新功能。
dubbo中使用version属性来设置和调用同一个接口的不同版本
服务提供者dubbo-service配置:
@Service(version="v2.0")
public class UserServiceImp12 implements UserService {...}
消费者dubbo-web配置:
@Reference(version = "v2.0")//远程注入
private UserService userService;
负载均衡是高可用网络基础架构的关键组件,通常用于将工作负载分布到多个服务器来提高网站、应用、数据库或其他服务的性能和可靠性。比如访问淘宝时,根据用户所在地区跳转到不同的机房,提高响应速度,降低单个地区服务器的压力。
Dubbo提供4种负载均衡策略 ,默认策略为Random
**Random:**按权重随机,默认值。按权重设置随机概率。
RoundRobin: 按权重轮询。
LeastActive: 最少活跃调用数,相同活跃数的随机。
**ConsistentHash:**一 致性Hash,相同参数的请求总是发到同一提供者。
启动3个服务提供者dubbo-service:
第一个服务提供者配置
sayHello()返回1
@Service
public class UserServiceImp1 implements UserService {
public String sayHello() {
return "1......";
}
}
tomcat端口:9000
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.1version>
<configuration>
<port>9000port>
<path>/path>
configuration>
plugin>
plugins>
build>
dubbo端口
<dubbo:protocol port="20880" />
<dubbo:application name="dubbo-service">
<dubbo:parameter key="qos.port" value="22222"/>
dubbo:application>
启动第一个dubbo-service
第二个服务提供者配置
sayHello()返回2
@Service
public class UserServiceImp1 implements UserService {
public String sayHello() {
return "2......";
}
}
tomcat端口:9001
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.1version>
<configuration>
<port>9001port>
<path>/path>
configuration>
plugin>
plugins>
build>
dubbo端口
<dubbo:protocol port="20881" />
<dubbo:application name="dubbo-service">
<dubbo:parameter key="qos.port" value="33333"/>
dubbo:application>
启动第二个dubbo-service
第三个服务提供者配置
sayHello()返回3
@Service
public class UserServiceImp1 implements UserService {
public String sayHello() {
return "3......";
}
}
tomcat端口:9002
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.1version>
<configuration>
<port>9002port>
<path>/path>
configuration>
plugin>
plugins>
build>
dubbo端口
<dubbo:protocol port="20882" />
<dubbo:application name="dubbo-service">
<dubbo:parameter key="qos.port" value="55555"/>
dubbo:application>
启动第三个dubbo-service
服务消费者dubbo-web设置负载均衡策略
//@Reference(loadbalance = "consistenthash")
//@Reference(loadbalance = "leastactive")
//@Reference(loadbalance = "roundrobin")
//@Reference(loadbalance = "random")
@Reference(loadbalance = RandomLoadBalance.NAME)//默认负载均衡策略
启动dubbo-web:http://localhost:8000/user/sayHello.do
Dubbo提供6种集群容错模式:
**Failover Cluster: **失败重试,默认容错模式。当出现失败时,重试其它服务器,默认重试2次,使用retries配置。
**Failfast Cluster *快速失败,发起一次调用,失败立即报错。通常用于写操作。
**Failsafe Cluster:**失败安全,出现异常时,直接忽略。返回一个空结果。
**Failback Cluster:**失败自动恢复,后台记录失败请求,定时重发。
**Forking Cluster *并行调用多个服务器,只要一个成功即返回。
Broadcast Cluster: 广播调用所有提供者,逐个调用,任意一台报错则报错。
消费者dubbo-web中配置
public class UserController {
@Reference(cluster = "failover")//远程注入
private UserService userService;
}
集群容错可选值:
failover, failfast, failsafe, failback, forking
服务降级:当服务器压力剧增的情况下,根据实际业务情况及流量,对一些服务和页面有策略的不处理或换种简单的方式处理,从而释放服务器资源以保证核心交易正常运作或高效运作
服务降级方式:
mock= force:return null:表示消费方对该服务的方法调用都直接返回null值,不发起远程调用。用来屏蔽不重要服务不可用时对调用方的影响。
mock=fail:return null:表示消费方对该服务的方法调用在失败后,再返回null值,不抛异常。用来容忍不重要服务不稳定时对调用方的影响
消费方dubbo-web中配置
public class UserController {
//远程注入
@Reference(mock ='force:return null")//不再调用userService的服务
private UserService userService;
}
nce(loadbalance = “random”)
@Reference(loadbalance = RandomLoadBalance.NAME)//默认负载均衡策略
1. 启动dubbo-web:http://localhost:8000/user/sayHello.do