【问题标题】:issues with deployed springboot application on heroku在heroku上部署的spring boot应用程序的问题
【发布时间】:2020-06-27 13:22:21
【问题描述】:

我在 Heroku 上部署了一个 springboot RestApi。当我通过http://localhost:8080/api/userLog 在我的计算机上运行这个项目时,我得到了如下所示的所需响应:

[ {
  "userId" : "ABC123XYZ",
  "sessionId" : "XYZ456ABC",
  "action" : [ {
    "time" : "2018-10-19T03:37:28.000+0000",
    "type" : "CLICK",
    "properties" : {
      "locationX" : null,
      "locationY" : null,
      "viewedId" : null,
      "pageFrom" : null,
      "pageTo" : null
    }
  }, {
    "time" : "2018-10-19T03:37:28.000+0000",
    "type" : "CLICK",
    "properties" : {
      "locationX" : null,
      "locationY" : null,
      "viewedId" : null,
      "pageFrom" : null,
      "pageTo" : null
    }
  }, {
    "time" : "2018-10-19T03:37:28.000+0000",
    "type" : "CLICK",
    "properties" : {
      "locationX" : null,
      "locationY" : null,
      "viewedId" : null,
      "pageFrom" : null,
      "pageTo" : null
    }
  }, {
    "time" : "2018-10-19T03:37:30.000+0000",
    "type" : "VIEW",
    "properties" : null
  }, {
    "time" : "2018-10-19T03:37:30.000+0000",
    "type" : "NAVIGATE",
    "properties" : null
  } ]
} ]

但是当我尝试使用 heroku 链接 https://iredafe-mylogapp.herokuapp.com/api/userLog 调用此端点时,我只得到这个空括号。

[ ]

我不知道问题是什么。 heroku 错误日志如下所示:

2020-03-16T11:54:40.130172+00:00 app[web.1]: 2020-03-16 11:54:40.129  INFO 4 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-03-16T11:54:40.198112+00:00 app[web.1]: 2020-03-16 11:54:40.197  WARN 4 --- [           main] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-03-16T11:54:40.485336+00:00 app[web.1]: 2020-03-16 11:54:40.484  INFO 4 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 35331 (http) with context path ''
2020-03-16T11:54:40.489500+00:00 app[web.1]: 2020-03-16 11:54:40.489  INFO 4 --- [           main] c.d.s.applogger.AppLoggerApplication     : Started AppLoggerApplication in 6.326 seconds (JVM running for 7.161)
2020-03-16T11:54:41.126105+00:00 heroku[web.1]: State changed from starting to up
2020-03-16T11:55:56.193750+00:00 app[web.1]: 2020-03-16 11:55:56.193  INFO 4 --- [io-35331-exec-4] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-03-16T11:55:56.193880+00:00 app[web.1]: 2020-03-16 11:55:56.193  INFO 4 --- [io-35331-exec-4] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-03-16T11:55:56.205768+00:00 app[web.1]: 2020-03-16 11:55:56.205  INFO 4 --- [io-35331-exec-4] o.s.web.servlet.DispatcherServlet        : Completed initialization in 12 ms
2020-03-16T11:55:56.575834+00:00 heroku[router]: at=info method=GET path="/" host=iredafe-mylogapp.herokuapp.com request_id=63782a64-1add-4bfe-b8c3-3160130033f7 fwd="129.205.113.98" dyno=web.1 connect=1ms service=413ms status=404 bytes=464 protocol=https
2020-03-16T11:57:16.839074+00:00 heroku[router]: at=info method=GET path="/" host=iredafe-mylogapp.herokuapp.com request_id=cb767ad2-867c-47ad-8953-85a024d14810 fwd="129.205.113.98" dyno=web.1 connect=1ms service=9ms status=404 bytes=464 protocol=https
2020-03-16T11:58:31.725548+00:00 heroku[router]: at=info method=GET path="/api/userlog" host=iredafe-mylogapp.herokuapp.com request_id=dd9d30b0-acdf-4bab-ad54-fc8f9c148171 fwd="129.205.113.98" dyno=web.1 connect=1ms service=12ms status=404 bytes=464 protocol=https
2020-03-16T11:58:39.978886+00:00 app[web.1]: 2020-03-16 11:58:39.978  INFO 4 --- [io-35331-exec-8] o.h.h.i.QueryTranslatorFactoryInitiator  : HHH000397: Using ASTQueryTranslatorFactory
2020-03-16T11:58:40.095567+00:00 app[web.1]: 2020-03-16 11:58:40.095  WARN 4 --- [io-35331-exec-8] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@57256054 (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.
2020-03-16T11:58:40.100280+00:00 app[web.1]: 2020-03-16 11:58:40.100  WARN 4 --- [io-35331-exec-8] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@2e6603ed (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.
2020-03-16T11:58:40.103074+00:00 app[web.1]: 2020-03-16 11:58:40.102  WARN 4 --- [io-35331-exec-8] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@523df1b5 (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.
2020-03-16T11:58:40.105776+00:00 app[web.1]: 2020-03-16 11:58:40.105  WARN 4 --- [io-35331-exec-8] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@41432ac9 (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.
2020-03-16T11:58:40.108589+00:00 app[web.1]: 2020-03-16 11:58:40.108  WARN 4 --- [io-35331-exec-8] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@34cfdd3d (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.
2020-03-16T11:58:40.111372+00:00 app[web.1]: 2020-03-16 11:58:40.111  WARN 4 --- [io-35331-exec-8] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@2a28d922 (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.
2020-03-16T11:58:40.113734+00:00 app[web.1]: 2020-03-16 11:58:40.113  WARN 4 --- [io-35331-exec-8] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@17a5358b (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.
2020-03-16T11:58:40.116145+00:00 app[web.1]: 2020-03-16 11:58:40.115  WARN 4 --- [io-35331-exec-8] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@4fe840d4 (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.
2020-03-16T11:58:40.118975+00:00 app[web.1]: 2020-03-16 11:58:40.118  WARN 4 --- [io-35331-exec-8] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@5045a21 (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.
2020-03-16T11:58:40.121925+00:00 app[web.1]: 2020-03-16 11:58:40.121  WARN 4 --- [io-35331-exec-8] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Failed to validate connection com.mysql.cj.jdbc.ConnectionImpl@6989626a (No operations allowed after connection closed.). Possibly consider using a shorter maxLifetime value.
2020-03-16T11:58:40.232358+00:00 heroku[router]: at=info method=GET path="/api/userLog" host=iredafe-mylogapp.herokuapp.com request_id=79953046-9fcb-4947-be2c-9f74795ad507 fwd="129.205.113.98" dyno=web.1 connect=0ms service=292ms status=200 bytes=160 protocol=https
2020-03-16T11:58:54.348660+00:00 heroku[router]: at=info method=GET path="/api/userLog" host=iredafe-mylogapp.herokuapp.com request_id=10512910-6ce9-4e7b-aa67-3ffbd8431c99 fwd="129.205.113.98" dyno=web.1 connect=0ms service=14ms status=200 bytes=160 protocol=https
2020-03-16T11:58:54.634662+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=iredafe-mylogapp.herokuapp.com request_id=407b89b6-d3c5-4083-bcb4-2d7825f1254e fwd="129.205.113.98" dyno=web.1 connect=0ms service=19ms status=200 bytes=1136 protocol=https
2020-03-16T11:58:58.578822+00:00 heroku[router]: at=info method=GET path="/api/userLog" host=iredafe-mylogapp.herokuapp.com request_id=d54813e4-a705-4063-a8b2-f2c564e2621f fwd="129.205.113.98" dyno=web.1 connect=0ms service=13ms status=200 bytes=160 protocol=https

更新

这里是用于从 mysql 获取日志的实体类,它们产生了我之前显示的结果。

首先是 UserLog 类

package com.dafe.spring.applogger.entity;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;


@Entity
@Table(name="log")
public class UserLog {

    //define field

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

    @Column(name="user_id")
    private String userId;

    @Column(name="session_id")
    private String sessionId;

    @OneToMany(mappedBy="userLog",cascade=CascadeType.ALL)
        private List<Action>action;
    //define constructors

    public UserLog() {

    }

    public UserLog(String userId, String sessionId) {
        this.userId = userId;
        this.sessionId = sessionId;
    }
    //define getters and setters


    public String getUserId() {
        return userId;
    }


    public void setUserId(String userId) {
        this.userId = userId;
    }


    public String getSessionId() {
        return sessionId;
    }


    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    public List<Action> getAction() {
        return action;
    }


    public void setAction(List<Action> action) {
        this.action = action;
    }

    @Override
    public String toString() {
        return "Log [userId=" + userId + ", sessionId=" + sessionId + "]";
    }

}

这是另一个称为 Action 类的实体。

package com.dafe.spring.applogger.entity;

import java.sql.Timestamp;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnore;


@Entity
@Table(name="action")
public class Action {

    //declare & annotate your fields
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
    private int id;

@Column(name="time")
    private Timestamp time;

@Column(name="type")
    private String type;

@JsonIgnore
@ManyToOne
@JoinColumn(name="log_id")
private UserLog userLog;


@OneToOne(mappedBy="action", cascade=CascadeType.ALL)
private Properties properties;


    //create and generate constructor
    public Action() {

    }

    public Action(Timestamp time, String type) {
        this.time = time;
        this.type = type;
    }

    //generate getters and setters

    public Timestamp getTime() {
        return time;
    }

    public void setTime(Timestamp time) {
        this.time = time;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    @Override
    public String toString() {
        return "Action [\n time=" + time + ",\n type=" + type  + "]";
    }

    //generate toString 



}

这里是最后一个——属性实体类

package com.dafe.spring.applogger.entity;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnore;

@Entity
@Table(name="properties")
public class Properties {

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


    @Column(name="locationX")
    private Integer locationX;

    @Column(name="locationY")
    private Integer locationY;

    @Column(name="viewedId")
    private String viewedId;

    @Column(name="pageFrom")
    private String pageFrom;

    @Column(name="pageTo")
    private String pageTo;

    @JsonIgnore
    @OneToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="action_id")
    private Action action;


    public Properties() {

    }

    public Properties(Integer locationX, Integer locationY, String viewedId, String pageFrom, String pageTo) {
        this.locationX = locationX;
        this.locationY = locationY;
        this.viewedId = viewedId;
        this.pageFrom = pageFrom;
        this.pageTo = pageTo;
    }


    public Integer getLocationX() {
        return locationX;
    }

    public void setLocationX(Integer locationX) {
        this.locationX = locationX;
    }

    public Integer getLocationY() {
        return locationX;
    }

    public void setLocationY(Integer locationY) {
        this.locationY = locationY;
    }

    public String getViewedId() {
        return viewedId;
    }

    public void setViewedId(String viewedId) {
        this.viewedId = viewedId;
    }

    public String getPageFrom() {
        return pageFrom;
    }

    public void setPageFrom(String pageFrom) {
        this.pageFrom = pageFrom;
    }

    public String getPageTo() {
        return pageTo;
    }

    public void setPageTo(String pageTo) {
        this.pageTo = pageTo;
    }

    @Override
    public String toString() {
        return "Properties [locationX=" + locationX +", locationY=" + locationY + ", viewedId=" + viewedId + ", pageFrom=" + pageFrom + ", pageTo="
                + pageTo + "]";
    }

}

如何使代码在 heroku 中运行?我已经在 heroku repo 上为 mysql 配置了 clearDB。

如果有任何帮助或建议,我将不胜感激。谢谢

【问题讨论】:

  • 在我看来它无法创建到 mysql 数据库的连接。您需要提供更多关于它的源代码,以便找出问题所在

标签: mysql spring spring-boot heroku cleardb


【解决方案1】:

您的数据在数据库中可用吗? 尝试用heroku提供的数据库URL连接mysql工作台进行数据库连接。

【讨论】:

    猜你喜欢
    • 2019-08-03
    • 2021-01-14
    • 2020-01-09
    • 2020-08-28
    • 1970-01-01
    • 2021-07-25
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多