【问题标题】:Deploy Cloudfoundry app with Service (Spring Boot)使用服务(Spring Boot)部署 Cloudfoundry 应用程序
【发布时间】:2014-06-09 22:56:06
【问题描述】:

我在使用 cloudfoundry 中的服务部署应用程序时遇到问题。已经一个星期了,我找不到解决方案。你能告诉我,我的代码有什么问题吗?顺便说一句,我可以在没有服务的情况下进行部署,但是在绑定服务并尝试连接到数据库时会挂起部署。我在部署中使用了 spring sts。我希望有人能帮我解决这个问题。

这是我的参考: https://github.com/jwlayug/spring-cloud 和 https://github.com/jwlayug/spring-cloud/tree/master/spring-service-connector

我的代码

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application extends AbstractCloudConfig {
    @Bean
    public DataSource inventoryDataSource() {
        return connectionFactory().dataSource("jwlpostgre");
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

@Entity
class User {
    @Id
    @GeneratedValue
    private long id;

    private String name;

    public User() {
    }

    public User(String name) {
        this.name = name;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String toString() {
        return this.name;
    }

}

interface UserRepository extends JpaRepository<User, Long> {

}

@Controller
class MyController {
    @Autowired
    UserRepository userRepository;

    @RequestMapping(value = "/")
    public String sample(Model model) {
        model.addAttribute("sample", "Sample");
        userRepository.save(new User("a"));
        userRepository.save(new User("b"));
        userRepository.save(new User("c"));
        model.addAttribute("findall", userRepository.findAll());
        for (User user : userRepository.findAll()) {
            System.out.println(user.getName());
        }
        return "sample";
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>

    <groupId>org.demo</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>springboot</name>
    <description>Demo project</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.0.2.RELEASE</version>
    </parent>
    <repositories>
        <repository>
            <id>org.springframework.maven.milestone</id>
            <name>Spring Maven Milestone Repository</name>
            <url>http://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.3-1101-jdbc41</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-service-connector</artifactId>
            <version>0.9.2</version>
        </dependency>
        <!-- If you intend to deploy the app on Cloud Foundry, add the following -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>cloudfoundry-connector</artifactId>
            <version>0.9.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <properties>
        <start-class>demo.Application</start-class>
        <java.version>1.7</java.version>
    </properties>

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

</project>

【问题讨论】:

    标签: spring-mvc cloud-foundry spring-boot


    【解决方案1】:

    我最好的猜测是你在 Spring Cloud 上有一个旧版本,它没有绑定,因为环境变量格式发生了变化。或者可能是您实际上没有具有该标识符的数据库。 (顺便说一下,Spring Cloud 项目是 here - 您引用的链接来自一个 fork。)

    【讨论】:

    • 我尝试将我的 cloudfoundry-connect 和 spring-service-connector 更新到 0.9.8(最新)并且我确定我的服务名称 jwlpostgre 。它仍然在部署中挂起。
    • "Hangs" 并没有真正告诉我们很多信息(cloudfoundry 没有任何东西挂起很长时间,因为它都是由超时控制的)。你有日志吗?
    【解决方案2】:

    Spring Cloud 的 AbstractCloudConfig 实现了 BeanFactoryAware,它会进行一些 bean 查找/添加。我认为让您的 Application 类从 AbstractCloudConfig 扩展会导致应用程序上下文创建和刷新的顺序出现问题。尝试将 dataSource @Bean 方法移出 Application 并移到单独的 @Configuration 类中。

    【讨论】:

      【解决方案3】:

      如果您没有多个数据库数据源,则无需添加该 bean 或为其命名。

      确保排除数据源自动配置,因为这会导致问题。

      package com.mycompany.myapp;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
      import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
      import org.springframework.context.annotation.ComponentScan;
      import org.springframework.context.annotation.Configuration;
      
      @Configuration
      @ComponentScan
      @EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class) 
      public class Application {
          public static void main(String[] args) {
              SpringApplication.run(Application.class, args);
          }
      }
      

      使用当前版本对我有用。更新到最新和最棒的。

      <?xml version="1.0" encoding="UTF-8"?>
      <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>
      
          <groupId>com.gopivotal</groupId>
          <artifactId>redisstore</artifactId>
          <version>0.0.1-SNAPSHOT</version>
          <packaging>jar</packaging>
      
          <name>myapp</name>
          <description>myapp</description>
      
          <parent>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-parent</artifactId>
              <version>1.1.3.RELEASE</version>
              <relativePath/> <!-- lookup parent from repository -->
          </parent>
      
          <dependencies>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-web</artifactId>
              </dependency>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-actuator</artifactId>
              </dependency>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-redis</artifactId>
              </dependency>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-data-jpa</artifactId>
              </dependency>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-test</artifactId>
                  <scope>test</scope>
              </dependency>       
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-jdbc</artifactId>
              </dependency>
              <dependency>
                  <groupId>postgresql</groupId>
                  <artifactId>postgresql</artifactId>
                  <version>${postgres-driver.version}</version>
                  <scope>runtime</scope>
              </dependency>
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-spring-service-connector</artifactId>
              <version>1.0.0.RELEASE</version>
          </dependency>
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-cloudfoundry-connector</artifactId>
              <version>1.0.0.RELEASE</version>
          </dependency>
          </dependencies>
          <!-- Transitively bring in the Spring IO Platform Bill-of-Materials `pom.xml` -->    
          <dependencyManagement>
              <dependencies>
                  <dependency>
                      <groupId>io.spring.platform</groupId>
                      <artifactId>platform-bom</artifactId>
                      <version>1.0.0.RELEASE</version>
                      <type>pom</type>
                      <scope>import</scope>
                  </dependency>
              </dependencies>
          </dependencyManagement>
      
      <repositories>
       <repository>
             <id>org.springframework.maven.milestone</id>
             <name>Spring Maven Milestone Repository</name>
             <url>http://repo.spring.io/milestone</url>
           </repository>
      </repositories>
      
          <properties>
              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
              <start-class>com.mycompany.myapp.Application</start-class>
              <java.version>1.7</java.version>
              <postgres-driver.version>9.0-801.jdbc4</postgres-driver.version>
          </properties>
      
          <build>
              <plugins>
                  <plugin>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-maven-plugin</artifactId>
                  </plugin>
              </plugins>
          </build>
      
      </project>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-03
        • 2019-01-31
        • 2020-11-27
        相关资源
        最近更新 更多