zoukankan      html  css  js  c++  java
  • (6)RPC之Dubbo初识

    大纲分析

     一、RPC

    1、RPC是什么

      RPC是Remote  Procedure  Call的缩写,译为远程过程调用,可以实现进程间的通信

      比如:两个进程可能在不同的机器上(同一台机器的两个进程也行),可以相互进行调用执行

      凡是能实现进程通信的,都可称RPC

       一直学习法则:3W1H:what(是什么)why(为什么)when(何时用)HOW(怎么用)  

    2、RPC的核心

      涉及三个角色:

      1、注册中心  实现统一调配,进行服务的管理

      2、服务提供者:提高对应的服务

      3、服务消费者:实现服务的调用

     3、RPC主流框架

      实际在使用过程中,可以通过以下途径完成RPC应用

      1、自定义RPC框架:基于Netty实现  难度大、稳定性差

      2、Dubbo/DuddoX

      3、SpringCloud  Netflix  / Alibaba

      4、gRpc

    二、Dubbo

    1、Dubbo是什么

      Apache Dubbo 是一个开源的基于java实现的RPC框架

      核心作用:

      1、实现服务的治理(服务注册和服务发现)

      2、远程方法调用

      3、容错和负载均衡处理  

    2、Dubbo的核心

      

       涉及角色:

      1、注册中心  官方推荐:Zookeeper

      2、提供者

      3、消费者

      4、监控中心  Dubbo-Admin

    3、注册中心

      推荐使用 Zookeeper 注册中心

      http://dubbo.apache.org/zh-cn/docs/user/references/registry/zookeeper.html

      Zookeeper 是 Apacahe Hadoop 的子项目,是一个树型的目录服务,支持变更推送,适合作为 Dubbo 服务的注册中心,工业强度较高,可用于生产环境,并推荐使用

      安装步骤:

      linux原生安装:http://dubbo.apache.org/zh-cn/docs/admin/install/zookeeper.html

      可在虚拟机上快速搭建一个zookeeper:https://blog.csdn.net/chenlu4447/article/details/100626398

                        http://dubbo.apache.org/zh-cn/docs/admin/install/zookeeper.html

    4、Dubbo的管控台

      必须搭建(入职到企业,项目一般都是做好的,这个时候导入企业的项目并且运行成功) 

      企业只关注结果

      小知识:

        node.js专门为前端人员准备的,可以实现接口的语言

        npm:前端的插件的管理工具,可以实现插件的下载和项目的管理;类似于JAVA的maven

      实现步骤:

        1、下载dubbo-admin源码;https://github.com/apache/dubbo-admin

          

        2、IDE工具导入下载的源码;

          

        3、修改admin-servr项目的配置文件;

        

        4、运行admin-Server项目;

        5、下载安装Node;http://nodejs.cn/download/

          安装过程:https://www.runoob.com/nodejs/nodejs-install-setup.html  

        6、cmd切换路径到admin-UI所在的目录;

          

        7、执行插入命令  npm install;若是安装失败,查看:

            https://blog.csdn.net/fredricen/article/details/99293372

        8、运行前端项目  npm run dev;

          

        9、测试

            

      

      如果时间充裕,建议研究一下admin-server源码 阿里巴巴开发的

    5、Dubbo的应用

     

    1、开始创建

      基于Dubbo的HelloWorld

      a、创建根项目

        dubbo_first_project

        继承SpringBoot

      b、创建公共项目

        纯粹的jar  其他项目进行依赖

        Dubbo_Common提供共有的各个类,比如:实体、工具、Vo、业务逻辑类

      黑板:1、基于Dubbo传输的类都必须实现序列化接口

          2、前后端分离项目,后端的返回需要统一

      c、创建服务提供者

        实现服务

        Dubbo_Provider服务提供者,暴露服务  SpringBoot项目,需要单独运行

        比如:实现业务逻辑层的代码、操作数据库等,发布服务

        敲黑板:

        1、SpringBoot项目+dubbo;

        2、需要去实现业务逻辑;

        3、发布服务,借助Dubbo

      d、创建服务消费者

        实现服务的远程调用

      e、运行测试

    三、Dubbo初体验

    1、公共项目

    2、服务提供者

      实现步骤:

      1、依赖jar

        pom.xml文件

    <dependencies>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <!--dubbo-->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>2.7.3</version>
                <exclusions>
                    <exclusion>
                        <artifactId>slf4j-log4j12</artifactId>
                        <groupId>org.slf4j</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
            <!--dubbo依赖-->
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>4.0.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-recipes</artifactId>
                <version>2.8.0</version>
            </dependency>
            <!--zookeeper注册中心-->
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>3.4.13</version>
                <type>pom</type>
                <exclusions>
                    <exclusion>
                        <artifactId>slf4j-log4j12</artifactId>
                        <groupId>org.slf4j</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
            <!--注册中心的客户端-->
            <dependency>
                <groupId>com.101tec</groupId>
                <artifactId>zkclient</artifactId>
                <version>0.10</version>
            </dependency>
            <!--公共项目jar 自定义-->
            <dependency>
                <groupId>com.ge</groupId>
                <artifactId>Dubbo_Common</artifactId>
            </dependency>
        </dependencies>

      2、实现业务逻辑层

        实现之前定义的业务逻辑层的接口

    @Service(version = "1.0.0")
    public class StudentImpl implements StudentService {
        @Override
        public R queryAll(int count) {
    //        模拟数据操作,查询指定数量的数据信息
            List<Student> list = new ArrayList<>();
            for(int i=1;i<=count;i++){
                Student stu = new Student();
                stu.setAddress("北京");
                stu.setName("乔治"+i);
                stu.setAddress("洛杉矶"+i);
                list.add(stu);
            }
            return R.ok(list);
        }
    }

      3、修改开关类(启动类)

        在启动上,开启注解,实现dubbo服务的扫描

    @SpringBootApplication
    @EnableDubbo  //启用Dubbo,就会扫描哪些类拥有@Service注解
    public class ProviderApplication {
        public static void main(String[] args) {
            SpringApplication.run(ProviderApplication.class,args);
        }
    
    }

      4、修改配置文件

        在项目的application.properties添加如下内容:

    #spring项目名
    spring.application.name=dubbo-hello-provider
    server.port=9091
    #指定dubbo项目的名称,在注册中心唯一 dubbo.application.id=dubbo-hello-provider dubbo.application.name=dubbo-hello-provider
    #设置传输协议的名称 dubbo.protocol.id=dubbo dubbo.protocol.name=dubbo
    #设置传输协议的端口号 dubbo.protocol.port=20880
    #设置注册中心的类型,支持多种注册中心 dubbo.registry.protocol=zookeeper
    #注册中心的地址 dubbo.registry.address=zookeeper://192.168.1.101:2181

      5、启动项目

        提供了服务,需要消费者来进行消费。

    3、服务消费者

      实现远程的消费

      Dubbo_Consumer  SpringBoot项目,需要发布运行

      比如:实现服务调用、提供接口、配置Swagger

      敲黑板:

        1、使用提供者的接口;

        2、创建控制器,提供对外的API接口

        3、配置(在线接口文档)

      实现步骤:

      1、依赖jar包

      <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!--swagger的jar-->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.9.2</version>
            </dependency>
            <!--swagger的jar  页面-->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.9.2</version>
            </dependency>
            <!--dubbo-->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>2.7.3</version>
                <exclusions>
                    <exclusion>
                        <artifactId>slf4j-log4j12</artifactId>
                        <groupId>org.slf4j</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
            <!--dubbo依赖-->
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>4.0.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-recipes</artifactId>
                <version>2.8.0</version>
            </dependency>
            <!--zookeeper注册中心-->
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>3.4.13</version>
                <type>pom</type>
                <exclusions>
                    <exclusion>
                        <artifactId>slf4j-log4j12</artifactId>
                        <groupId>org.slf4j</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
            <!--注册中心的客户端-->
            <dependency>
                <groupId>com.101tec</groupId>
                <artifactId>zkclient</artifactId>
                <version>0.10</version>
            </dependency>
            <!--公共项目jar 自定义-->
            <dependency>
                <groupId>com.ge</groupId>
                <artifactId>Dubbo_Common</artifactId>
                <version>0.0.1-SNAPSHOT</version>
                <scope>compile</scope>
            </dependency>
        </dependencies>

      2、编写控制器

        实现对外的接口开发

    @RestController
    public class StudentController {
       // @Autowired这是springIOC的注解,现在用的是dubbo注解;retries:属性,重试次数默认是二
        @Reference(version = "1.0.0",retries = 0)
        private StudentService studentService;
    
        @GetMapping("/api/student/all.do")
        public R all(int count){
            return studentService.queryAll(count);
        }
    }

      3、编写swagger的配置文件类

      4、修改开关类

        启用Swagger

      5、修改配置文件

    #dubbo  configration
    #项目的名称 dubbo.application.name=dubbo-hello-consumer
    #注册中心的名称 dubbo.registry.protocol=zookeeper
    #注册中心的地址 dubbo.registry.address=zookeeper://192.168.1.101:2181 #避免端口冲突 server.port=9092 #jackson的相关配置 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss #时区必须要设置 spring.jackson.time-zone=GMT+8

      6、运行测试

    四、Dubbo项目的梳理

    1、服务提供者

      整个dubbo项目的核心;

      实现作用:

        1、数据库的连接

        2、持久层的操作

        3、逻辑业务层的实现

          注意:这里使用的注解是:@Service(Dubbo不是SpringIOC)指定版本号 version

      配置:

        1、项目名称

        2.传输协议

        3、注册中心地址:

        4、数据库连接

      开关类使用@EnableDoubbo注解完成服务的扫描

    2、服务消费者

      被动  享受  提供者的服务

      实现作用:

      1、完成提供者的服务消费

        注意:使用注解@Reference完成远程接口的实例化  属性:version版本号  retries重试次数(服务重试)

      2、实现对外接口

          控制器

      3、配置在线接口文档Swagger

    整个dubbo项目的调用过程: 

     

     3、提供者整合Mybatis

      实现人员进出登记

      1、数据库

    #创建库
    create DATABASE db_yq;
    use db_yq;
    #创建表  记录人员进村登记
    create table t_person(
            id int PRIMARY key AUTO_INCREMENT,
            name VARCHAR(20),
            address VARCHAR(50),
            stime datetime,
            etime datetime,
            info VARCHAR(50) 
    );

      在配置文件中配置数据库

    #数据库的相关配置
    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/db_yq?characterEncoding=UTF-8&&serverTimezone=UTC
    spring.datasource.username=root
    spring.datasource.password=123456
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.type:com.alibaba.druid.pool.DruidDataSource

      2、公共项目

        完成实体类

    @Data
    public class Person implements Serializable {
        private Integer id;
        private String name;
        private String address;
        private String info;
        private Date stime;
        private Date etime;
    }

        业务逻辑层

    public interface PersonService {
        R save(Person person);
        R queryAll();
    
    }

      3、服务提供者

        持久化:

    public interface PersonDao {
        // 新增
        @Insert("insert into t_person (name,info,address,stime,etime)" +
                "values(#{name},#{info},#{address},now(),null)")
        int insert(Person person);
    
    //    查询
        @Select("select * from t_person order by id desc")
        @ResultType(Person.class)
        List<Person> queryAll();
    }

      发布服务:

    @Component //IOC目的,需要使用Dao层实例
    @Service(version = "1.0.0")
    public class PersonServiceImpl implements PersonService {
        @Autowired
        private PersonDao personDao;
    
        @Override
        public R save(Person person) {
            if (personDao.insert(person) > 0) {
                return R.ok(null);
            } else {
                return R.fail();
            }
        }
    
        @Override
        public R queryAll() {
            return R.ok(personDao.queryAll());
        }
    }

      配置文件加上数据库的配置

      开关类启用Mybatis的扫描

      4、服务消费者

        实现服务的消费,创建控制器

    @Api(value = "控制器操作人",tags = "控制器操作人")
    @RestController
    public class PersonController {
        @Reference(version = "1.0.0",retries = 0,timeout = 10000)
        private PersonService personService;
    
        ///新增
        @PostMapping("api/person/save.do")
        public R save(@RequestBody Person person){
            return personService.save(person);
        }
        //查询全部
        @GetMapping("api/person/queryAll.do")
        public R queryAll(){
            return personService.queryAll();
        }
    }

     五、Dubbo项目常用注解

    1、@Service发布服务--发布到指定的注册中心;

    2、@Reference发现服务--从注册中心查询服务并完成实例化

    3、@EnableDubbo--扫描要发布的服务,用在服务提供者上

    六、Dubbo和SpringCloud的区别   

    Dubbo发布的是接口Service,业务逻辑的实现类;基于hessian,需要序列化

    SpringCloud:发布到是控制器  HTTP+Rest

  • 相关阅读:
    GMA Round 1 数列求单项
    GMA Round 1 双曲线与面积
    多线程环境中安全使用集合API(含代码)
    使用synchronized获取互斥锁的几点说明
    ThreadPoolExecutor线程池
    线程状态转换
    volatile关键字
    守护线程与线程阻塞的四种情况
    线程挂起,恢复与终止
    线程中断
  • 原文地址:https://www.cnblogs.com/Ge-Zsj/p/12727012.html
Copyright © 2011-2022 走看看