nacos + dubbo + springboot 3.x搭建微服务调用环境

1. 环境准备

JDK17+
Maven 或 Gradle(用于构建项目)
nacos server (安装步骤 windowsdocker desktop

2. 创建 Spring Boot 项目

2.1. 创建空的父类项目,更改项目打包方式为pom,引入相关依赖

在这里插入图片描述

......
<packaging>pom</packaging>

 <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>3.2.10</version>
            </dependency>

            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-registry-nacos</artifactId>
                <version>3.2.10</version>
            </dependency>
        </dependencies>
    </dependencyManagement>       
2.2. 创建一个服务提供方和消费方都依赖的api模块。新建一个空的子项目,增加service

在这里插入图片描述

public interface DubboExampleService{

    String sayHello(String name);
}

2.3. 创建服务提供方项目

更新配置文件, 引入相关依赖

server:
  port: 8080

spring:
  application:
    name: dubbo-example-provider

dubbo:
  application:
    name: ${spring.application.name}
  protocol:
    name: dubbo # 通信协议
    port: -1 # dubbo协议端口
  registry:
    address: nacos://localhost:8848
        <dependency>
            <groupId>cn.myth</groupId>
            <artifactId>dubbo-example-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-nacos</artifactId>
        </dependency>

启动类增加EnableDubbo注解
在这里插入图片描述
实现api模块中的service
在这里插入图片描述

2.4. 创建消费者模块

更新配置文件,增加依赖

server:
  port: 8082

spring:
  application:
    name: dubbo-example-consumer

dubbo:
  application:
    name: ${spring.application.name}
  protocol:
    name: dubbo
    port: -1
  registry:
    address: nacos://localhost:8848
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
         <dependency>
            <groupId>cn.myth</groupId>
            <artifactId>dubbo-example-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-nacos</artifactId>
        </dependency>

增加服务调用,服务注入使用dubbo相关注解 DubboReference,启动列增加注解EnableDubbo
在这里插入图片描述

3. 启动服务测试

  1. 启动nacos
  2. 启动dubbo-example-provider
  3. 启动dubbo-example-consumer

ok,测试完成
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值