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

springCloudAlibaba集成Dubbo

文章目录

  • 简介
      • 1、父工程
      • 2、接口模块
      • 3、生产者
      • 4、消费者
      • 5、启动

简介

使用springboot版本 : 2.2.5
cloud版本 :2.2.1.RELEASE

1、父工程

创建一个 名为springCloudAlibaba-dubbo父工程,pom.xml默认就好,然后除了pom.xml文件之外全都删掉,

2、接口模块

先创建一个子模块,继承父工程

<parent><artifactId>springCloudAlibaba-dubbo</artifactId><groupId>org.example</groupId><version>1.0-SNAPSHOT</version>
</parent>

然后添加一个接口

接口代码如下:

public interface HelloService {String hello(String name);
}

3、生产者

pom.xml文件继承父工程

<parent><artifactId>springCloudAlibaba-dubbo</artifactId><groupId>org.example</groupId><version>1.0-SNAPSHOT</version>
</parent>

pom.xml文件添加以下依赖

<!--引入公共模块--><dependency><groupId>org.example</groupId><artifactId>common</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId><version>2.2.1.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.2.5.RELEASE</version></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><version>2.2.1.RELEASE</version></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-dubbo</artifactId><version>2.2.1.RELEASE</version></dependency><!--Dubbo Spring Cloud基于Spring Cloud Commons开发的,org.apache.http.client.HttpClient类确实存在于旧版本的httpclient中,但是它使用内部的org.apache.http.impl.client.HttpClientBuilder类在新版本中却不存在--><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.4</version><scope>compile</scope></dependency>

application.yml 配置文件

com:nacos_address: 10.114.57.125:8848
spring:application:name: dubbo-productcloud:nacos:discovery:server-addr: ${com.nacos_address}  # nacos服务注册中心的地址cluster-name: consumer-cluster  #集群名称namespace: b7ef9579-df75-41c2-8a95-e04aa03c273a  #命名空间config:server-addr: ${com.nacos_address}  # nacos配置中心地址namespace: b7ef9579-df75-41c2-8a95-e04aa03c273afile-extension: yaml  #配置文件的后缀名refresh-enabled: true  #默认true:自动刷新
dubbo:scan:base-packages: org.example.service # 扫描带有 org.apache.dubbo.config.annotation.@Service 注解的实现类protocol: #Dubbo 服务暴露的协议配置,其中子属性 name 为协议名称,port 为协议端口( -1 表示自增端口,从 20880 开始)name: dubboport: -1  #dubbo协议缺省端口为20880,rmi协议缺省端口为1099,http和hessian协议缺省端口为80;如果没有配置port,则自动采用默认端口,如果配置为-1,则会分配一个没有被占用的端口。Dubbo 2.4.0+,分配的端口在协议缺省端口的基础上增长,确保端口段可控registry:#dubbo服务注册端口,注册中心服务器地址,如果地址没有端口缺省为9090,同一集群内的多个地址用逗号分隔,如:ip:port,ip:port#其中前缀spring-cloud说明:挂载到 Spring Cloud注册中心address: spring-cloud://10.114.57.125:8848#check: false  #关闭注册中心是否启动的相关检查,false表示不检查注册中心是否启动,就不会报错cloud:subscribed-services: dubbo-productconsumer:check: false  #关闭订阅服务是否启动的检查【检查时,没有服务提供者会报错】

启动类 DubboServerApplication.java

package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient //【此注解可以省略】
@SpringBootApplication
public class DubboServerApplication {public static void main(String[] args) {SpringApplication.run(DubboServerApplication.class, args);}
}

实现类 HelloServiceImpl.java

package org.example.service;
import org.apache.dubbo.config.annotation.Service;
@Service(version = "1.0.0")
public class HelloServiceImpl implements HelloService {@Overridepublic String hello(String name) {String s = "hello " + name;System.out.println("调用了:"+s);return s;}
}

生产者模块目录全览

4、消费者

pom.xml文件继承父工程

<parent><artifactId>springCloudAlibaba-dubbo</artifactId><groupId>org.example</groupId><version>1.0-SNAPSHOT</version>
</parent>

pom.xml加入以下依赖

    <dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><dependency><groupId>org.example</groupId><artifactId>common</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId><version>2.2.1.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.2.5.RELEASE</version></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><version>2.2.1.RELEASE</version></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-dubbo</artifactId><version>2.2.1.RELEASE</version></dependency><!--Dubbo Spring Cloud基于Spring Cloud Commons开发的,org.apache.http.client.HttpClient类确实存在于旧版本的httpclient中,但是它使用内部的org.apache.http.impl.client.HttpClientBuilder类在新版本中却不存在--><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.4</version><scope>compile</scope></dependency>

application.yml 配置文件内容


com:nacos_address: 10.114.57.125:8848
server:port: 8081
spring:application:name: dubbo-consumercloud:nacos:discovery:server-addr: ${com.nacos_address}  # nacos服务注册中心的地址cluster-name: consumer-cluster  #集群名称namespace: b7ef9579-df75-41c2-8a95-e04aa03c273a  #命名空间config:server-addr: ${com.nacos_address}  # nacos配置中心地址namespace: b7ef9579-df75-41c2-8a95-e04aa03c273agroup: test-groupfile-extension: yaml  #配置文件的后缀名
dubbo:protocol:    #Dubbo 服务暴露的协议配置,其中子属性 name 为协议名称,port 为协议端口( -1 表示自增端口,从 20880 开始)name: dubboport: -1  #dubbo协议缺省端口为20880,rmi协议缺省端口为1099,http和hessian协议缺省端口为80;如果没有配置port,则自动采用默认端口,如果配置为-1,则会分配一个没有被占用的端口。Dubbo 2.4.0+,分配的端口在协议缺省端口的基础上增长,确保端口段可控registry:#其中前缀spring-cloud说明:挂载到 Spring Cloud注册中心address: spring-cloud://${com.nacos_address}  #dubbo服务注册端口,注册中心服务器地址,如果地址没有端口缺省为9090,同一集群内的多个地址用逗号分隔,如:ip:port,ip:portcloud:subscribed-services: dubbo-product

启动类和controller层 ConsumerApplication.java

package org.example;
import org.apache.dubbo.config.annotation.Reference;
import org.example.service.HelloService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@EnableDiscoveryClient
@SpringBootApplication
@RestController
public class ConsumerApplication {// 版本号必须和 @Service注解上的 version一致@Reference(version = "1.0.0",check = false)private HelloService helloService;public static void main(String[] args) {SpringApplication.run(ConsumerApplication.class, args);}@GetMapping("/test")public String test() {return helloService.hello("didispace.com");}
}

消费者模块全览

5、启动

注意:如果 @Reference注解没有加上 check = false配置,就必须先启动生产者,然后在启动消费者,否则会报错!

启动成功后如下图

在浏览器访问消费者接口 http://localhost:8081/test,可以正常访问

http://www.dtcms.com/a/303552.html

相关文章:

  • 【版本更新】火语言 0.9.94.0 更新
  • 虚拟面孔,真实革命
  • Product Hunt 每日热榜 | 2025-07-28
  • JAVA_EIGHTEEN_特殊文件
  • STM32——寄存器映射
  • LLaMA-Factory微调教程2:命令行sft微调
  • 【拓扑排序 缩点】P2272 [ZJOI2007] 最大半连通子图|省选-
  • 【跳跃游戏】
  • BUUCTF-MISC-[HBNIS2018]caesar1
  • Linux驱动22 --- RV1126 环境搭建设备树修改
  • 从零到一:我是如何用深度学习打造高性能书籍推荐系统的
  • mp核心功能
  • 零基础学习性能测试第九章:全链路追踪-项目实操
  • 猎板 PCB 控深槽工艺:5G 基站散热模块的关键支撑
  • 解决c++运行时提示:first defined here (重复定义问题)
  • **线程与进程的区别与联系**
  • Qt下使用图形视图框架实现图像上各图形绘制
  • 一个Pycharm窗口添加多个项目来满足运行多个项目的需求
  • linux常用的指令
  • HTML响应式SEO公司网站源码
  • MVSNet系列网络概述
  • 7寸工业模组 XA070Y2-L01芯显科技详细参数资料
  • MCU中的外设总线是什么?
  • 带 USB 接口的多功能 AI 降噪消回音模组 A-59P:革新语音处理体验​
  • 基于Flask的智能停车场管理系统开发实践
  • Java基础-IO流
  • Python day27
  • GoLand 项目从 0 到 1:第三天 —— 图数据库版本管理方案调研与中间件部署
  • 064_不可变集合与同步集合
  • python列表与元组--python005