Dubbo学习第二次课程

Dubbo 学习第二次课

一、前情回顾:

1、dubbo的四种配置方式:

  • List item
  • xml(常用)
  • properties
  • annotation(常用)
  • api方式

2、dubbo标签有个继承关系,下层会继承上层属性配置。(原因:很多提供者只知道自己的接口配置什么属性合适,eg:timeout)。

二、今天课程内容

  1. 把mvc web项目变成dubbo
  2. dubbo里面常用的一些配置使用、学习。
    什么是依赖?(成员变量、方法入参、方法返回、方法体内的对象),如下图:
    在这里插入图片描述项目拆分:
    mall-support:(把需要暴露的interface 和entity存放在mall-support,此module不需要运行)
    在这里插入图片描述
    order-server:(pom.xml中引入mall-support module)
<!-- 业务支持包 -->
    <dependency>
      <groupId>com.enjoy</groupId>
      <artifactId>mall-support</artifactId>
      <version>1.0</version>
    </dependency>
    <!-- dubbo -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.5.7</version>
      <exclusions>
        <exclusion>
          <artifactId>spring</artifactId>
          <groupId>org.springframework</groupId>
        </exclusion>
      </exclusions>
    </dependency>
<!-- spring支持 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.0.2.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.0.2.RELEASE</version>
    </dependency>
    <!-- zkclient -->
    <dependency>
      <groupId>com.github.sgroschupf</groupId>
      <artifactId>zkclient</artifactId>
      <version>0.1</version>
    </dependency>

    <!-- zookeeper -->
    <dependency>
      <groupId>org.apache.zookeeper</groupId>
      <artifactId>zookeeper</artifactId>
      <version>3.4.6</version>
    </dependency>
  </dependencies>

在order-server resources目录下引入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:context="http://www.springframework.org/schema/context"
       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://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
	 http://code.alibabatech.com/schema/dubbo
	 http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!--扫描-->
    <context:component-scan base-package="com.enjoy"/>
	<!--module 名称-->
    <dubbo:application name="order-server" />
    <!--module 注册中心-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <!--name指示使用什么协议:dubbo/rmi/rest-->
    <dubbo:protocol id="d1"  name="dubbo" port="20880" />
    <!-- 或者-->
    <dubbo:protocol id="d2"  name="rmi" port="20882" />
    <!--全局配置, 其他的接口可以默认此属性-->
    <dubbo:provider timeout="3000" />
    <dubbo:consumer timeout="2000" check="false" />
	<!-- 暴露服务的类对象-->
    <dubbo:service interface="com.enjoy.service.OrderService" ref="orderService" protocol="d1" />
	
    <bean id="orderService" class="com.enjoy.service.OrderServiceImpl" />



</beans>

启动order-server spring 容器:

 ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:dubbo.xml");

        ctx.start();
        

三、Dubbo常用配置策略用法:

  1. 启动检查check=false,防止循环依赖,抛异常。
  2. 集群 <dubbo:reference id=“XX” class=“XXX” cluster=“failover、failfast”、“failSafe、Failback、Forking”/>

    failOver:当出现失败,重试其它服务器,通常用于读操作(推荐使用)。 重试会带来更长延迟。
    failfast:只发起一次调用,失败立即报错,通常用于非幂等性的写操作。
    Failsafe:出现异常时,直接忽略
    Forking:并行调用
  3. 负载均衡< dubbo:consumer loadbalance="random,roundrobin、leastActive "/ >
    random: 设置weight 在<dubbo:service > 提供方中设置
    roundrobin: 轮询 在< dubbo:reference >中设置
  4. 接口缓存 lru:基于最近最少使用原则删除缓存 (key:value=入参:返回值)
    threadLocal 当前线程缓存(只把返回结果返回在线程中)
  5. dubbo异步调用<dubbo:reference class=“XX.XX.UserService” async=“true”>
userService.getDetail(2);
Future future = RpcContext.getContext.getFuture();
Object o = future.get;

第一次写博客,草草之处还请见谅。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值