8000 Add Cloud Sql sample for Java 11 (#1414) · llozano100/java-docs-samples@144784a · GitHub
[go: up one dir, main page]

Skip to content

Commit 144784a

Browse files
authored
Add Cloud Sql sample for Java 11 (GoogleCloudPlatform#1414)
* Add Cloud Sql sample * Add comments to pom * Update pom * Update cloudsql sample for Java 11
1 parent 46821aa commit 144784a

File tree

3 files changed

+241
-0
lines changed

3 files changed

+241
-0
lines changed

appengine-java11/cloudsql/README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Connecting to Cloud SQL - MySQL with Java 11 on Google App Engine Standard
2+
3+
This sample demonstrates how to use
4+
[Cloud SQL - MySQL](https://cloud.google.com/sql/docs/mysql/) on
5+
Google App Engine Standard.
6+
7+
## Code
8+
9+
The sample code is located in `java-docs-samples/cloud-sql/mysql/servlet`. This directory has the supplemental files needed to deploy to Google App Engine Standard with Java 11.
10+
11+
## Setup your Cloud SQL Database
12+
13+
- Create a 2nd Gen Cloud SQL Instance by following these
14+
[instructions](https://cloud.google.com/sql/docs/mysql/create-instance). Note the connection string,
15+
database user, and database password that you create.
16+
17+
- Create a database for your application by following these
18+
[instructions](https://cloud.google.com/sql/docs/mysql/create-manage-databases). Note the database
19+
name.
20+
21+
- Create a service account with the 'Cloud SQL Client' permissions by following these
22+
[instructions](https://cloud.google.com/sql/docs/mysql/connect-external-app#4_if_required_by_your_authentication_method_create_a_service_account).
23+
Download a JSON key to use to authenticate your connection.
24+
25+
## Setup the App
26+
27+
- See [Prerequisites](../README.md#Prerequisites).
28+
29+
- Add the [appengine-simple-jetty-main](../README.md#appengine-simple-jetty-main)
30+
Main class to your classpath:
31+
```
32+
cd java-docs-samples/appengine-java11/appengine-simple-jetty-main
33+
mvn install
34+
```
35+
36+
- Move into the sample directory:
37+
```
38+
cd ../../cloud-sql/mysql/servlet/
39+
```
40+
41+
- In the new Java 11 runtime, you must remove your `appengine-web.xml` file and create an `app.yaml` file to configure your application settings. Create an `src/main/appengine` directory and copy the `app.yaml` provided:
42+
```bash
43+
mkdir src/main/appengine
44+
cp ../../../appengine-java11/cloudsql/app.yaml src/main/appengine/
45+
```
46+
47+
- Use the information from creating your database to replace the
48+
environment variables in your `app.yaml`:
49+
```YAML
50+
CLOUD_SQL_CONNECTION_NAME: '<MY-PROJECT>:<INSTANCE-REGION>:<MY-DATABASE>'
51+
DB_NAME: 'my_db'
52+
DB_USER: 'my-db-user'
53+
DB_PASS: 'my-db-pass'
54+
```
55+
56+
- Java 11 has specific requirements on packaging your app. Replace the `pom.xml` with the Java 11 `pom.xml`:
57+
```bash
58+
cp ../../../appengine-java11/cloudsql/pom.xml ./
59+
```
60+
61+
### Deploy to Google Cloud
62+
63+
The following command will deploy the application to your Google Cloud project:
64+
```
65+
mvn clean package appengine:deploy -Dapp.deploy.projectId=<your-project-id>
66+
```
67+
68+
View your application:
69+
```
70+
gcloud app browse
71+
```
72+
or by visiting `https://<your-project-id>.appspot.com`.

appengine-java11/cloudsql/app.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
runtime: java11
16+
instance_class: F2
17+
entrypoint: 'java -cp * com.example.appengine.demo.jettymain.Main cloudsql.war'
18+
19+
env_variables:
20+
CLOUD_SQL_CONNECTION_NAME: "my-project:region:instance"
21+
DB_NAME: "my_db"
22+
DB_USER: "my-db-user"
23+
DB_PASS: "my-db-password"

appengine-java11/cloudsql/pom.xml

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2019 Google LLC
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
19+
<modelVersion>4.0.0</modelVersion>
20+
<packaging>war</packaging>
21+
<version>1.0-SNAPSHOT</version>
22+
<groupId>com.example.cloudsql</groupId>
23+
<artifactId>cloudsql</artifactId>
24+
25+
<!--
26+
The parent pom defines common style checks and testing strategies for our samples.
27+
Removing or replacing it should not affect the execution of the samples in anyway.
28+
-->
29+
<parent>
30+
<groupId>com.google.cloud.samples</groupId>
31+
<artifactId>shared-configuration</artifactId>
32+
<version>1.0.11</version>
33+
</parent>
34+
35+
<properties>
36+
<maven.compiler.target>11</maven.compiler.target>
37+
<maven.compiler.source>11</maven.compiler.source>
38+
<failOnMissingWebXml>false</failOnMissingWebXml>
39+
</properties>
40+
41+
<!-- All dependencies are scoped as provided.
42+
Dependent-jars are created and added to
43+
${project.build.directory}/appengine-staging -->
44+
<dependencies>
45+
<!-- Dependency from folder `../appengine-simple-jetty-main` -->
46+
<!-- See README for instructions for more information -->
47+
<dependency>
48+
<groupId>com.example.appengine.demo</groupId>
49+
<artifactId>simple-jetty-main</artifactId>
50+
<version>1</version>
51+
<scope>provided</scope>
52+
</dependency>
53+
<dependency>
54+
<groupId>javax.servlet</groupId>
55+
<artifactId>javax.servlet-api</artifactId>
56+
<version>3.1.0</version>
57+
<type>jar</type>
58+
<scope>provided</scope>
59+
</dependency>
60+
<dependency>
61+
<groupId>javax.servlet</groupId>
62+
<artifactId>jstl</artifactId>
63+
<version>1.2</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>org.slf4j</groupId>
67+
<artifactId>slf4j-simple</artifactId>
68+
<version>1.7.25</version>
69+
<scope>provided</scope>
70+
</dependency>
71+
<!-- Dependencies for Cloud SQL -->
72+
<dependency>
73+
<groupId>mysql</groupId>
74+
<artifactId>mysql-connector-java</artifactId>
75+
<version>8.0.11</version>
76+
<scope>provided</scope>
77+
</dependency>
78+
<dependency>
79+
<groupId>com.google.cloud.sql</groupId>
80+
<artifactId>mysql-socket-factory-connector-j-8</artifactId>
81+
<version>1.0.12</version>
82+
<scope>provided</scope>
83+
</dependency>
84+
<dependency>
85+
<groupId>com.zaxxer</groupId>
86+
<artifactId>HikariCP</artifactId>
87+
<version>3.3.0</version>
88+
<scope>provided</scope>
89+
</dependency>
90+
</dependencies>
91+
92+
<build>
93+
<finalName>cloudsql</finalName>
94+
<plugins>
95+
<plugin>
96+
<groupId>org.apache.maven.plugins</groupId>
97+
<artifactId>maven-war-plugin</artifactId>
98+
<version>3.2.2</version>
99+
</plugin>
100+
<plugin>
101+
<groupId>org.apache.maven.plugins</groupId>
102+
<artifactId>maven-dependency-plugin</artifactId>
103+
<version>3.1.1</version>
104+
<executions>
105+
<execution>
106+
<id>copy</id>
107+
<phase>prepare-package</phase>
108+
<goals>
109+
<goal>copy-dependencies</goal>
110+
</goals>
111+
<configuration>
112+
<outputDirectory>
113+
${project.build.directory}/appengine-staging
114+
</outputDirectory>
115+
</configuration>
116+
</execution>
117+
</executions>
118+
</plugin>
119+
<plugin>
120+
<artifactId>maven-resources-plugin</artifactId>
121+
<version>2.7</version>
122+
<executions>
123+
<execution>
124+
<id>copy-resources</id>
125+
<phase>install</phase>
126+
<goals>
127+
<goal>copy-resources</goal>
128+
</goals>
129+
<configuration>
130+
<outputDirectory>${project.build.directory}/appengine-staging/cprof</outputDirectory>
131+
</configuration>
132+
</execution>
133+
</executions>
134+
</plugin>
135+
136+
<plugin>
137+
<groupId>com.google.cloud.tools</groupId>
138+
<artifactId>appengine-maven-plugin</artifactId>
139+
<version>2.0.0</version>
140+
<configuration>
141+
<version>cloudsql</version>
142+
</configuration>
143+
</plugin>
144+
</plugins>
145+
</build>
146+
</project>

0 commit comments

Comments
 (0)
0