Spring Boot集成Dubbo

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

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

Spring Boot集成Dubbo_第1张图片

接口工程

package com.dyf.dubbo.service;

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

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

}

服务提供者

依赖导入

<dependencies>
    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
    dependency>

    
    <dependency>
        <groupId>com.alibaba.spring.bootgroupId>
        <artifactId>dubbo-spring-boot-starterartifactId>
        <version>2.0.0version>
    dependency>

    
    <dependency>
        <groupId>com.101tecgroupId>
        <artifactId>zkclientartifactId>
        <version>0.11version>
    dependency>

    
    <dependency>
        <groupId>com.dyf.dubbogroupId>
        <artifactId>springBoot-dubbo-interfaceartifactId>
        <version>1.0-SNAPSHOTversion>
    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>
    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
    dependency>

    
    <dependency>
        <groupId>com.alibaba.spring.bootgroupId>
        <artifactId>dubbo-spring-boot-starterartifactId>
        <version>2.0.0version>
    dependency>

    
    <dependency>
        <groupId>com.101tecgroupId>
        <artifactId>zkclientartifactId>
        <version>0.11version>
    dependency>

    
    <dependency>
        <groupId>com.dyf.dubbogroupId>
        <artifactId>springBoot-dubbo-interfaceartifactId>
        <version>1.0-SNAPSHOTversion>
    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/胜利队

你可能感兴趣的:(SpringBoot,Dubbo,dubbo,spring,boot,java)