Spring Boot集成Dubbo

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/胜利队

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值