dubbo 基本配置(入门)

0、dobbo 架构

0.1、节点角色说明 (5个)

节点 角色说明 作用
Container 服务容器 -
Registry 注册中心 服务注册与发现的
Provider 服务提供者 提供服务
Consumer 服务消费者 订阅服务的
Monitor 监控中心 统计服务的调用次数和调用时间

0.2、调用关系说明 (6步骤)

  1. (0.start - 启动): 服务容器 负责启动,加载,运行 服务提供者
  2. (1.register - 注册): 服务提供者 在启动时,向 注册中心 注册 自己提供的服务。
  3. (2.subscribe - 订阅): 服务消费者 在启动时,向 注册中心 订阅 自己所需的服务。
  4. (3.notify - 通知,推送): 注册中心 返回服务提供者 地址列表消费者。如果有变更,注册中心 将基于 长连接 推送变更数据给 消费者
  5. (4.invoke - 调用): 服务消费者 从提供者地址列表中,基于软 负载均衡 算法,选一台 服务提供者 进行调用,如果调用失败,再选另一台调用。
  6. (5.count - 统计): 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟 发送 一次统计数据到 监控中心

一、配置

provider.xml 示例

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       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">
       
    <dubbo:application name="demo-provider"/>
    
    <!--  <dubbo:registry address="zookeeper://127.0.0.1:2181"/> -->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
    
    <dubbo:protocol name="dubbo" port="20880"/>
    
    <bean id="demoService" class="org.apache.dubbo.samples.basic.impl.DemoServiceImpl"/>
    <dubbo:service interface="org.apache.dubbo.samples.basic.api.DemoService" ref="demoService"/>
    
</beans>

consumer.xml 示例

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       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">
       
    <dubbo:application name="demo-consumer"/>
    
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    
    <dubbo:reference id="demoService" check="false" interface="org.apache.dubbo.samples.basic.api.DemoService"/>

</beans>

二、直连

消费者 通过URL配置,直接连接 服务提供方,不通注册中心。常用于开发和测试环境。
配置很简单,consumer.xml 中的其他信息不变,

  • <dubbo:registry > 注释掉 ;
  • <dubbo:reference > 增加 url 属性,值为dubbo://localhost:20880
    其中的dubbo20880服务提供方<dubbo:protocol > 的配置一致 。
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       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">
       
    <dubbo:application name="demo-consumer"/>
    
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    
    <dubbo:reference id="demoService" check="false" interface="org.apache.dubbo.samples.basic.api.DemoService" 
    												url="dubbo://localhost:20880" />

</beans>

三、(消费者)启动不检查的配置

此操作是在在 消费者 的consumer.xml文件中配置的。

3.1、单个消费者的配置

<dubbo:reference > 标签中,增加check="false" 属性

<dubbo:reference  interface="com.atguigu.gmall.service.UserService" check="false" />

3.2、所有消费者的配置

配置当前所有消费者的服务都不检查
<dubbo:consumer> 标签中,增加check="false" 属性

扫描二维码关注公众号,回复: 5497396 查看本文章
<dubbo:consumer  check="false" />

3.3、关闭 注册中心 启动时检查

<dubbo:registry check="false" />

四、超时时间

超时时间 涉及到两个标签<dubbo:method><dubbo:reference><dubbo:consumer> ,属性是timeout ,默认时间是 1000 毫秒 。

4.1、方法级的配置

<dubbo:method> 标签中,增加timeout="3000" 属性

<dubbo:reference interface="com.atguigu.gmall.service.UserService" id="userService" >
	<dubbo:method name="getUserAddressList" timeout="3000"></dubbo:method>
</dubbo:reference>

4.2、接口级的配置

<dubbo:reference > 标签中,增加timeout="5000" 属性

<dubbo:reference  interface="com.atguigu.gmall.service.UserService"  timeout="5000" />

4.3、全局配置的配置

所有消费者的服务超时时间的配置。
<dubbo:consumer> 标签中,增加timeout="5000" 属性

<dubbo:consumer timeout="5000"  />

上面例举的是 消费方的配置,其实 服务提供方也可以配置超时时间。下面说说它们的优先级。

五、配置的优先级

timeout 为例,下图显示了配置的查找顺序,其它 retries, loadbalance, actives 等类似:

  • 方法级 优先,接口级 次之,全局配置 再次之。
  • 如果级别一样,则 消费方 优先,服务提供者 次之。

其中,服务提供者的配置,通过 URL 经由注册中心传递给消费方

六、重试次数 retries

重试次数 (retries)表示失败后,重试的次数,不包含第一次调用。
retries="2" 为例,第一次调用失败后,还可以再调用2次,所以总共可以调用3次。

retries="0" 代表不重试 。

  • 幂等(设置重试次数)【查询、删除、修改】
  • 非幂等(不能设置重试次数)【新增】

七、本地存根

远程服务后,服务提供方 想在 客户端也执行部分逻辑,比如:参数校验等,校验成功后。
如果校验失败,则返回 伪造容错数据
如果校验通过,再远程调用服务。

在API 中带上 Stub,客户端生成 Proxy 实例,会把 Proxy 通过构造函数传给 Stub [1],然后把 Stub 暴露给用户,Stub 可以决定要不要去调 Proxy。

  • Stub 实现 UserService 接口,并有一个传入远程 UserService 实例的构造函数
  • Stub 在重写方法实现业务逻辑,如参验校验等。

服务提供方的工程增加

提供 Stub 的实现 :

public class UserServiceStub implements UserService {
	
	private final UserService userService;	

	/**
	 * 传入的是userService远程的代理对象
	 * @param userService
	 */
	public UserServiceStub(UserService userService) {
		super();
		this.userService = userService;
	}

	@Override
	public List<UserAddress> getUserAddressList(String userId) {
		System.out.println("UserServiceStub.....");
		if(!StringUtils.isEmpty(userId)) { //如果userId为空,就不再远程调用
			return userService.getUserAddressList(userId);
		}
		return null;
	}
}
<dubbo:service interface="com.foo.UserService" stub="true" />
或
<dubbo:service interface="com.foo.UserService" stub="com.foo.UserServiceStub" />

通过如上配置,UserServiceStub 的代码最终是在 客户端执行

猜你喜欢

转载自blog.csdn.net/xiaojin21cen/article/details/88047253