Spring Cloud Alibaba学习(九):Dubbo集成

上一篇:Spring Cloud Alibaba学习(八):配置中心

Dubbo作为SpringCloud生态中服务治理的一部分,同样需要有服务提供者和服务消费者。

由于服务提供者和服务消费者都要实现共同的接口,所以可以将接口单独作为一个模块,因此建立三个子模块:dubbo-api,dubbo-provider,dubbo-consumer。右键父模块,创建maven模块,再将子模块加入父模块pom.xml的<module>即可

dubbo-api

只做接口的定义,不需要引入依赖

接口

public interface DubboApiService {
    String helloDubboService();
}

目录结构

dubbo-provider

pom.xml

引入两个依赖:

  • spring-cloud-starter-dubbo:dubbo的依赖
  • dubbo-api:我们自己建立的公共接口模块
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud-alibaba-icydate</artifactId>
        <groupId>com.icydate</groupId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-provider</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- Spring Boot Begin -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Spring Boot End -->

        <!-- Spring Cloud Begin -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <!-- Dubbo Spring Cloud Starter -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
        </dependency>

        <!-- Spring Cloud End -->
        
        <!-- 引入公共接口的模块 -->
        <dependency>
            <groupId>com.icydate</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.DubboConsumerApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

application.yml

spring:
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
  application:
    name: icydate-dubbo-provider
server:
  port: 8084
dubbo:
  protocol:
    # 协议名称
    name: dubbo
    # dubbo协议缺省端口为20880,rmi协议缺省端口为1099,http和hessian协议缺省端口为80;如果没有配置port,则自动采用默认端口,如果配置为-1,则会分配一个没有被占用的端口
    # Dubbo 2.4.0+,分配的端口在协议缺省端口的基础上增长,确保端口段可控
    port: -1
  registry:
    # 前缀spring-cloud说明:挂载到 Spring Cloud注册中心
    address: spring-cloud://localhost:8848

 SpringBoot启动类

@DubboComponentScan:扫描指定包下作为dubbo服务的类

@SpringBootApplication
@EnableDiscoveryClient
@DubboComponentScan(value = {"com.icydate.service"})
public class DubboProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(DubboProviderApplication.class, args);
    }
}

服务类

@DubboService:标注此类为dubbo服务

@DubboService
public class DubboProviderService implements DubboApiService {
    
    // 实现公共接口,内容为具体的服务业务逻辑
    @Override
    public String helloDubboService() {
        return "provider dubbo";
    }
}

目录结构

dubbo-consumer

pom.xml

引入与dubbo-provider相同的依赖

application.yml

spring:
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
  application:
    name: icydate-dubbo-consumer
server:
  port: 8085
dubbo:
  protocol:
    name: dubbo
    port: -1
  registry:
    address: spring-cloud://localhost:8848
  cloud:
    # 要订阅的服务
    subscribed-services: icydate-dubbo-provider

SpringBoot启动类

@SpringBootApplication
@EnableDiscoveryClient
public class DubboConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(DubboConsumerApplication.class, args);
    }
}

 Controller接口

@DubboReference:指定要消费的服务接口

@RestController
public class DubboConsumerController {

    @DubboReference
    DubboApiService dubboApiService;

    @GetMapping("/call/dubbo")
    public String callDubboProvider() {
        return dubboApiService.helloDubboService();
    }
}

目录结构

 验证

分别启动nacos服务,dubbo-provider,dubbo-consumer模块

可以看到nacos中已有服务列表

调用dubbo-consumer提供的接口,发现能够成功调用到dubbo-provider的返回

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值