0

我有一个通过rabbitmq 进行通信的微服务。它的pom如下:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

 
  <artifactId>demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.0</version>
    <relativePath/> 
  </parent>

  <dependencies>

    <dependency>
      <groupId>org.springframework.integration</groupId>
      <artifactId>spring-integration-amqp</artifactId>
    </dependency>

   
    <dependency>
      <groupId>org.springframework.amqp</groupId>
      <artifactId>spring-rabbit</artifactId>
    </dependency>


    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
   
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <scope>runtime</scope>
      <optional>true</optional>
    </dependency>
   
   <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>
    
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework.amqp</groupId>
      <artifactId>spring-rabbit-test</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework.integration</groupId>
      <artifactId>spring-integration-ip</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis-reactive</artifactId>
    </dependency>

    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.12.0</version>
    </dependency>

    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
    </dependency>
   
   <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
    </dependency>
   
   <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
    </dependency>

  </dependencies>


  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

</project>

RabbitMqConfiguration 在 @Configuration 类中配置:

  @Bean
  public ConnectionFactory connectionFactory() {
    com.rabbitmq.client.ConnectionFactory connectionFactoryClient = new com.rabbitmq.client.ConnectionFactory();
    connectionFactoryClient.setUsername(username);
    connectionFactoryClient.setPassword(password);
    connectionFactoryClient.setHost(hostname);
    connectionFactoryClient.setVirtualHost(vhost);
    return new ThreadChannelConnectionFactory(connectionFactoryClient);}

  @Bean
  @Primary
  public RabbitTemplate rabbitTemplateAdapter(ConnectionFactory connectionFactory) {
    RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
    rabbitTemplate.setMandatory(true);
    return rabbitTemplate;
  }

我还在 AppConfiguration 类中定义了 ObjectToJsonTransformer 和 JsonToObjectTransformer bean:

  @Bean
  public ObjectToJsonTransformer objectToJsonTransformer() {
    return new ObjectToJsonTransformer();
  }

  @Bean
  public JsonToObjectTransformer jsonToObjectTransformer() {
    return new JsonToObjectTransformer();
  }

我有一个使用 ObjectToJsonTransformer 的 @Component 类:

public class RabbitMessageService {
  private final RabbitTemplate rabbitTemplateAdapter;
  private final ObjectToJsonTransformer objectToJsonTransformer;
  
  public void messageTransforming(Message<?> message) {
     Message<?> jsonMessage = objectToJsonTransformer.transform(message);
    try {...}
}
....}

问题是,当我将其部署到 Cloud Foundry(版本 6.46.0+29d6257f1.2019-07-09)时,出现以下错误:

    Application run failed
           2021-06-14T16:54:21.36+0300 Error creating bean with name 'rabbitMessageService ' defined in file [....]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: 
    Error creating bean with name 'objectToJsonTransformer' defined in class path resource [/path to ApplicationConfiguration class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate
[org.springframework.integration.json.ObjectToJsonTransformer]: Factory method 'objectToJsonTransformer' threw exception; nested exception is java.lang.VerifyError: Bad return type
           2021-06-14T16:54:21.36+0300  OUT Exception Details:
           2021-06-14T16:54:21.36+0300  OUT   Location:
           2021-06-14T16:54:21.36+0300  OUT     com/fasterxml/jackson/databind/cfg/MapperBuilder.streamFactory()Lcom/fasterxml/jackson/core/TokenStreamFactory; @7: areturn
           2021-06-14T16:54:21.36+0300  OUT   Reason:
           2021-06-14T16:54:21.36+0300  OUT     Type 'com/fasterxml/jackson/core/JsonFactory' (current frame, stack[0]) is not assignable to 'com/fasterxml/jackson/core/TokenStreamFactory' (from method signature)
           2021-06-14T16:54:21.36+0300  OUT   Current Frame:
           2021-06-14T16:54:21.36+0300  OUT     bci: @7
           2021-06-14T16:54:21.36+0300  OUT     flags: { }
           2021-06-14T16:54:21.36+0300  OUT     locals: { 'com/fasterxml/jackson/databind/cfg/MapperBuilder' }
           2021-06-14T16:54:21.36+0300  OUT     stack: { 'com/fasterxml/jackson/core/JsonFactory' }
           2021-06-14T16:54:21.36+0300  OUT   Bytecode:
           2021-06-14T16:54:21.36+0300  OUT     0x0000000: 2ab4 0002 b600 08b0
           2021-06-14T16:54:21.36+0300  OUT     at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) ~[spring-beans-5.3.7.jar:5.3.7]
           2021-06-14T16:54:21.36+0300  OUT     at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229) ~[spring-beans-5.3.7.jar:5.3.7]
           2021-06-14T16:54:21.36+0300  OUT     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1354) ~[spring-beans-5.3.7.jar:5.3.7]
           2021-06-14T16:54:21.36+0300  OUT     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1204) ~[spring-beans-5.3.7.jar:5.3.7]
           2021-06-14T16:54:21.36+0300  OUT     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564) ~[spring-beans-5.3.7.jar:5.3.7]
           2021-06-14T16:54:21.36+0300  OUT     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.7.jar:5.3.7]
           2021-06-14T16:54:21.36+0300  OUT     at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.7.jar:5.3.7]
           2021-06-14T16:54:21.36+0300  OUT     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.7.jar:5.3.7]
           2021-06-14T16:54:21.36+0300  OUT     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.7.jar:5.3.7]
           2021-06-14T16:54:21.36+0300  OUT     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.7.jar:5.3.7]
           2021-06-14T16:54:21.36+0300  OUT     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.7.jar:5.3.7]
           2021-06-14T16:54:21.36+0300  OUT     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.7.jar:5.3.7]
           2021-06-14T16:54:21.36+0300  OUT     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.7.jar:5.3.7]
           2021-06-14T16:54:21.36+0300  OUT     at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) [spring-boot-2.5.0.jar:2.5.0]
           2021-06-14T16:54:21.36+0300  OUT     at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:438) [spring-boot-2.5.0.jar:2.5.0]
           2021-06-14T16:54:21.36+0300  OUT     at org.springframework.boot.SpringApplication.run(SpringApplication.java:337) [spring-boot-2.5.0.jar:2.5.0]
           2021-06-14T16:54:21.36+0300  OUT     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1336) [spring-boot-2.5.0.jar:2.5.0]
           2021-06-14T16:54:21.36+0300  OUT     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1325) [spring-boot-2.5.0.jar:2.5.0]
           2021-06-14T16:54:21.36+0300  OUT     at pathToFile.ApplicationRun.main(ApplicationRun.java:10) [classes/:0.0.1-SNAPSHOT]
           2021-06-14T16:54:21.36+0300  OUT   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_202]
           2021-06-14T16:54:21.36+0300  OUT     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_202]
           2021-06-14T16:54:21.36+0300  OUT     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_202]
           2021-06-14T16:54:21.36+0300  OUT     at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_202]
           2021-06-14T16:54:21.36+0300  OUT     at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) [app/:na]
           2021-06-14T16:54:21.36+0300  OUT     at org.springframework.boot.loader.Launcher.launch(Launcher.java:108) [app/:na]
           2021-06-14T16:54:21.36+0300  OUT     at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) [app/:na]
           2021-06-14T16:54:21.36+0300  OUT     at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88) [app/:na]

在 pom.xml 中,我还使用spring-boot-starter-data-redis-reactiveroder 中的依赖项将有效负载保存在 redis 中。

我认为这可能是依赖关系之间的冲突?我不知道如何绕过这个。

4

0 回答 0