SpringBoot2.1.9+dubbo2.7.3+Nacos1.1.4构建你的微服务体系

举报
JAVA日知录 发表于 2022/04/14 01:40:17 2022/04/14
【摘要】 使用最新版的DUBBO构建你的微服务体系! NACOS注册中心 从github下载最新版本的nacos 上传至服务器并解压 单机启动sh startup.sh -m standalone nacos 控制台访问地址http://192.168.136.129:8848/nacos,使用账号nacos/nacos登录...

使用最新版的DUBBO构建你的微服务体系!

NACOS注册中心

  • 从github下载最新版本的nacos

  • 上传至服务器并解压

  • 单机启动sh startup.sh -m standalone

  • nacos 控制台访问地址http://192.168.136.129:8848/nacos,使用账号nacos/nacos登录并访问

项目框架

本次案例包含三个组件

  • 公共接口层 dubbo-api

  • 生产者  dubbo-provider

  • 消费者 dubbo-consumer 代码目录如下:

父目录dubbo-demo

父目录主要是定义公共组件依赖,版本号,pom文件如下


   
  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <packaging>pom</packaging>
  6. <modules>
  7. <module>dubbo-api</module>
  8. <module>dubbo-provider</module>
  9. <module>dubbo-consumer</module>
  10. </modules>
  11. <groupId>com.jianzh5</groupId>
  12. <artifactId>dubbo-demo</artifactId>
  13. <version>1.0-SNAPSHOT</version>
  14. <properties>
  15. <java.version>1.8</java.version>
  16. <netty-all.version>4.0.35.Final</netty-all.version>
  17. <spring-boot.version>2.1.9.RELEASE</spring-boot.version>
  18. <dubbo.version>2.7.3</dubbo.version>
  19. <nacos-client.version>1.1.4</nacos-client.version>
  20. </properties>
  21. <dependencyManagement>
  22. <dependencies>
  23. <!--SpringBoot-->
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-dependencies</artifactId>
  27. <version>${spring-boot.version}</version>
  28. <type>pom</type>
  29. <scope>import</scope>
  30. </dependency>
  31. <!-- Apache Dubbo -->
  32. <dependency>
  33. <groupId>org.apache.dubbo</groupId>
  34. <artifactId>dubbo-dependencies-bom</artifactId>
  35. <version>${dubbo.version}</version>
  36. <type>pom</type>
  37. <scope>import</scope>
  38. </dependency>
  39. <!-- Dubbo Registry Nacos -->
  40. <dependency>
  41. <groupId>org.apache.dubbo</groupId>
  42. <artifactId>dubbo-registry-nacos</artifactId>
  43. <version>${dubbo.version}</version>
  44. </dependency>
  45. <dependency>
  46. <groupId>com.alibaba.nacos</groupId>
  47. <artifactId>nacos-client</artifactId>
  48. <version>${nacos-client.version}</version>
  49. </dependency>
  50. <!-- Dubbo Spring Boot Starter -->
  51. <dependency>
  52. <groupId>org.apache.dubbo</groupId>
  53. <artifactId>dubbo-spring-boot-starter</artifactId>
  54. <version>${dubbo.version}</version>
  55. </dependency>
  56. <dependency>
  57. <groupId>org.apache.dubbo</groupId>
  58. <artifactId>dubbo</artifactId>
  59. <version>${dubbo.version}</version>
  60. <exclusions>
  61. <exclusion>
  62. <groupId>org.springframework</groupId>
  63. <artifactId>spring</artifactId>
  64. </exclusion>
  65. <exclusion>
  66. <groupId>javax.servlet</groupId>
  67. <artifactId>servlet-api</artifactId>
  68. </exclusion>
  69. <exclusion>
  70. <groupId>log4j</groupId>
  71. <artifactId>log4j</artifactId>
  72. </exclusion>
  73. </exclusions>
  74. </dependency>
  75. <!--接口-->
  76. <dependency>
  77. <groupId>com.jianzh5</groupId>
  78. <artifactId>dubbo-api</artifactId>
  79. <version>1.0-SNAPSHOT</version>
  80. </dependency>
  81. </dependencies>
  82. </dependencyManagement>
  83. <build>
  84. <plugins>
  85. <plugin>
  86. <groupId>org.springframework.boot</groupId>
  87. <artifactId>spring-boot-maven-plugin</artifactId>
  88. <version>${spring-boot.version}</version>
  89. <executions>
  90. <execution>
  91. <goals>
  92. <goal>repackage</goal>
  93. </goals>
  94. </execution>
  95. </executions>
  96. </plugin>
  97. </plugins>
  98. </build>
  99. </project>

在pom文件中直接依赖接口,这样生产者和消费者都可以使用了

接口层dubbo-api定义

  • pom


   
  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <parent>
  5. <artifactId>dubbo-demo</artifactId>
  6. <groupId>com.jianzh5</groupId>
  7. <version>1.0-SNAPSHOT</version>
  8. </parent>
  9. <modelVersion>4.0.0</modelVersion>
  10. <groupId>com.jianzh5</groupId>
  11. <artifactId>dubbo-api</artifactId>
  12. <packaging>jar</packaging>
  13. </project>
  • 定义真实的接口


   
  1. /**
  2. * @author jianzh5
  3. * @date 2019/11/5 10:45
  4. */
  5. public interface HelloService {
  6. String sayHello();
  7. }

生产者dubbo-provider

  • pom


   
  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <parent>
  5. <artifactId>dubbo-demo</artifactId>
  6. <groupId>com.jianzh5</groupId>
  7. <version>1.0-SNAPSHOT</version>
  8. </parent>
  9. <modelVersion>4.0.0</modelVersion>
  10. <groupId>com.jianzh5</groupId>
  11. <artifactId>dubbo-provider</artifactId>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter</artifactId>
  16. </dependency>
  17. <!-- Dubbo -->
  18. <dependency>
  19. <groupId>org.apache.dubbo</groupId>
  20. <artifactId>dubbo-spring-boot-starter</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.apache.dubbo</groupId>
  24. <artifactId>dubbo</artifactId>
  25. </dependency>
  26. <!-- Dubbo Registry Nacos -->
  27. <dependency>
  28. <groupId>org.apache.dubbo</groupId>
  29. <artifactId>dubbo-registry-nacos</artifactId>
  30. </dependency>
  31. <dependency>
  32. <groupId>com.alibaba.nacos</groupId>
  33. <artifactId>nacos-client</artifactId>
  34. </dependency>
  35. <dependency>
  36. <groupId>com.jianzh5</groupId>
  37. <artifactId>dubbo-api</artifactId>
  38. </dependency>
  39. </dependencies>
  40. <build>
  41. <plugins>
  42. <plugin>
  43. <groupId>org.springframework.boot</groupId>
  44. <artifactId>spring-boot-maven-plugin</artifactId>
  45. </plugin>
  46. </plugins>
  47. </build>
  48. </project>
  • 配置文件application.xml


   
  1. dubbo.application.name = dubbo-provider
  2. dubbo.registry.address = nacos://192.168.136.129:8848
  3. dubbo.scan.base-packages=com.jianzh5.provider.service.impl
  4. dubbo.protocol.port=20881
  5. dubbo.protocol.name=dubbo
  • 接口实现


   
  1. @Service
  2. public class HelloServiceImpl implements HelloService {
  3. public String sayHello() {
  4. return "欢迎关注微信公众号:JAVA日知录";
  5. }
  6. }

注意这里的@service引用的是org.apache.dubbo.config.annotation.Service,不要引用错了

  • 启动类


   
  1. @SpringBootApplication
  2. @EnableDubbo
  3. public class PrivoderBootstrap {
  4. public static void main(String[] args) {
  5. SpringApplication.run(PrivoderBootstrap.class, args);
  6. }
  7. }

需要引入@EnableDubbo注解

消费者dubbo-consumer

  • pom


   
  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <parent>
  5. <artifactId>dubbo-demo</artifactId>
  6. <groupId>com.jianzh5</groupId>
  7. <version>1.0-SNAPSHOT</version>
  8. </parent>
  9. <modelVersion>4.0.0</modelVersion>
  10. <groupId>com.jianzh5</groupId>
  11. <artifactId>dubbo-consumer</artifactId>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter</artifactId>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-web</artifactId>
  20. </dependency>
  21. <!-- Dubbo -->
  22. <dependency>
  23. <groupId>org.apache.dubbo</groupId>
  24. <artifactId>dubbo-spring-boot-starter</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.apache.dubbo</groupId>
  28. <artifactId>dubbo</artifactId>
  29. </dependency>
  30. <!-- Dubbo Registry Nacos -->
  31. <dependency>
  32. <groupId>org.apache.dubbo</groupId>
  33. <artifactId>dubbo-registry-nacos</artifactId>
  34. </dependency>
  35. <dependency>
  36. <groupId>com.alibaba.nacos</groupId>
  37. <artifactId>nacos-client</artifactId>
  38. </dependency>
  39. <dependency>
  40. <groupId>com.jianzh5</groupId>
  41. <artifactId>dubbo-api</artifactId>
  42. </dependency>
  43. </dependencies>
  44. <build>
  45. <plugins>
  46. <plugin>
  47. <groupId>org.springframework.boot</groupId>
  48. <artifactId>spring-boot-maven-plugin</artifactId>
  49. </plugin>
  50. </plugins>
  51. </build>
  52. </project>
  • 配置文件application.properties


   
  1. spring.application.name=dubbo-consumer
  2. dubbo.registry.address = nacos://192.168.136.129:8848
  3. server.port=9090
  • 消费者HelloController


   
  1. /**
  2. * @author jianzh5
  3. * @date 2019/11/5 11:29
  4. */
  5. @RestController
  6. public class HelloController {
  7. @Reference
  8. private HelloService helloService;
  9. @GetMapping("/sayHello")
  10. public String sayHello(){
  11. return helloService.sayHello();
  12. }
  13. }

使用@Reference注解注入HelloService

  • 接口启动类


   
  1. @SpringBootApplication
  2. public class ConsumerBootstrap {
  3. public static void main(String[] args) {
  4. SpringApplication.run(ConsumerBootstrap.class, args);
  5. }
  6. }

项目启动

  • 启动生产者 dubbo-provider

  • 启动消费者 dubbo-consumer

  • 查看nacos控制台,观察是否注册

  • 访问浏览器http://localhost:9090/sayHello查看接口返回

 

文章来源: jianzh5.blog.csdn.net,作者:飘渺Jam,版权归原作者所有,如需转载,请联系作者。

原文链接:jianzh5.blog.csdn.net/article/details/102927802

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。