Dubbo - Dubbo框架搭建

版权声明:写文章辛苦,请不要复制粘贴,如果要,请注明来处 https://blog.csdn.net/u012627861/article/details/82972559

简单说明

Dubbo是一个基于RPC协议的分布式框架,RPC的全称为Remote Procedure Call,其目的为实现远程调用。而分布式的目的是为了能够更好的针对不同的业务进行横向扩展,从而使得程序稳定高可用,响应速度快。所以一个基于RPC协议的分布式框架,是一个非常不错的Idea。Dubbo使用RPC协议而不用HTTP协议,我们可以在百度百科上了解到,RPC协议跨了OSI架构的应用层和传输层,传输速度步骤简化所以速度会更快。不得不说Dubbo对技术起点的选择真的很棒!

基础概念

  1. 在Dubbo中,提供服务的应用叫做提供者provider。消费服务的应用叫做消费者consumer。当一个provider需要调用别的provider提供的服务,那么这个provider既是提供者也是消费者
  2. 所有的分布式服务,都会将服务暴露出去,所谓暴露就是提供接口让别的应用调用。常见的Http接口提供方实际上也可以认为是一个分布式应用。
  3. Dubbo中的服务暴露或者说服务注册,需要依赖第三方的注册中心,如ZooKeeper。provider将服务注册到zk中,consumer在zk中查找并调用服务。但服务最终是在提供者所在服务器执行

一、安装ZooKeeper

一下载,二配置,三安装。详情请参阅《Dubbo - ZooKeeper的安装和基本使用》

二、搭建提供者项目

搭建提供者项目基本思路如下:

  1. 搭建一个Maven多模块工程,至少需要分成三个子工程,一个用于暴露,一个用于实现,一个用于启动。
  2. 添加jar依赖
  3. 添加dubbo配置文件
  4. 增加Dubbo提供者启动类

四个步骤即可完成提供者的搭建。

  1. 搭建一个Maven多模块工程
    在这里插入图片描述
    我们假设为用户模块单独搭建一个服务,那么得到以上用户模块服务提供者项目。可以看出这是一个Maven多模块项目。现在一一解释:
  • dubbo-user-service工程为父工程

  • user-servce工程为暴露工程,负责定义对外暴露的接口和对象

  • user-service-impl工程为接口实现工程,负责对接口的实现

  • user-startup工程为启动工程,它将加载上下文并启动Dubbo服务

    这些工程中我简单说明以下几点:

    1. user-service, user-service-impl, user-startup工程的parent都是dubbo-user-service。
    2. user-service-impl依赖了user-serivce
    3. user-startup依赖了user-service-impl

消费者只需要引入user-service即可对用户模块dubbo服务的调用。

  1. 添加jar依赖
    当我们搭建好一个这样的多模块项目后,我们需要引入依赖的jar包
  • 在dubbo-user-service/pom.xml中增加
<properties>
	<dubbo.version>2.6.3</dubbo.version>
</properties>

在parent工程中定义dubbo的版本,便于管理包版本。这里采用dubbo-2.6.3

  • 在user-service-impl/pom.xml中增加
<dependencies>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context</artifactId>
		<version>4.3.6.RELEASE</version>
		<scope>provided</scope>
	</dependency>
	<dependency>
		<groupId>com.chinack</groupId>
		<artifactId>user-service</artifactId>
		<version>1.0-SNAPSHOT</version>
	</dependency>
</dependencies>

增加spring-context包是为了在实现类中使用@Service注解来声明service组件。可以看到spring-context的scope为provided,因为该包在user-startup中已经引入。

  • 在user-startup/pom.xml中增加
<dependencies>
	<!-- Dubbo -->
	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>dubbo</artifactId>
		<version>${dubbo.version}</version>
	</dependency>
	<!-- ZooKeeper -->
	<dependency>
		<groupId>org.apache.zookeeper</groupId>
		<artifactId>zookeeper</artifactId>
		<version>3.4.13</version>
	</dependency>
	<!-- 依赖包,在启动时依赖org/apache/curator/RetryPolicy类 -->
	<dependency>
		<groupId>org.apache.curator</groupId>
		<artifactId>curator-framework</artifactId>
		<version>4.0.1</version>
	</dependency>
	<!-- 引入接口实现工程 -->
	<dependency>
		<groupId>com.chinack</groupId>
		<artifactId>user-service-impl</artifactId>
		<version>${project.version}</version>
	</dependency>
</dependencies>

搭建Dubbo提供者最重要的三个包就在这里

引入Dubbo依赖包,版本为2.6.3(版本在dubbo-user-service/pom.xml>中定义);
引入zk包用于将服务注册到zk中;
引入了curator依赖包。

最后引入了我们的接口实现工程。之前有说过user-startup依赖了spring的包,那是因为Dubbo本身就依赖了它所需要的spring包
这样,所有的jar包依赖都做好了。

  1. 增加配置文件
    user-startup/src/main/resources中增加spring.xmluser-dubbo-provider.xml配置文件,如下:
  • user-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="user"/>

    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <!-- 用dubbo协议在20880端口暴露服务,以下为默认配置 -->
    <!-- 每个dubbo提供者应用启动后实则启动了一个nettyserver,这里的端口则是nettyserver端口 -->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!-- 声明需要暴露的服务接口,interface表示接口地址,ref为接口名称(必须) -->
    <dubbo:service interface="com.chinack.dubbo.user.service.UserService" ref="userService" />
</beans>
  • spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <context:component-scan base-package="com.chinack.dubbo" />
    <import resource="classpath:user-dubbo-provider.xml" />
</beans>
  1. 增加Dubbo提供者启动类
    我们在user-startup工程中构建一个Application类用于启动,代码如下:
public class Application {

    public static void main(String[] args){
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring.xml");
        applicationContext.start();
        System.out.println("User provider started");
        try {
        	// 为了防止上下文加载后自动关闭,通过接收用户输入流来阻止。
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

到这里,服务提供者就算搭建完成了。我们可以编写一个服务如UserService,然后搭建一个Dubbo Admin用于查看是否成功注册到本地zk中,如下图:
在这里插入图片描述

三、搭建消费者项目

搭建消费者基本思路:

  1. 搭建一个web项目
  2. 添加jar依赖
  3. 添加消费者配置

三个步骤即可完成消费者项目的搭建,这里我以Spring Boot为例

  1. 搭建一个Spring Boot项目(略)
  2. 添加jar依赖
<properties>
	<dubbo.version>2.6.3</dubbo.version>
</properties>
<dependencies>
	<!-- Spring Boot -->
	<!-- 略 -->
	<!-- Dubbo -->
	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>dubbo</artifactId>
		<version>${dubbo.version}</version>
	</dependency>
	<!-- 依赖包,在启动时依赖org/apache/curator/RetryPolicy类 -->
	<dependency>
		<groupId>org.apache.curator</groupId>
		<artifactId>curator-framework</artifactId>
		<version>4.0.1</version>
	</dependency>
	<!-- 引入User服务 -->
	<dependency>
		<groupId>com.chinack</groupId>
		<artifactId>user-service</artifactId>
		<version>1.0-SNAPSHOT</version>
	</dependency>
</dependencies>
  1. 添加消费者配置
    在resources下增加配置文件dubbo-consumer.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       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应用信息 -->
    <dubbo:application name="consumer"/>

    <!-- 配置注册中心,从注册中心发现服务,这里的注册中心为zookeeper -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <!-- 声明服务消费,声明后该对象则可以通过Dubbo进行远程调用该类提供的方法 -->
    <dubbo:reference id="userService" interface="com.chinack.dubbo.user.service.UserService" />
</beans>

增加配置类,加载dubbo-consumer.xml文件

package com.chinack.consumer.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

/**
 * 加载Dubbo消费者配置
 * @author Chinack
 * @date 2018/10/8 16:53
 */
@Configuration
@ImportResource("classpath:dubbo-consumer.xml")
public class DubboConsumerConfig {

}

这样,消费者项目就搭建完成了。我们可以尝试创建Controller调用提供者的service。注意:默认情况下消费者会对提供者进行检查,如果提供者未启动,则会出现Failed to check the status of the service xxx。成功启动并调用到了user-service提供的用户服务后得到下图:
在这里插入图片描述

具体内容可以看源码,我把源码放在了gitee上。
源码地址:https://gitee.com/ck-tech/dubbo

(完)

猜你喜欢

转载自blog.csdn.net/u012627861/article/details/82972559