【dubbo】——开发必备技能

    在使用dubbo开发过程中,多人通用一套环境,会造成明明程序启动却不执行断点的情况,执行结果和预期不符却找不到问题等等影响开发效率的问题。针对这些情况,dubbo也提供了响应的解决方案,减少开发过程的困扰。

直连提供者

    公司本地开发和联调环境公用一套zk+dubbo环境,为了减少联调时候的乌龙事件。在本地开发时,需要绕过注册中心,这样本地调用指定的服务提供者,即点对点直连。所谓点对点直联方式,将以服务接口为单位,忽略注册中心的提供者列表,即A接口配置点对点,不影响B接口从注册中心获取列表。

三种配置方式

xml配置单个服务
配置url指向提供者,可以绕过注册中心,多个地址,采用分号隔开。

<dubbo:reference id="xxxService" interface="com.xxx.XxxService" url="dubbo://localhost:20890" />

配置JVM -D参数制定
在jvm启动参数中加入-D参数映射服务地址

java -Dcom.xxx.XxxService=dubbo://localhost:20890

文件映射
    针对服务比较多的情况,使用文件映射,通过JVM参数-Ddubbo.resolve.file制定映射文件路径,此配置优先级最高

java -Ddubbo.resolve.file=xxx.properties
//在配置文件中加入配置如下,key/value形式,key为服务名,value为提供者url
com.xxx.XxxService=dubbo://localhost:20890

2.0以上版本dubbo自动加载电脑上${user.home}/dubbo-resolve.properties文件,不再需要上述配置(非常推荐使用)


只订阅

    在开发过程中如果有开发人员个人的服务注册到注册中心上,可能会影响到其他服务开发人员正常调用,所以我们希望开发过程中本地调试的服务可以不注册,而是只订阅其需要的服务。即只订阅模式提供两种方式如下
禁用注册配置

<dubbo:registry address="xxx.xxx.xxx.xx:9090" register="false" />
//或者
<dubbo:registry address="xxx.xxx.xxx.xx:9090?register=false" />

使用dubbo.properties禁用

dubbo.registry.register=false

只注册

    如果一个服务只需要对外提供服务,而自己本身不需要调用外服务,或者存在两个镜像环境,两个注册中心,但是只有其中一个注册中心部署了,但是另一个注册中心还没有完全部署,但是它又需要依赖其他服务,这个可以配置此服务在未部署好注册中心只注册,而不需要订阅服务。
禁用配置

<dubbo:registry id="hzRegistry" address="10.20.153.10:9090" />
<dubbo:registry id="qdRegistry" address="10.20.141.150:9090" subscribe="false" />
//或者
<dubbo:registry id="qdRegistry" address="10.20.141.150:9090?subscribe=false" />

启动时检查

    Dubbo缺省情况下会在启动时检查依赖的服务是否可用,不可用会抛出异常,阻止spring初始化的完成,以便能及时发现问题,即默认为check=”true”,可以通过check=”false”关闭检查,一般在开发或者测试过程对某些服务不关心,或者服务之间出现循环依赖,必须有一方先启动时采用此方式。如果Spring容器是懒加载,或者API编程延迟引用服务,也需要设置check=”false”,否则服务临时不可用的时候也会抛出异常,如果check=false,当服务恢复时,可以自动连接上
取消启动时检查配置如下
关闭某个服务的启动时检查(没有提供者时报错)

<dubbo:reference interface="com.foo.Xxxx" check="false" />

关闭所有服务启动时检查

<dubbo:consumer check="false" />

关闭注册中心启动时检查(注册订阅失败时报错),当消费者订阅成功,但是提供者列表为空,如果注册订阅失败,也允许启动,则需要使用此选项,将在后台定时重试

<dubbo:registry check="false" />

配置

    dubbo一共支持四种配置方式,便于大家灵活选用。
XML配置
一般采用此种方式都是利用spring进行配置的。如果不希望使用spring,可以采用API的方式启动

<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"
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">  
    <dubbo:application name="hello-world-app"  />  
    <dubbo:registry address="multicast://224.5.6.7:1234" />  
    <dubbo:protocol name="dubbo" port="20880" />  
    <dubbo:service interface="com.dubbo.demo.DemoService" ref="demoServiceLocal" />  
    <dubbo:reference id="demoServiceRemote" interface="com.dubbo.demo.DemoService" />  
</beans>

所有的标签支持自定义参数,用于不同扩展点实现特殊的配置。如

<dubbo:protocol name="jms">
    <dubbo:parameter key="queue" value="your_queue" />
</dubbo:protocol>
//或者如下
<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:p="http://www.springframework.org/schema/p"
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">  
    <dubbo:protocol name="jms" p:queue="your_queue" />  
</beans>

如有不理解每个标签的作用,请参见官网
xml配置属性优先级
1、方法最优,其次接口,再之全局配置
2、相同配置,消费者优先,提供者最后
属性配置
    如果公共配置比较简单,或者多个spring容器想共享配置,可以使用dubbo.properties作为缺省值配置。
    dubbo可以自动加载classpath根目录下的dubbo.properties,或者通过jvm启动参数-Ddubbo.properties.file=xxx.properties改变缺省配置位置。
映射规则
将xml配置的标签名,加属性名,用点分隔,多个属性拆成多行

dubbo.registry.address=10.20.153.10:9090
//等价于
<dubbo:registry address="10.20.153.10:9090" />

如果 XML 有多行同名标签配置,可用 id 号区分,如果没有 id 号将对所有同名标签生效

dubbo.registry.china.address=10.20.153.10:9090
//等价于
<dubbo:registry id="china" address="10.20.153.10:9090" />

覆盖策略
1、JVM 启动 -D参数优先
2、xml配置其次
3、properties最后
API 配置
API 属性与配置项一对一,例如

ApplicationConfig.setName("xxx") 对应 <dubbo:application name="xxx" />

注解配置
注意:2.5.7以上才支持
服务提供方
@Service注解暴露服务(注意引用dubbo的注解,而不是spring的)
javaconfig形式配置公共模块

@Configuration
public class DubboConfiguration {
    @Bean
    public ApplicationConfig applicationConfig() {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("provider-test");
        return applicationConfig;
    }

    @Bean
    public RegistryConfig registryConfig() {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("zookeeper://127.0.0.1:2181");
        registryConfig.setClient("curator");
        return registryConfig;
    }
}

指定dubbo扫描路径

@SpringBootApplication
@DubboComponentScan(basePackages = "com.dubbo.test.service.impl")
public class ProviderTestApp {
    // ...
}

服务消费方
@Reference注解引用服务


总结

    本文主要是整理小编自己在开发过程常用的一些功能,便于dubbo上手的使用。上述内容均参考dubbo官网,如有偏颇之处,请详见官网

猜你喜欢

转载自blog.csdn.net/jiadajing267/article/details/81089099