如何使用从Cloud Foundry引导访问我的本地MySQL数据库?

时间:2016-10-14 01:31:42

标签: java spring rest spring-boot pivotal-cloud-foundry

如何从Cloud Foundry访问我的本地MySQL数据库?

我已经从 STS-3.8.2 创建了一个Spring Boot Rest应用程序,以执行在本地环境中完美运行的Crud操作。当部署到云代工厂时,其余的调用只需简单的操作即可。但是当我打电话给休息来执行Crud操作时,它会说

  

无法打开连接;嵌套异常是org.hibernate.exception.JDBCConnectionException:无法打开连接

请帮我解决问题。

CloudFoundry上我部署Spring Boot Application的链接

上面是一个问候消息的链接,第二个用于crud操作以从我的笔记本电脑中获取MySQL的列表。

SpringBootAndMySqlApplication.java:

@SpringBootApplication

public class SpringBootAndMySqlApplication {

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

HomeController.java:

@RestController

public class HomeController {

    @Autowired
    private TaskDao taskDao;

    @RequestMapping("/greeting")
    public String greeting() {
        return "Welcome to Boot + SQL";
    }

    @RequestMapping("/list")
    public List<TaskEntity> taskList() {
        return taskDao.findByTaskArchived(0);
    }
}

TaskDao.java:

public interface TaskDao extends CrudRepository<TaskEntity, Integer> {

    List<TaskEntity> findByTaskArchived(Integer tas);

}

TaskEntity.java:

@Entity

@Table(name = "task_list")

public class TaskEntity 

{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "task_id")
    private int id;

    @Column(name = "task_name")
    private String taskName;

    @Column(name = "task_description")
    private String taskDescription;

    @Column(name = "task_priority")
    private String taskPriority;

    @Column(name = "task_status")
    private String taskStatus;

    @Column(name = "task_archived")
    private int taskArchived = 0;

// getter & setter

}

application.properties:

spring.datasource.url = jdbc:mysql://127.0.0.1:3306/taskmanager
spring.datasource.username = root
spring.datasource.password = root
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.datasource.driverClassName = com.mysql.jdbc.Driver

MySQL脚本:

create database taskmanager;

use taskmanager;

create table task_list(task_id int not null auto_increment, task_name varchar(100) not null, task_description text,task_priority varchar(20),task_status varchar(20),task_start_time datetime not null default CURRENT_TIMESTAMP,task_end_time datetime not null default CURRENT_TIMESTAMP,task_archived bool default false,primary key(task_id));

insert into task_list values(1,'Gathering Requirement','Requirement Gathering','MEDIUM','ACTIVE',curtime(),curtime() + INTERVAL 3 HOUR,0);

insert into task_list values(2,'Application Designing','Application Designing','MEDIUM','ACTIVE',curtime(),curtime() + INTERVAL 2 HOUR,0);

insert into task_list values(3,'Implementation','Implementation','MEDIUM','ACTIVE',curtime(),curtime() + INTERVAL 3 HOUR,0);

insert into task_list values(4,'Unit Testing','Unit Testing','LOW','ACTIVE',curtime(),curtime() + INTERVAL 4 HOUR,0);

insert into task_list values(5,'Maintanence','Maintanence','LOW','ACTIVE',curtime(),curtime() + INTERVAL 5 HOUR,0);

select * from task_list;

在STS中创建春季启动应用程序之上,并在STS中从BootDashBoard执行CloudFoundry中的拖放操作。并成功推动。现在,我可以从YML文件中定义的主机访问我的应用程序。但是当我试图打电话给我的休息来进行粗暴操作时,它就失败了。

任何解决方案?

1 个答案:

答案 0 :(得分:0)

您已加入application.properties。这是什么配置文件? 云配置文件有application.properties吗?

Cloud Foundry通常附带MySql作为服务。要利用它,您需要首先创建一个服务实例。

如果您希望您的应用使用外部数据库(在云代工厂之外),则必须创建用户提供的服务(CUPS)定义。

查看Spring Music应用。它将为您提供有关本地和云配置文件的详细信息。

https://github.com/cloudfoundry-samples/spring-music