zoukankan      html  css  js  c++  java
  • Dubbo-环境搭建

    一、RPC:

      RPC【Remote Procedure Call】是指远程过程调用,是一种进程间通信方式,他是一种技术的思想,而不是规范。它允许程序调用另一个地址空间(通常是共享网络的另一台机器上)的过程或函数,而不用程序员显式编码这个远程调用的细节。即程序员无论是调用本地的还是远程的函数,本质上编写的调用代码基本相同。

    RPC基本原理

      RPC两个核心模块:通讯、序列化。

                           

    二、dubbo:

      Apache Dubbo: 是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。

          

        服务提供者(Provider:暴露服务的服务提供方,服务提供者在启动时,向注册中心注册自己提供的服务。

             服务消费者(Consumer: 调用远程服务的服务消费方,服务消费者在启动时,向注册中心订阅自己所需的服务,服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。

             注册中心(Registry:注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者

             监控中心(Monitor:服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心

    • 调用关系说明

        l  服务容器负责启动,加载,运行服务提供者。

        l  服务提供者在启动时,向注册中心注册自己提供的服务。

        l  服务消费者在启动时,向注册中心订阅自己所需的服务。

        l  注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。

        l  服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。

        l  服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

    三、dubbo环境搭建:

      首先安装好注册中心zookeeper;其次可以安装好dubbo-admin管理控制台,方便查看服务

      1. 创建一个公共的模块:

        存放,model,service,exception等公共信息,可以在提供端和消费端进行引入。

      2. 创建provider模块:提供端

        一般是微服务,服务层提供服务。

      3. 创建consumer模块:消费端

        一般是web服务器,控制层进程调用。

      4. 导入依赖包:

        <dependencies>
            <dependency>
                <groupId>cn.ll.dubbo</groupId>
                <artifactId>gmall-interface</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
            <!-- 引入dubbo-->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.7.2</version>
            </dependency>
         <!-- zookeeper客户端--> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.2.0</version> </dependency> <!--netty依赖包 --> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.37.Final</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-metadata-report-zookeeper</artifactId> <version>2.7.2</version> </dependency> </dependencies>

      5. 提供端:

        》在resources下创建dubbo子包:META-INFspring“”(dubbo要求的必须这样创建)

        》在“META-INFspring”下创建Spring与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://dubbo.apache.org/schema/dubbo" xmlns:contex="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/context
            http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!--1.指定当前服务、应用的名称(同样的服务名称相同、不要和别的服务重名) -->
        <dubbo:application name="user-service-provider" />
    <!--2.指定注册中心、注册中心、元数据 --> <dubbo:registry simplified="true" address="zookeeper://192.168.28.219:2181?backup=192.168.28.220:2181,192.168.28.221:2181" />
    <!--3.指定通信规则(通信协议、通信端口),port值-1 表示让dubbo自行找一个可用的port-->
      <dubbo:protocol name="dubbo" port="20880" />
        <!--4.暴露服务,ref:指向真正的实现对象 -->
        <dubbo:service interface="com.ll.service.UserService" ref="userServiceImpl" />
    <!--5.统一设置服务提供方规则: --> <dubbo:provider timeout="1000" />
    <contex:component-scan base-package="com.ll.service" /> </beans>

      》服务端启动:

    》1.通过dubbo方式启动dubbo
      import org.apache.dubbo.container.Main;
        public static void main(String[] args) {
            Main.main(args); // 启动Dubbo服务
        }
    》2.通过spring启动dubbo
      public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml"); context.start(); System.in.read(); // 按任意键退出 }

      6. 消费端:

        》在resources下创建dubbo子包:META-INFspring“”(dubbo要求的必须这样创建)

        》在“META-INFspring”下创建Spring与dubbo的配置文件配置文件:consumer.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://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/context
           http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!--1.指定当前服务、应用的名称(同样的服务名称相同、不要和别的服务重名) -->
        <dubbo:application name="order-service-consumer" />
    
        <!--2.指定注册中心的位置 -->
        <dubbo:registry address="zookeeper://192.168.28.219:2181?backup=192.168.28.220:2181,192.168.28.221:2181" />
    <!--3.声明需要调用的远程服务接口;生产远程服务代理--> <dubbo:reference interface="com.ll.service.UserService" id="userService" url="192.168.28.219:2181" />
    <!--4.配置当前消费者的同一规则: check="false":启动时所有服务都不检查 timeout="1000":超时时间,默认1000毫秒 retries="3":重试次数,不包含第一次调用 --> <dubbo:consumer check="false" timeout="5000" retries="3" loadbalance="" /> <context:component-scan base-package="com.ll.service.impl"/> </beans>

      》消费端启动:

    public static void main(String[] args) throws Exception {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
            context.start();
            OrderService orderService = context.getBean(OrderService.class);// 获取远程服务代理
            orderService.initOrder("1") ;
            System.out.println("调用完成.....");
            System.in.read() ;
        }
  • 相关阅读:
    Reflector 插件
    Tips for ILMerge
    WaitAll for multiple handles on a STA thread is not supported 解决方案
    MSI: UAC return 0x800704C7
    SET与SETX的区别
    年在Copyright中的含义
    gacutil : 添加.NET 4.0 assembly 到GAC失败
    LicenseContext.GetSavedLicenseKey 需要 FileIOPermission
    Linq学习之linq基础知识
    SQL Server 2008如何导出带数据的脚本文件
  • 原文地址:https://www.cnblogs.com/luliang888/p/11216468.html
Copyright © 2011-2022 走看看