Skip to content

Commit f35fdbc

Browse files
rghetiaahmetb
authored andcommitted
Initial commit for Ads Service. (GoogleCloudPlatform#21)
* Initial commit for Ads Service. * update comments for AdsService and AdsServiceClient * Refactor Ads to Ad Move building AdService to Docker Use default setting for Stackdriver Exporter. Add license text. * Revert the projectId - also remove commented code from frontend/rpc.go * Add adservie to skaffold.yaml * Remove skaffold-adservice.yaml * Replace personal projectId with demo projectId. * Fix the crash in adservice when ran in locally. * Ignore .skaffold*yaml file and .kubernetes-manifests-*/ dir for easy ProjectID switch. * Fixed review comments. 1. Changed Ad redirect urls to products. 2. Removed leftovers from Dockerfile/kub*manifests*yaml 3. Added retry for StackDriver. 4. Added log for Ad request. 5. Added comment for gradle caching. 6. Added README.md to src/adservice. * Added GRPC Health service to Ad Service Also added 1. timeout to getAd RPC call in frontend. 2. Async thread for stackdriver init.
1 parent 1d266bf commit f35fdbc

19 files changed

+1457
-272
lines changed

Diff for: .gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ pkg/
77
.vscode/
88
.vs/slnx.sqlite
99
.vs/microservices-demo/v15/.suo
10+
.idea
11+
.skaffold-*.yaml
12+
.kubernetes-manifests-*/

Diff for: kubernetes-manifests/adservice.yaml

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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: adservice
19+
spec:
20+
template:
21+
metadata:
22+
labels:
23+
app: adservice
24+
spec:
25+
terminationGracePeriodSeconds: 5
26+
containers:
27+
- name: server
28+
image: gcr.io/microservices-demo-app/adservice
29+
ports:
30+
- containerPort: 9555
31+
env:
32+
- name: PORT
33+
value: "9555"
34+
resources:
35+
requests:
36+
cpu: 200m
37+
memory: 64Mi
38+
limits:
39+
cpu: 300m
40+
memory: 128Mi
41+
readinessProbe:
42+
tcpSocket:
43+
port: 9555
44+
initialDelaySeconds: 5
45+
periodSeconds: 10
46+
livenessProbe:
47+
tcpSocket:
48+
port: 9555
49+
initialDelaySeconds: 10
50+
periodSeconds: 10
51+
---
52+
apiVersion: v1
53+
kind: Service
54+
metadata:
55+
name: adservice
56+
spec:
57+
type: ClusterIP
58+
selector:
59+
app: adservice
60+
ports:
61+
- name: grpc
62+
port: 9555
63+
targetPort: 9555

Diff for: pb/demo.proto

+5-5
Original file line numberDiff line numberDiff line change
@@ -218,18 +218,18 @@ message PlaceOrderResponse {
218218
OrderResult order = 1;
219219
}
220220

221-
// ------------Ads service------------------
221+
// ------------Ad service------------------
222222

223-
service AdsService {
224-
rpc GetAds(AdsRequest) returns (AdsResponse) {}
223+
service AdService {
224+
rpc GetAds(AdRequest) returns (AdResponse) {}
225225
}
226226

227-
message AdsRequest {
227+
message AdRequest {
228228
// List of important key words from the current page describing the context.
229229
repeated string context_keys = 1;
230230
}
231231

232-
message AdsResponse {
232+
message AdResponse {
233233
repeated Ad ads = 1;
234234
}
235235

Diff for: skaffold.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ build:
3838
workspace: src/frontend
3939
- imageName: gcr.io/microservices-demo-app/loadgenerator
4040
workspace: src/loadgenerator
41+
- imageName: gcr.io/microservices-demo-app/adservice
42+
workspace: src/adservice
4143
deploy:
4244
kubectl:
4345
manifests:

Diff for: src/adservice/.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.iml
2+
*.ipr
3+
*.iws
4+
.gradle/**
5+
.idea/**
6+
build/**
7+
8+

Diff for: src/adservice/Dockerfile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# adsservice
2+
FROM openjdk:8
3+
RUN apt-get update && apt-get install net-tools telnet
4+
WORKDIR /app
5+
6+
# Next three steps are for caching dependency downloads
7+
# to improve subsequent docker build.
8+
COPY ["build.gradle", "gradlew", "./"]
9+
COPY gradle gradle
10+
RUN ./gradlew downloadRepos
11+
12+
COPY . .
13+
RUN ./gradlew installDist
14+
EXPOSE 9555
15+
ENTRYPOINT ["/app/build/install/hipstershop/bin/AdService"]

Diff for: src/adservice/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Ad Service
2+
3+
The Ad service provides advertisement based on context keys. If no context keys are provided then it returns random ads.
4+
5+
## Local Build
6+
7+
The Ad service uses gradlew to compile/install/distribute. Gradle wrapper is already part of the source code. To build Ad Service, run
8+
```
9+
cd src/adservice; ./gradlew installDist
10+
```
11+
It will create executable script src/adservice/build/install/hipstershop/bin/AdService
12+
13+
### Upgrade gradle version
14+
If you need to upgrade the version of gradle then run
15+
```
16+
cd src/adservice ; ./gradlew wrapper --gradle-version <new-version>
17+
```
18+
19+
## Docker Build
20+
21+
From repository root, run:
22+
23+
```
24+
docker build --file src/adservice/Dockerfile .
25+
```
26+

Diff for: src/adservice/build.gradle

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
description = 'Ad Service'
2+
3+
buildscript {
4+
repositories {
5+
mavenCentral()
6+
mavenLocal()
7+
maven {
8+
url "https://plugins.gradle.org/m2/"
9+
}
10+
}
11+
dependencies {
12+
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.3'
13+
}
14+
}
15+
16+
apply plugin: 'idea'
17+
apply plugin: 'java'
18+
apply plugin: 'com.google.protobuf'
19+
20+
repositories {
21+
mavenCentral()
22+
mavenLocal()
23+
}
24+
25+
group = "adservice"
26+
version = "0.1.0-SNAPSHOT" // CURRENT_OPENCENSUS_VERSION
27+
28+
def opencensusVersion = "0.15.0" // LATEST_OPENCENSUS_RELEASE_VERSION
29+
def grpcVersion = "1.10.1" // CURRENT_GRPC_VERSION
30+
def prometheusVersion = "0.3.0"
31+
32+
tasks.withType(JavaCompile) {
33+
sourceCompatibility = '1.8'
34+
targetCompatibility = '1.8'
35+
}
36+
37+
ext {
38+
speed = project.hasProperty('speed') ? project.getProperty('speed') : false
39+
offlineCompile = new File("$buildDir/output/lib")
40+
}
41+
42+
dependencies {
43+
if (speed) {
44+
compile fileTree(dir: offlineCompile, include: '*.jar')
45+
} else {
46+
compile "com.google.api.grpc:proto-google-common-protos:1.11.0",
47+
"io.opencensus:opencensus-exporter-stats-prometheus:${opencensusVersion}",
48+
"io.opencensus:opencensus-exporter-stats-stackdriver:${opencensusVersion}",
49+
"io.opencensus:opencensus-exporter-trace-stackdriver:${opencensusVersion}",
50+
"io.opencensus:opencensus-exporter-trace-logging:${opencensusVersion}",
51+
"io.grpc:grpc-protobuf:${grpcVersion}",
52+
"io.grpc:grpc-stub:${grpcVersion}",
53+
"io.grpc:grpc-netty:${grpcVersion}",
54+
"io.grpc:grpc-services:${grpcVersion}",
55+
"io.prometheus:simpleclient_httpserver:${prometheusVersion}"
56+
57+
runtime "io.opencensus:opencensus-impl:${opencensusVersion}",
58+
"io.netty:netty-tcnative-boringssl-static:2.0.8.Final"
59+
}
60+
}
61+
62+
protobuf {
63+
protoc {
64+
artifact = 'com.google.protobuf:protoc:3.5.1-1'
65+
}
66+
plugins {
67+
grpc {
68+
artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}"
69+
}
70+
}
71+
generateProtoTasks {
72+
all()*.plugins {
73+
grpc {}
74+
}
75+
ofSourceSet('main')
76+
}
77+
}
78+
79+
// Inform IDEs like IntelliJ IDEA, Eclipse or NetBeans about the generated code.
80+
sourceSets {
81+
main {
82+
java {
83+
srcDirs 'hipstershop'
84+
srcDirs 'build/generated/source/proto/main/java/hipstershop'
85+
srcDirs 'build/generated/source/proto/main/grpc/hipstershop'
86+
}
87+
}
88+
}
89+
90+
// Provide convenience executables for trying out the examples.
91+
apply plugin: 'application'
92+
93+
startScripts.enabled = false
94+
95+
// This to cache dependencies during Docker image building. First build will take time.
96+
// Subsequent build will be incremental.
97+
task downloadRepos(type: Copy) {
98+
from configurations.compile
99+
into offlineCompile
100+
from configurations.runtime
101+
into offlineCompile
102+
}
103+
104+
task adService(type: CreateStartScripts) {
105+
mainClassName = 'hipstershop.AdService'
106+
applicationName = 'AdService'
107+
outputDir = new File(project.buildDir, 'tmp')
108+
classpath = jar.outputs.files + project.configurations.runtime
109+
}
110+
111+
task adServiceClient(type: CreateStartScripts) {
112+
mainClassName = 'hipstershop.AdServiceClient'
113+
applicationName = 'AdServiceClient'
114+
outputDir = new File(project.buildDir, 'tmp')
115+
classpath = jar.outputs.files + project.configurations.runtime
116+
}
117+
118+
applicationDistribution.into('bin') {
119+
from(adService)
120+
from(adServiceClient)
121+
fileMode = 0755
122+
}

Diff for: src/adservice/genproto.sh

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash -eu
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+
17+
#!/bin/bash -e
18+
19+
# protos are needed in adservice folder for compiling during Docker build.
20+
21+
mkdir -p proto && \
22+
cp ../../pb/demo.proto src/main/proto

Diff for: src/adservice/gradle/wrapper/gradle-wrapper.jar

53.1 KB
Binary file not shown.
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
zipStoreBase=GRADLE_USER_HOME
4+
zipStorePath=wrapper/dists
5+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-bin.zip

0 commit comments

Comments
 (0)