当前位置:   article > 正文

SpringBoot+Dubbo+Nacos 开发实战教程_springboot+nacos+double

springboot+nacos+double

本文来写个详细的例子来说下dubbo+nacos+springboot开发实战。本文不会讲述太多的理论的知识,会写一个最简单的例子来说明dubbo如何与nacos整合,快速搭建开发环境。

文章目录

  • 环境准备

    • dubbo简介

    • Nacos环境准备

    • Nacos与dubbo整合

  • 项目管理规范

  • dubbo整合nacos案例

    • 创建公共接口模块

    • 创建服务提供者模块

    • 创建服务消费者模块

    • 服务调用测试

  • 本文小结

环境准备

dubbo简介

dubbo核心节点之间的调用关系

节点说明

调用关系说明

  • 服务容器负责启动,加载,运行服务提供者。

  • 服务提供者在启动时,向注册中心注册自己提供的服务。

  • 服务消费者在启动时,向注册中心订阅自己所需的服务。

  • 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。

  • 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。

  • 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

Nacos环境准备

启动nacos,本文使用的是nacos1.4.3版本

打开nacos的控制面板

Nacos与dubbo整合

为了方便大家理解,这里写出对应的生产方、消费方 Demo 代码,以及使用的注册中心。

项目管理规范

springboot,netflix和cloud alibaba之间有版本之间的对应关系,本文选择的版本如下

dubbo和cloud alibaba之间有版本之间的对应关系,本文选择的版本如下

为了后续方便使用SpringCloud Alibaba进行开发,首先创建一个pom类型的父项目,主要用于项目技术栈版本管理,创建一个maven项目,名称为spring-cloud-alibaba-example,去除src文件,修改pom文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.     <modelVersion>4.0.0</modelVersion>
  6.     <groupId>ah.wideth</groupId>
  7.     <artifactId>spring-cloud-alibaba-example</artifactId>
  8.     <version>1.0-SNAPSHOT</version>
  9.     <parent>
  10.         <groupId>org.springframework.boot</groupId>
  11.         <artifactId>spring-boot-starter-parent</artifactId>
  12.         <version>2.3.12.RELEASE</version>
  13.         <relativePath/> <!-- lookup parent from repository -->
  14.     </parent>
  15.     <packaging>pom</packaging>
  16.     <properties>
  17.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18.         <java.version>1.8</java.version>
  19.         <spring-cloud.version>Hoxton.SR12</spring-cloud.version>
  20.         <com-alibaba-cloud.version>2.2.7.RELEASE</com-alibaba-cloud.version>
  21.     </properties>
  22.     <!--对项目版本进行管理-->
  23.     <dependencyManagement>
  24.         <dependencies>
  25.             <dependency>
  26.                 <groupId>org.springframework.cloud</groupId>
  27.                 <artifactId>spring-cloud-dependencies</artifactId>
  28.                 <version>${spring-cloud.version}</version>
  29.                 <type>pom</type>
  30.                 <scope>import</scope>
  31.             </dependency>
  32.             <dependency>
  33.                 <groupId>com.alibaba.cloud</groupId>
  34.                 <artifactId>spring-cloud-alibaba-dependencies</artifactId>
  35.                 <version>${com-alibaba-cloud.version}</version>
  36.                 <type>pom</type>
  37.                 <scope>import</scope>
  38.             </dependency>
  39.         </dependencies>
  40.     </dependencyManagement>
  41. </project>

后续创建的项目都放到此目录下,只需要声明groupId和artifactId,会自动引用父项目spring-cloud-alibaba-example的版本。与其说是父项目,不如说是根项目: 因为下面每学习一个新的技术,就会新建一个真正的父项目,而在对应的父项目下面又会创建许多的子项目

dubbo整合nacos案例

本文的案例是在上文nacos开发实例的基础之上继续编写的。下面开始创建我们的项目,贴上我的目录结构。

模块说明

  • public-api公共接口模块(接口),供服务消费者和服务提供者调用。

  • dubbo-provider服务提供者模块(接口实现类),引入了public-api模块

  • dubbo-consumer服务消费者模块(controller),引入了public-api模块

  • 消费者和提供者通过公共接口模块进行rpc远程调用

父工程pom文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.     <modelVersion>4.0.0</modelVersion>
  5.     <parent>
  6.         <artifactId>spring-cloud-alibaba-example</artifactId>
  7.         <groupId>ah.wideth</groupId>
  8.         <version>1.0-SNAPSHOT</version>
  9.     </parent>
  10.     <modules>
  11.         <module>public-api</module>
  12.         <module>dubbo-provider</module>
  13.         <module>dubbo-consumer</module>
  14.     </modules>
  15.     <artifactId>dubbo-nacos-example</artifactId>
  16.     <name>dubbo-nacos-example</name>
  17.     <description>duboo与nacos整合的父工程</description>
  18.     <packaging>pom</packaging>
  19.     <properties>
  20.         <java.version>1.8</java.version>
  21.         <project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
  22.         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  23.         <dubbo.version>2.7.13</dubbo.version>
  24.         <nacos.version>1.4.1</nacos.version>
  25.     </properties>
  26.     <dependencies>
  27.         <dependency>
  28.             <groupId>com.alibaba.cloud</groupId>
  29.             <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  30.             <!--解决nacos-client2.0报错的问题-->
  31.             <exclusions>
  32.                 <exclusion>
  33.                     <artifactId>nacos-client</artifactId>
  34.                     <groupId>com.alibaba.nacos</groupId>
  35.                 </exclusion>
  36.             </exclusions>
  37.         </dependency>
  38.         <!--dubbo相关-->
  39.         <dependency>
  40.             <groupId>org.apache.dubbo</groupId>
  41.             <artifactId>dubbo-spring-boot-starter</artifactId>
  42.             <version>${dubbo.version}</version>
  43.         </dependency>
  44.         <dependency>
  45.             <groupId>org.apache.dubbo</groupId>
  46.             <artifactId>dubbo-registry-nacos</artifactId>
  47.             <version>${dubbo.version}</version>
  48.         </dependency>
  49.         <dependency>
  50.             <groupId>com.alibaba.nacos</groupId>
  51.             <artifactId>nacos-client</artifactId>
  52.             <version>${nacos.version}</version>
  53.         </dependency>
  54.         <!-- 解决dubbo2.7.13jar包冲突问题-->
  55.         <dependency>
  56.             <groupId>com.alibaba.spring</groupId>
  57.             <artifactId>spring-context-support</artifactId>
  58.             <version>1.0.11</version>
  59.         </dependency>
  60.         <dependency>
  61.             <groupId>org.apache.dubbo</groupId>
  62.             <artifactId>dubbo</artifactId>
  63.             <version>${dubbo.version}</version>
  64.             <exclusions>
  65.                 <exclusion>
  66.                     <groupId>org.springframework</groupId>
  67.                     <artifactId>spring</artifactId>
  68.                 </exclusion>
  69.                 <exclusion>
  70.                     <groupId>javax.servlet</groupId>
  71.                     <artifactId>servlet-api</artifactId>
  72.                 </exclusion>
  73.                 <exclusion>
  74.                     <groupId>log4j</groupId>
  75.                     <artifactId>log4j</artifactId>
  76.                 </exclusion>
  77.             </exclusions>
  78.         </dependency>
  79.         
  80.     </dependencies>
  81. </project>

创建公共接口模块

pom文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.     <modelVersion>4.0.0</modelVersion>
  5.     <parent>
  6.         <artifactId>dubbo-nacos-example</artifactId>
  7.         <groupId>ah.wideth</groupId>
  8.         <version>1.0-SNAPSHOT</version>
  9.     </parent>
  10.     <artifactId>public-api</artifactId>
  11.     <name>public-api</name>
  12.     <description>api公用接口</description>
  13.     <packaging>jar</packaging>
  14.     <properties>
  15.         <java.version>1.8</java.version>
  16.         <project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
  17.         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  18.     </properties>
  19.     <dependencies>
  20.         <dependency>
  21.             <groupId>org.springframework.boot</groupId>
  22.             <artifactId>spring-boot-starter-web</artifactId>
  23.         </dependency>
  24.     </dependencies>
  25.     <build>
  26.         <plugins>
  27.             <plugin>
  28.                 <groupId>org.springframework.boot</groupId>
  29.                 <artifactId>spring-boot-maven-plugin</artifactId>
  30.             </plugin>
  31.         </plugins>
  32.     </build>
  33. </project>

公共接口模块里面只有一个接口,没有配置文件,打jar包

  1. package ah.wideth.api;
  2. /**
  3.  * 让生产者和服务消
  4.  * 费者来使用这个接口
  5.  */
  6. public interface InfoService {
  7.     String getInfo();
  8. }

创建服务提供者模块

pom文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.     <modelVersion>4.0.0</modelVersion>
  5.     <parent>
  6.         <artifactId>dubbo-nacos-example</artifactId>
  7.         <groupId>ah.wideth</groupId>
  8.         <version>1.0-SNAPSHOT</version>
  9.     </parent>
  10.     <artifactId>dubbo-provider</artifactId>
  11.     <name>dubbo-provider</name>
  12.     <description>dubbo的服务提供者模块</description>
  13.     <properties>
  14.         <java.version>1.8</java.version>
  15.         <project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
  16.         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  17.     </properties>
  18.     <dependencies>
  19.         <dependency>
  20.             <groupId>org.springframework.boot</groupId>
  21.             <artifactId>spring-boot-starter-web</artifactId>
  22.         </dependency>
  23.         <!--引入公共接口模块-->
  24.         <dependency>
  25.             <groupId>${project.groupId}</groupId>
  26.             <artifactId>public-api</artifactId>
  27.             <version>${project.version}</version>
  28.         </dependency>
  29.     </dependencies>
  30.     <build>
  31.         <plugins>
  32.             <plugin>
  33.                 <groupId>org.springframework.boot</groupId>
  34.                 <artifactId>spring-boot-maven-plugin</artifactId>
  35.             </plugin>
  36.         </plugins>
  37.     </build>
  38. </project>

application.yml配置文件

  1. server:
  2.   port: 8180
  3. spring:
  4.   application:
  5.     name: dubbo-provider
  6. dubbo:
  7.   registry:
  8.     address: nacos://127.0.0.1:8848 #注册地址
  9.   application:
  10.     name: dubbo-provider #应用名
  11.   protocol:
  12.     name: dubbo #dubbo协议
  13.     port: 20880 #协议端口
  14.   scan:
  15.     base-packages: ah.wideth.impl #扫包范围
  16.   provider:
  17.     timeout: 30000 #超时时间

接口实现类,该类实现了上面我们在公共接口模块创建的接口

  1. package ah.wideth.impl;
  2. import ah.wideth.api.InfoService;
  3. import org.apache.dubbo.config.annotation.DubboService;
  4. import org.springframework.stereotype.Component;
  5. // dubbo提供的Service注解,用于声明对外暴露服务
  6. // Service引入的是org.apache.dubbo.config.annotation.Service包
  7. @Component
  8. @DubboService
  9. public class InfoServiceImpl implements InfoService {
  10.     @Override
  11.     public String getInfo() {
  12.         return "hello,这里是dubbo-provider模块!";
  13.     }
  14. }

服务提供者启动类

  1. package ah.wideth;
  2. import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  6. @EnableDubbo
  7. @EnableDiscoveryClient
  8. @SpringBootApplication
  9. public class DubboProviderApplication {
  10.     public static void main(String[] args) {
  11.         SpringApplication.run(DubboProviderApplication.class, args);
  12.         System.out.println("dubbo服务提供者8180启动了");
  13.     }
  14. }

创建服务消费者模块

pom文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.     <modelVersion>4.0.0</modelVersion>
  5.     <parent>
  6.         <artifactId>dubbo-nacos-example</artifactId>
  7.         <groupId>ah.wideth</groupId>
  8.         <version>1.0-SNAPSHOT</version>
  9.     </parent>
  10.     <artifactId>dubbo-consumer</artifactId>
  11.     <name>dubbo-consumer</name>
  12.     <description>dubbo的服务消费者模块</description>
  13.     <properties>
  14.         <java.version>1.8</java.version>
  15.         <project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
  16.         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  17.     </properties>
  18.     <dependencies>
  19.         <dependency>
  20.             <groupId>org.springframework.boot</groupId>
  21.             <artifactId>spring-boot-starter-web</artifactId>
  22.         </dependency>
  23.         <!--引入公共接口模块-->
  24.         <dependency>
  25.             <groupId>${project.groupId}</groupId>
  26.             <artifactId>public-api</artifactId>
  27.             <version>${project.version}</version>
  28.         </dependency>
  29.     </dependencies>
  30.     <build>
  31.         <plugins>
  32.             <plugin>
  33.                 <groupId>org.springframework.boot</groupId>
  34.                 <artifactId>spring-boot-maven-plugin</artifactId>
  35.             </plugin>
  36.         </plugins>
  37.     </build>
  38. </project>

application.yml配置文件

  1. server:
  2.   port: 8181
  3. spring:
  4.   application:
  5.     name: dubbo-consumer
  6. dubbo:
  7.   registry:
  8.     address: nacos://127.0.0.1:8848 #注册地址
  9.   application:
  10.     name: dubbo-consumer #应用名
  11.   consumer:
  12.     timeout: 30000 #超时时间

controller,调用公共接口模块创建的接口

  1. package ah.wideth.controller;
  2. import ah.wideth.api.InfoService;
  3. import org.apache.dubbo.config.annotation.DubboReference;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. @RestController
  7. public class InfoController {
  8.     //dumbo提供的Reference注解,用于调用远程服务
  9.     @DubboReference(check = false)
  10.     private InfoService infoService;
  11.     @GetMapping("/getInfo")
  12.     public String getInfo(){
  13.         return infoService.getInfo();
  14.     }
  15. }

服务消费者启动类

  1. package ah.wideth;
  2. import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  6. @EnableDubbo
  7. @EnableDiscoveryClient
  8. @SpringBootApplication
  9. public class DubboConsumerApplication {
  10.     public static void main(String[] args) {
  11.         SpringApplication.run(DubboConsumerApplication.class, args);
  12.         System.out.println("dubbo服务消费者8181启动了");
  13.     }
  14. }

服务调用测试

打开Nacos控制面板查看注册中心中的服务

启动nacos,启动服务提供者和服务消费者,调用服务消费者的getInfo方法,服务提供者会返回结果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/165640
推荐阅读
相关标签
  

闽ICP备14008679号