之前因为业务需要而使用了Rabbit,恰时想到更新服务配置时,兼职运维的我都是去各个服务器逐个重启项目
现在想来,那真是既费劲又傻xx。慢慢的服务开始增多,为了方便运维,决定整合SpringCloudBus。
SpringCloudBus(以下简称sb bus)微服务消息总线,其可以为各服务广播配置文件的更新或者提供微服务之间的通讯。实时刷新配置过于实用,正是小可所需,遂开始搞它!
项目版本介绍 important!
jdk = 1.8springBootVersion = '1.5.8.RELEASE'springCloudVersion = 'Dalston.SR4'
Step 1:导包!
//添加SpringBoot对Amqp的支持: Version:2.1.3.RELEASEcompile group: 'org.springframework.boot', name: 'spring-boot-starter-amqp', version: '2.1.3.RELEASE'//消息总线: Version:2.1.1.RELEASEcompile group: 'org.springframework.cloud', name: 'spring-cloud-starter-bus-amqp', version: '2.1.1.RELEASE'
此为我们的业务服务,导入Amqp的支持是因为项目之前所需RabbitMq,在不清楚bus特性前,没有特意删除。但已经预计到它俩可能会有[冲突]问题,遂标记于上。
仅仅多导了个消息总线的包。导完,启动即报错:
ERROR org.springframework.boot.SpringApplication - Application startup failedjava.lang.NoSuchMethodError: org.springframework.boot.builder.SpringApplicationBuilder.<init>([Ljava/lang/Class;)V
解决方法:
1.版本冲突,使用1.x的spring bus即可
2.或者去除原先spring-boot-strarter-amqp的包(原功能能否健用尚未测试)
原因:spring bus2.x与1.x区别很大,为照顾整个项目的兼容,故选用低版本
解决完上述问题,再次重启,又报了个奇葩Error
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.integration.internalMessagingAnnotationPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call:Method getIntegrationProperties(com.example.spring_bus_demo.springBean.BeanFactory$$EnhancerBySpringCGLIB$$8a224a2f) cannot be found on type org.springframework.integration.context.IntegrationContextUtils
原因出在该类:
com.example.spring_bus_demo.springBean.BeanFactory
为什么说它莫名其妙呢?因为这个是我自定义的配置类:
@Configurationpublic class BeanFactory {/*** 注册过滤器*/@Beanpublic FilterRegistrationBean filterRegistrationBean() {FilterRegistrationBean registrationBean = new FilterRegistrationBean();registrationBean.setFilter(new CustomFilter());return registrationBean;}}
仅仅是我写的一个过滤器,在这里注册下交给Spring管理而已。怎么会报上面那些错?索性我把BeanFactory中的方法都删了,再次启动,仍然报如上错误!
那干脆就把这个类删掉好了,啪!成功!
So , what's the problem?
真的只是因为这个类名而已!一口老血含苞欲放,是个狼人
告辞!
解决方法:类名不要起BeanFactory!若碰见有Spring组件也叫这个名字的(亲测bus就是了)你真的会万脸懵逼~
Step 2: 配置中心
ConfigServer:配置中心,该项目管理所有微服务的配置文件置于git上,具体也无甚内容,非本篇主题,不做置评。其依赖如下:
compile('org.springframework.cloud:spring-cloud-starter-eureka')compile('org.springframework.cloud:spring-cloud-config-server')compile('org.springframework.cloud:spring-cloud-starter-bus-amqp')compile('org.springframework.boot:spring-boot-starter-actuator')
然后客户端(即step1中的项目)增加依赖:
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-config', version: '2.0.2.RELEASE'
Step 3: 刷新配置
将配置文件更新并提交到git仓库。然后调用配置中心的如下接口:
GET: localhost:20001/bus/refresh
发现业务服务中的配置字段并没有更新,并且控制台报错信息如下:
org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException: Listener threw exception......Caused by: org.springframework.messaging.MessagingException: Exception thrown while invoking org.springframework.cloud.bus.BusAutoConfiguration#acceptRemote[1 args];nested exception is java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask@4c497351 rejected from java.util.concurrent.ScheduledThreadPoolExecutor@61424807[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 7]......
解决方法:
spring-cloud-config-server
与spring-cloud-starter-config统一使用1.x版本即可
然后重新导包,启动俩项目后,再改自定义配置文件提交git,成功后向配置中心项目调用/bus/refresh接口,其便会通知各个服务重新抓取最新配置文件。

THE END
此篇主要记录了我整合SpringBus框架时所遇到的些许问题,虽寥寥几笔,但仅第一步就不知道扰我青丝几何。君不见奇葩报错,度娘无迹可寻,只得抓耳挠腮静待灵光一闪,随运编程debug,今夜于此一记,愿可为后人铺路。




