dubbo整合spring基础配置和协议对比

dubbo协议对比

协议特性对比

协议

连接个数

连接方式

传输协议

传输方式

序列化

适用范围

适用场景

dubbo(默认)

单连接

长连接

TCP

NIO 异步传输

Hessian 二进制

传入传出参数数据包较小(建议小于100K),消费者比提供者个数多,单一消费者无法压满提供者,尽量不要用 dubbo 协议传输大文件或超大字符串。

常规远程服务方法调用

rmi

多连接

短连接

TCP

同步传输

Java 标准二进制序列化

传入传出参数数据包大小混合,消费者与提供者个数差不多,可传文件。常规远程服务方法调用,与原生RMI服务互操作

hessian

多连接

短连接

HTTP

同步传输

Hessian二进制序列化

传入传出参数数据包较大,提供者比消费者个数多,提供者压力较大,可传文件。

页面传输,文件传输,或与原生hessian服务互操作

http

多连接

短连接

HTTP

同步传输

表单序列化

传入传出参数数据包大小混合,提供者比消费者个数多,可用浏览器查看,可用表单或URL传入参数,暂不支持传文件。

需同时给应用程序和浏览器 JS 使用的服务。

WebService

多连接

短连接

HTTP

同步传输

SOAP 文本序列化

 

系统集成,跨语言调用

      其他协议还有trifit,memcached,redis,rest,trifit是基于thrift协议的扩展,memcached是基于memcached实现rpc,redis也一样,rest是基于JAX-RS 2.0实现rest风格调用,使用http传输协议。

搭建基础配置

1.公共配置  (三种配置方式)

(1)整合spring通过tomcat启动如下配置

<?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://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
       http://www.springframework.org/schema/beans/spring-beans.xsd  
       http://code.alibabatech.com/schema/dubbo  
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!-- 服务名  一般为项目名 -->
    <dubbo:application name="" />
    <!-- 注册的zookeeper地址 -->
    <dubbo:registry address="zookeeper://host:port" />
    <!-- 指定暴露的端口 name是协议名 -->
    <dubbo:protocol name="dubbo" port="20880" />

</beans>

(2)整合spring通过dubbo的main方法启动则在classpath下新增一个dubbo.properties文件,配置如下

# 指定容器 
dubbo.container=spring
# 指定容器名
dubbo.application.name=
# 指定日志类型
dubbo.application.logger=slf4j
# 指定注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
# 指定端口
dubbo.protocol.port=28030
# 指定提供服务的超时时间
dubbo.provider.timeout=3000
# 指定配置文件位置
dubbo.spring.config=classpath:*.xml

(3)整合springboot的配置方式  如上新建一个dubbo.properties,main方法上使用@PropertySource引入该配置

新建一个配置类,注入spring容器

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DubboConfig {

	@Value("${dubbo.application.name}")
    private String applicationName;
	
	@Value("${dubbo.registry.address}")
    private String registryAddress;
	
	@Value("${dubbo.protocol.port}")
	private int port;
	
    @Bean
    public ApplicationConfig applicationConfig() {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName(appName);
        return applicationConfig;
    }
    
    @Bean
    public RegistryConfig registryConfig() {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress(registryAddress);
        return registryConfig;
    }
    
    @Bean
    public ProtocolConfig protocolConfig(){
    	ProtocolConfig protocolConfig = new ProtocolConfig();
    	protocolConfig.setPort(port);
    	return protocolConfig;
    }

    @Bean
    public ConsumerConfig consumerConfig() {
        ConsumerConfig consumerConfig = new ConsumerConfig();
        consumerConfig.setCheck(false);
        return consumerConfig;
    }
    
}

2.注册配置(建议新建一个provider 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://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
       http://www.springframework.org/schema/beans/spring-beans.xsd  
       http://code.alibabatech.com/schema/dubbo  
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!-- 声明需要暴露的服务接口 ref填写加载到spring中bean名,扫描进去的bean以小写字母开头 -->
    <dubbo:service interface="me.lance.service.TestService" ref="testServiceImpl" />
</beans>

3.消费配置(建议新建一个consumer 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://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
       http://www.springframework.org/schema/beans/spring-beans.xsd  
       http://code.alibabatech.com/schema/dubbo  
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!-- 声明消费的服务 -->
    <dubbo:reference interface="me.lance.testService"  id="testService" />
</beans>

4.pom文件

<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.9</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.8.4</version>
</dependency>
<dependency>
	<groupId>com.github.sgroschupf</groupId>
	<artifactId>zkclient</artifactId>
	<version>0.1</version>
	<exclusions>
	    <exclusion>
		<groupId>org.apache.zookeeper</groupId>
		<artifactId>zookeeper</artifactId>
		</exclusion>
	</exclusions>
</dependency>

5.启动方式

(1)通过tomcat启动

(2)通过配置maven启动  如图

exec:java -Dexec.mainClass=com.alibaba.dubbo.container.Main

(3)springboot通过main方法启动即可

 

集群容错模式

配置方式

<!-- 服务提供方 -->
<dubbo:service cluster="failsafe" />

<!-- 服务消费方 -->
<dubbo:reference cluster="failsafe" />

Failover Cluster(默认)

失败自动切换,当出现失败,重试其它服务器 。通常用于读操作,但重试会带来更长延迟。可通过 retries="2" 来设置重试次数。

Failfast Cluster

快速失败,只发起一次调用,失败立即报错。通常用于非幂等性的写操作,比如新增记录。

Failsafe Cluster

失败安全,出现异常时,直接忽略。通常用于写入审计日志等操作。

Failback Cluster

失败自动恢复,后台记录失败请求,定时重发。通常用于消息通知操作。

Forking Cluster

并行调用多个服务器,只要一个成功即返回。通常用于实时性要求较高的读操作,但需要浪费更多服务资源。可通过 forks="2" 来设置最大并行数。

Broadcast Cluster

广播调用所有提供者,逐个调用,任意一台报错则报错。通常用于通知所有提供者更新缓存或日志等本地资源信息。

负载均衡

Random LoadBalance(默认)

  • 随机,按权重设置随机概率。
  • 在一个截面上碰撞的概率高,但调用量越大分布越均匀,而且按概率使用权重后也比较均匀,有利于动态调整提供者权重。

RoundRobin LoadBalance

  • 轮循,按公约后的权重设置轮循比率。
  • 存在慢的提供者累积请求的问题,比如:第二台机器很慢,但没挂,当请求调到第二台时就卡在那,久而久之,所有请求都卡在调到第二台上。

LeastActive LoadBalance

  • 最少活跃调用数,相同活跃数的随机,活跃数指调用前后计数差。
  • 使慢的提供者收到更少请求,因为越慢的提供者的调用前后计数差会越大。

ConsistentHash LoadBalance

  • 一致性 Hash,相同参数的请求总是发到同一提供者。
  • 当某一台提供者挂时,原本发往该提供者的请求,基于虚拟节点,平摊到其它提供者,不会引起剧烈变动。
  • 缺省只对第一个参数 Hash,如果要修改,请配置 <dubbo:parameter key="hash.arguments" value="0,1" />
  • 缺省用 160 份虚拟节点,如果要修改,请配置 <dubbo:parameter key="hash.nodes" value="320" />

实例

<!-- 服务端调用方式 -->
<dubbo:service interface="..." loadbalance="roundrobin" weight="2" />

 

本文章参考自dubbo文档

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值