zoukankan      html  css  js  c++  java
  • 6、Dubbo-配置(1)

     

    覆盖关系

    下图展示了配置覆盖关系的优先级,从上到下优先级依次降低

     

     启动时检查

    Dubbo 缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止 Spring 初始化完成,以便上线时,能及早发现问题,默认 check="true"
    可以通过 check="false" 关闭检查,比如,测试时,有些服务不关心,或者出现了循环依赖,必须有一方先启动。
    另外,如果你的 Spring 容器是懒加载的,或者通过 API 编程延迟引用服务,请关闭 check,否则服务临时不可用时,会抛出异常,拿到 null 引用,
    如果 check="false",总是会返回引用,当服务恢复时,能自动连上。

     

    <?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://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-4.3.xsd
    http://dubbo.apache.org/schema/dubbo
    http://dubbo.apache.org/schema/dubbo/dubbo.xsd
    http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.3.xsd">
                  
              <context:component-scan  base-package="com.cr.service.impl"></context:component-scan>
         <!-- 1、指定当前服务/应用的名字(同样的服务名字,不要和别的服务同名 -->
         <dubbo:application  name="user-order-consumer"></dubbo:application>         
                
         <!-- 2、指定注册中心的位置 -->    
         <dubbo:registry address="zookeeper://127.0.0.1:2181" />    
                
         <!-- 3、声明 需要调用的远程服务接口:生成远程服务代理-->
         <!-- userService注册到容器中 -->
         <dubbo:reference interface="com.cr.service.UserService"  id="userService"  check="false"/>
                
         <!-- 监控中心 -->       
         <dubbo:monitor protocol="registry"></dubbo:monitor>
                
    </beans>

     

    如果服务较多时  均需要同意进行设置,会比较麻烦此时可以使用一个标签进行对整体服务设置
    关闭所有服务的启动时检查 (没有提供者时报错):
          <!-- 配置当前消费者的同意规则:所有的服务都不检查 -->      
          <dubbo:consumer check="false"></dubbo:consumer>  

     

     注意:不仅可以配置服务启动时检查还可以配置注册中心启动时检查

     

      <dubbo:consumer>

    超时设置

    服务消费方在使用服务提供方提供的服务时,如果遇到延迟等问题
    会有大量的线程阻塞,可能会引起性能下降
    超时设置在指定的时间内没有返回就立即终止

     1、对于服务提供方的代码进行睡眠4秒中

    public class UserServiceImpl  implements UserService {
         @Override
         public List<UserAddress> getUserAddressList(String userId)  {
               UserAddress address1 = new UserAddress(1, "安徽合肥蜀山区", "2", "程老师", "", "12345");
               UserAddress address2 = new UserAddress(2, "安徽合肥包河区", "2", "程老师", "", "12345");
               List<UserAddress> list = new  ArrayList<UserAddress>();
               list.add(address2);
               list.add(address1);
               try {
                    Thread.sleep(4000);
               } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
               }
               return list;
         }
    }
    消费方不进行超时配置
     
    看结果打印:
    log4j:WARN No appenders could be found for logger  (org.springframework.core.env.StandardEnvironment).
    log4j:WARN Please initialize the log4j system properly.
    log4j:WARN See  http://logging.apache.org/log4j/1.2/faq.html#noconfig for more  info.
    SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
    SLF4J: Defaulting to no-operation (NOP) logger implementation
    SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder  for further details.
    com.alibaba.dubbo.common.bytecode.proxy0@704deff2
    用户id:2
    Exception in thread "main" com.alibaba.dubbo.rpc.RpcException:  Failed to invoke the method 
    getUserAddressList in the service com.cr.service.UserService. Tried 3 times of the providers
    [169.254.77.19:20080] (1/1) from the registry 127.0.0.1:2181 on the consumer 169.254.77.19

    using the dubbo version 2.6.2. Last error is: Invoke remote method timeout. method:
    getUserAddressList, provider: d
    ubbo://169.254.77.19:20080/com.cr.service.UserService?anyhost=true&application=user-order-consumer&check=false&default.check=false&dubbo=2.6.2&generic=false&interface=com.cr.service.UserService&methods=getUserAddressList&monitor=dubbo%3A%2F%2F127.0.0.1%3A2181%2Fcom.alibaba.dubbo.regi

     此时的超时没有进行配置,可知其有默认值

    可知默认值是一秒

     

    可以理解在线程阻塞的时间内,超时等待5秒

         <dubbo:reference interface="com.cr.service.UserService"  id="userService"  timeout="5000"/>

     此时的等待时间大于线程休眠时间(5s > 4s)

     

    同理服务较多时可以使用dubbo:consumer 进行配置
     <dubbo:consumer check="false"  timeout="5000"></dubbo:consumer>   

     此时需要考虑两个配置同时配置的优先级

     http://dubbo.apache.org/zh-cn/docs/user/configuration/xml.html

     

    (建议由服务提供方设置超时,因为一个方法需要执行多长时间,服务提供方更清楚,

    如果一个消费方同时引用多个服务,就不需要关心每个服务的超时设置)

     

    精确到方法!!!

       <dubbo:reference interface="com.cr.service.UserService"  id="userService"  timeout="5000">
          <dubbo:method name="getUserAddressList"  timeout="2000"></dubbo:method>
         </dubbo:reference>

     此时时方法的配置生效  时间为2000ms

     统一提供

     

  • 相关阅读:
    装备购买 线性基+贪心
    花园 状压DP+矩阵快速幂
    数学作业 递推+矩阵快速幂
    石头游戏 构造+矩阵快速幂
    sumdiv 算术基本定理的推论
    huffman
    Integer 类型比较大小
    java 中的 String 相加
    Java 中的 static 关键字
    JAVA 基础
  • 原文地址:https://www.cnblogs.com/Mrchengs/p/10473979.html
Copyright © 2011-2022 走看看