zoukankan      html  css  js  c++  java
  • Dubbo简单DEMO以及重要配置项

    DEMO

    pom.xml

    消费方和服务提供方一致

    <properties>
        <spring.version>4.0.6.RELEASE</spring.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.8</version>
        </dependency>
    
        <!-- spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    

    服务提供方

    dubbo-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">
    
        <!-- 提供方应用信息,用于计算依赖关系 -->
        <dubbo:application name="hello-world-app"  />
    
        <!-- 使用multicast广播注册中心暴露服务地址 -->
        <dubbo:registry address="multicast://224.5.6.7:1234" />
    
        <!-- 用dubbo协议在20880端口暴露服务 -->
        <dubbo:protocol name="dubbo" port="20880" />
    
        <!-- 声明需要暴露的服务接口 -->
        <dubbo:service interface="com.boothsun.client.DemoService" ref="demoService" />
    
        <!-- 声明服务提供的bean-->
        <bean id="demoService" class="com.boothsun.provider.DemoServiceImpl" />
    </beans>
    

    接口定义

    实际项目中 都是有个专门的jar工程,然后服务提供方和消费方都引用这个jar。本demo,为了简单方便,就直接在消费方工程里和提供方工程里都定义一个全限定名一样的类即可。

    public interface DemoService {
        String sayHello(String name);
    }
    

    接口实现

    public class DemoServiceImpl implements DemoService {
        public String sayHello(String name) {
            return "Hello " + name;
        }
    }
    

    启动容器

    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring/dubbo-provider.xml"});
        context.start();
        System.in.read(); // 按任意键退出
    }
    

    服务消费方

    consumer-dubbo.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:application name="consumer-of-helloworld-app"  />
    
        <!-- 使用multicast广播注册中心暴露发现服务地址 -->
        <dubbo:registry address="multicast://224.5.6.7:1234" />
    
        <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
        <dubbo:reference id="demoService" interface="com.boothsun.client.DemoService" />
    </beans>
    

    消费方

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring/dubbo-consumer.xml"});
    context.start();
    DemoService demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理
    String hello = demoService.sayHello("world"); // 执行远程方法
    System.out.println( hello ); // 显示调用结果
    

    基础XML配置含义

    标签 用途 解释
    < dubbo:service /> 服务配置 用于暴露一个服务,定义服务的元信息,一个服务可以用多个协议暴露,一个服务也可以注册到多个注册中心
    < dubbo:reference /> 2 引用配置 用于创建一个远程服务代理,一个引用可以指向多个注册中心
    < dubbo:protocol /> 协议配置 用于配置提供服务的协议信息,协议由提供方指定,消费方被动接受
    < dubbo:application /> 应用配置 用于配置当前应用信息,不管该应用是提供者还是消费者
    < dubbo:module /> 模块配置 用于配置当前模块信息,可选
    < dubbo:registry /> 注册中心配置 用于配置连接注册中心相关信息
    < dubbo:monitor /> 监控中心配置 用于配置连接监控中心相关信息,可选
    < dubbo:provider /> 提供方配置 当 ProtocolConfig 和 ServiceConfig 某属性没有配置时,采用此缺省值,可选
    < dubbo:consumer /> 消费方配置 当 ReferenceConfig 某属性没有配置时,采用此缺省值,可选
    < dubbo:method /> 方法配置 用于 ServiceConfig 和 ReferenceConfig 指定方法级的配置信息
    < dubbo:argument /> 参数配置 用于指定方法参数配置

    启动时检查

    Dubbo 缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止Spring初始化完成,以便上线时,能及早发现问题,默认 check="true"。

    可以通过 check="false" 关闭检查,比如,测试时,有些服务不关心,或者出现了循环依赖,必须有一方先启动。

    另外,如果你的 Spring 容器是懒加载的,或者通过API编程延迟引用服务,请关闭check,否则服务临时不可用时,会抛出异常,拿到 null 引用,如果 check="false",总是会返回引用,当服务恢复时,能自动连上。

    报错如下:

    具体配置方式

    单个接口层:

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

    关闭所有服务的启动时检查 (没有提供者时不报错):

    <dubbo:consumer check="false" />
    

    关闭注册中心启动时检查 (注册订阅失败时不报错):

    <dubbo:registry check="false" />
    

    接口调用异常容错方案

    在集群调用失败时,Dubbo提供了多种容错方案,缺省为failover重试,默认重试次数为0,也就是不重试。

    Failover Cluster(失败重试)

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

    重试次数配置如下:

    // 服务暴露方配置
    <dubbo:service retries="2" />
    

    // 接口调用方配置
    <dubbo:reference retries="2" />
    

    // 方法层面
    <dubbo:reference>
        <dubbo:method name="findFoo" retries="2" />
    </dubbo:reference>
    

    重试需要下游系统能保证幂等性,否则重试就出问题。 比如之前负责订单系统,上游dubbo服务调用订单系统下单接口,订单系统处理超时,上游dubbo接口收到超时异常,再次重试,导致给用户生成两笔同样的订单。

    Failfast Cluster(快速失败)

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

    Failsafe Cluster(失败忽略)

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

    Failback Cluster(失败自动恢复)

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

    Forking Cluster(并发调用 有一个成功,就认为整体成功)

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

    Broadcast Cluster(广播形式)

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

    配置方式(服务提供方和服务消费方)

    <!-- 服务提供方 -->
    <dubbo:service cluster="failsafe" />
    
    <!-- 服务消费方 -->
    <dubbo:reference cluster="failsafe" />
    
  • 相关阅读:
    android 使用Activity做窗口弹出(模拟Dialog)
    解决ListView 跟ScroolView 共存 listItem.measure(0, 0) 空指针
    基于iview使用jsx扩展成可编辑的表格
    vue token 过期处理
    组件通信 eventtBus
    组件通信 $ref
    组件通信 Provide&&inject
    Vue 生命周期
    layui token 过期 重新登陆
    Python(3) 进制转换
  • 原文地址:https://www.cnblogs.com/boothsun/p/8184508.html
Copyright © 2011-2022 走看看