当前位置: 首页 > news >正文

nacos+Dubbo整合快速入门

官网:Nacos Spring Boot 快速开始

  1. 下载下载链接
  2. 启动:进入bin目录,startup.cmd -m standalone
  3. 引入依赖
    <dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo</artifactId><version>3.0.9</version></dependency><dependency><groupId>com.alibaba.nacos</groupId><artifactId>nacos-client</artifactId><version>2.1.0</version></dependency>

4.配置application.yml 

dubbo:application:name: dubbo-springboot-demo-providerprotocol:name: dubboport: -1registry:id: nacos-registryaddress: nacos://localhost:8848

5. 提供者:provider包下创建service服务

public interface DemoService {String sayHello(String name);String sayHello2(String name);default CompletableFuture<String> sayHelloAsync(String name) {return CompletableFuture.completedFuture(sayHello(name));}}@DubboService
public class DemoServiceImpl implements DemoService {@Overridepublic String sayHello(String name) {System.out.println("Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());return "Hello " + name;}@Overridepublic String sayHello2(String name) {return "mumu";}}

6.消费者:创建service,需要用到demoservice

package org.mumu.project.provider;import java.util.concurrent.CompletableFuture;public interface DemoService {String sayHello(String name);String sayHello2(String name);default CompletableFuture<String> sayHelloAsync(String name) {return CompletableFuture.completedFuture(sayHello(name));}}

 7.消费者方调用远程服务

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
@EnableDubbo
public class ApiGatewayApplication {@DubboReferenceprivate DemoService demoService;public static void main(String[] args) {ConfigurableApplicationContext context = SpringApplication.run(ApiGatewayApplication.class, args);ApiGatewayApplication application = context.getBean(ApiGatewayApplication.class);String result = application.doSayHello("world");String result2 = application.doSayHello2("world");System.out.println("result: " + result);System.out.println("result: " + result2);}public String doSayHello(String name) {return demoService.sayHello(name);}public String doSayHello2(String name) {return demoService.sayHello2(name);}
}

报错:Failed to check the status of the service org.mumu.apigateway.project.provider.DemoService. No provider available for the service org.mumu.apigateway.project.provider.DemoService from the url

需要注意的是:服务提供方和调用方需要包名一致

http://localhost:8848/nacos/index.html  nacos中心

http://www.lryc.cn/news/171623.html

相关文章:

  • QT实现钟表
  • 准备我们心爱的IDEA写Jsp
  • 将近 5 万字讲解 Python Django 框架详细知识点(更新中)
  • Arcgis提取每个像元的多波段反射率值
  • JavaScript面试题整理(一)
  • 数据结构:树和二叉树之-堆排列 (万字详解)
  • 爬虫入门基础:深入解析HTTP协议的工作过程
  • k8备份与恢复-Velero
  • 基于Python开发的火车票分析助手(源码+可执行程序+程序配置说明书+程序使用说明书)
  • 旺店通·企业奇门与金蝶云星空对接集成订单查询连通销售订单新增(旺店通销售-金蝶销售订单-小红书)
  • 卡尔曼滤波应用在数据处理方面的应用
  • PROFIBUS主站转ETHERCAT协议网关
  • Vue路由的使用及node.js下载安装和环境搭建
  • 【算法训练-二叉树 三】【最大深度与直径】求二叉树的最大深度、求二叉树的直径
  • 查看linux是centos还是Ubuntu
  • win10怎么关闭自动更新,这个方法你知道吗?
  • 「语音芯片」常见的OTP芯片故障分析
  • 孩子写作业买什么样台灯合适?适合孩子读写台灯推荐
  • DBAPI插件开发指南
  • 线程池使用之自定义线程池
  • Puppeteer无头浏览器:开启自动化之门,掌握浏览器世界的无限可能
  • Ubuntu 23.10/24.04 LTS 放弃默认使用 snap 版 CUPS 打印堆栈
  • Linux CentOS7 history命令
  • XC5350A 单节锂电池保护芯片 过放2.9V/2.8V/2.4V保护IC
  • 单片机论文参考:1、基于单片机的电子琴
  • Opencv源码解析(2)算法
  • 让Mac菜单栏变得更加美观整洁——Bartender 5
  • 服务器迁移:无缝过渡指南
  • 安卓开发中ViewBinding的使用
  • 【初阶数据结构】树(tree)的基本概念——C语言