[英]Add Java/JRE/JVM in Python Flask Cloud Foundry/IBM Cloud application
I am running a python flask application in a Cloud Foundry/IBM Cloud-environment.我正在 Cloud Foundry/IBM Cloud 环境中运行 python Flask 应用程序。 In my application I try to connect to DB2 Warehouse on Cloud with the IBMDBPY-package.在我的应用程序中,我尝试使用 IBMDBPY 包连接到 DB2 Warehouse on Cloud。 This packages needs a package called jaydebeapi to be able to run.这个包需要一个叫做 jaydebeapi 的包才能运行。 For the jaydebeapi to work I think I need JRE/JVM somehow installed on the server.为了让 jaydebeapi 工作,我想我需要在服务器上以某种方式安装 JRE/JVM。 I tried adding the Server JRE for Linux based OS, but it didn't work either.我尝试为基于 Linux 的操作系统添加服务器 JRE ,但它也不起作用。 My error I got before I tried to upload the Sever JRE was this:我在尝试上传 Sever JRE 之前遇到的错误是:
idadb = IdaDataBase(dsn=jdbc) #Establish a connection to our DB2-service
1/20/2018 12:05:45 PM ERR undefined File "/home/vcap/deps/0/python./lib/python2.7/site-packages/ibmdbpy/base.py", line 282, in __init__
1/20/2018 12:05:45 PM ERR undefined jpype.startJVM(jpype.getDefaultJVMPath(), '-Djava.class.path=%s' % jarpath)
1/20/2018 12:05:45 PM ERR undefined File "/home/vcap/deps/0/python/lib/python2.7/site-packages/jpype/_core.py", line 114, in get_default_jvm_path
1/20/2018 12:05:45 PM ERR undefined return finder.get_jvm_path()
1/20/2018 12:05:45 PM ERR undefined File "/home/vcap/deps/0/python/lib/python2.7/site-packages/jpype/_jvmfinder.py", line 121, in get_jvm_path
1/20/2018 12:05:45 PM ERR undefined jvm = method()
1/20/2018 12:05:45 PM ERR undefined File "/home/vcap/deps/0/python/lib/python2.7/site-packages/jpype/_jvmfinder.py", line 164, in _get_from_known_locations
1/20/2018 12:05:45 PM ERR undefined for home in self.find_possible_homes(self._locations):
1/20/2018 12:05:45 PM ERR undefined File "/home/vcap/deps/0/python/lib/python2.7/site-packages/jpype/_jvmfinder.py", line 95, in find_possible_homes
1/20/2018 12:05:45 PM ERR undefined for childname in sorted(os.listdir(parent)):
1/20/2018 12:05:45 PM ERR undefined OSError: [Errno 2] No such file or directory: '/usr/lib/jvm'
1/20/2018 12:05:46 PM OUT undefined Exit status 1
Does anyone know how I can solve this?有谁知道我该如何解决这个问题?
After much trial and error, the solution that worked for me was a multi build pack deployment as described below:经过多次反复试验,对我有用的解决方案是多构建包部署,如下所述:
cf push -b https://github.com/cloudfoundry/multi-buildpack
and in the root of your project include a multi-buildpack.yml
with the following并在您的项目的根目录中包含一个multi-buildpack.yml
其中包含以下内容
buildpacks:
- https://github.com/cloudfoundry/apt-buildpack
- https://github.com/cloudfoundry/python-buildpack
and an apt.yml
with the following:和一个apt.yml
包含以下内容:
---
packages: openjdk-8-jre
repos: deb http://ppa.launchpad.net/openjdk-r/ppa/ubuntu trusty main
keys: https://keyserver.ubuntu.com/pks/lookup?op=get&search=0xEB9B1D8886F44E2A
In the runtime.txt
file, (also in the root of the project) is the version of python python-3.6.6
在runtime.txt
文件中,(也在项目的根目录下)是python的版本python-3.6.6
Unfortunately, JAVA gets installed in your home deps
directory and as such you'll have to create a JAVA_HOME
environment variable in the manifest.yml
.不幸的是,JAVA 安装在您的home deps
目录中,因此您必须在manifest.yml
创建一个JAVA_HOME
环境变量。
JAVA_HOME: /home/vcap/deps/0/apt/usr/lib/jvm/java-8-openjdk-amd64/jre/
I also tried to add jre/bin to the path this way我也尝试以这种方式将 jre/bin 添加到路径中
PATH: /bin:/usr/bin:/home/vcap/deps/0/apt/usr/lib/jvm/java-8-openjdk-amd64/jre/bin
However the push wipes this out and installs only the default path /bin;/usr/bin
fortunately for me, JAVA_HOME
was enough to get jaydebapi
to work with the database driver for the jar
files I had.然而,推送会清除它并只安装默认路径/bin;/usr/bin
对我来说幸运的是, JAVA_HOME
足以让jaydebapi
与我拥有的jar
文件的数据库驱动程序一起工作。 If you need this environment variable, maybe try using the python
os package to issue a command to modify the path as part of the startup.如果你需要这个环境变量,也许可以尝试使用python
os 包发出命令来修改路径作为启动的一部分。
To include a more modern answer.包括一个更现代的答案。 As I write this, most (all worth using) versions of Cloud Foundry you encounter will support multiple buildpacks out-of-the-box.在我撰写本文时,您遇到的大多数(所有值得使用的)Cloud Foundry 版本都将支持多个开箱即用的构建包。 Thus you don't need the multi-buildpack buildpack anymore.因此,您不再需要多构建包 buildpack。
Instead, you can simply cf push
and specify multiple buildpacks.相反,您可以简单地cf push
并指定多个构建包。
https://docs.cloudfoundry.org/buildpacks/use-multiple-buildpacks.html https://docs.cloudfoundry.org/buildpacks/use-multiple-buildpacks.html
This can either be done by setting multiple -b
flags to cf push
or by using a manifest.yml
file and doing something like this:这可以通过将多个-b
标志设置为cf push
或使用manifest.yml
文件并执行如下操作来完成:
...
buildpacks:
- buildpack_1
- buildpack_2
...
In either case, the execute in the order you list them.在任何一种情况下,都按照您列出它们的顺序执行。
https://docs.cloudfoundry.org/devguide/deploy-apps/manifest-attributes.html#buildpack https://docs.cloudfoundry.org/devguide/deploy-apps/manifest-attributes.html#buildpack
The rest of the answer remains the same as @lamonaki's answer.其余答案与@lamonaki 的答案相同。
Invoke both the apt-buildpack and the Python buildpack, in that order.按此顺序调用 apt-buildpack 和 Python buildpack。
Add the apt.yml
file and in it, indicate the Java package you'd like to install.添加apt.yml
文件,并在其中指明要安装的 Java 包。
Ex from @lamonaki's answer:来自@lamonaki 的回答:
--- packages: openjdk-8-jre repos: deb http://ppa.launchpad.net/openjdk-r/ppa/ubuntu trusty main keys: https://keyserver.ubuntu.com/pks/lookup?op=get&search=0xEB9B1D8886F44E2A
Add runtime.txt
to set the Python version you'd like to install添加runtime.txt
以设置您要安装的 Python 版本
Add a .profile
file to the root of the project, just like apt.yml
and runtime.txt
.将.profile
文件添加到项目的根目录,就像apt.yml
和runtime.txt
。 In that, add lines for export JAVA_HOME=/home/vcap/deps/0/apt/usr/lib/jvm/java-8-openjdk-amd64/jre/
and export PATH=$PATH:$JAVA_HOME/bin
and potentially LD_LIBRARY_PATH
if you need to reference any custom shared libraries in your Java or Python code.在其中,添加用于export JAVA_HOME=/home/vcap/deps/0/apt/usr/lib/jvm/java-8-openjdk-amd64/jre/
和export PATH=$PATH:$JAVA_HOME/bin
和潜在的LD_LIBRARY_PATH
如果您需要在 Java 或 Python 代码中引用任何自定义共享库。
You might be thinking, why apt-buildpack instead of the Java buildpack.您可能会想,为什么使用 apt-buildpack 而不是 Java buildpack。 Unfortunately, the Java buildpack in its current incarnation only supports running as a final buildpack (ie the last buildpack in the list of buildpacks).不幸的是,当前版本的 Java 构建包仅支持作为最终构建包(即构建包列表中的最后一个构建包)运行。 That rules it out as a good candidate here since you want the Python buildpack to be last.这排除了它作为一个很好的候选者,因为你希望 Python buildpack 是最后一个。 The Java Cloud Native Buildpacks will resolve the issue, but as I write this, there are no Cloud Native Buildpacks which run natively on CF. Java Cloud Native Buildpacks 将解决这个问题,但在我写这篇文章时,没有在 CF 上本地运行的 Cloud Native Buildpacks。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.