alibaba / spring-cloud-alibaba

Spring Cloud Alibaba provides a one-stop solution for application development for the distributed solutions of Alibaba middleware.
https://sca.aliyun.com
Apache License 2.0
27.94k stars 8.33k forks source link

Spring Cloud Stream整合RocketMQ出错 #1334

Open Zengsh123 opened 4 years ago

Zengsh123 commented 4 years ago

我们鼓励使用英文,如果不能直接使用,可以使用翻译软件,您仍旧可以保留中文原文。 We recommend using English. If you are non-native English speaker, you can use the translation software.

Which Component RocketMQ

Describe what problem you have encountered 我按照例子尝试用spring cloud alibaba stream整合rocketMQ发送消息,但是运行不起来,以下是我的代码: I try to use spring cloud Alibaba stream to integrate rocketmq to send messages as an example, but I can't run it. Here is my code:

  1. pom依赖(POM dependence)
    
    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <springboot.version>2.2.5.RELEASE</springboot.version>
        <spring-cloud-alibaba.version>2.1.0.RELEASE</spring-cloud-alibaba.version>
    </properties>
com.alibaba.cloud spring-cloud-starter-stream-rocketmq org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-starter-test
2. yml配置(YML configuration)
```yaml
spring:
  application:
    name: shop-order-service
  cloud:
    stream:
      bindings:
        output:
          destination: orderTopic
          content-type: application/json
        input:
          destination: orderTopic
          content-type: application/json
          group: order_orderTopic_cancel_group
      rocketmq:
        binder:
          name-server: IP1, IP2
        bindings:
          output:
            producer:
              group: orderProducerGroup
  1. 消息发送类(Message sending tool class)

    @Service
    public class SenderService {
    /**
     * 获取name为output的binding
     */
    @Autowired
    private MessageChannel output;
    
    public <T> void sendObject(T msg, String tag) throws Exception {
        Message message = MessageBuilder.withPayload(msg)
                .setHeader(MessageConst.PROPERTY_TAGS, tag)
                .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
                .build();
        output.send(message);
    }
    }
  2. 测试代码(Test code)

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = OrderApplication.class)
    public class TestApplication {
    @Autowired
    private SenderService senderService;
    
    @Test
    public void send(){
        CancelOrderMQ cancelOrderMQ = new CancelOrderMQ();
        cancelOrderMQ.setOrderId(6);
        cancelOrderMQ.setCouponId(6L);
        cancelOrderMQ.setGoodsId(6L);
        cancelOrderMQ.setGoodsNumber(6);
        cancelOrderMQ.setUserId(6L);
        cancelOrderMQ.setUserMoney(BigDecimal.valueOf(233L));
        try {
            senderService.sendObject(cancelOrderMQ, "order_test");
        } catch (Exception ex) {
            System.out.println("发送确认订单失败消息-----失败");
            ex.printStackTrace();
        }
    }
    }
  3. 遇到错误(error information)

    
    java.lang.IllegalStateException: Failed to load ApplicationContext
    
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:123)
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190)
    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:244)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
    Caused by: java.lang.IllegalStateException: Error processing condition on com.alibaba.cloud.stream.binder.rocketmq.config.RocketMQComponent4BinderAutoConfiguration.rocketMQTemplate
    at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:60)
    at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:108)
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(ConfigurationClassBeanDefinitionReader.java:184)
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:144)
    at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:120)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:331)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:236)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:706)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:126)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
    ... 24 more
    Caused by: java.lang.IllegalStateException: Failed to introspect Class [com.alibaba.cloud.stream.binder.rocketmq.config.RocketMQComponent4BinderAutoConfiguration] from ClassLoader [sun.misc.Launcher$AppClassLoader@18b4aac2]
    at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:481)
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:358)
    at org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods(ReflectionUtils.java:414)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.lambda$getTypeForFactoryMethod$2(AbstractAutowireCapableBeanFactory.java:743)
    at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:742)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:681)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:649)
    at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1605)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:520)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:491)
    at org.springframework.boot.autoconfigure.condition.OnBeanCondition.collectBeanNamesForType(OnBeanCondition.java:230)
    at org.springframework.boot.autoconfigure.condition.OnBeanCondition.getBeanNamesForType(OnBeanCondition.java:223)
    at org.springframework.boot.autoconfigure.condition.OnBeanCondition.getBeanNamesForType(OnBeanCondition.java:213)
    at org.springframework.boot.autoconfigure.condition.OnBeanCondition.getMatchingBeans(OnBeanCondition.java:167)
    at org.springframework.boot.autoconfigure.condition.OnBeanCondition.getMatchOutcome(OnBeanCondition.java:142)
    at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:47)
    ... 40 more
    Caused by: java.lang.NoClassDefFoundError: org/apache/rocketmq/spring/config/TransactionHandlerRegistry
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.getDeclaredMethods(Class.java:1975)
    at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:463)
    ... 56 more
    Caused by: java.lang.ClassNotFoundException: org.apache.rocketmq.spring.config.TransactionHandlerRegistry
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 60 more

Process finished with exit code -1


**Describe what information you have read**
 I have read the reference doc of RocketMQ
echooymxq commented 4 years ago

Please check your dependencies more carefully.

Zengsh123 commented 4 years ago

Please check your dependencies more carefully.

我跟官网的例子仔细地核对了一遍,实在搞不清楚哪的依赖出了问题 I checked it carefully with the example on the official website. I really don't know what's wrong with the dependency

zbjincheng commented 4 years ago

spring-cloud-starter-stream-rocketmq 2.1.0.release引入rocketmq包的版本为4.4.0,版本较低,可能与你的mq服务不兼容,检查下服务的版本。

fangjian0423 commented 4 years ago
  1. 确保 MQ 客户端和服务端版本能兼容
  2. Spring Boot 2.2.x 版本需要跟 Spring Cloud Alibaba 2.2.x 版本对齐
Zengsh123 commented 4 years ago

好的,谢谢

dingfei98 commented 4 years ago

你好,你的问题解决了吗

Zengsh123 commented 4 years ago

你好,你的问题解决了吗

原因是rocketmq版本太高了,Spring Cloud Stream本身支持的版本太低了,跟不上rocketmq更新的速度

dingfei98 commented 4 years ago

你好,你的问题解决了吗吗

原因是rocketmq版本太高了,Spring Cloud Stream本身支持的版本太低了,跟不上rocketmq更新的速度

是的问题我查到了,我查找了阿里提供的stream和springcloud的stream,发现里面导入的rocketmq都是4.4.0版本的,最多只支持到4.5.0版本,4.6以后rocketmq的代码好像改动了不少,发送消息的时候不用在添加group了,但是stream却不支持了,我看见阿里云仓库里面有第三方的包我尝试了一下,第三方的包是可以支持最新版本的4.7,但是官方版没有使用,我就把版本退回到了4.5

zkzlx commented 3 years ago

please try the beta version #2029 to see if it solves your problem, or provide us more information to help find the latent bug.

kim709394 commented 3 years ago

你好,你的问题解决了吗

原因是rocketmq版本太高了,Spring Cloud Stream本身支持的版本太低了,跟不上rocketmq更新的速度

我的rocketmq是4.4.0的版本,依然还是出现这个问题,你是如何解决的呢?