SpringCloud Alibaba集成Dubbo实现远程服务间调用

dubbo远程调用 dubbo远程调用springcloud接口_dubbo

工程创建

一、创建springBoot分模块项目,父工程:springcloud-alibaba以及子模块product-dubbo-provider、order-dubbo-consumer等

项目基本结构图如下所示:

dubbo远程调用 dubbo远程调用springcloud接口_spring cloud_02

二、依赖引入

在以上两个子模块的pom.xml文件中分别引入如下依赖:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
        </dependency>
        <!--模板引擎依赖,即使不需要生成模板,也需要引入-->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.2</version>
        </dependency>
        <!--mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--Spring Cloud Alibaba-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>0.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
        </dependency>
        <!--Dubbo-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
        </dependency>
        <!--对api的依赖-->
        <dependency>
            <groupId>com.springcloud.xxkfz</groupId>
            <artifactId>dubbo-base</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

服务生产者

一、在编写生产者服务代码之前,我们首先在dubbo-base定义一个RPC接口queryList供消费者调用。那么这个接口在哪里去实现呢?

package com.simplememory.xxkfz.service;

import java.util.List;

/**
 * @version 1.0.0
 * @ClassName ConsumerImpl.java
 * @Description TODO
 * @createTime 2023年02月18日 20:41:00
 */
public interface IProviderService {

	List<String> queryList();
}

二、在product-dubbo-provider模块中创建实现类ProviderServiceImpl.java实现dubbo-base声明的queryList接口。其中@Service是Dubbo提供的注解,表示当前服务会发布成一个远程服务,不要和Spring提供的搞混哦。

package com.simplememory.xxkfz.service;

import org.apache.dubbo.config.annotation.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @version 1.0.0
 * @ClassName ProviderServiceImpl.java
 * @Description TODO
 * @createTime 2023年02月18日 20:44:00
 */
@Service
public class ProviderServiceImpl implements IProviderService {


	@Override
	public List<String> queryList() {
		return Arrays.asList
	}
}

三、在配置文件application.yml中配置数据源信息、dubbo相关配置等;完整配置信息如下:

# 数据源配置
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/xk_cloud?characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=GMT%2B8
    username: root
    password: root
  application:
    name: provider-service
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
server:
  port: 8050
# dubbo相关配置
dubbo:
  scan:
    # dubbo服务实现类的扫描基准包路径
    base-packages: com.simplememory.xxkfz.service
    #Dubbo服务暴露的协议配置
  protocol:
    name: dubbo
    port: 1

服务消费者

编写基本的相关代码结构

一、创建ConsumerController.java,提供查询列表数据的接口。

package com.simplememory.xxkfz.controller;

import com.simplememory.xxkfz.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @version 1.0.0
 * @ClassName ConsumerImpl.java
 * @Description TODO
 * @createTime 2023年02月18日 20:41:00
 */
@RestController
@RequestMapping("/dubbo/consumer")
public class ConsumerController {

	@Autowired
	private ConsumerService consumerService;

	@GetMapping
	public List<String> list() {
		return consumerService.list();
	}
}

二、创建ConsumerService.java

package com.simplememory.xxkfz.service;

import java.util.List;

/**
 * @version 1.0.0
 * @ClassName ConsumerService.java
 * @Description TODO
 * @createTime 2023年02月18日 20:41:00
 */
public interface ConsumerService {


	List<String> list();

}

三、创建实现类ConsumerServiceImpl.java,并使用使用@Reference注解注入相应的service,就可以像调用本地jar包一样,调用远程服务。

package com.simplememory.xxkfz.service.impl;

import com.simplememory.xxkfz.service.ConsumerService;
import com.simplememory.xxkfz.service.IProviderService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @version 1.0.0
 * @ClassName ConsumerImpl.java
 * @Description TODO
 * @createTime 2023年02月18日 20:41:00
 */
@Service
public class ConsumerServiceImpl implements ConsumerService {

	@Reference
	private IProviderService providerService;

	@Override
	public List<String> list() {
		return providerService.queryList();
	}
}

四、在配置文件application.yml中配置基本信息,主要配置了要订阅的服务名;完整配置信息如下:

# 数据源配置
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/xk_cloud?characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=GMT%2B8
    username: root
    password: root
  application:
    name: consumer-service
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
server:
  port: 8020
#dubbo配置
#要订阅的服务名,多个用,隔开
dubbo:
  cloud:
    subscribed-services: provider-service

测试

  • 启动nacos注册中心。
  • nacos注册中心启动完成后,依次启动DubboProviderMainApplication,DubboConsumerMainApplication生产者、消费者服务,可以看到Nacos服务列表里有两个服务。

dubbo远程调用 dubbo远程调用springcloud接口_dubbo_03

  • 在浏览器地址栏访问:http://127.0.0.1:8020/dubbo/consumer

dubbo远程调用 dubbo远程调用springcloud接口_dubbo远程调用_04