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

目录
相关文章
|
5月前
|
人工智能 Java Nacos
基于 Spring AI Alibaba + Nacos 的分布式 Multi-Agent 构建指南
本文将针对 Spring AI Alibaba + Nacos 的分布式多智能体构建方案展开介绍,同时结合 Demo 说明快速开发方法与实际效果。
4301 85
|
5月前
|
人工智能 运维 Java
Spring AI Alibaba Admin 开源!以数据为中心的 Agent 开发平台
Spring AI Alibaba Admin 正式发布!一站式实现 Prompt 管理、动态热更新、评测集构建、自动化评估与全链路可观测,助力企业高效构建可信赖的 AI Agent 应用。开源共建,现已上线!
6567 91
|
6月前
|
安全 Java 应用服务中间件
Spring Boot + Java 21:内存减少 60%,启动速度提高 30% — 零代码
通过调整三个JVM和Spring Boot配置开关,无需重写代码即可显著优化Java应用性能:内存减少60%,启动速度提升30%。适用于所有在JVM上运行API的生产团队,低成本实现高效能。
765 3
|
5月前
|
安全 前端开发 Java
《深入理解Spring》:现代Java开发的核心框架
Spring自2003年诞生以来,已成为Java企业级开发的基石,凭借IoC、AOP、声明式编程等核心特性,极大简化了开发复杂度。本系列将深入解析Spring框架核心原理及Spring Boot、Cloud、Security等生态组件,助力开发者构建高效、可扩展的应用体系。(238字)
|
5月前
|
监控 Cloud Native Java
Spring Boot 3.x 微服务架构实战指南
🌟蒋星熠Jaxonic,技术宇宙中的星际旅人。深耕Spring Boot 3.x与微服务架构,探索云原生、性能优化与高可用系统设计。以代码为笔,在二进制星河中谱写极客诗篇。关注我,共赴技术星辰大海!(238字)
1052 2
Spring Boot 3.x 微服务架构实战指南
|
5月前
|
XML Java 测试技术
《深入理解Spring》:IoC容器核心原理与实战
Spring IoC通过控制反转与依赖注入实现对象间的解耦,由容器统一管理Bean的生命周期与依赖关系。支持XML、注解和Java配置三种方式,结合作用域、条件化配置与循环依赖处理等机制,提升应用的可维护性与可测试性,是现代Java开发的核心基石。
|
5月前
|
消息中间件 缓存 Java
Spring框架优化:提高Java应用的性能与适应性
以上方法均旨在综合考虑Java Spring 应该程序设计原则, 数据库交互, 编码实践和系统架构布局等多角度因素, 旨在达到高效稳定运转目标同时也易于未来扩展.
381 8
|
5月前
|
人工智能 监控 Java
Spring AI Alibaba实践|后台定时Agent
基于Spring AI Alibaba框架,可构建自主运行的AI Agent,突破传统Chat模式限制,支持定时任务、事件响应与人工协同,实现数据采集、分析到决策的自动化闭环,提升企业智能化效率。
Spring AI Alibaba实践|后台定时Agent
|
Java 开发工具 git
Java开发初级6.24.3
5.在Git使用过程中,进行Git配置的操作命令是哪个() A. config B. config -g C. config -a D. git config 相关知识点: 在git中,经常使用git config 命令用来配置git的配置文件,git配置级别主要有:仓库级别 local 【优先级最高】、用户级别 global【优先级次之】、系统级别 system【优先级最低】 正确答案:D 10.RDBMS是什么? A. Rela Database Management Systems B. Relational Database Management Systems C. Relation
252 0

热门文章

最新文章