Skip to content

Commit 4172cf5

Browse files
lesvdpebot
authored andcommitted
CloudSql for Flexible update (#455)
1 parent ed65477 commit 4172cf5

File tree

6 files changed

+87
-38
lines changed

6 files changed

+87
-38
lines changed

flexible/cloudsql/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/bin/
2+
target

flexible/cloudsql/README.md

+20-23
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
1-
# Cloud SQL sample for Google Managed VMs
2-
This sample demonstrates how to use [Cloud SQL](https://cloud.google.com/sql/) on Google Managed VMs.
1+
# Cloud SQL sample for Google App Engine Flexible
2+
This sample demonstrates how to use [Cloud SQL](https://cloud.google.com/sql/) on Google App Engine
3+
Flexible.
34

45
## Setup
5-
Before you can run or deploy the sample, you will need to do the following:
6+
Before you can run or deploy the sample, you will need to create a [Cloud SQL instance)](https://cloud.google.com/sql/docs/create-instance)
67

7-
1. Create a [Second Generation Cloud SQL](https://cloud.google.com/sql/docs/create-instance) instance. You can do this from the [Cloud Console](https://console.developers.google.com) or via the [Cloud SDK](https://cloud.google.com/sdk). To create it via the SDK use the following command:
8+
1. Create a new user and database for the application. The easiest way to do this is via the [Google
9+
Developers Console](https://console.cloud.google.com/sql/instances). Alternatively, you can use
10+
MySQL tools such as the command line client or workbench.
11+
2. Change the root password (under Access Control) and / or create a new user / password.
12+
3. Create a Database (under Databases) (or use MySQL with `gcloud sql connect <instance> --user=root`)
13+
4. Note the **Instance connection name** under Overview > Properties
14+
(It will look like project:region:zone for 2nd Generation)
815

9-
$ gcloud sql instances create YOUR_INSTANCE_NAME \
10-
--activation-policy=ALWAYS \
11-
--tier=db-n1-standard-1
12-
13-
1. Set the root password on your Cloud SQL instance:
14-
15-
$ gcloud sql instances set-root-password YOUR_INSTANCE_NAME --password YOUR_INSTANCE_ROOT_PASSWORD
16-
17-
1. Use the MySQL command line tools (or a management tool of your choice) to create a [new user](https://cloud.google.com/sql/docs/create-user) and [database](https://cloud.google.com/sql/docs/create-database) for your application:
16+
## Deploying
1817

19-
$ mysql -h [IP Address of database] -u root -p
20-
mysql> create database YOUR_DATABASE;
21-
mysql> create user 'YOUR_USER'@'%' identified by 'PASSWORD';
22-
mysql> grant all on YOUR_DATABASE.* to 'YOUR_USER'@'%';
18+
```bash
19+
$ mvn clean appengine:deploy -DINSTANCE_CONNECTION_NAME=instanceConnectionName -Duser=root
20+
-Dpassword=myPassword -Ddatabase=myDatabase
21+
```
2322

24-
1. Set the connection string environment variable in src/main/appengine/app.yaml
23+
Or you can update the properties in `pom.xml`
2524

2625
## Running locally
27-
Export local variables
28-
$ export SQL_URL="jdbc:mysql://google/YOUR-DB-NAME?cloudSqlInstance=YOUR-INSTANCE-NAME&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=USERNAME&password=PASSWORD"
29-
$ mvn clean jetty:run
3026

31-
## Deploying
32-
$ mvn clean appengine:deploy
27+
```bash
28+
$ mvn clean jetty:run -DINSTANCE_CONNECTION_NAME=instanceConnectionName -Duser=root -Dpassword=myPassowrd -Ddatabase=myDatabase
29+
```

flexible/cloudsql/pom.xml

+45-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
Copyright 2016 Google Inc. All Rights Reserved.
2+
Copyright 2016 Google Inc.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -27,15 +27,28 @@
2727
<relativePath>../..</relativePath>
2828
</parent>
2929

30+
<!-- [START properties] -->
3031
<properties>
32+
<!-- INSTANCE_CONNECTION_NAME from Cloud Console > SQL > Instance Details > Properties
33+
or `gcloud sql instances describe <instance> | grep connectionName`
34+
project:region:instance for Cloud SQL 2nd Generation
35+
-->
36+
<INSTANCE_CONNECTION_NAME></INSTANCE_CONNECTION_NAME>
37+
<user>root</user>
38+
<password></password>
39+
<database>sqldemo</database>
40+
<!-- [START_EXCLUDE] -->
3141
<maven.compiler.target>1.8</maven.compiler.target>
3242
<maven.compiler.source>1.8</maven.compiler.source>
3343

3444
<appengine.maven.plugin>1.0.0</appengine.maven.plugin>
3545
<jetty.maven.plugin>9.3.8.v20160314</jetty.maven.plugin>
3646

3747
<failOnMissingWebXml>false</failOnMissingWebXml> <!-- REQUIRED -->
48+
<!-- [END_EXCLUDE] -->
49+
<sqlURL>jdbc:mysql://google/${database}?cloudSqlInstance=${INSTANCE_CONNECTION_NAME}&amp;socketFactory=com.google.cloud.sql.mysql.SocketFactory&amp;user=${user}&amp;password=${password}&amp;useSSL=false</sqlURL>
3850
</properties>
51+
<!-- [END properties] -->
3952

4053
<dependencies>
4154
<dependency>
@@ -46,23 +59,51 @@
4659
<scope>provided</scope>
4760
</dependency>
4861
<!-- [START dependencies] -->
62+
<dependency> <!-- http://dev.mysql.com/doc/connector-j/en/ -->
63+
<groupId>mysql</groupId>
64+
<artifactId>mysql-connector-java</artifactId>
65+
<version>6.0.5</version>
66+
</dependency>
4967
<dependency>
5068
<groupId>com.google.cloud.sql</groupId>
51-
<artifactId>mysql-socket-factory</artifactId>
69+
<artifactId>mysql-socket-factory-connector-j-6</artifactId>
5270
<version>1.0.2</version>
5371
</dependency>
5472
<!-- [END dependencies] -->
5573
</dependencies>
5674
<build>
75+
<resources>
76+
<resource>
77+
<directory>src/main/resources</directory>
78+
<filtering>true</filtering>
79+
</resource>
80+
</resources>
5781
<!-- for hot reload of the web application -->
5882
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
5983
<plugins>
84+
85+
<!--
86+
<plugin>
87+
<groupId>org.apache.maven.plugins</groupId>
88+
<artifactId>maven-war-plugin</artifactId>
89+
<version>3.0.0</version>
90+
<configuration>
91+
<webResources>
92+
<resource>
93+
<directory>${basedir}/src/main/appengine</directory>
94+
<filtering>true</filtering>
95+
<targetPath></targetPath>
96+
</resource>
97+
</webResources>
98+
</configuration>
99+
</plugin>
100+
-->
101+
102+
60103
<plugin>
61104
<groupId>com.google.cloud.tools</groupId>
62105
<artifactId>appengine-maven-plugin</artifactId>
63106
<version>${appengine.maven.plugin}</version>
64-
<configuration>
65-
</configuration>
66107
</plugin>
67108

68109
<plugin>
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2016 Google Inc. All Rights Reserved.
1+
# Copyright 2016 Google Inc.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -13,16 +13,13 @@
1313
# limitations under the License.
1414

1515
runtime: java
16-
vm: true
16+
env: flex
1717

1818
handlers:
1919
- url: /.*
2020
script: this field is required, but ignored
2121
secure: always
2222

23-
# [START env_variables]
24-
# YOUR-INSTANCE-NAME is ProjectID:Region:DbInstance
25-
# YOUR-DB-NAME is your DatabaseName
26-
env_variables:
27-
SQL_URL: jdbc:mysql://google/YOUR-DB-NAME?cloudSqlInstance=YOUR-INSTANCE-NAME&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=USERNAME&password=PASSWORD
28-
# [END env_variables]
23+
automatic_scaling:
24+
min_num_instances: 1
25+
max_num_instances: 2

flexible/cloudsql/src/main/java/com/example/cloudsql/CloudSqlServlet.java

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016 Google Inc. All Rights Reserved.
2+
* Copyright 2016 Google Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828
import java.sql.SQLException;
2929
import java.sql.Timestamp;
3030
import java.util.Date;
31+
import java.util.Properties;
3132

3233
import javax.servlet.ServletException;
3334
import javax.servlet.annotation.WebServlet;
@@ -39,6 +40,7 @@
3940
@SuppressWarnings("serial")
4041
@WebServlet(name = "cloudsql", value = "")
4142
public class CloudSqlServlet extends HttpServlet {
43+
String url;
4244

4345
@Override
4446
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException,
@@ -64,8 +66,6 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOExc
6466
+ "LIMIT 10";
6567
PrintWriter out = resp.getWriter();
6668
resp.setContentType("text/plain");
67-
// Detect if running remotely or locally and select correct connection url
68-
String url = System.getenv("SQL_URL");
6969

7070
try (Connection conn = DriverManager.getConnection(url);
7171
PreparedStatement statementCreateVisit = conn.prepareStatement(createVisitSql)) {
@@ -86,5 +86,17 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOExc
8686
throw new ServletException("SQL error", e);
8787
}
8888
}
89+
90+
@Override
91+
public void init() {
92+
try {
93+
Properties properties = new Properties();
94+
properties.load(
95+
getServletContext().getResourceAsStream("/WEB-INF/classes/config.properties"));
96+
url = properties.getProperty("sqlUrl");
97+
} catch (IOException e) {
98+
log("no property", e); // Servlet Init should never fail.
99+
}
100+
}
89101
}
90102
// [END example]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sqlUrl=${sqlURL}

0 commit comments

Comments
 (0)