首页 > 编程学习 > Spring Boot集成Dubbo

Spring Boot集成Dubbo

发布时间:2022/9/5 17:52:30

SpringBoot集成Dubbo分布式框架项目结构

  1. 接口工程:存放实体bean和业务接口
  2. 服务提供者:业务接口的实现类并将服务暴露且注册到注册中心,调用数据持久层
    • 添加依赖(dubbo、注册中心、接口工程)
    • 配置服务提供者核心配置文件
  3. 服务消费者:处理浏览器客户端发送的请求,从注册中心调用服务提供者所提供的服务
    • 添加依赖(dubbo、注册中心、接口工程)
    • 配置服务消费者核心配置文件

接口工程

package com.dyf.dubbo.service;

/**
 * @author: dyf
 * @date: 2022/9/5 16:44
 * @version: 1.0
 */
public interface StudentService {

    /**
     * 学生总数
     * @return
     */
    int studentTotal();

}

服务提供者

依赖导入

<dependencies>
    <!-- SpringBoot框架web项目起步依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Dubbo集成SpringBoot框架起步依赖 -->
    <dependency>
        <groupId>com.alibaba.spring.boot</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>2.0.0</version>
    </dependency>

    <!-- 注册中心 -->
    <dependency>
        <groupId>com.101tec</groupId>
        <artifactId>zkclient</artifactId>
        <version>0.11</version>
    </dependency>

    <!-- 接口工程 -->
    <dependency>
        <groupId>com.dyf.dubbo</groupId>
        <artifactId>springBoot-dubbo-interface</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>

配置

application.yml

# 设置内嵌Tomcat端口号
server:
  port: 8080
  servlet:
    context-path: /

# 设置dubbo的配置
spring:
  application:
    name: springBoot-dubbo-provider
  dubbo:
    # 当前工程时一个服务提供者
    server: true
    # 设置注册中心
    registry: zookeeper://localhost:2181

接口实现类

package com.dyf.dubbo.service.impl;

import com.dyf.dubbo.service.StudentService;
import org.springframework.stereotype.Service;

/**
 * @author: dyf
 * @date: 2022/9/5 16:47
 * @version: 1.0
 */
@Service
@com.alibaba.dubbo.config.annotation.Service(interfaceClass = StudentService.class,version = "1.0",timeout = 15000)
public class UserServiceImpl implements StudentService {

    @Override
    public int studentTotal() {
        return 250;
    }

}

启动类

@SpringBootApplication    // 开始Spring配置
@EnableDubboConfiguration // 开始dubbo配置
public class SpringBootDubboProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootDubboProviderApplication.class, args);
    }

}

服务消费者

依赖导入

<dependencies>
    <!-- SpringBoot框架web项目起步依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Dubbo集成SpringBoot框架起步依赖 -->
    <dependency>
        <groupId>com.alibaba.spring.boot</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>2.0.0</version>
    </dependency>

    <!-- 注册中心 -->
    <dependency>
        <groupId>com.101tec</groupId>
        <artifactId>zkclient</artifactId>
        <version>0.11</version>
    </dependency>

    <!-- 接口工程 -->
    <dependency>
        <groupId>com.dyf.dubbo</groupId>
        <artifactId>springBoot-dubbo-interface</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>

配置

application.yml

# 设置Tomcat
server:
  port: 8081
  servlet:
    context-path: /

# 设置dubbo的配置
spring:
  application:
    name: springBoot-dubbo-consumer
  dubbo:
    # 设置注册中心
    registry: zookeeper://localhost:2181

Controller

@RestController
public class StudentController {

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

    @GetMapping("/student/total/{school}")
    public String studentTotal(@PathVariable("school") String school) {
        int studentTotal = studentService.studentTotal();
        return school + "学生总数为:" + studentTotal;
    }

}

启动类

@SpringBootApplication    // 开始Spring配置
@EnableDubboConfiguration // 开始dubbo配置
public class SpringBootDubboConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootDubboConsumerApplication.class, args);
    }

}

测试

启动zookeeper

启动springBoot-dubbo-provider

启动springBoot-dubbo-consumer

输入网址localhost:8081/student/total/胜利队

Copyright © 2010-2022 mfbz.cn 版权所有 |关于我们| 联系方式|豫ICP备15888888号