【问题标题】:AWS (Elastic Beanstalk) + Java (Spring) = Reason: Error reading from remote serverAWS (Elastic Beanstalk) + Java (Spring) = 原因:从远程服务器读取错误
【发布时间】:2014-07-09 02:06:49
【问题描述】:

我不会说英语。 (我是巴西人)。

我正在尝试使用 Spring4 + JPA2 + Hibernate4 运行 Java webapp。在我的本地服务器上目前运行很酷,工作一段时间后放在 AWS 上会发生以下错误:

链接:http://almocaquiweb-a.elasticbeanstalk.com/rest/restaurant/C1F15300-8BFC-496B-B90C-CF57596C8319/detail

代理错误

代理服务器收到来自上游服务器的无效响应。 代理服务器无法处理请求 GET /rest/restaurant/C1F15300-8BFC-496B-B90C-CF57596C8319/detail。

原因:从远程服务器读取错误

我的文件:(不要使用任何 XML 文件,所有设置都带有注释和 Java 类。)

application.properties

# Server
server.port=8080
server.sessionTimeout=30

# MVC
spring.view.prefix=/WEB-INF/jsp/
spring.view.suffix=.jsp

java.runtime.version=1.7

#DataSource
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.url=jdbc:sqlserver://xx.database.windows.net:1433;database=xxx;encrypt=true;hostNameInCertificate=*.database.windows.net;loginTimeout=30;
spring.datasource.username=xxxx
spring.datasource.password=xxxx
spring.datasource.validationQuery=SELECT 1
spring.datasource.testOnBorrow=true
spring.datasource.poolPreparedStatements=true

# JPA
spring.jpa.database-platform=org.hibernate.spatial.dialect.sqlserver.SqlServer2008SpatialDialect
spring.jpa.generate-ddl=true
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.autocommit=true
spring.data.jpa.repositories.enabled=true

# Tomcat
tomcat.accessLogEnabled=false
tomcat.protocolHeader=x-forwarded-proto
tomcat.remoteIpHeader=x-forwarded-for
tomcat.backgroundProcessorDelay=30
server.tomcat.uri-encoding=UTF-8
server.session-timeout=40

RestaurantRestController.java

@RestController
@RequestMapping(value = "/rest/restaurant")
public class RestaurantRestController {

    private final RestaurantService service;

    @Inject
    public RestaurantRestController(final RestaurantService service) {
        this.service = service;
    }
    
    @RequestMapping(value = "/{uuid}/detail", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Restaurant getDetail(@PathVariable("uuid") String uuid) {
        Restaurant restaurant = service.getRestaurant(uuid);          
        return restaurant;
    }
}

RestaurantServiceImpl

@Service
@Validated
public class RestaurantServiceImpl implements RestaurantService {

    private final RestaurantRepository repository;
    
    @Inject
    public RestaurantServiceImpl(final RestaurantRepository repository) {
        this.repository = repository;
    }
     @Override
    @Transactional(readOnly = true)
    public Restaurant getRestaurant(String uuid){
        return repository.findOneByUUID(uuid);
        
    }
}

编辑(包括日志)

我意识到一段时间后(可能会话过期),本地需要将近一分钟才能“重新打开”。按照日志:

2014-07-07 10:32:29 DEBUG o.s.s.w.u.m.AntPathRequestMatcher:145 - Checking match of request : '/rest/restaurant/c1f15300-8bfc-496b-b90c-cf57596c8319/detail'; against '/rest/restaurant'
2014-07-07 10:32:29 DEBUG o.s.s.w.u.m.AntPathRequestMatcher:145 - Checking match of request : '/rest/restaurant/c1f15300-8bfc-496b-b90c-cf57596c8319/detail'; against '/rest/restaurant/**'
2014-07-07 10:32:29 DEBUG o.s.s.w.FilterChainProxy:180 - /rest/restaurant/C1F15300-8BFC-496B-B90C-CF57596C8319/detail has an empty filter list
2014-07-07 10:32:29 DEBUG o.s.w.s.DispatcherServlet:838 - DispatcherServlet with name 'dispatcherServlet' processing GET request for [/almocaqui/rest/restaurant/C1F15300-8BFC-496B-B90C-CF57596C8319/detail]
2014-07-07 10:32:29 DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping:246 - Looking up handler method for path /rest/restaurant/C1F15300-8BFC-496B-B90C-CF57596C8319/detail
2014-07-07 10:32:29 DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping:251 - Returning handler method [public com.snowmanlabs.almocaqui.domain.Restaurant com.snowmanlabs.almocaqui.rest.controller.RestaurantRestController.getDetail(java.lang.String)]
2014-07-07 10:32:29 DEBUG o.s.b.f.s.DefaultListableBeanFactory:249 - Returning cached instance of singleton bean 'restaurantRestController'
2014-07-07 10:32:29 DEBUG o.s.w.s.DispatcherServlet:925 - Last-Modified value for [/almocaqui/rest/restaurant/C1F15300-8BFC-496B-B90C-CF57596C8319/detail] is: -1
2014-07-07 10:32:29 DEBUG o.s.o.j.s.OpenEntityManagerInViewInterceptor:87 - Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
2014-07-07 10:32:29 DEBUG o.s.b.f.s.DefaultListableBeanFactory:249 - Returning cached instance of singleton bean 'transactionManager'
2014-07-07 10:32:29 DEBUG o.s.o.j.JpaTransactionManager:334 - Found thread-bound EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@1f85dbd] for JPA transaction
****2014-07-07 10:32:29 DEBUG o.s.o.j.JpaTransactionManager:367 - Creating new transaction with name [com.snowmanlabs.almocaqui.service.implement.RestaurantServiceImpl.getRestaurant]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly; ''
2014-07-07 10:33:24 DEBUG o.s.o.j.JpaTransactionManager:403 - Exposing JPA transaction as JDBC transaction [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@565da00b]
2014-07-07 10:33:24 DEBUG o.s.o.j.JpaTransactionManager:755 - Initiating transaction commit
2014-07-07 10:33:24 DEBUG o.s.o.j.JpaTransactionManager:510 - Committing JPA transaction on EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@1f85dbd]
2014-07-07 10:33:25 DEBUG o.s.o.j.JpaTransactionManager:603 - Not closing pre-bound JPA EntityManager after transaction
2014-07-07 10:33:25 DEBUG o.s.w.s.m.m.a.RequestResponseBodyMethodProcessor:145 - Written [com.snowmanlabs.almocaqui.domain.Restaurant@48e83911] as "application/json" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@5bfa9eb7]
2014-07-07 10:33:25 DEBUG o.s.w.s.DispatcherServlet:1012 - Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2014-07-07 10:33:25 DEBUG o.s.o.j.s.OpenEntityManagerInViewInterceptor:112 - Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2014-07-07 10:33:25 DEBUG o.s.o.j.EntityManagerFactoryUtils:435 - Closing JPA EntityManager
2014-07-07 10:33:25 DEBUG o.s.w.s.DispatcherServlet:991 - Successfully completed request

服务器上的这种延迟,小时工作并且小时返回错误“从远程服务器读取错误”


编辑 2

我认为是连接数据库的时间问题,我尝试配置一个连接池。

dbConfig.java 文件

@PropertySource(value = "classpath:db.properties")
@EnableTransactionManagement(proxyTargetClass = true)
@EnableJpaRepositories("com.snowmanlabs.almocaqui.repository")
@Configuration
public class dbConfig {
 
    @Autowired
    Environment env;
 
    @Bean
    public BoneCPDataSource boneCPDataSource() {
 
        BoneCPDataSource boneCPDataSource = new BoneCPDataSource();
        boneCPDataSource.setDriverClass(env.getProperty("jdbc.driverClass"));
        boneCPDataSource.setJdbcUrl(env.getProperty("jdbc.url"));
        boneCPDataSource.setUsername(env.getProperty("jdbc.username"));
        boneCPDataSource.setPassword(env.getProperty("jdbc.password"));
        boneCPDataSource.setIdleConnectionTestPeriodInMinutes(60);
        boneCPDataSource.setIdleMaxAgeInMinutes(420);
        boneCPDataSource.setMaxConnectionsPerPartition(30);
        boneCPDataSource.setMinConnectionsPerPartition(10);
        boneCPDataSource.setPartitionCount(3);
        boneCPDataSource.setAcquireIncrement(5);
        boneCPDataSource.setStatementsCacheSize(100);
 
        return boneCPDataSource;
 
    }
 
    @Bean
    public HibernateExceptionTranslator hibernateExceptionTranslator() {
        return new HibernateExceptionTranslator();
    }
 
    @Bean
    @Autowired
    public EntityManagerFactory entityManagerFactory(BoneCPDataSource dataSource) {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(true);
        vendorAdapter.setShowSql(false);
        vendorAdapter.setDatabasePlatform(env.getProperty("jdbc.database-platform"));
        vendorAdapter.setDatabase(Database.SQL_SERVER);
 
        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan("com.snowmanlabs.almocaqui.domain");
        factory.setDataSource(dataSource);
 
        Properties properties = new Properties();
        properties.setProperty("hibernate.cache.use_second_level_cache", "true");
        properties.setProperty("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.EhCacheRegionFactory");
        properties.setProperty("hibernate.cache.use_query_cache", "true");
        properties.setProperty("hibernate.generate_statistics", "true");
 
        factory.setJpaProperties(properties);
 
        factory.afterPropertiesSet();
 
        return factory.getObject();
    }
 
    @Bean
    @Autowired
    public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
        JpaTransactionManager txManager = new JpaTransactionManager();
        JpaDialect jpaDialect = new HibernateJpaDialect();
        
        txManager.setEntityManagerFactory(entityManagerFactory);
        txManager.setJpaDialect(jpaDialect);
        return txManager;
    }
 
}

10 分钟后出错(关闭会话)

白标错误页面

这个应用程序没有明确的 /error 映射,所以你看到 这是一个后备。

Tue Jul 08 15:01:52 BRT 2014 出现意外错误 (类型=内部服务器错误,状态=500)。无法打开 JPA EntityManager 用于事务;嵌套异常是 javax.persistence.PersistenceException: org.hibernate.TransactionException:JDBC 开始事务失败:

【问题讨论】:

  • 嗨,我遇到了同样的问题,在本地测试时,我可以访问控制器,但是当我将战争上传到 AWS 时,我找不到资源。当我将spring boot与jpa集成时产生了这个错误,之前没有问题。任何帮助将不胜感激。谢谢

标签: java spring jpa amazon-web-services tomcat7


【解决方案1】:

您的应用服务器可能位于reverse proxy 后面。通常反向代理有一个超时设置。如果从后端获取答案所需的时间较长,则会报错。

我曾经遇到过类似的问题,这是因为我使用了localhost 主机引用。原来localhost 可能是模棱两可的,它可以解释为 ipv4 或 ipv6。在某些配置中,ipv4 和 v6 之间的切换花费了很长时间,并且出现了代理错误。

从那以后我改用127.0.0.1,所以它不会切换到ipv6。

【讨论】:

  • 是的,可以。在我的本地服务器上,启动应用程序需要将近 1 分钟,连接到另一台服务器(Windows Azure)上的 SQL Server 需要很长时间。我怎么能改变这个?我唯一拥有的就是访问终端 SSH Shell,除此之外什么都不知道。
【解决方案2】:

我的回答是:

{"name":"Peperone Buffet","categories":[],"address":"Rua A, 4","wifi":false,"parking":false,"cards":[],"free":23.23,"kilo":45.34}

所以要么你已经修复了它:) 要么你的错误与你的本地网络设置有关,与 Spring 或 AWS 无关,你的应用程序是好的。

【讨论】:

  • 系统从昨天开始运行没有倒下,这个话题之后我没有改变任何东西。昨天正在访问 10 分钟和 10 分钟:内部发生了一些事情,日志中没有任何内容被注册并开始出现 satada 错误消息。 (昨天一整天都发生了)*对不起我的英语
猜你喜欢
  • 2016-03-07
  • 2020-02-20
  • 1970-01-01
  • 2021-09-22
  • 2019-11-21
  • 2017-01-13
  • 1970-01-01
  • 2020-02-19
  • 2017-01-22
相关资源
最近更新 更多