当前位置:   article > 正文

springboot+dubbo+nacos整合_springboot dubbo nacos

springboot dubbo nacos

一,创建一个springboot项目

二,创建consumer-serverprovider-server

provider-server

1,引入maven坐标

  1. <!-- nacos服务注册发现 -->
  2. <dependency>
  3. <groupId>com.alibaba.cloud</groupId>
  4. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  5. <version>2.2.2.RELEASE</version>
  6. </dependency>
  7. <!-- dubbo依赖 -->
  8. <dependency>
  9. <groupId>org.apache.dubbo</groupId>
  10. <artifactId>dubbo-spring-boot-starter</artifactId>
  11. <version>2.7.8</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.apache.dubbo</groupId>
  15. <artifactId>dubbo-registry-nacos</artifactId>
  16. <version>2.7.8</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>com.fan</groupId>
  20. <artifactId>provider-server</artifactId>
  21. <version>0.0.1-SNAPSHOT</version>
  22. </dependency>

1.创建service

  1. public interface HelloService {
  2. String sayHello(String name);
  3. }

1.2实现

  1. public class HelloServiceimpl implements HelloService{
  2. @Override
  3. public String sayHello(String name) {
  4. return "张三"+name;
  5. }
  6. }

1.3application.properties

  1. server.port=8080
  2. spring.application.name=dubbo-demo-provider
  3. spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
  4. dubbo.protocol.name=dubbo
  5. dubbo.protocol.port=-1
  6. dubbo.registry.address=nacos://127.0.0.1:8848
  7. dubbo.application.name=dubbo-demo-provider
  8. dubbo.consumer.check=false

consumer-server

1.引入maven坐标

  1. <!-- nacos服务注册发现 -->
  2. <dependency>
  3. <groupId>com.alibaba.cloud</groupId>
  4. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  5. <version>2.2.2.RELEASE</version>
  6. </dependency>
  7. <!-- dubbo依赖 -->
  8. <dependency>
  9. <groupId>org.apache.dubbo</groupId>
  10. <artifactId>dubbo-spring-boot-starter</artifactId>
  11. <version>2.7.8</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.apache.dubbo</groupId>
  15. <artifactId>dubbo-registry-nacos</artifactId>
  16. <version>2.7.8</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>com.fan</groupId>
  20. <artifactId>provider-server</artifactId>
  21. <version>0.0.1-SNAPSHOT</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>com.fan</groupId>
  25. <artifactId>provider-server</artifactId>
  26. <version>0.0.1-SNAPSHOT</version>
  27. </dependency>

2创建controller

  1. @RestController
  2. public class MainController
  3. {
  4. @DubboReference
  5. private HelloService helloService;
  6. @GetMapping("/")
  7. public String index()
  8. {
  9. return helloService.sayHello("dubbo");
  10. }
  11. }

3application.properties

  1. server.port=9090
  2. spring.application.name=dubbo-demo-consumer
  3. spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
  4. dubbo.protocol.name=dubbo
  5. dubbo.protocol.port=-1
  6. dubbo.registry.address=nacos://127.0.0.1:8848
  7. dubbo.application.name=dubbo-demo-consumer
  8. dubbo.consumer.check=false

nacos启动后启动项目

然后访问9090端口发现报错

在provider-server启动类加上@EnableDubbo 重新启动查看 还是报错检查发现HelloServiceimpl中忘记了@DubboService 加上重新启动测试

完成

三,配置文件说明

  1. server.port=8080 #应用端口
  2. spring.application.name=dubbo-demo-provider #服务名称
  3. spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 #将服务注册到nacos
  4. dubbo.protocol.name=dubbo #使用协议
  5. dubbo.protocol.port=-1 #调用协议地址
  6. dubbo.registry.address=nacos://127.0.0.1:8848 #dubbo注册中心
  7. dubbo.application.name=dubbo-demo-provider #dubbo服务名称
  8. dubbo.consumer.check=false #dubbo启动时检查

前面使用了spring-cloud-starter-alibaba-nacos-discovery的jar

也可以使用。流程也是一样的

  1. <!--dubbo和nacos-->
  2. <dependency>
  3. <groupId>org.apache.dubbo</groupId>
  4. <artifactId>dubbo-spring-boot-starter</artifactId>
  5. <version>2.7.8</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.apache.dubbo</groupId>
  9. <artifactId>dubbo</artifactId>
  10. <version>2.7.8</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.apache.dubbo</groupId>
  14. <artifactId>dubbo-registry-nacos</artifactId>
  15. <version>2.7.8</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>com.alibaba.nacos</groupId>
  19. <artifactId>nacos-client</artifactId>
  20. <version>1.1.4</version>
  21. </dependency>

注意

dubbo.protocol.port=20881

这个端口有一样的应用会启动不起来

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

闽ICP备14008679号