首页 > 其他分享 >Spring Boot集成Dubbo

Spring Boot集成Dubbo

时间:2022-09-05 17:58:17浏览次数:54  
标签:dubbo Dubbo spring boot Boot class Spring com public

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

标签:dubbo,Dubbo,spring,boot,Boot,class,Spring,com,public
From: https://www.cnblogs.com/duya12345/p/16659013.html

相关文章

  • 八、Spring Boot 中自定义 SpringMVC 配置
    转发:https://www.javaboy.org/2019/0816/spring-boot-springmvc.html先说结论,使用Java8的,自定义配置使用实现WebMvcConfigurer接口,Java8之前使用WebMvcConfigurerAdapte......
  • 【博学谷学习记录】超强总结,用心分享|狂野架构师SpringCloud-Gateway
    目录SpringCloudGateway的工作流程Gateway动态路由配置中基于Path的路由方式过滤器分类常见默认过滤器添加响应头前缀处理增加前缀自定义过滤器1、实现GatewayFilter接口2......
  • (Spring)文件上传和下载
    文件上传的时候,浏览器将图片以MultipartFile的形式传到服务器,服务器将保存完的图片名响应给浏览器。文件下载的时候,浏览器收到图片名,再向服务器请求图片资源,服务器以流的......
  • SpringBoot-整合Druid
    1.添加jar包<dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.8</version></dependency>......
  • 14.Springboot多环境配置2
    1.主配置文件application.ymlspring:profiles:active:@profile.active@#需要在pom文件中指定变量#active:pro#include:mvcgroup:"pro......
  • Spring Cloud Security OAuth2.0学习
    一、OAuth2授权机制1.1为什么需要OAuth2授权参考OAuth2.0的一个简单解释-阮一峰的网络日志(ruanyifeng.com)1.2OAuth2四种授权方式参考OAuth2.0的四种方式-......
  • Dubbo-Adaptive实现原理
    前言前面我们已经分析DubboSPI相关的源码,看过的小伙伴相信已经知晓整个加载过程,我们也留下两个问题,今天我们先来处理下其中关于注解Adaptive的原理。什么是@Adaptive对......
  • Spring事务
    1.事务的四大特性(ACID)●原子性(Atomicity)共生死,要么全部成功,要么全部失败!●一致性(Consistency)事务在执行前后,数据库中数据要保持一致性状态。(如转账的过程账户......
  • Dubbo 线程池占满可能原因分析
    可能原因:(1)dubbo中有httpClient调用。由于http工具默认仅支持5个并发,且有线程池队列,当请求量超过5个的时候,多余的请求会在队列中堆积。前一批http请求结束之后其他......
  • springboot聚合项目搭建
    springboot聚合项目搭建1、简介1.1、什么是聚合项目?一个项目中包含多个子项目的项目。结构:|-父模块---|子模块1---|子模块2---|子模块31.2、聚合项目有什么......