简体   繁体   English

如何使用Cloud Foundry通过Vue前端和H2数据库为Spring Boot Web应用提供服务?

[英]How to use Cloud Foundry to serve Spring Boot web app with Vue front end and H2 database?

I have followed a tutorial using Vue as front end and using spring boot as backend. 我遵循了一个使用Vue作为前端并使用Spring Boot作为后端的教程。 I write the front end and put the built files from Vue's dist folder to spring boot web's src\\main\\resources\\static folder. 我写了前端,然后将Vue的dist文件夹中的构建文件放到spring boot web的src\\main\\resources\\static文件夹中。

The tutorial told me to use axios to transfer data to the backend. 本教程告诉我使用axios将数据传输到后端。 It's configuration is as follows: 它的配置如下:

var axios = require('axios')
axios.defaults.baseURL = 'http://localhost:8090/api'

It works good on my local pc. 在我的本地PC上运行良好。 But I want to put it and make it run on cloud. 但我想放它并使它在云上运行。 I build the jar with mvn clean install . 我用mvn clean install构建jar。 Then uploaded it to IBM's cloud foundry. 然后将其上传到IBM的Cloud Foundry。 The frontend works. 前端工作。 However, it does not talk to the backend. 但是,它与后端无关。 The browser's console log shows: 浏览器的控制台日志显示:

XHR failed loading: OPTIONS "<URL>".
4xhr.js:178 OPTIONS http://localhost:8090/api/login net::ERR_CONNECTION_REFUSED

The demo is uploaded here 演示在这里上传

I also want to include H2 database, but I tried and it only works on mem mode or file mode. 我也想包含H2数据库,但我尝试过,它仅适用于内存模式或文件模式。 The jdbc:h2:tcp://localhost/~/test mode does not work on the cloud. jdbc:h2:tcp://localhost/~/test模式在云上不起作用。 So how to make it run on the cloud? 那么如何使其在云上运行呢? Is there anyother way to make Vue talk with the java backend without axios? 还有其他方法可以使Vue与不使用axios的java后端进行对话吗? Or if it's must, can I configure cloud foundry to make the link work? 或者,如果必须,我可以配置cloud Foundry来使链接正常工作吗? Or if cloud foundry can't do this, (I use cloud foundry just because it's easy, just upload the jar, no need to configure) can k8s do this? 还是如果Cloud Foundry无法做到这一点(我之所以使用Cloud Foundry是因为它很容易,只需上传jar,无需配置),k8s可以做到吗?

XHR failed loading: OPTIONS "". XHR加载失败:选项“”。 4xhr.js:178 OPTIONS http://localhost:8090/api/login net::ERR_CONNECTION_REFUSED 4xhr.js:178选项http:// localhost:8090 / api / login net :: ERR_CONNECTION_REFUSED

This is failing because your app is no longer running on your local machine, it's running on Cloud Foundry. 之所以失败,是因为您的应用程序不再在本地计算机上运行,​​而是在Cloud Foundry上运行。 You need to update your axios.defaults.baseURL setting to reference the route that you bound to your app. 您需要更新axios.defaults.baseURL设置,以引用绑定到应用程序的路由。

You could hard code this in the config, which is not great but does work, or you could reference the VCAP_APPLICATION environment variable which is set by Cloud Foundry and contains information about your app, including the bound routes (there can be more than one). 您可以在配置中对此进行硬编码,虽然这不是很好,但是可以工作,或者可以引用由Cloud Foundry设置的VCAP_APPLICATION环境变量,其中包含有关您的应用程序的信息,包括绑定的路由(可以有多个路由) 。 You could read this, pick a route and dynamically configure your app. 您可以阅读此书,选择一条路线并动态配置您的应用程序。

Ex: 例如:

 "VCAP_APPLICATION": {
  "application_id": "<guid>",
  "application_name": "<app-name>",
  "application_uris": [
   "app-name.apps.example.com",
   "some-other-route.example.com"
  ],
  "application_version": "df82308c-7add-4f2b-bb44-a58680084a79",
  "cf_api": "https://api.system.example.com",
  "limits": {
   "disk": 1024,
   "fds": 16384,
   "mem": 64
  },
  "name": "<app-name>",
  "space_id": "<space-guid>",
  "space_name": "<space-name>",
  "uris": [
   "app-name.apps.example.com",
   "some-other-route.example.com"
  ],
  "users": null,
  "version": "df82308c-7add-4f2b-bb44-a58680084a79"
 }

I also want to include H2 database, but I tried and it only works on mem mode or file mode. 我也想包含H2数据库,但我尝试过,它仅适用于内存模式或文件模式。 The jdbc:h2:tcp://localhost/~/test mode does not work on the cloud. jdbc:h2:tcp://localhost/~/test模式在云上不起作用。

I don't see why this technically wouldn't work, so long as you're app is talking to the database over localhost, all the traffic would be inside the app container. 我不明白为什么这从技术上讲是行不通的,只要您的应用通过本地主机与数据库通信,所有流量都将在应用容器内。 You'd have to be more specific about what exactly is failing. 您必须更确切地说明失败的原因。

That said, I don't think you'd want to use H2 like that, at least not beyond a small test/demo. 也就是说,我不希望您使用H2,至少不要在较小的测试/演示中使用它。 First off, the app containers are ephemeral, so none of your data would survive a restart/crash/restage/push or anything that triggers the container to restart (platform maintenance can do this too, so it's not just actions you initiate). 首先,应用程序容器是短暂的,因此您的任何数据都不会在重新启动/崩溃/重新上载/推送或任何触发容器重新启动的情况下幸存(平台维护也可以执行此操作,因此,这不仅仅是您启动的操作)。 Second, you really wouldn't be able to scale your app beyond one instance because each instance of the app would have it's own copy of the database, which is going to be problematic. 其次,您实际上无法将应用程序扩展到一个实例之外,因为该应用程序的每个实例都拥有自己的数据库副本,这将成为问题。

What you really want to do is create a service & bind that to your app. 您真正想要做的是创建服务并将其绑定到您的应用程序。 Then, just like VCAP_APPLICTION , you can pull your service credentials from VCAP_SERVICES and dynamically configure your app to connect to your database. 然后,就像VCAP_APPLICTION一样,您可以从VCAP_SERVICES提取服务凭据,并动态配置您的应用程序以连接到数据库。 See the second part of this answer for details on how to do that. 有关如何执行此操作的详细信息,请参见此答案的第二部分。

Try running cf marketplace to see a list of the services your provider offers. 尝试运行cf marketplace以查看提供商提供的服务列表。 Many services even have free tier service plans, so you can try them or use them for small apps and demos. 许多服务甚至都有免费套餐服务计划,因此您可以尝试使用它们或将其用于小型应用程序和演示。

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM