Spring Cloud Alibaba & Dubbo整合|Java 开发实战

简介: Spring Cloud Netflix已经不流行了,因为它已经不维护更新了,也不是说它不能用,只是有比它更好更新的。

开篇

Spring Cloud Netflix已经不流行了,因为它已经不维护更新了,也不是说它不能用,只是有比它更好更新的。那就是Spring Cloud Alibaba系列。下面是Spring Cloud Alibaba的特性描述:

1d96553fe80cec3a0467f95e66bd3b2.png

使用的版本:

  • spring.boot.version:2.5.0 (目前最新)
  • spring-cloud-alibaba.version:2.2.1.RELEASE (目前最新)
  • dubbot: 2.7.6 (目前最新3.0.0)

关于项目结构

cc8d35cc05b28d1700d024ff4b9d3c7.png

以上的项目结构就可以理解成是一个微服务项目了,一个父工程中包含了2个子工程。 顾名思义,shop-api中存放的是接口和接口对象

6f5750fdad6072e9adaa628001fdc44.png

shop-service就是对于接口的实现。

370946b4b214aa0c0f4c1c3642eac74.png

和大多数项目开发的架构一样,没有太多区别。

关于POM依赖

shop-api 的 pom.xml

没有什么特别的地方,就是需要什么用什么,和本文的Spring Cloud Alibaba & Dubbo整合主题无关。

<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
        <groupId>com.dove.jls</groupId>
        <artifactId>dove-common</artifactId>
    </dependency>
    <!--swagger api doc-->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
    </dependency>
    <dependency>
        <groupId>com.github.xiaoymin</groupId>
        <artifactId>knife4j-spring-boot-starter</artifactId>
    </dependency>
    <!--swagger api doc-->
    <!--参考 https://blog.csdn.net/w57685321/article/details/106783433/-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
</dependencies>
复制代码

shop-service 的 pom.xml

在shop-service中引入如下依赖,具有了dubbo和nacos相关的功能。

<!--整合spring cloud alibaba dubbo  -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<!--整合spring cloud alibaba nacos-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    <exclusions>
        <!-- 解决 AnnotationConfigApplicationContext already exit-->
        <exclusion>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-context</artifactId>
        </exclusion>
    </exclusions>
</dependency>
复制代码

这里没有明确版本号,是因为在父工程的pom.xml有相关定义

<dependencyManagement>
    <dependencies>
        <!--spring-cloud-alibaba 相关的依赖pom-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>${spring-cloud-alibaba.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!--spring-boot 相关的依赖pom-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring.boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
复制代码
</ dependency >
< dependency >
< groupId > org . apache . dubbr < artifactid 
 dubbo 
< version >2.7.6v/ version >< scope > compile </ scope >
< exclusions >
< exclusion >
< artifactId > spring - context </ artifactId >
 sroupId >
 ar LifactId >

关于配置

具体介绍餐卡:dubbo配置参考手册

# 解决Invalid name=“org.apache.dubbo.config.ApplicationConfig#0“
dubbo.application.id=shop-service
dubbo.application.name=shop-service
dubbo.registry.address=nacos://localhost:8848
dubbo.protocol.name=dubbo
dubbo.protocol.port=20881
dubbo.protocol.dispatcher=all
dubbo.protocol.threadpool=fixed
dubbo.protocol.threads=10
复制代码

异常情况

启动的时候报了关于Ribbon错误,想到是和请求相关,如果引入可以解决,

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>${spring-cloud.version}</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>
复制代码
2021-06-08 18:20:51.551 I1F0 14200 --- [  main] com.alibaba.spring.util.BeanRegistrar :The Infrastructure bean definition [Root bean: class [org.apache.dubbo.config.spring.context.DubboBootstrapApplicationListener]; 
dependencyCheclc0:autowireCandidate=true:primarycfalse:factoryBeanlame=mll: factoryllthodane=mll: initethodlaneml:destroyethodane=mllwith name[dubboBootstrapApplicationlistener] has ben registered
2021-06-08 18:20:51.551 I11F0 14200 --- [ main] com.alibaba. spring.util.BeanRegistrar  :The Infrastructure bean definition [Root bean: class [org.apache.dubbo.config spring beans.factory config DubboConfigDefaultProper 
:+  autowirllode=0: dependeneychedleo: antoirecandidattru prinarysfals:facteryamnemll: facteryethedanemll; initetheaamnemll: destroyl thodlancnulwith no[duboconfielefmltfropertyalesanpostfrocerorl has  
2021-06-08 18:20:51.596 ERROR 14200 --- [ main] o.s.boot.SpringApplication  : Application run failed  
javalangIllegalStateException: Error processing condition on org.springframework. cloud. netf ix.ribbon.RibbonAutoConfiguration.loadBalancerClient
at org.springframeworkboot.autoconfigure.conditionSpringBootConditionmatches(SpringBootConditioniava60)[spring-boot-autoconfigure-2.3.5.RELEASE. jar:2.3.5.RELEASEI
at orgspringframework.contextannotationConditionEvaluator.shouldSkipConditionEvaluatorjava108)[springcontext-5.2.10.RELEASE.jar:5.2.10. RELEASE
at ore springfrumemork context.annotationConfigwationclssBeanDefinitioneader1oadsenDefinitionForBeanethodConfiurationasBeanefinitioneaderiava18)[springcontext-5.2 10. RLEASE jur:5 2.10.RELEASE]
at ore springfraneorkcontextamnotationConfigwrationCssBeanefiniionkeader1adeanefinitionsFerConfigrationClssConfuationCasseamDefinitinenderisv14springcontext-5.2 10O RELEASE. jur:5.2.10.RELEASE
at orespringframeworkcontext.anotationConfigurationclassBeanlefinitionkeaderoadBeanDefinitionsConfigurationClasBeanDefinitionReaderjsva:120)[springcontext-5.2.10.RELEASE. ar:5.2.10.RELEASE]
at orgspringfranework context.anotationConfigurationClassPostProcessorprocessCnfigeanDefinitionsConfigurationCassPostProcesoriva331[springcontext-5.2.10.RELEASE jar:5.2.10.RELEASE
at org springframewor context annotation ConficurationClassPostProcessor  ProcessEeanDefinitionRegistryConfisurationC1assPostProcessoriava:236)~[spring-context-5.2.10. RELEASE.jar:5.2.10.RELEA

但是想到openFeign比Ribbon更好,所以试了试单独引入,问题解决。(3.0.3需要springboot版本2.4.x之上)

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>3.0.3</version>
</dependency>
复制代码

效果

45 WAR1520!---[askescheduler-1]acdmr DubbServiclletadstaRepository:Cwrent aplicaionwill suberibeall services(size:0)in eristryalot of memoryand PU cyeles may be used. thus it's stronely reconm
ces’ to specify the services
204 I1FO 15204 --- [  main] c.a.c.d.s.DubboMetadataServiceExporter  :The Dubbo service[<dubbo:service unexported="false" exported="true" />] has been exported  
230 I1FO 15204 --- [  main] c.a.c.n.registry.lacosServiceRegistry nacos registry DEFAULT GROUP shop-service 10.130.11.216:8080 register finished  
.563 I1TF0 15204 --- [  main] commty.jls.shop.ShopServiceApplication:Started ShopServiceApplication in 18.599 seconds (JVM running for 20.702)  
565 I1FO 15204 --- [  main] o.s.b.a.ApplicationAvailabilityBean :Application availability state LivenessState changed to CORRECT  
567 I1FO 15204 --- [  main] o.s.b.a.ApplicationAvailabilityBean :Application availability state ReadinessState changed to ACCEPTING_TRAFFIC 
366 WA15204 --[askescheduler1] acdmr Duboserviceletadatakepository:Cwrent pplication will subsribe all serviceGize:5)in reristryalot of memory and cpu cyeles may be used thus it's stronely recom
ces, to specify the services
576 0152 --- [elient.listener]a DubbServiceDiscoveryhutoconfigration:Theevent of the service instancesane:providers conmtyilsshop aIbOrderService10.0, size:1] chane is about to be dispatcha
578 IF015204--- [client.listener] c.a.c.d.registry.SpringCloudRegistry  :The Dubbo Service uRL:provider:192168.561:2081/commtyjls shop pi mhfroduetservice?version-1.0.0]b机上掘金使味社区  
1.0.0:1

06e171bc9b334d7f9c4f4e28761c447.png

目录
相关文章
|
人工智能 Java 决策智能
Java版Manus实现来了,Spring AI Alibaba发布开源OpenManus实现
此次官方发布的 Spring AI Alibaba OpenManus 实现,包含完整的多智能体任务规划、思考与执行流程,可以让开发者体验 Java 版本的多智能体效果。它能够根据用户的问题进行分析,操作浏览器,执行代码等来完成复杂任务等。
2479 57
|
人工智能 Java 定位技术
Java 开发玩转 MCP:从 Claude 自动化到 Spring AI Alibaba 生态整合
本文详细讲解了Java开发者如何基于Spring AI Alibaba框架玩转MCP(Model Context Protocol),涵盖基础概念、快速体验、服务发布与调用等内容。重点包括将Spring应用发布为MCP Server(支持stdio与SSE模式)、开发MCP Client调用服务,以及在Spring AI Alibaba的OpenManus中使用MCP增强工具能力。通过实际示例,如天气查询与百度地图路线规划,展示了MCP在AI应用中的强大作用。最后总结了MCP对AI开发的意义及其在Spring AI中的实现价值。
3145 9
|
负载均衡 Dubbo Java
Spring Cloud Alibaba与Spring Cloud区别和联系?
Spring Cloud Alibaba与Spring Cloud区别和联系?
|
SpringCloudAlibaba 负载均衡 Dubbo
【SpringCloud Alibaba系列】Dubbo高级特性篇
本章我们介绍Dubbo的常用高级特性,包括序列化、地址缓存、超时与重试机制、多版本、负载均衡。集群容错、服务降级等。
2076 7
【SpringCloud Alibaba系列】Dubbo高级特性篇
|
人工智能 SpringCloudAlibaba 自然语言处理
SpringCloud Alibaba AI整合DeepSeek落地AI项目实战
在现代软件开发领域,微服务架构因其灵活性、可扩展性和模块化特性而受到广泛欢迎。微服务架构通过将大型应用程序拆分为多个小型、独立的服务,每个服务运行在其独立的进程中,服务与服务间通过轻量级通信机制(通常是HTTP API)进行通信。这种架构模式有助于提升系统的可维护性、可扩展性和开发效率。
5576 2
|
存储 SpringCloudAlibaba Java
【SpringCloud Alibaba系列】一文全面解析Zookeeper安装、常用命令、JavaAPI操作、Watch事件监听、分布式锁、集群搭建、核心理论
一文全面解析Zookeeper安装、常用命令、JavaAPI操作、Watch事件监听、分布式锁、集群搭建、核心理论。
【SpringCloud Alibaba系列】一文全面解析Zookeeper安装、常用命令、JavaAPI操作、Watch事件监听、分布式锁、集群搭建、核心理论
|
SpringCloudAlibaba JavaScript Dubbo
【SpringCloud Alibaba系列】Dubbo dubbo-admin安装教程篇
本文介绍了 Dubbo-Admin 的安装和使用步骤。Dubbo-Admin 是一个前后端分离的项目,前端基于 Vue,后端基于 Spring Boot。安装前需确保开发环境(Windows 10)已安装 JDK、Maven 和 Node.js,并在 Linux CentOS 7 上部署 Zookeeper 作为注册中心。
4245 1
【SpringCloud Alibaba系列】Dubbo dubbo-admin安装教程篇
|
SpringCloudAlibaba Dubbo Java
【SpringCloud Alibaba系列】Dubbo基础入门篇
Dubbo是一款高性能、轻量级的开源Java RPC框架,提供面向接口代理的高性能RPC调用、智能负载均衡、服务自动注册和发现、运行期流量调度、可视化服务治理和运维等功能。
【SpringCloud Alibaba系列】Dubbo基础入门篇
|
人工智能 自然语言处理 Java
Spring Cloud Alibaba AI 入门与实践
本文将介绍 Spring Cloud Alibaba AI 的基本概念、主要特性和功能,并演示如何完成一个在线聊天和在线画图的 AI 应用。
4343 8