问题
I have a JBoss EWS Tomcat 7 Java application on openshift, installed with the MySQL 5.5 and PhpMyAdmin 4.0 cartridges. I have copied the repository locally, and deleted the source folder. Instead, I simply have a compiled Java application, a WAR file, that has been copied into the webapps directory and git pushed to the OpenShift servers.
If I have some database, how would I be able to access it in the webapp compiled war file. Would standard MySQL JDBC work? According to some posts I have read, OpenShift blocks the accessing of external requests to the database, and in this case, the webapp directory's war file may be making an "external request". The source for that is here: https://www.openshift.com/forums/express/external-access-to-mysql (as answered by reputable OpenShift developers).
I actually tried JDBC in the past and it did not work, but that may have been due to incorrect code. If someone wants to write some code that shows how this would be done, I would appreciate it. And if you could test it that would be even nicer :)
By the way, in the answer please do not include port forwarding. I know that works, I have tried it before, and port forwarding works. But it is often a little insecure, and has to be started each time from a computer.
回答1:
I have resolved the issue by myself and was extremely excited. Although I did not receive help myself, I hope someone else comes along this thread and is able to get it working!
So I have a Vaadin application, which has been compiled into a WAR file. I deployed it to OpenShift servers by the following steps:
Deployment of compiled webapp to OpenShift
- Open OpenShift in your web browser. Log in to Openshift. Navigate to the application in question.
- Get the ssh code of that application (it should be right of screen), located to the right of the cartridges. Copy that code using Command-C or Ctrl-C.
- Open Terminal and type
git clone ssh:\\xxxxxxxxxxxxxxxx... - If you are on Mac, like I am, it should create a project directory at
Users/Username/Appname. Inside that directory, delete the source folder andpom.xml. Take your compiled WAR file and copy it into thewebappsdirectory . - Go to Terminal. Type
cd Appname, and thengit add .,git commit -m "Deployment", and finallygit push. - Your application should now fully function at
www.openshiftappname-domainname.rhcloud.com/warfilename
MySQL access
- Install the cartridges for MySQL and phpMyAdmin. This should be available through
Add Cartidgeat youropenshift.comapp hub. - Note your
usernameandpasswordto the MySQL database that OpenShift automatically generates for you. Go towww.openshiftappname-domainname.rhcloud.com/phpmyadmin, enter the authentication credentials. - Inside phpMyAdmin there should be the server IP address; it looks something like
127.x.y.z:3306.x, y, and zcan be single digit to three digit numbers. - Quickly create a new
databasenamed whatever you want. I am going to name ittestand then consequently produce a new table in there namedtesttable. - So remember your deployed WAR application? Well, if you are using MySQL, I bet you have already included it in your application. The basic steps to establish a connection to MySQL is as such.
Java Code
Go the open IDE project that you compiled into your WAR file. Go to the pom.xml inside your project if it is a Maven project, and add the dependency:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
</dependency>
- Then use the following code.
String s = "jdbc:mysql://" + host + ":" + port + "/" + name", where host is the server IP address, the port is 3306 and name is the database name, in my case, test.
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
Connection con = DriverManager.getConnection(s, username, password);
}
catch (SQLException e) {
e.printStackTrace();
}
Now if (con == null) it did not work. And if it is not null, it did.
To test, you should recompile your WAR file (after putting some way to visually test it). If you need further help, please leave a comment. It should work when you compile WAR file and redo steps 4-6 in the first section: Deployment of compiled webapp to OpenShift. Thanks!
回答2:
Try checking out this KB about using the default mysql/postgresql connections that are available when using the java server cartridges on OpenShift: https://help.openshift.com/hc/en-us/articles/202399720-How-to-use-the-pre-configured-MySQLDS-and-PostgreSQLDS-data-sources-in-the-Java-cartridges
来源:https://stackoverflow.com/questions/25478390/openshift-webapps-mysql-java-connection