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

spring cloud 之 dubbo nacos整合

整体思路:

+  搭建本地nacos服务,详见docker安装nacos_xgjj68163的博客-CSDN博客

+   共三个工程,生产者服务、消费者服务、生产者和消费者共同依赖的接口工程(打成jar,供生产者和消费者依赖);

+   生产者注册服务到nacos,消费者调用nacos上的生产者服务;

目录

1. 共同依赖的接口服务搭建

1.1 pom

1.2 公共接口

1.3 maven install , 将可依赖jar包安装到本地仓库 

 2. 生产者服务搭建

2.1 生产者服务pom

2. 配置文件及注册服务

3. 消费者服务搭建

3.1 消费者服务pom

3.2 nacos及dubbo配置

 3.3 调用dubbo服务

4. 测试

4.1 生产者服务注册成功

 4.2 消费者服务注册成功

 4.3 测试controller,消费者调用生成者服务


1. 共同依赖的接口服务搭建

1.1 pom

注意:

其中build plugins spring-boot-maven-plugin插件,classifier为exec,表示构建可依赖的jar包及可启动的jar包

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>hj.example</groupId><artifactId>springboot-provider</artifactId><version>0.0.1-SNAPSHOT</version><relativePath/> <!-- lookup parent from repository --></parent><artifactId>sample-api</artifactId><version>0.0.1-SNAPSHOT</version><name>sample-api</name><description>sample-api</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><classifier>exec</classifier></configuration></plugin></plugins></build></project>

1.2 公共接口

package hj.example.sample;public interface IHelloService {String sayHello(String name);
}

1.3 maven install , 将可依赖jar包安装到本地仓库 

 2. 生产者服务搭建

2.1 生产者服务pom

包括4个依赖:接口依赖sample-api、nacos配置中心依赖spring-cloud-starter-alibaba-nacos-config、nacos注册中心依赖spring-cloud-starter-alibaba-nacos-discovery,spring-cloud dubbo依赖spring-cloud-starter-dubbo

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>hj.example</groupId><artifactId>springboot-provider</artifactId><version>0.0.1-SNAPSHOT</version><relativePath/> <!-- lookup parent from repository --></parent><artifactId>sample-provider</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>sample-provider</name><description>sample-provider</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>hj.example</groupId><artifactId>sample-api</artifactId><version>0.0.1-SNAPSHOT</version></dependency><!-- nacos --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><version>2.2.5.RELEASE</version></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId><version>2.2.5.RELEASE</version></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-dubbo</artifactId><version>2.2.5.RELEASE</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><classifier>exec</classifier></configuration></plugin></plugins></build>
</project>

2. 配置文件及注册服务

通过@DubboService注解,将dubbo服务注册到nacos上;

dubbo配置,如果不在bootstrap.properties上配置spring.cloud.nacos.config.prefix,默认连接nacos配置中心的dubbo.properties配置文件;

程序优先读取bootstrap.properties配置文件,内容为:

spring.cloud.nacos.config.server-addr=127.0.0.1:8948
spring.cloud.nacos.config.username=nacos
spring.cloud.nacos.config.password=spring.cloud.nacos.config.enabled=false

application.propertes文件内容为:

spring.application.name=sample-provider
server.port=8089spring.cloud.nacos.discovery.server-addr=127.0.0.1:8948
spring.cloud.nacos.discovery.username=nacos
spring.cloud.nacos.discovery.password=
spring.cloud.nacos.discovery.service=sample-provider

nacos上dubbo.properties文件内容:

 

 启动类:

package hj.example.sampleprovider;import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.ConfigurableApplicationContext;@DubboComponentScan
@EnableDiscoveryClient
@SpringBootApplication
@EnableDubbo(scanBasePackages="hj.example.sampleprovider.sample")
public class SampleProviderApplication {public static void main(String[] args) {
//        Main.main(args);ConfigurableApplicationContext context = SpringApplication.run(SampleProviderApplication.class, args);String info = context.getEnvironment().getProperty("info");System.out.println("==========" + info);}
}

注册服务类:

package hj.example.sampleprovider.sample;import hj.example.sample.IHelloService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Value;@DubboService
public class HelloServiceImpl implements IHelloService {@Value("${dubbo.application.name}")private String serviceName;public String sayHello(String name) {return String.format("[%s]: Hello, %s", serviceName, name);}
}

3. 消费者服务搭建

3.1 消费者服务pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.10.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>hj.example</groupId><artifactId>sample-consumer</artifactId><version>0.0.1-SNAPSHOT</version><name>sample-consumer</name><description>sample-consumer</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>hj.example</groupId><artifactId>sample-api</artifactId><version>0.0.1-SNAPSHOT</version></dependency><!-- nacos --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><version>2.2.5.RELEASE</version></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId><version>2.2.5.RELEASE</version></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-dubbo</artifactId><version>2.2.5.RELEASE</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><classifier>exec</classifier></configuration></plugin></plugins></build></project>

3.2 nacos及dubbo配置

配置中心配置bootstrap.properties及nacos配置中心文件dubboConsumer.properties

bootstrap.properties

spring.cloud.nacos.config.server-addr=127.0.0.1:8948
spring.cloud.nacos.config.username=nacos
spring.cloud.nacos.config.password=spring.cloud.nacos.config.prefix=dubboConsumer.properties

dubboConsumer.properties

 

 3.3 调用dubbo服务

使用注解@DubboReference调用dubbo服务,测试controller

package hj.example.sampleconsumer.controller;import hj.example.sample.IHelloService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class TestController {@DubboReferenceprivate IHelloService iHelloService;@RequestMapping("/test")public ResponseEntity<Object> test() {System.out.println("=========consumer test");String sayHelloRs = iHelloService.sayHello("hj");return new ResponseEntity<>(sayHelloRs, HttpStatus.OK);}
}

启动类:

package hj.example.sampleconsumer;import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication
@EnableDubbo
@EnableDiscoveryClient
public class SampleConsumerApplication {public static void main(String[] args) {SpringApplication.run(SampleConsumerApplication.class, args);}
}

4. 测试

4.1 生产者服务注册成功

 4.2 消费者服务注册成功

 4.3 测试controller,消费者调用生成者服务

相关文章:

spring cloud 之 dubbo nacos整合

整体思路&#xff1a; 搭建本地nacos服务&#xff0c;详见docker安装nacos_xgjj68163的博客-CSDN博客 共三个工程&#xff0c;生产者服务、消费者服务、生产者和消费者共同依赖的接口工程&#xff08;打成jar&#xff0c;供生产者和消费者依赖&#xff09;&#xff1b; …...

MySQL如何进行表之间的关联更新

在实际编程工作或运维实践中&#xff0c;对MySQL数据库表进行关联更新是一种比较常见的应用场景&#xff0c;比如在电商系统中&#xff0c;订单表里保存了商品名称的信息&#xff08;冗余字段设计&#xff09;&#xff0c;但如果商品名称发生变化&#xff0c;则需要通过关联商品…...

Docker创建 LNMP 服务+Wordpress 网站平台

Docker创建 LNMP 服务Wordpress 网站平台 一.环境及准备工作 1.项目环境 公司在实际的生产环境中&#xff0c;需要使用 Docker 技术在一台主机上创建 LNMP 服务并运行 Wordpress 网站平台。然后对此服务进行相关的性能调优和管理工作。 容器 系统 IP地址 软件 nginx centos…...

node没有自动安装npm时,如何手动安装 npm

之前写过一篇使用 nvm 管理 node 版本的文章&#xff0c;node版本管理&#xff08;Windows&#xff09; 有时候&#xff0c;我们使用 nvm 下载 node 时&#xff0c;node 没有自动下载 npm &#xff0c;此时就需要我们自己手动下载 npm 1、下载 npm下载地址&#xff1a;&…...

C# 使用递归方法实现汉诺塔步数计算

C# 使用递归方法实现汉诺塔步数计算 Part 1 什么是递归Part 2 汉诺塔Part 3 程序 Part 1 什么是递归 举一个例子&#xff1a;计算从 1 到 x 的总和 public int SumFrom1ToX(int x) {if(x 1){return 1;}else{int result x SumFrom1ToX_2(x - 1); // 调用自己return result…...

窗口函数大揭秘!轻松计算数据累计占比,玩转数据分析的绝佳利器

上一篇文章《如何用窗口函数实现排名计算》中小编为大家介绍了窗口函数在排名计算场景中的应用&#xff0c;但实际上窗口函数除了可以进行单行计算&#xff0c;还可以在每行上打开一个指定大小的计算窗口&#xff0c;这个计算窗口可以由SQL中的语句具体指定&#xff0c;大到整个…...

健康检测智能睡眠床垫方案

《2022中国睡眠质量调查报告》调查结果显示&#xff0c;16&#xff05;的被调查者存在夜间睡眠时间不足6个小时&#xff0c;表现为24点以后才上床睡觉&#xff0c;并且在6点之前起床&#xff1b;有83.81&#xff05;的被调查者经常受到睡眠问题困扰&#xff0c;其中入睡困难占2…...

计网第三章(数据链路层)(五)

目录 一、以太网交换机自学习和转发帧的过程 1.两层交换机和三层交换机 2.以太网交换机的基本原理 3.具体实现过程 一、以太网交换机自学习和转发帧的过程 1.两层交换机和三层交换机 大家可能注意到平常做题时有叫两层交换机&#xff0c;或者三层交换机的。 两层交换机就…...

嵌入式系统中常见内存的划分方法

看到有小伙伴在讨论关于单片机内存的话题&#xff0c;今天就结合STM32给大家描述一下常见的划分区域。 在一个STM32程序代码中&#xff0c;从内存高地址到内存低地址&#xff0c;依次分布着栈区、堆区、全局区&#xff08;静态区&#xff09;、常量区、代码区&#xff0c;其中全…...

深入理解与实现:常见搜索算法的Java示例

深入理解与实现&#xff1a;常见搜索算法的Java示例 搜索算法在计算机科学中扮演着重要角色&#xff0c;用于在数据集中查找特定元素或解决问题。在本篇博客中&#xff0c;我们将深入探讨图算法的一个重要分支&#xff1a;图的搜索算法。具体而言&#xff0c;我们将介绍图的深…...

PHP自己的框架实现操作成功失败跳转(完善篇四)

1、实现效果&#xff0c;操作成功后失败成功自动跳转 2、创建操作成功失败跳转方法CrlBase.php /**成功后跳转*跳转地址$url* 跳转显示信息$msg* 等待时间$wait* 是否自动跳转$jump*/protected function ok($urlNULL,$msg操作成功,$wait3,$jump1){$code1;include KJ_CORE./tp…...

【汇编语言】CS、IP寄存器

文章目录 修改CS、IP的指令转移指令jmp问题分析 修改CS、IP的指令 理论&#xff1a;CPU执行何处的指令&#xff0c;取决于CS:IP应用&#xff1a;程序员可以通过改变CS、IP中的内容&#xff0c;进行控制CPU即将要执行的目标指令&#xff1b;问题&#xff1a;如何改变CS、IP中的…...

Nvidia Jetson 编解码开发(3)解决H265解码报错“PPS id out of range”

1.问题描述 基于之前的开发程序 Nvidia Jetson 编解码开发(2)Jetpack 4.x版本Multimedia API 硬件编码开发--集成encode模块_free-xx的博客-CSDN博客 通过Jetson Xavier NX 硬编码的H265发出后, 上位机断点播放发出来的H265码流, 会报“PPS id out of range” 错误 …...

Angular中如何获取URL参数?

Angular中的ActivatedRoute中保存着路由信息&#xff0c;可用来提取URL中的路由参数。 constructor(private route: ActivatedRoute){}ngOnInit(): void {this.getUser();}getUser(): void {const id this.route.snapshot.paramMap.get(id);} }route.snapshot是一个路由信息的…...

uniapp编写微信小程序和H5遇到的坑总结

uniapp编写微信小程序和H5遇到的坑总结 1、阻止事件冒泡2、二维码生成3、H5跨域配置4、H5时&#xff0c;地址栏上添加版本号5、H5时&#xff0c;tabBar遮挡部分内容6、uniapp使用webview通信6.1、uniapp编写的小程序嵌入h5之间的通信6.1.1、小程序向h5发送消息6.1.2、h5向小程序…...

课程表-广度优先和图

你这个学期必须选修 numCourses 门课程&#xff0c;记为 0 到 numCourses - 1 。 在选修某些课程之前需要一些先修课程。 先修课程按数组 prerequisites 给出&#xff0c;其中 prerequisites[i] [ai, bi] &#xff0c;表示如果要学习课程 ai 则 必须 先学习课程 bi 。 例如&am…...

机器学习|决策树:数学原理及代码解析

机器学习&#xff5c;决策树&#xff1a;数学原理及代码解析 决策树是一种常用的监督学习算法&#xff0c;适用于解决分类和回归问题。在本文中&#xff0c;我们将深入探讨决策树的数学原理&#xff0c;并提供 Python 示例代码帮助读者更好地理解和实现该算法。 决策树数学原…...

1.0的星火2.0必将燎原——图文声影PPT全测试

一、前言 大家好&#xff0c;勇哥又来分享AI模型了&#xff0c;前几天讯飞发布的星火大模型2.0迅速的进入了我们圈子里&#xff0c;为了有更多更好的模型分享给大家&#xff0c;分享星火大模型2.0是必须做的&#xff0c;我做一个传递着&#xff0c;希望大家也星火相传啊。 我…...

[MySQL]主从服务器布置

配置主服务器 配置文件 /etc/my.cnf 在[mysqld]下进行配置 log_binON //启动二进制日志 log-bin mysql-bin //启用二进制日志&#xff0c;用于记录主服务器的更新操作 server-id 1 // 用来表示mysql服务id,保证集成环境中的唯一性 , 范围 [1,2^32) read-only0 // 1表示只…...

图像处理算法大全(基于libyuv或IPP)----NV12转成I420,RGB24,ARGB集合

《周星星教你学ffmpeg》技巧 libyuv源码&#xff1a; static void NV12ToI420(BYTE* pNV12_Y, BYTE* pNV12_UV, BYTE* pYV12, int width, int height) { libyuv::NV12ToI420(pNV12_Y, width, pNV12_UV, width, pYV12, width, pYV12 height*width, width / 2, pYV12 hei…...

React 第五十五节 Router 中 useAsyncError的使用详解

前言 useAsyncError 是 React Router v6.4 引入的一个钩子&#xff0c;用于处理异步操作&#xff08;如数据加载&#xff09;中的错误。下面我将详细解释其用途并提供代码示例。 一、useAsyncError 用途 处理异步错误&#xff1a;捕获在 loader 或 action 中发生的异步错误替…...

应用升级/灾备测试时使用guarantee 闪回点迅速回退

1.场景 应用要升级,当升级失败时,数据库回退到升级前. 要测试系统,测试完成后,数据库要回退到测试前。 相对于RMAN恢复需要很长时间&#xff0c; 数据库闪回只需要几分钟。 2.技术实现 数据库设置 2个db_recovery参数 创建guarantee闪回点&#xff0c;不需要开启数据库闪回。…...

【力扣数据库知识手册笔记】索引

索引 索引的优缺点 优点1. 通过创建唯一性索引&#xff0c;可以保证数据库表中每一行数据的唯一性。2. 可以加快数据的检索速度&#xff08;创建索引的主要原因&#xff09;。3. 可以加速表和表之间的连接&#xff0c;实现数据的参考完整性。4. 可以在查询过程中&#xff0c;…...

STM32F4基本定时器使用和原理详解

STM32F4基本定时器使用和原理详解 前言如何确定定时器挂载在哪条时钟线上配置及使用方法参数配置PrescalerCounter ModeCounter Periodauto-reload preloadTrigger Event Selection 中断配置生成的代码及使用方法初始化代码基本定时器触发DCA或者ADC的代码讲解中断代码定时启动…...

ElasticSearch搜索引擎之倒排索引及其底层算法

文章目录 一、搜索引擎1、什么是搜索引擎?2、搜索引擎的分类3、常用的搜索引擎4、搜索引擎的特点二、倒排索引1、简介2、为什么倒排索引不用B+树1.创建时间长,文件大。2.其次,树深,IO次数可怕。3.索引可能会失效。4.精准度差。三. 倒排索引四、算法1、Term Index的算法2、 …...

Rust 异步编程

Rust 异步编程 引言 Rust 是一种系统编程语言,以其高性能、安全性以及零成本抽象而著称。在多核处理器成为主流的今天,异步编程成为了一种提高应用性能、优化资源利用的有效手段。本文将深入探讨 Rust 异步编程的核心概念、常用库以及最佳实践。 异步编程基础 什么是异步…...

智能仓储的未来:自动化、AI与数据分析如何重塑物流中心

当仓库学会“思考”&#xff0c;物流的终极形态正在诞生 想象这样的场景&#xff1a; 凌晨3点&#xff0c;某物流中心灯火通明却空无一人。AGV机器人集群根据实时订单动态规划路径&#xff1b;AI视觉系统在0.1秒内扫描包裹信息&#xff1b;数字孪生平台正模拟次日峰值流量压力…...

Linux C语言网络编程详细入门教程:如何一步步实现TCP服务端与客户端通信

文章目录 Linux C语言网络编程详细入门教程&#xff1a;如何一步步实现TCP服务端与客户端通信前言一、网络通信基础概念二、服务端与客户端的完整流程图解三、每一步的详细讲解和代码示例1. 创建Socket&#xff08;服务端和客户端都要&#xff09;2. 绑定本地地址和端口&#x…...

基于Java Swing的电子通讯录设计与实现:附系统托盘功能代码详解

JAVASQL电子通讯录带系统托盘 一、系统概述 本电子通讯录系统采用Java Swing开发桌面应用&#xff0c;结合SQLite数据库实现联系人管理功能&#xff0c;并集成系统托盘功能提升用户体验。系统支持联系人的增删改查、分组管理、搜索过滤等功能&#xff0c;同时可以最小化到系统…...

基于TurtleBot3在Gazebo地图实现机器人远程控制

1. TurtleBot3环境配置 # 下载TurtleBot3核心包 mkdir -p ~/catkin_ws/src cd ~/catkin_ws/src git clone -b noetic-devel https://github.com/ROBOTIS-GIT/turtlebot3.git git clone -b noetic https://github.com/ROBOTIS-GIT/turtlebot3_msgs.git git clone -b noetic-dev…...