dubbo 生产者和消费者的配置(入门)

一、配置

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" 属性

<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 经由注册中心传递给消费方

dubbo 生产者和消费者的配置(入门)

六、重试次数 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 的代码最终是在 客户端执行