分布式RPC系统框架Dubbo

Posted 陈彦斌

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了分布式RPC系统框架Dubbo相关的知识,希望对你有一定的参考价值。

导读

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

dubbo官网:点我直达

第一个Dubbo程序(小试牛刀)

创建业务接口工程

项目结构

创建包和接口类

安装项目

创建提供者Provider工程

项目结构

pom.xml

<?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>org.cyb</groupId>
    <artifactId>02-first-provider</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--编译器依赖-->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>13</maven.compiler.source>
        <maven.compiler.target>13</maven.compiler.target>
        <!--自定义版本号-->
        <spring-version>4.3.16.RELEASE</spring-version>
    </properties>
    <dependencies>
        <!--自定义工程依赖-->
        <dependency>
            <groupId>org.cyb</groupId>
            <artifactId>01-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--dubbo依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.7</version>
        </dependency>
        <!--netty-->
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.32.Final</version>
        </dependency>


        <!--spring依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <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-expression</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-aspects</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-jdbc</artifactId>
            <version>${spring-version}</version>
        </dependency>
    </dependencies>
</project>

注意

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://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="02-first-provider"/>
    <!--注册Service,将来服务的提供者-->
    <bean id="someService" class="com.cyb.service.SomeServiceImpl"></bean>
    <!--暴露服务,采用直连的方式-->
    <dubbo:service interface="com.cyb.service.SomeService" ref="someService" registry="N/A"></dubbo:service>
</beans>

SomeServiceImpl.java

package com.cyb.service;

public class SomeServiceImpl implements SomeService{
    @Override
    public String hello(String name) {
        System.out.println("Dubbo World Welcome You"+name);
        return "chenyanbin";
    }
}

RunProvider.java

package com.cyb.run;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class RunProvider {
    public static void main(String[] args) throws IOException {
        //创建spring容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring-dubbo-provider.xml");
        //启动spring容器
        ((ClassPathXmlApplicationContext) ac).start();
        //将当前主线程阻塞
        System.in.read();
    }
}

创建消费者Consumer工程

项目结构图

pom.xml

<?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>org.cyb</groupId>
    <artifactId>02-first-consumer</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--编译器依赖-->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>13</maven.compiler.source>
        <maven.compiler.target>13</maven.compiler.target>
        <!--自定义版本号-->
        <spring-version>4.3.16.RELEASE</spring-version>
    </properties>

    <dependencies>
        <!--自定义工程依赖-->
        <dependency>
            <groupId>org.cyb</groupId>
            <artifactId>01-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--dubbo依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.7</version>
        </dependency>
        <!--netty-->
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.32.Final</version>
        </dependency>
        <!--spring依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <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-expression</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-aspects</artifactId>
            <version>${spring-version}</version>
        </dependency>
    </dependencies>
</project>

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://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="02-first-consumer"/>
    <!--消费引用-->
    <dubbo:reference id="someSerivce" interface="com.cyb.service.SomeService" url="dubbo://localhost:20880"></dubbo:reference>
</beans>

注:端口号20880固定

RunConsumer.java

package com.cyb.run;

import com.cyb.service.SomeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class RunConsumer {
    public static void main(String[] args) {
        //创建Spring容器
        ApplicationContext ac=new ClassPathXmlApplicationContext("spring-dubbo-consumer.xml");
        SomeService service = (SomeService) ac.getBean("someSerivce");
        service.hello("tom");
    }
}

运行

Zookeeper注册中心&Zookeeper集群(Dubbo官网推荐)

拷贝生产者和消费者工程

注册中心其他方式

地址:http://dubbo.apache.org/zh-cn/docs/user/references/registry/introduction.html

Zookeeper工程搭建

配置参考地址:点我直达

启动zookeeper服务

修改提供者工程

pom.xml添加curator客户端依赖 

        <!-- zk客户端依赖:curator-framework-->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.0.1</version>
        </dependency>

注意事项

修改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://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="03-provider-zk"/>
    <!--注册Service,将来服务的提供者-->
    <bean id="someService" class="com.cyb.service.SomeServiceImpl"></bean>

    <!-- 声明zk服务中心 -->
    <!-- 单机版 -->
    <!-- 方式一 -->
    <dubbo:registry address="zookeeper://192.168.1.111:2181"/>
    <!-- 方式二 -->
    <!-- <dubbo:registry protocol="zookeeper" address=""/> -->

    <!-- 集群配置 -->
    <!-- 方式一 -->
    <!-- <dubbo:registry address="zookeeper://10.20.153.10:2181?backup=10.20.153.11:2181,10.20.153.12:2181" /> -->
    <!-- 方式二 -->
    <!-- <dubbo:registry protocol="zookeeper" address="10.20.153.10:2181,10.20.153.11:2181,10.20.153.12:2181" /> -->

    <!-- 同一Zookeeper,分成多组注册中心 -->
    <!-- <dubbo:registry id="chinaRegistry" protocol="zookeeper" address="10.20.153.10:2181" group="china" />
    <dubbo:registry id="intlRegistry" protocol="zookeeper" address="10.20.153.10:2181" group="intl" /> -->

    <!-- 暴露服务,将服务暴露给zk服务中心 -->
<!-- <dubbo:service interface="com.cyb.service.SomeService" ref="someService" registry="N/A"></dubbo:service> -->
    <dubbo:service interface="com.cyb.service.SomeService" ref="someService"></dubbo:service>

</beans>

修改消费者工程

pom.xml添加curator客户端依赖

        <!-- zk客户端依赖:curator-framework-->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.0.1</version>
        </dependency>

修改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://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="03-consumer-zk"/>

    <!-- 声明zk服务中心 -->
    <!-- 单机版 -->
    <dubbo:registry address="zookeeper://192.168.1.111:2181"/>

    <!--消费引用-->
    <!-- <dubbo:reference id="someSerivce" interface="com.cyb.service.SomeService" url="dubbo://localhost:20880"></dubbo:reference> -->
    <dubbo:reference id="someSerivce" interface="com.cyb.service.SomeService"></dubbo:reference>
</beans分布式RPC系统框架-Dubbo(2.7)教程,分布式dubbo教程下载百度云

分布式RPC系统框架Dubbo

分布式 RPC 系统框架 Dubbo入门介绍

分布式系统如何自己设计一个类似 Dubbo 的 RPC 框架?什么是 CAP 定理?分区容错性?

分布式RPC框架Apache Dubbo

解密Dubbo:自己动手编写一个较为完善的RPC框架(两万字干货)