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

目录
相关文章
|
28天前
|
安全 前端开发 Java
《深入理解Spring》:现代Java开发的核心框架
Spring自2003年诞生以来,已成为Java企业级开发的基石,凭借IoC、AOP、声明式编程等核心特性,极大简化了开发复杂度。本系列将深入解析Spring框架核心原理及Spring Boot、Cloud、Security等生态组件,助力开发者构建高效、可扩展的应用体系。(238字)
|
1月前
|
消息中间件 缓存 Java
Spring框架优化:提高Java应用的性能与适应性
以上方法均旨在综合考虑Java Spring 应该程序设计原则, 数据库交互, 编码实践和系统架构布局等多角度因素, 旨在达到高效稳定运转目标同时也易于未来扩展.
110 8
|
2月前
|
安全 Java 应用服务中间件
Spring Boot + Java 21:内存减少 60%,启动速度提高 30% — 零代码
通过调整三个JVM和Spring Boot配置开关,无需重写代码即可显著优化Java应用性能:内存减少60%,启动速度提升30%。适用于所有在JVM上运行API的生产团队,低成本实现高效能。
230 3
|
2月前
|
消息中间件 人工智能 Java
抖音微信爆款小游戏大全:免费休闲/竞技/益智/PHP+Java全筏开源开发
本文基于2025年最新行业数据,深入解析抖音/微信爆款小游戏的开发逻辑,重点讲解PHP+Java双引擎架构实战,涵盖技术选型、架构设计、性能优化与开源生态,提供完整开源工具链,助力开发者从理论到落地打造高留存、高并发的小游戏产品。
|
2月前
|
存储 Java 关系型数据库
Java 项目实战基于面向对象思想的汽车租赁系统开发实例 汽车租赁系统 Java 面向对象项目实战
本文介绍基于Java面向对象编程的汽车租赁系统技术方案与应用实例,涵盖系统功能需求分析、类设计、数据库设计及具体代码实现,帮助开发者掌握Java在实际项目中的应用。
90 0
|
3月前
|
安全 Java 数据库
Java 项目实战病人挂号系统网站设计开发步骤及核心功能实现指南
本文介绍了基于Java的病人挂号系统网站的技术方案与应用实例,涵盖SSM与Spring Boot框架选型、数据库设计、功能模块划分及安全机制实现。系统支持患者在线注册、登录、挂号与预约,管理员可进行医院信息与排班管理。通过实际案例展示系统开发流程与核心代码实现,为Java Web医疗项目开发提供参考。
178 2
|
3月前
|
JavaScript 安全 前端开发
Java开发:最新技术驱动的病人挂号系统实操指南与全流程操作技巧汇总
本文介绍基于Spring Boot 3.x、Vue 3等最新技术构建现代化病人挂号系统,涵盖技术选型、核心功能实现与部署方案,助力开发者快速搭建高效、安全的医疗挂号平台。
202 3
|
1月前
|
JSON 网络协议 安全
【Java】(10)进程与线程的关系、Tread类;讲解基本线程安全、网络编程内容;JSON序列化与反序列化
几乎所有的操作系统都支持进程的概念,进程是处于运行过程中的程序,并且具有一定的独立功能,进程是系统进行资源分配和调度的一个独立单位一般而言,进程包含如下三个特征。独立性动态性并发性。
115 1
|
1月前
|
JSON 网络协议 安全
【Java基础】(1)进程与线程的关系、Tread类;讲解基本线程安全、网络编程内容;JSON序列化与反序列化
几乎所有的操作系统都支持进程的概念,进程是处于运行过程中的程序,并且具有一定的独立功能,进程是系统进行资源分配和调度的一个独立单位一般而言,进程包含如下三个特征。独立性动态性并发性。
128 1