组件整合之RPC服务调用Dubbo
DUBBO官网:https://dubbo.apache.org/zh/
随着服务化的进一步发展,服务越来越多,服务之间的调用和依赖关系也越来越复杂,诞生了面向服务的架构体系(SOA),也因此衍生出了一系列相应的技术,如对服务提供、服务调用、连接处理、通信协议、序列化方式、服务发现、服务路由、日志输出等行为进行封装的服务框架。就这样为分布式系统的服务治理框架就出现了,Dubbo 也就这样产生了。
Dubbo 是一款高性能、轻量级的开源 RPC 框架,提供服务自动注册、自动发现等高效服务治理方案, 可以和 Spring 框架无缝集成。
快速入门
涉及到Dubbo-Parent(父项目)、Dubbo-Api、Dubbo-Consumer、Dubbo-Provider这几个。
Dubbo-Parent 父项目
<?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>com.study</groupId>
<artifactId>Dubbo-Parent</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>Dubbo-Api</module>
<module>Dubbo-Provider</module>
<module>Dubbo-Consumer</module>
</modules>
<packaging>pom</packaging>
<properties>
<spring.version>5.0.6.RELEASE</spring.version>
<jackson.version>2.11.4</jackson.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</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-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</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-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!--测试框架-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
<scope>test</scope>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<scope>provided</scope>
</dependency>
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
<!-- dubbo 依赖 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>3.0.4</version>
</dependency>
<!-- slf4j config start -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.25</version>
<scope>runtime</scope>
</dependency>
<!-- slf4j config end -->
<!-- log4j2 config start -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.17.0</version>
<scope>runtime</scope>
</dependency>
<!-- log4j2 config end -->
<!--Jackson-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.11.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.11.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.4</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Dubbo-Api 公共模块
主要是提供服务接口和相关的实体类
pom.xml:
<?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>Dubbo-Parent</artifactId>
<groupId>com.study</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>Dubbo-Api</artifactId>
<packaging>jar</packaging>
</project>
DemoDubboService 对外提供的服务接口
public interface DemoDubboService {
List<OrderInfo> listOrderInfo(Map<String, Object> params);
OrderInfo getOrderInfoById(Long orderId);
}
OrderInfo 实体类
public class OrderInfo implements Serializable {
private static final long serialVersionUID = 3356867898636903393L;
private Long orderId;
private String orderDesc;
public OrderInfo(){}
public OrderInfo(Long orderId, String orderDesc) {
this.orderId = orderId;
this.orderDesc = orderDesc;
}
public Long getOrderId() {
return orderId;
}
public void setOrderId(Long orderId) {
this.orderId = orderId;
}
public String getOrderDesc() {
return orderDesc;
}
public void setOrderDesc(String orderDesc) {
this.orderDesc = orderDesc;
}
}
Dubbo-Provider 服务提供者
pom.xml
<?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>
<parent>
<groupId>com.study</groupId>
<artifactId>Dubbo-Parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>Dubbo-Provider</artifactId>
<packaging>war</packaging>
<name>Dubbo-Provider</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.study</groupId>
<artifactId>Dubbo-Api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<!--测试框架-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<!-- dubbo 依赖 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<!-- slf4j config start -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<scope>runtime</scope>
</dependency>
<!-- slf4j config end -->
<!-- log4j2 config start -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<scope>runtime</scope>
</dependency>
<!-- log4j2 config end -->
<!--Jackson-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.2</version>
<configuration>
<target>1.8</target>
<source>1.8</source>
<encoding>utf-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
dubbo-provider.properties
#以下的配置是有固定格式的,前缀都是dubbo
#dubbo.application表示是对于schema为application的配置
#具体有哪些schema,参考:https://dubbo.apache.org/zh/docs/v2.7/user/references/xml/
dubbo.application.name=provider
dubbo.registry.address=multicast://224.5.6.7:1234
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
# 解决qos-server can not bind xxx, Address already in use: bind
dubbo.application.qosAcceptForeignIp=false
dubbo.application.qosPort=33300
dubbo.application.qosEnable=true
ProviderConfig 服务提供配置
@Configuration
@EnableDubbo(scanBasePackages = "com.study.provider.service.impl") //扫描@DubboService的服务实现类
@PropertySource("classpath:/dubbo-provider.properties")
public class ProviderConfig {
}
DemoDubboServiceImpl 服务接口具体的实现
@DubboService
public class DemoDubboServiceImpl implements DemoDubboService {
private static Map<Long, OrderInfo> orderInfoMap = new HashMap();
static {
orderInfoMap.put(1L, new OrderInfo(1L, "订单1"));
orderInfoMap.put(2L, new OrderInfo(2L, "订单2"));
orderInfoMap.put(3L, new OrderInfo(3L, "订单3"));
orderInfoMap.put(4L, new OrderInfo(4L, "订单4"));
orderInfoMap.put(5L, new OrderInfo(5L, "订单5"));
orderInfoMap.put(6L, new OrderInfo(6L, "订单6"));
orderInfoMap.put(7L, new OrderInfo(7L, "订单7"));
orderInfoMap.put(8L, new OrderInfo(8L, "订单8"));
}
@Override
public List<OrderInfo> listOrderInfo(Map<String, Object> params) {
return new ArrayList(orderInfoMap.values());
}
@Override
public OrderInfo getOrderInfoById(Long orderId) {
return orderInfoMap.get(orderId);
}
}
Dubbo-Consumer 服务调用方
pom.xml
<?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>
<parent>
<groupId>com.study</groupId>
<artifactId>Dubbo-Parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>Dubbo-Consumer</artifactId>
<packaging>war</packaging>
<name>Dubbo-Consumer</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.study</groupId>
<artifactId>Dubbo-Api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<!--测试框架-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<!-- dubbo 依赖 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<!-- slf4j config start -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<scope>runtime</scope>
</dependency>
<!-- slf4j config end -->
<!-- log4j2 config start -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<scope>runtime</scope>
</dependency>
<!-- log4j2 config end -->
<!--Jackson-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.2</version>
<configuration>
<target>1.8</target>
<source>1.8</source>
<encoding>utf-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
dubbo-consumer.properties
#以下的配置是有固定格式的,前缀都是dubbo
#dubbo.application表示是对于schema为application的配置
#具体有哪些schema,参考:https://dubbo.apache.org/zh/docs/v2.7/user/references/xml/
dubbo.application.name=consumer
dubbo.registry.address=multicast://224.5.6.7:1234
dubbo.consumer.timeout=3000
# 解决qos-server can not bind xxx, Address already in use: bind
dubbo.application.qosAcceptForeignIp=false4
dubbo.application.qosPort=23451
dubbo.application.qosEnable=true
ConsumerConfig 服务消费者配置
@Configuration
@EnableDubbo(scanBasePackages = "com.study.consumer.service") //扫描@DubboReference或@DubboService注解的服务实现类
@PropertySource("classpath:/dubbo-consumer.properties")
public class ConsumerConfig {
}
OrderInfoServiceImpl 服务调用方
@Service
public class OrderInfoServiceImpl {
@DubboReference
private DemoDubboService demoDubboService;
public Map<String, Object> findOrderInfoList(Map<String, Object> queryCondition){
Map<String, Object> resultMap = new HashMap();
List<OrderInfo> orderInfos = demoDubboService.listOrderInfo(queryCondition);
resultMap.put("success", true);
resultMap.put("code", 200);
resultMap.put("data", orderInfos);
return resultMap;
}
public Map<String, Object> getOrderInfoById(Long orderId){
Map<String, Object> resultMap = new HashMap();
OrderInfo orderInfo = demoDubboService.getOrderInfoById(orderId);
resultMap.put("success", true);
resultMap.put("code", 200);
resultMap.put("data", orderInfo);
return resultMap;
}
}
OrderInfoController 客户端调用
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderInfoController {
@Autowired
private OrderInfoServiceImpl orderInfoService;
@PostMapping("findOrderInfoList")
public Map<String, Object> findOrderInfoList(@RequestBody Map<String, Object> queryCondition){
return orderInfoService.findOrderInfoList(queryCondition);
}
@GetMapping("getOrderInfoById")
public Map<String, Object> getOrderInfoById(Long orderId){
return orderInfoService.getOrderInfoById(orderId);
}
}
Dubbo常用的配置参数
dubbo:application
应用信息配置。对应的配置类:org.apache.dubbo.config.ApplicationConfig
属性 | 类型 | 默认值 | 描述 |
name | string | 当前应用名称 | |
logger | string | slf4j | 日志输出方式,可选:slf4j,jcl,log4j,log4j2,jdk |
dubbo:consumer
服务消费者缺省值配置。配置类: org.apache.dubbo.config.ConsumerConfig 。同时该标签为 <dubbo:reference> 标签的缺省值设置。
属性 | 类型 | 默认值 | 描述 |
timeout | int | 1000 | 远程服务调用超时时间(毫秒) |
retries | int | 2 | 远程服务调用重试次数,不包括第一次调用,不需要重试请设为0,仅在cluster为failback/failover时有效 |
loadbalance | string | random | 负载均衡策略,可选值:random,roundrobin,leastactive,分别表示:随机,轮询,最少活跃调用 |
async | boolean | false | 是否缺省异步执行,不可靠异步,只是忽略返回值,不阻塞执行线程 |
connections | int | 100 | 每个服务对每个提供者的最大连接数,rmi、http、hessian等短连接协议支持此配置,dubbo协议长连接不支持此配置 |
check | boolean | true | 启动时检查提供者是否存在,true报错,false忽略 |
actives | int | 0 | 每服务消费者每服务每方法最大并发调用数 |
cluster | string | failover | 集群方式,可选:failover/failfast/failsafe/failback/forking |
filter | string | 服务消费方远程调用过程拦截器名称,多个名称用逗号分隔 | |
registry | string | 缺省向所有registry注册 | 向指定注册中心注册,在多个注册中心时使用,值为<dubbo:registry>的id属性,多个注册中心ID用逗号分隔,如果不想将该服务注册到任何registry,可将值设为N/A |
cache | string/boolean | 以调用参数为key,缓存返回结果,可选:lru, threadlocal, jcache等 | |
validation | boolean | 是否启用JSR303标准注解验证,如果启用,将对方法参数上的注解进行校验 |
dubbo:method
方法级配置。对应的配置类: org.apache.dubbo.config.MethodConfig。同时该标签为 <dubbo:service> 或 <dubbo:reference> 的子标签,用于控制到方法级。
参考:dubbo-method
属性 | 类型 | 默认值 | 描述 |
name | string | 方法名 | |
timeout | int | 缺省为的timeout | 方法调用超时时间(毫秒) |
retries | int | 缺省为<dubbo:reference>的retries | 远程服务调用重试次数,不包括第一次调用,不需要重试请设为0 |
loadbalance | string | 缺省为的loadbalance | 负载均衡策略,可选值:random,roundrobin,leastactive,分别表示:随机,轮询,最少活跃调用 |
async | boolean | 缺省为<dubbo:reference>的async | 是否异步执行,不可靠异步,只是忽略返回值,不阻塞执行线程 |
dubbo:protocol
服务提供者协议配置。对应的配置类: org.apache.dubbo.config.ProtocolConfig。同时,如果需要支持多协议,可以声明多个 <dubbo:protocol> 标签,并在 <dubbo:service> 中通过 protocol 属性指定使用的协议。
属性 | 类型 | 默认值 | 描述 |
name | string | dubbo | 协议名称 |
port | int | dubbo协议缺省端口为20880,rmi协议缺省端口为1099,http和hessian协议缺省端口为80;如果没有配置port,则自动采用默认端口,如果配置为-1,则会分配一个没有被占用的端口。 | 服务端口 |
host | string | 自动查找本机IP | 服务主机名,建议不要配置,让Dubbo自动获取本机IP |
threadpool | string | fixed | 线程池类型,可选:fixed/cached |
threads | int | 200 | 服务线程池大小(固定大小) |
iothreads | int | cpu个数+1 | io线程池大小(固定大小) |
accepts | int | 0 | 服务提供方最大可接受连接数 |
payload | int | 8388608(=8M) | 请求及响应数据包大小限制,单位:字节 |
serialization | string | dubbo协议缺省为hessian2,rmi协议缺省为java,http协议缺省为json | 协议序列化方式,当协议支持多种序列化方式时使用,比如:dubbo协议的dubbo,hessian2,java,compactedjava,以及http协议的json等 |
queues | int | 0 | 线程池队列大小,当线程池满时,排队等待执行的队列大小,建议不要设置,当线程池满时应立即失败,重试其它服务提供机器,而不是排队,除非有特殊需求。 |
heartbeat | int | 0 | 心跳间隔,对于长连接,当物理层断开时,比如拔网线,TCP的FIN消息来不及发送,对方收不到断开事件,此时需要心跳来帮助检查连接是否已断开 |
register | boolean | true | 该协议的服务是否注册到注册中心 |
dubbo:provider
服务提供者缺省值配置。对应的配置类: org.apache.dubbo.config.ProviderConfig。同时该标签为 <dubbo:service> 和 <dubbo:protocol> 标签的缺省值设置。
属性 | 类型 | 默认值 | 描述 |
protocol | string | dubbo | 协议名称 |
host | string | 自动查找本机IP | 服务主机名,多网卡选择或指定VIP及域名时使用,为空则自动查找本机IP,建议不要配置,让Dubbo自动获取本机IP |
serialization | string | dubbo协议缺省为hessian2,rmi协议缺省为java,http协议缺省为json | 协议序列化方式,当协议支持多种序列化方式时使用,比如:dubbo协议的dubbo,hessian2,java,compactedjava,以及http协议的json,xml等 |
delay | int | 0 | 延迟注册服务时间(毫秒)- ,设为-1时,表示延迟到Spring容器初始化完成时暴露服务 |
timeout | int | 1000 | 远程服务调用超时时间(毫秒) |
retries | int | 2 | 远程服务调用重试次数,不包括第一次调用,不需要重试请设为0 |
connections | int | 0 | 对每个提供者的最大连接数,rmi、http、hessian等短连接协议表示限制连接数,dubbo等长连接协表示建立的长连接个数 |
loadbalance | string | random | 负载均衡策略,可选值:random,roundrobin,leastactive,分别表示:随机,轮询,最少活跃调用 |
async | boolean | false | 是否缺省异步执行,不可靠异步,只是忽略返回值,不阻塞执行线程 |
registry | string | 缺省向所有registry注册 | 向指定注册中心注册,在多个注册中心时使用,值为<dubbo:registry>的id属性,多个注册中心ID用逗号分隔,如果不想将该服务注册到任何registry,可将值设为N/A |
dubbo:reference
服务消费者引用服务配置。对应的配置类: org.apache.dubbo.config.ReferenceConfig。
dubbo:registry
注册中心配置。对应的配置类: org.apache.dubbo.config.RegistryConfig。同时如果有多个不同的注册中心,可以声明多个 <dubbo:registry> 标签,并在 <dubbo:service> 或 <dubbo:reference> 的 registry 属性指定使用的注册中心。
dubbo:service
服务提供者暴露服务配置。对应的配置类:org.apache.dubbo.config.ServiceConfig