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

相关文章
|
26天前
|
安全 Java 数据安全/隐私保护
【深入浅出Spring原理及实战】「EL表达式开发系列」深入解析SpringEL表达式理论详解与实际应用
【深入浅出Spring原理及实战】「EL表达式开发系列」深入解析SpringEL表达式理论详解与实际应用
57 1
|
6天前
|
JSON Java fastjson
Spring Boot 底层级探索系列 04 - Web 开发(2)
Spring Boot 底层级探索系列 04 - Web 开发(2)
15 0
|
6天前
|
Java 关系型数据库 MySQL
一套java+ spring boot与vue+ mysql技术开发的UWB高精度工厂人员定位全套系统源码有应用案例
UWB (ULTRA WIDE BAND, UWB) 技术是一种无线载波通讯技术,它不采用正弦载波,而是利用纳秒级的非正弦波窄脉冲传输数据,因此其所占的频谱范围很宽。一套UWB精确定位系统,最高定位精度可达10cm,具有高精度,高动态,高容量,低功耗的应用。
一套java+ spring boot与vue+ mysql技术开发的UWB高精度工厂人员定位全套系统源码有应用案例
|
7天前
|
负载均衡 Java 开发者
细解微服务架构实践:如何使用Spring Cloud进行Java微服务治理
【4月更文挑战第17天】Spring Cloud是Java微服务治理的首选框架,整合了Eureka(服务发现)、Ribbon(客户端负载均衡)、Hystrix(熔断器)、Zuul(API网关)和Config Server(配置中心)。通过Eureka实现服务注册与发现,Ribbon提供负载均衡,Hystrix实现熔断保护,Zuul作为API网关,Config Server集中管理配置。理解并运用Spring Cloud进行微服务治理是现代Java开发者的关键技能。
|
8天前
|
Java API 对象存储
对象存储OSS产品常见问题之使用Spring Cloud Alibaba情况下文档添加水印如何解决
对象存储OSS是基于互联网的数据存储服务模式,让用户可以安全、可靠地存储大量非结构化数据,如图片、音频、视频、文档等任意类型文件,并通过简单的基于HTTP/HTTPS协议的RESTful API接口进行访问和管理。本帖梳理了用户在实际使用中可能遇到的各种常见问题,涵盖了基础操作、性能优化、安全设置、费用管理、数据备份与恢复、跨区域同步、API接口调用等多个方面。
24 2
|
16天前
|
SpringCloudAlibaba 监控 Java
SpringCloud Alibaba微服务-- Sentinel的使用(保姆级)
SpringCloud Alibaba微服务-- Sentinel的使用(保姆级)
|
16天前
|
SpringCloudAlibaba Java API
SpringCloud Alibaba微服务工程搭建(保姆级)
SpringCloud Alibaba微服务工程搭建(保姆级)
|
16天前
|
SpringCloudAlibaba Java Nacos
SpringCloud Alibaba微服务 -- Nacos使用以及注册中心和配置中心的应用(保姆级)
SpringCloud Alibaba微服务 -- Nacos使用以及注册中心和配置中心的应用(保姆级)
|
21天前
|
开发框架 安全 Java
探索 Spring 框架:企业级应用开发的强大工具
探索 Spring 框架:企业级应用开发的强大工具
19 1
|
23天前
|
缓存 算法 Java
开发必懂的Spring循环依赖图解 Spring 循环依赖
开发必懂的Spring循环依赖图解 Spring 循环依赖
18 1