Question

I'm having a problem deploying an app with a service in cloudfoundry. It's been a week and i can't find a solution. Can you tell me guys whats wrong with my code? BTW, i can deploy without a service but when binding a service and trying to connect to the database hangs the deployment. I'm using spring sts in my deployment. I hope someone can help me figure this out.

this is my reference btw : https://github.com/jwlayug/spring-cloud and https://github.com/jwlayug/spring-cloud/tree/master/spring-service-connector

My Code

@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>
Was it helpful?

Solution

if you don't have more than one database datasource, you don't need to add that bean or name it.

make sure you exclude datasource auto config as this causes an issue.

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);
    }
}

using the current versions worked for me. Update to the latests and greatest.

<?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>

OTHER TIPS

Spring Cloud's AbstractCloudConfig implements BeanFactoryAware, and it does some bean lookups/additions. I think having your Application class extend from AbstractCloudConfig is causing problems with the order of application context creation and refreshing. Try moving the dataSource @Bean method out of Application and into a separate @Configuration class.

My best guess is you have an older version on Spring Cloud and it isn't binding because the environment variable format changed. Or it could be that you actually don't have a database with that identifier. (The Spring Cloud project is here by the way - the links you referenced are from a fork.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top