8000 Google Cloud Memorystore Sample (#1096) · ScarTheSilent/java-docs-samples@02192c4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 02192c4

Browse files
jabubakekurtisvg
authored andcommitted
Google Cloud Memorystore Sample (GoogleCloudPlatform#1096)
* adding code sample for Google Cloud Memorystore * Addressing review comments memorystore_ region tags * Adding README links
1 parent 71fa3f9 commit 02192c4

File tree

12 files changed

+481
-0
lines changed

12 files changed

+481
-0
lines changed

memorystore/redis/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Getting started with Googe Cloud Memorystore
2+
Simple HTTP server example to demonstrate connecting to [Google Cloud Memorystore](https://cloud.google.com/memorystore/docs/redis)
3+
This sample uses the [Jedis client](https://mvnrepository.com/artifact/redis.clients/jedis).
4+
Please see other client library options [here](https://redis.io/clients#java).
5+
6+
## Running on GCE
7+
8+
Follow the instructions in [this guide](https://cloud.google.com/memorystore/docs/redis/connect-redis-instance-gce) to deploy the sample application on a GCE VM.
9+
10+
## Running on GKE
11+
12+
Follow the instructions in [this guide](https://cloud.google.com/memorystore/docs/redis/connect-redis-instance-gke) to deploy the sample application on GKE.
13+
14+
## Running on Google App Engine Flex
15+
16+
Follow the instructions in [this guide](https://cloud.google.com/memorystore/docs/redis/connect-redis-instance-flex) to deploy the sample application on GAE Flex.
17+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2018 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+
# [START memorystore_app_yaml]
15+
runtime: java
16+
env: flex
17+
# [END memorystore_app_yaml]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/bin/bash
2+
#
3+
# Copyright 2018 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+
# [START memorystore_deploy_sh]
17+
if [ -z "$GCS_APP_LOCATION" ]; then
18+
if [ -z "$BUCKET"]; then
19+
echo "Must set \$BUCKET. For example: BUCKET=my-bucket-name"
20+
exit 1
21+
fi
22+
GCS_APP_LOCATION="gs://$BUCKET/gce/"
23+
echo $GCS_APP_LOCATION
24+
fi
25+
26+
if [ -z "$ZONE" ]; then
27+
ZONE=$(gcloud config get-value compute/zone -q)
28+
echo $ZONE
29+
fi
30+
31+
if [ -z "$WAR" ]; then
32+
WAR=visitcounter-1.0-SNAPSHOT.war
33+
fi
34+
35+
#Build the WAR package
36+
cd ..
37+
mvn clean package
38+
39+
#Copy the WAR artifact to the GCS bucket location
40+
gsutil cp -r target/${WAR} ${GCS_APP_LOCATION}
41+
42+
cd gce_deployment
43+
44+
# Create an instance
45+
gcloud compute instances create my-instance \
46+
--image-family=debian-9 \
47+
--image-project=debian-cloud \
48+
--machine-type=g1-small \
49+
--scopes cloud-platform \
50+
--metadata-from-file startup-script=startup-script.sh \
51+
--metadata app-location=${GCS_APP_LOCATION},app-war=$WAR \
52+
--zone $ZONE \
53+
--tags http-server
54+
55+
gcloud compute firewall-rules create allow-http-server-8080 \
56+
--allow tcp:8080 \
57+
--source-ranges 0.0.0.0/0 \
58+
--target-tags http-server \
59+
--description "Allow port 8080 access to http-server"
60+
# [END memorystore_deploy_sh]
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#! /bin/bash
2+
3+
# Copyright 2018 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+
# [START memorystore_startup_script_sh]
17+
set -ex
18+
19+
# Talk to the metadata server to get the project id and location of application binary.
20+
PROJECTID=$(curl -s "http://metadata.google.internal/computeMetadata/v1/project/project-id" -H "Metadata-Flavor: Google")
21+
GCS_APP_LOCATION=$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/app-location" -H "Metadata-Flavor: Google")
22+
WAR=$(curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/app-war" -H "Metadata-Flavor: Google")
23+
24+
gsutil cp "$GCS_APP_LOCATION"** .
25+
26+
# Install dependencies from apt
27+
apt-get update
28+
apt-get install -qq openjdk-8-jdk-headless
29+
30+
# Make Java8 the default
31+
update-alternatives --set java /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
32+
33+
# Jetty Setup
34+
mkdir -p /opt/jetty/temp
35+
mkdir -p /var/log/jetty
36+
37+
# Get Jetty
38+
curl -L https://repo1.maven.org/maven2/org/eclipse/jetty/jetty-distribution/9.4.10.v20180503/jetty-distribution-9.4.10.v20180503.tar.gz -o jetty9.tgz
39+
tar xf jetty9.tgz --strip-components=1 -C /opt/jetty
40+
41+
# Add a Jetty User
42+
useradd --user-group --shell /bin/false --home-dir /opt/jetty/temp jetty
43+
44+
cd /opt/jetty
45+
# Add running as "jetty"
46+
java -jar /opt/jetty/start.jar --add-to-startd=setuid
47+
cd /
48+
49+
# very important - by renaming the war to root.war, it will run as the root servlet.
50+
mv $WAR /opt/jetty/webapps/root.war
51+
52+
# Make sure "jetty" owns everything.
53+
chown --recursive jetty /opt/jetty
54+
55+
# Configure the default paths for the Jetty service
56+
cp /opt/jetty/bin/jetty.sh /etc/init.d/jetty
57+
echo "JETTY_HOME=/opt/jetty" > /etc/default/jetty
58+
{
59+
echo "JETTY_BASE=/opt/jetty"
60+
echo "TMPDIR=/opt/jetty/temp"
61+
echo "JAVA_OPTIONS=-Djetty.http.port=8080"
62+
echo "JETTY_LOGS=/var/log/jetty"
63+
} >> /etc/default/jetty
64+
65+
66+
# Reload daemon to pick up new service
67+
systemctl daemon-reload
68+
69+
# Install logging monitor. The monitor will automatically pickup logs sent to syslog.
70+
curl -s "https://storage.googleapis.com/signals-agents/logging/google-fluentd-install.sh" | bash
71+
service google-fluentd restart &
72+
73+
service jetty start
74+
service jetty check
75+
76+
echo "Startup Complete"
77+
# [END memorystore_startup_script_sh]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
# Copyright 2018 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+
# [START memorystore_teardown_sh]
17+
gcloud compute instances delete my-instance
18+
19+
gcloud compute firewall-rules delete allow-http-server-8080
20+
# [END memorystore_teardown_sh]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2018 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+
FROM launcher.gcr.io/google/jetty
16+
17+
ADD target/visitcounter-1.0-SNAPSHOT.war $JETTY_BASE/webapps/root.war
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright 2018 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+
apiVersion: extensions/v1beta1
16+
kind: Deployment
17+
metadata:
18+
name: visit-counter
19+
labels:
20+
app: visit-counter
21+
spec:
22+
replicas: 1
23+
template:
24+
metadata:
25+
labels:
26+
app: visit-counter
27+
spec:
28+
containers:
29+
- name: visit-counter
30+
image: "gcr.io/<PROJECT_ID>/visit-counter:v1"
31+
env:
32+
- name: REDISHOST
33+
valueFrom:
34+
configMapKeyRef:
35+
name: redishost
36+
key: REDISHOST
37+
ports:
38+
- name: http
39+
containerPort: 8080
40+
---
41+
apiVersion: v1
42+
kind: Service
43+
metadata:
44+
name: visit-counter
45+
spec:
46+
type: LoadBalancer
47+
selector:
48+
app: visit-counter
49+
ports:
50+
- port: 80
51+
targetPort: 8080
52+
protocol: TCP

memorystore/redis/pom.xml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<!--
2+
Copyright 2018 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
<project>
17+
<modelVersion>4.0.0</modelVersion>
18+
<packaging>war</packaging>
19+
<version>1.0-SNAPSHOT</version>
20+
<groupId>com.example.redis</groupId>
21+
<artifactId>visitcounter</artifactId>
22+
23+
<!--
24+
The parent pom defines common style checks and testing strategies for our samples.
25+
Removing or replacing it should not affect the execution of the samples in anyway.
26+
-->
27+
<parent>
28+
<groupId>com.google.cloud.samples</groupId>
29+
<artifactId>shared-configuration</artifactId>
30+
<version>1.0.10</version>
31+
</parent>
32+
33+
<properties>
34+
<maven.compiler.target>1.8</maven.compiler.target>
35+
<maven.compiler.source>1.8</maven.compiler.source>
36+
<jetty>9.4.10.v20180503</jetty>
37+
<failOnMissingWebXml>false</failOnMissingWebXml>
38+
</properties>
39+
40+
<!-- [START memorystore_dependencies] -->
41+
<dependencies>
42+
<dependency>
43+
<groupId>javax.servlet</groupId>
44+
<artifactId>javax.servlet-api</artifactId>
45+
<version>3.1.0</version>
46+
<type>jar</type>
47+
<scope>provided</scope>
48+
</dependency>
49+
<dependency>
50+
<groupId>redis.clients</groupId>
51+
<artifactId>jedis</artifactId>
52+
<version>2.9.0</version>
53+
</dependency>
54+
</dependencies>
55+
<!-- [END memorystore_dependencies] -->
56+
57+
<build>
58+
<!-- for hot reload of the web application -->
59+
<outputDirectory>servlet/target/visitcounter-1.0-SNAPSHOT/WEB-INF/classes</outputDirectory>
60+
<plugins>
61+
<!--- Only required to test application locally -->
62+
<plugin>
63+
<groupId>org.eclipse.jetty</groupId>
64+
<artifactId>jetty-maven-plugin</artifactId>
65+
<version>${jetty}</version>
66+
</plugin>
67+
<!-- [START memorystore_appengine_maven_plugin] -->
68+
<!-- Deployment plugin for App Engine Flexible -->
69+
<plugin>
70+
<groupId>com.google.cloud.tools</groupId>
71+
<artifactId>appengine-maven-plugin</artifactId>
72+
<version>1.3.2</version>
73+
</plugin>
74+
<!-- [END memorystore_appengine_maven_plugin] -->
75+
</plugins>
76+
</build>
77+
</project>

0 commit comments

Comments
 (0)
0