Cloudfoundry:使用Spring和RabbitMQ部署独立应用程序

时间:2013-04-23 16:48:07

标签: spring rabbitmq cloudfoundry

我正在尝试将独立应用程序部署到CloudFoundry。我在我的ApplicationContext方法中使用Spring main来初始化Spring,我有一个简单的Bean来处理与RabbitMQ的消息传递。

spring.xml我配置了RabbitMQ,在pom文件中,我添加了cglib-nodepspring-rabbitcloudfoundry-runtime的必要依赖项。我进一步使用maven-assembly-plugin插件创建一个包含所有库的jar文件,然后使用vmc工具将其部署到cloudfoundry。

当我将jar - 文件部署到CloudFoundry时,我收到以下错误:

Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException:
Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace
[http://www.springframework.org/schema/context]

Offending resource: class path resource [spring.xml]

[stacktrace ommited]

这是我的spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:rabbit="http://www.springframework.org/schema/rabbit"
    xmlns:cloud="http://schema.cloudfoundry.org/spring"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/rabbit
        http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd
        http://schema.cloudfoundry.org/spring
        http://schema.cloudfoundry.org/spring/cloudfoundry-spring-0.7.xsd">

    <context:component-scan base-package="mypackage" />

    <!-- Obtain a connection to the RabbitMQ via cloudfoundry-runtime: -->
    <cloud:rabbit-connection-factory id="connectionFactory"/>

    <!-- Set up the AmqpTemplate/RabbitTemplate: -->
    <rabbit:template connection-factory="connectionFactory"/>

    <!-- Request that queues, exchanges and bindings be automatically
         declared on the broker: -->
    <rabbit:admin connection-factory="connectionFactory"/>

    <!-- Declare the "messages" queue: -->
    <rabbit:queue name="messages" durable="true"/>
</beans>

maven-assembly-plugin的配置:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <mainClass>at.ac.tuwien.infosys.dse2013s.group17.allocator.ui.StartUpAllocator</mainClass>
            </manifest>
        </archive>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
    <executions>
        <execution>
          <id>make-assembly</id> <!-- this is used for inheritance merges -->
          <phase>package</phase> <!-- bind to the packaging phase -->
          <goals>
            <goal>single</goal>
          </goals>
        </execution>
    </executions>
</plugin>

上述错误可能是什么原因?我如何配置maven-assembly-plugin有什么问题,或者spring.xml中确实存在问题?

更新

我能够通过将spring-context依赖项添加到我的root pom作为托管依赖项,然后从没有version元素的模块引用它来解决错误。现在我收到了同样的错误,但这次Exception抱怨http://schema.cloudfoundry.org/spring架构。

1 个答案:

答案 0 :(得分:1)

[更新] 我建议您查看cf-maven-plugin,这样可以更轻松地部署到CloudFoundry,因此不再需要直接使用vmc工具。 GitHub页面解释了如何使用此插件部署普通独立应用程序,并解释了如何将appassembler-maven-plugin用于独立应用程序。这个blog post对于入门也很有用。 的 [/更新]

事实证明我正在以错误的方式将应用程序部署到CloudFoundry。我正在使用maven-assembly-plugin,它将所有依赖项的类文件解压缩到jar文件中。实际上我必须使用appassembler-maven-plugin

<plugin>
     <groupId>org.codehaus.mojo</groupId>
     <artifactId>appassembler-maven-plugin</artifactId>
     <version>1.1.1</version>
     <executions>
         <execution>
             <phase>package</phase>
             <goals>
                 <goal>assemble</goal>
             </goals>
             <configuration>
                 <assembledirectory>target</assembledirectory>
                 <programs>
                     <program>
                         <mainClass>my.package.myclass</mainClass>
                     </program>
                 </programs>
            </configuration>
        </execution>
    </executions>
</plugin>

然后,我从target/文件夹

将应用程序部署到CloudFoundry
vmc push <myappname> --path=appassembler

作为运行命令,我需要在appassembler/bin文件夹中选择运行脚本的名称。例如bin/start-up-myapp