开篇
Spring Cloud Netflix已经不流行了,因为它已经不维护更新了,也不是说它不能用,只是有比它更好更新的。那就是Spring Cloud Alibaba系列。下面是Spring Cloud Alibaba的特性描述:
使用的版本:
- spring.boot.version:2.5.0 (目前最新)
- spring-cloud-alibaba.version:2.2.1.RELEASE (目前最新)
- dubbot: 2.7.6 (目前最新3.0.0)
关于项目结构
以上的项目结构就可以理解成是一个微服务项目了,一个父工程中包含了2个子工程。 顾名思义,shop-api中存放的是接口和接口对象
shop-service就是对于接口的实现。
和大多数项目开发的架构一样,没有太多区别。
关于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