dubbo实现及配置

dubbo 实现

  1. 创建项目
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sean.dubbo</groupId>
    <artifactId>dubbo-order</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>order-api</module>
        <module>order-provider</module>
    </modules>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.6</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>4.3.10.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>4.3.10.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>4.3.10.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>4.3.10.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.5.3</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>com.github.sgroschupf</groupId>
                <artifactId>zkclient</artifactId>
                <version>0.1</version>
            </dependency>
            <dependency>
                <groupId>com.caucho</groupId>
                <artifactId>hessian</artifactId>
                <version>4.0.38</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.5.3</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.0.2</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
    </build>
</project>
  1. api项目中创建接口和实体(实体需要实现序列化接口,dubbo协议传输需要),并在xml中配置消费者的引用

    1. 创建接口

      public interface OrderService {
      
          BaseResponse saveOrder(Order order);
      
          BaseResponse<Order> queryOrder(String id);
      
      }
      
    2. 创建实体

      @Data
      @ToString
      @NoArgsConstructor
      @AllArgsConstructor
      public class Order  implements Serializable {
          private String id;
          private String orderName;
          private int orderCount;
      }
      
      @Data
      @ToString
      @NoArgsConstructor
      @AllArgsConstructor
      public class BaseResponse<T> implements Serializable {
          private String code;
          private String msg;
          private T data;
      }
      
    3. 提供消费者的引用

      <?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.0.xsd
              http://code.alibabatech.com/schema/dubbo
              http://code.alibabatech.com/schema/dubbo/dubbo.xsd" default-autowire="byName">
      
          <context:annotation-config></context:annotation-config>
      
      	<!--订阅接口-->
          <dubbo:reference id="orderService" interface="com.sean.dubbo.order.service.OrderService" protocol="dubbo" version="1.0"/>
      </beans>
      
  2. provider项目实现api中创建的接口

    @Service("orderService")
    public class OrderServiceImpl implements OrderService {
    
        private static List<Order> orders = new ArrayList<Order>();
    
        public BaseResponse saveOrder(Order order) {
            if (order == null) {
                return new BaseResponse("010", "order is null", null);
            }
            orders.add(order);
            return new BaseResponse("0", "success", null);
        }
    
        public BaseResponse<Order> queryOrder(String id) {
            if (StringUtils.isEmpty(id)) {
                return new BaseResponse("010", "id is null", null);
            }
            for (Order order : orders) {
                if (order == null) {
                    continue;
                }
                if (id.equals(order.getId())) {
                    return new BaseResponse("0", "success", order);
                }
            }
            return new BaseResponse("010", "no order", null);
        }
    }
    
  3. 发布接口至注册中心(会将接口存储至zookeeper的一个节点上,包含接口的各部分信息)

    <?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.0.xsd
            http://code.alibabatech.com/schema/dubbo
            http://code.alibabatech.com/schema/dubbo/dubbo.xsd" default-autowire="byName">  
        <context:annotation-config></context:annotation-config>
        <context:component-scan base-package="com.sean.dubbo.order"/>
        <!--当前项目在整个分布式架构里面的唯一名称,计算依赖关系的标签-->
        <dubbo:application name="order-provider" owner="sean"/>
        <!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
        <dubbo:registry protocol="zookeeper" address="192.168.71.19:2181"/>
        <!--当前服务发布所依赖的协议;webserovice、Thrift、Hessain、http-->
        <dubbo:protocol name="dubbo" port="20880" host="10.13.65.17"/>
        <!--增加hessian协议-->
        <dubbo:protocol name="hession" port="8090" server="jetty"/>
    
        <!--服务发布的配置,需要暴露的服务接口-->
        <dubbo:service interface="com.sean.dubbo.order.service.OrderService" ref="orderService" protocol="dubbo" version="1.0" loadbalance="random"/>
        <!--服务发布的配置,需要暴露的服务接口-->
        <dubbo:service interface="com.sean.dubbo.order.service.OrderService" ref="orderService" protocol="hession" version="1.2" loadbalance="random"/>
    
    </beans>
    

5.消费者注册订阅

<?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.0.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd" default-autowire="byName">

    <context:annotation-config></context:annotation-config>
    <context:component-scan base-package="com.sean.dubbo.user"/>

    <!--当前项目在整个分布式架构里面的唯一名称,计算依赖关系的标签-->
    <dubbo:application name="user-provider" owner="mic"/>
    <!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
    <dubbo:registry protocol="zookeeper" address="192.168.71.19:2181"/>
    
    <!--引用消费的配置文件-->
    <import resource="classpath*:order-customer.xml"/>
</beans>
  1. 实现测试

    public class App {
        public static void main(String[] args) {
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("user-provider.xml");
            // 获取接口,调用
            OrderService orderService = (OrderService) applicationContext.getBean("orderService");
            Order order = new Order("aaa", "sss", 12);
            BaseResponse baseResponse = orderService.saveOrder(order);
            System.out.println(baseResponse);
            BaseResponse<Order> aaa = orderService.queryOrder("aaa");
            System.out.println(aaa);
    
        }
    }
    

dubbo 控制台

控制台主要用于服务治理,控制服务的权重,路由等

  1. 在以下链接的最后,找到Dubbo Admin

在这里插入图片描述

  1. 克隆或下载源码到本地`

  2. 修改以下文件的注册地址 dubbo-admin-server/src/main/resources/application-production.properties

  3. maven 进行打包

  • mvn clean package
  1. 启动项目
  • mvn --projects dubbo-admin-server spring-boot:run
    或者
  • cd dubbo-admin-distribution/target; java -jar dubbo-admin-0.1.jar
  1. 访问 http://localhost:8080

dubbo 监控中心

监控中心主要查看服务间的调用情况和调用关系以及响应事件

  1. 在以下链接拉取整个dubbo源码
  1. 配置monitor项目下的dubbo.properties文件的注册地址
  2. 启动项目,使用命令
  3. 访问 http://localhost:8080

dubbo 配置

相关配置

  1. 启动服务检查

    <!--check 会检查引用的服务是否存在,默认检查-->
        <dubbo:reference id="orderService" interface="com.sean.dubbo.order.service.OrderService" check="false" />
    
  2. 多协议

        <!--dubbo协议-->
        <dubbo:reference id="orderService" interface="com.sean.dubbo.order.service.OrderService" protocol="dubbo"/>
        <!--hessian协议-->
        <dubbo:reference id="orderService2" interface="com.sean.dubbo.order.service.OrderService" protocol="hessian"/>
    
  3. 多版本

        <!--1.0.0版本-->
        <dubbo:reference id="orderService" interface="com.sean.dubbo.order.service.OrderService" version="1.0.0"/>
        <!--1.2.0版本-->
        <dubbo:reference id="orderService2" interface="com.sean.dubbo.order.service.OrderService" version="1.1.0"/>
    
  4. 多注册中心

        <dubbo:registry protocol="zookeeper" address="192.168.71.19:2181" check="true"/>
        <dubbo:registry protocol="redis" address="xxx" check="true"/>
    
  5. 异步调用

        <!--采取异步调用策略-->
        <dubbo:reference id="orderService" interface="com.sean.dubbo.order.service.OrderService" async="true"/>
    
  6. 只订阅

        <!--只订阅服务-->
        <dubbo:registry protocol="zookeeper" address="192.168.71.19:2181" register="false"/>
    
  7. 只注册

        <!--只注册服务-->
        <dubbo:registry protocol="zookeeper" address="192.168.71.19:2181" subscribe="false"/>
    
  8. 负载均衡

    <!--随机的负载均衡-->
    <dubbo:reference id="orderService" interface="com.sean.dubbo.order.service.OrderService" loadbalance="random"/>
    
  9. 集群容错

        <!--快速失败的容错方式,可选:failover/failfast/failsafe/failback/forking-->
        <dubbo:reference id="orderService" interface="com.sean.dubbo.order.service.OrderService" cluster="failfast"/>
    
  10. 配置管理员信息

        <!--当前项目在整个分布式架构里面的唯一名称,计算依赖关系的标签-->
        <dubbo:application name="user-provider" owner="mic"/>
    

    application上面配置的owner,建议配置2个人以上,因为owner都能够在监控中心看到。

  11. 配置dubbo缓存文件

        <!--配置缓存文件-->
        <dubbo:registry protocol="zookeeper" address="192.168.71.19:2181" file="xxx"/>
    

    重启后,内存数据不存在,也无法连接注册中心的情况下,可以通过缓存文件找到注册中心列表和服务提供者列表

配置优先级

消费端方法优先级最高– 服务端提供者最低

在这里插入图片描述

dubbo 源码

  1. 自定义spring中dubbo的xml标签,并加入对应的解析器
  2. 解析xml标签,初始化实例对象,并在类上实现initializingBean,DisposableBean等接口,对实体对象做代理功能的扩展,如容错或负载均衡
  3. 服务的发布是通过spring的afterPropertiesSet方法进行发布,通过代理工厂获取对应的Invoker,再通过相应的协议调用invoker,进行发布;服务发布实际是将协议的url和接口的数据保存到注册中心,如zookeeper,则是保存到znode节点下
  4. 服务的订阅则是通过spring的afterPropertiesSet方法创建代理对象,再通过协议获取InvokerProxy,用户调用service时,会通过调用Invoker的invoke方法发起请求;服务订阅实际是订阅注册中心下的节点数据,获取接口信息,再进行调用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值