根据this link,我应该能够将Google App Engine Web应用程序连接到外部数据库(在这种情况下,我想将其连接到heroku ClearCB-mysql)。
"Other cloud providers
App Engine apps can connect to external databases that are hosted on other public clouds as
long as those database servers and your firewall are configured properly to accept
connections. Your App Engine app connects over the Internet using that external
service's public IP address."
有人可以指出我对服务器和防火墙的正确配置吗?我找不到关于它的任何文档,甚至不知道从哪里开始(我需要对傻瓜o_O进行解释);)。
当我将文件上传到Google App Engine超时时,我可以通过spring-jpa连接到DB,它可以在localhost上正常工作。
ApplicationConfig.java
@Configuration
@EnableJpaRepositories(basePackages = "dominikazb")
@EnableTransactionManagement
@ComponentScan(basePackages = "dominikazb")
public class ApplicationConfig extends WebMvcConfigurationSupport {
@Bean
public InternalResourceViewResolver jspViewResolver(){
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/jsp/");
viewResolver.setSuffix(".jsp");
viewResolver.setViewClass(JstlView.class);
return viewResolver;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em
= new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] { "dominikazb" });
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}
@Bean
public DataSource dataSource(){
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://hostname/databasename");
dataSource.setUsername( "username" );
dataSource.setPassword( "password" );
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
return new PersistenceExceptionTranslationPostProcessor();
}
Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
return properties;
}
}
UserRepository.java
@Repository
public interface UserRepository extends JpaRepository<User, Integer> {
}
User.java
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int user_id;
private String username;
private String password;
//getters, setters and constructor
}
HomeController.java
@Controller
public class HomeController {
@Autowired
private UserRepository userRepository;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String takeMeHome() {
return "index";
}
@RequestMapping(value = "/getall", method = RequestMethod.GET)
public @ResponseBody Iterable<User> getAllUsers() {
return userRepository.findAll();
}
}
pom.xml(相关依赖项)
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>8.0.19</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.4.2.Final</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.0.pr3</version>
</dependency>
答案 0 :(得分:1)
对于任何与我一样遇到同样问题的人。我在Google App Engine的“结算”部分找到了答案。我的意思是我用光了我可以添加到我的Billing帐户中的项目,并且这些项目与外部数据库的连接受到限制。将我的应用程序上载到启用了计费功能的项目后,一切都变得很顺畅。