dubbo入门详细教程

486 阅读2分钟

日期:2022年3月10日

出处:

黑马程序员Dubbo快速入门,Java分布式框架必会的dubbo教程

www.bilibili.com/video/BV1VE…

正文


基本概念:

dubbo是一个Java RPC框架

RPC是远程过程调用(Remote Procedure Call)

官网:dubbo.apache.org/zh/

快速入门

目录结构

reason:

\

实现步骤:

1.创建服务提供者Provider模块

以下是该模块的核心配置文件

<?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.xsd
        http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">


<!--	spring包扫描-->
<!--	<context:component-scan base-package="com.itheima.service" />-->

<!--	dubbo的配置-->
<!--	1.配置项目的名称,唯一-->
	<dubbo:application name="dubbo_service"/>
<!--	2.配置注册中心的地址-->
	<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!--	3.配置dubbo的包扫描-->
	<dubbo:annotation package="com.itheima.service.impl" />

</beans>

2.创建服务消费者Consumer模块

以下是该模块的核心配置文件

<?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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
         http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">


<!--    打开注解驱动-->
    <mvc:annotation-driven/>
<!--    包扫描-->
    <context:component-scan base-package="com.itheima.controller"/>

    <!--	dubbo的配置-->
    <!--	1.配置项目的名称,唯一-->
    <dubbo:application name="dubbo_web">
        <dubbo:parameter key="qos.port" value="33333"/>
    </dubbo:application>
    <!--	2.配置注册中心的地址-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
    <!--	3.配置dubbo的包扫描-->
    <dubbo:annotation package="com.itheima.controller" />
</beans>

3.在服务提供者模块编写UserServiceImpl提供服务

package com.itheima.service.impl;

import com.itheima.service.UserService;
import org.apache.dubbo.config.annotation.Service;

//@Service//将该类的对象创建出来,放到spring的ioc容器(控制反转)中 bean的定义
@Service//将这个类提供的方法(服务)对外发布 。将访问的地址,ip端口,路径注册到注册中心
public class UserServiceImpl implements UserService {

    public String sayHello() {
        return "hello dubbo!!! 2";
    }
}

4.在服务消费者中的UserController远程调用UserServiceImpl提供服务

package com.itheima.controller;

import com.itheima.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    //注入Service
//    @Autowired//本地注入
    /*远程注入功能
     * 1.从注册中心获取userService的访问url
     * 2.进行远程调用RPC
     * 3.将结果封装为一个代理对象。给变量赋值*/
    @Reference//远程注入
    private UserService userService;

    @RequestMapping("/sayHello")
    public String sayHello(){
        return userService.sayHello();
    }
}

5.分别启动两个服务,测试

注意:1.在测试之前需要install

2.测试前还要先将zookeeper启动起来

本节课新学习的知识点

<!--	配置dubbo的包扫描-->
	<dubbo:annotation package="包路径" />
import org.apache.dubbo.config.annotation.Service;

//将这个类提供的方法(服务)对外发布 。将访问的地址,ip端口,路径注册到注册中心
@Service
import org.apache.dubbo.config.annotation.Reference;
@Reference//远程注入
    /*远程注入功能
     * 1.从注册中心获取userService的访问url
     * 2.进行远程调用RPC
     * 3.将结果封装为一个代理对象。给变量赋值*/

遇到的问题及解决方案

解决方案:www.cnblogs.com/peng-28/p/1…

总结


今天学习了dubbo的相关知识内容,这套视频是通过基础的SSM过度到dubbo框架的,理解了dubbo的特点即每一个服务都可以不依赖其他服务单独启动,基本的细节知识点已经完全掌握,但是以现在的能力暂时还没有办法自己独立完成框架的搭建,这说明对于dubbo框架的流程还是不熟练,仍需要多加练习。