spring cloud 之 dubbo nacos整合

news2024/10/6 8:33:34

整体思路:

+  搭建本地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=8089

spring.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 {

    @DubboReference
    private 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,消费者调用生成者服务

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/910571.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【面试题】前端面试复习6---性能优化

前端面试题库 &#xff08;面试必备&#xff09; 推荐&#xff1a;★★★★★ 地址&#xff1a;前端面试题库 性能优化 一、性能指标 要在 Chrome 中查看性能指标&#xff0c;可以按照以下步骤操作&#xff1a; 打开 Chrome 浏览器&#xff0c;并访问你想要测试…

【 欧凯 网页 test】

骨钙素&#xff08;BGP&#xff09; 抗体参数 名称抗人骨钙素抗体&#xff08;BGP antibody&#xff09;应用平台免疫荧光&#xff0c;化学发光货号K135c2K131c1推荐用途捕获检测来源鼠单抗&#xff0c;体外培养获得缓冲液1PBS纯度Protein A/G纯化&#xff0c;纯度>96%储存…

结构型(五) - 适配器模式

一、概念 适配器模式&#xff08;Adapter Pattern&#xff09;&#xff1a;这个模式就是用来做适配的&#xff0c;它将不兼容的接口转换为可兼容的接口&#xff0c;让原本由于接口不兼容而不能一起工作的类可以一起工作。 应用场景&#xff1a;适配器模式是一种事后的补救策略…

分布式事务(4):两阶段提交协议与三阶段提交区别

1 两阶段提交协议 两阶段提交方案应用非常广泛&#xff0c;几乎所有商业OLTP数据库都支持XA协议。但是两阶段提交方案锁定资源时间长&#xff0c;对性能影响很大&#xff0c;基本不适合解决微服务事务问题。 缺点&#xff1a; 如果协调者宕机&#xff0c;参与者没有协调者指…

通过springBoot自动装配实现api封装

1.在resource目录下创建META-INF目录&#xff0c;并在其中创建resources\META-INF\spring.factories org.springframework.boot.autoconfigure.EnableAutoConfiguration\com.tanhua.autoconfig.TanhuaAutoConfiguration springBoot在启动之后会自动扫描这个文件&#xff0c;并…

SAP LTMC基础教程之物料主数据详细操作示例

SAP LTMC基础教程之物料主数据详细操作示例 SAP S/4HANA 1610版本的推出已经不再建议使用LSMW了&#xff0c;使用中会受到很多限制&#xff08;比如特性、类的导入&#xff09;&#xff0c;而是推出了新工具LTMC。记录并分享LTMC的操作。 有几个注意点能够搞明白基本都能成功…

爱校对如何帮助企业和博客主提高在线可见性?

在数字化时代&#xff0c;内容质量已经成为增强在线曝光率的关键因素。企业和博客主经常面临挑战&#xff0c;如何制作高质量、无误的内容以吸引更多的在线用户。此文将详细分析“爱校对”如何帮助用户优化内容&#xff0c;从而提高在线可见性。 1.互联网内容的挑战 搜索引擎…

git介绍+集成到IDEA中+使用gitee

目录 git介绍 本地工作流程 IDEA集git 添加到暂存区 添加到本地仓库 gitee使用 添加到远程仓库 git介绍 git是一个开源的分布式版本控制工具&#xff0c;效率高。可以记录历史代码&#xff0c;多人代码共享 知识小点&#xff1a; 集中式版本控制&#xff1a;使用中央存…

在浏览器中打包 TypeScript 系列2:在浏览器中打包 TypeScript

原文地址 这是“在浏览器中打包 TypeScript 系列”的第 2 部分。 第 1 部分&#xff1a;ES 模块和导入映射import maps 打包和转译( Bundling & Transpiling ) 毫无疑问&#xff0c;打包和转译对于 Web 开发至关重要。在深入讨论该主题之前&#xff0c;让我们重申一下什…

Java动态代理、反射

文章目录 动态代理调用者--->代理--->对象为什么需要代理代理的详细实现过程代码详情 反射反射概念反射中常用的方法所有代码 动态代理 调用者—>代理—>对象 动态代理就是无侵入式的给代码增加新的功能&#xff0c;通过接口保证后面的对象和代理需要实现同一个接…

Kubernetes教程—查看 Pod 和节点

目标 了解 Kubernetes Pod。了解 Kubernetes 节点。对已部署的应用故障排除。 Kubernetes Pod 在模块 2 中创建 Deployment 时, Kubernetes 创建了一个 Pod 来托管你的应用实例。Pod 是 Kubernetes 抽象出来的&#xff0c; 表示一组一个或多个应用容器&#xff08;如 Docker…

Nexus2迁移升级到Nexus3

与 Nexus 2.x 相比&#xff0c;Nexus 3.x 为我们提供了更多实用的新特性。SonaType 官方建议我们&#xff0c;使用最新版本 Nexus 2.x 升级到最新版本 Nexus 3.x&#xff0c;并在 Nexus 升级兼容性 一文中为我们提供了各个版本 Nexus 升级到最新版本 Nexus 3.x 的流程&#xff…

opencv如何调用YOLOv5(无pytorch)

目录 一、前言 二.正文 2.1定义颜色 2.2目标检测主代码详解 2.3读取视频or图片进行检测 注意&#xff1a;opencv-python 本文使用的版本为4.5.2.52 一、前言 YOLO系列是one-stage且是基于深度学习的回归方法&#xff0c;而R-CNN、Fast-RCNN、Faster-RCNN等是two-stage且…

情人节特别定制:多种语言编写动态爱心网页(附完整代码)

写在前面案例1&#xff1a;HTML Three.js库案例2&#xff1a;HTML CSS JavaScript案例3&#xff1a;Python环境 Flask框架结语 写在前面 随着七夕节的临近&#xff0c;许多人都在寻找独特而令人难忘的方式来表达爱意。在这个数字时代&#xff0c;结合创意和技术&#xff0…

maven 从官网下载指定版本

1. 进入官网下载页面 Maven – Download Apache Maven 点击下图所示链接 2. 进入文件页&#xff0c;选择需要的版本 3. 选binaries 4. 选文件&#xff0c;下载即可

十亿次实验,用概率解读周易大衍筮法的奥秘

还记得封神电影里的文王占卜吗&#xff1f; 也就是著名的大衍筮法。 《易传》曰&#xff1a;大衍之数五十&#xff0c;其用四十有九。分而为二以象两&#xff0c;挂一以象三&#xff0c; 揲之以四以象四时&#xff0c;归奇于扐以象闰&#xff0c;五岁再闰&#xff0c;故再扐而…

苹果电脑怎么录屏?步骤详解,看到就是赚到

苹果电脑作为一款受欢迎的高性能设备&#xff0c;不仅在日常工作中发挥着重要作用&#xff0c;还可以用于创造内容&#xff0c;如录制屏幕内容。录屏功能能够帮助用户将屏幕上的活动记录成视频&#xff0c;方便分享、演示或存档。可是您知道苹果电脑怎么录屏吗&#xff1f;通过…

Lnton羚通云算力平台【PyTorch】教程:torch.nn.SiLU

torch.nn.SiLU 原型 CLASS torch.nn.SiLU(inplaceFalse) torch.nn.SiLU 是 PyTorch 深度学习框架中的一个激活函数&#xff0c;它代表 Sigmoid-Weighted Linear Unit&#xff08;SiLU&#xff09;&#xff0c;也称为 Swish 激活函数。SiLU 激活函数在深度学习中被广泛使用&…

Unittest+Selenium模块驱动自动化测试实战

UnittestSelenium自动化测试框架使用模块驱动测试模型将冗余的代码封装成类&#xff0c;且基于PageObject的自动化设计模式&#xff0c;通过分层的方式将页面对象、操作、业务分开处理。 1、首先创建自动化测试框架的文件模块架构&#xff0c;创建common、base、testcase、rep…

Kafka单节点部署

&#x1f388; 作者&#xff1a;互联网-小啊宇 &#x1f388; 简介&#xff1a; CSDN 运维领域创作者、阿里云专家博主。目前从事 Kubernetes运维相关工作&#xff0c;擅长Linux系统运维、开源监控软件维护、Kubernetes容器技术、CI/CD持续集成、自动化运维、开源软件部署维护…