栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

SpringBoot (七) --------- SpringBoot 集成 Dubbo(springboot默认访问页面)

SpringBoot (七) --------- SpringBoot 集成 Dubbo(springboot默认访问页面)

目录

一、创建接口 module二、创建服务提供者 module三、创建消费者 module四、测试应用


阿里巴巴提供了 dubbo 集成 springBoot 开源项目,可以到 GitHub 上 https://github.com/apache/dubbo-spring-boot-project 查看入门教程


下载 为 ZIP 压缩包,在本地打开即可

一、创建接口 module

按照 Dubbo 官方开发建议,创建一个接口项目,该项目只定义接口和 model 类。

项目名称 :interface-api

此项目就是一个普通的 maven 项目

新建maven模块

我们在空项目中以新建模块的形式创建接口服务,便于项目管理

设置 gav

使用本地配置与仓库

创建 model 类

package com.fancy.model;

import java.io.Serializable;

public class Student implements Serializable {
    private Integer id;
    private String name;
    private Integer age;

    public Student() {
    }

    public Student(Integer id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
      @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", age=" + age +
                '}';
    }
}

服务接口

package com.fancy.service;

import com.fancy.model.Student;

public interface StudentService {
    Student queryStudent(Integer studentId);
}

打包

点击 install 即可将项目打包成 jar 发布到本地仓库


出现如下界面打包成功

二、创建服务提供者 module

实现 api 项目中的接口

项目名称:service-prvoider

使用 SpringBoot Initializer 创建项目, 不用选择依赖

新建项目


不用选择依赖

pom.xml

在 pom.xml 中添加 Dubbo 和 Zookeeper 依赖


    com.fancy
    interface-api
    1.0.0



    org.apache.dubbo
    dubbo-spring-boot-starter
    2.7.8



    org.apache.dubbo
    dubbo-dependencies-zookeeper
    2.7.8
    pom

application.properties

创建接口的实现类

@DubboService 注解用于暴露服务,供消费者消费

package com.example.serviceprovider.service;

import com.fancy.model.Student;
import com.fancy.service.StudentService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

@Component
@DubboService(interfaceClass = StudentService.class, version = "1.0")
public class StudentServiceImpl implements StudentService {

   @Override
   public Student queryStudent(Integer studentId) {
       Student student = new Student();
       student.setId(studentId);
       student.setName("张三");
       student.setAge(20);
       return student;
   }
}

主启动类型 ServiceProviderApplication

我们运行主启动类时,在上方加入 @EnableDubbo 注解,我们可以使用这个注解来开启 Dubbo 服务,使用基于注解的 Dubbo 的 RPC 调用。

注意 : 在运行主启动类之前,我们要打开 Zookeeper 否则连接不上



出现下图,则连接 Zookeeper 成功

不过在其启动时我们发现日志报红,这是因为日志依赖 SLF4j 多次加入了,只需要依赖一次就可以。

解决 :排除多余的 SLF4j 依赖, 提供者和消费者项目都需要这样做。

三、创建消费者 module

项目名称:consumer
使用 Spring Boot Initializer 创建项目, 选择 web 依赖

创建项目

选择 web 依赖

pom.xml


   com.fancy
   interface-api
   1.0.0



   org.apache.dubbo
   dubbo-spring-boot-starter
   2.7.8


   org.apache.dubbo
   dubbo-dependencies-zookeeper
   2.7.8
   pom
   
       
           slf4j-log4j12
           org.slf4j
       
   

application.properties

创建 controller 类

@DubboReference 注解将创建好的代理对象,注入给 studentService

package com.example.customer.controller;

import com.fancy.model.Student;
import com.fancy.service.StudentService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @DubboReference(interfaceClass = StudentService.class, version = "1.0", check = false)
    private  StudentService studentService;



    @GetMapping("/student/{id}")
    public String searchStudent(@PathVariable Integer id) {
        Student student = studentService.queryStudent(id);
        return "查询学生 : " + student.toString();

    }

}

主启动类 CustomerApplication

在主启动类上加上 @EnableDubbo 注解

四、测试应用

① 先启动 zookeeper

② 运行服务提供者 service-provider

③ 运行消费者 consumer

④ 在浏览器执行 http://localhost:8080/myweb/student/id=1

模拟发送请求即可


集成 dubbo 成功

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/771487.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号