Skip to content

Commit

Permalink
Initial commit for Ads Service. (GoogleCloudPlatform#21)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
rghetia authored and ahmetb committed Aug 23, 2018
1 parent 1d266bf commit f35fdbc
Show file tree
Hide file tree
Showing 19 changed files with 1,457 additions and 272 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ pkg/
.vscode/
.vs/slnx.sqlite
.vs/microservices-demo/v15/.suo
.idea
.skaffold-*.yaml
.kubernetes-manifests-*/
63 changes: 63 additions & 0 deletions kubernetes-manifests/adservice.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: adservice
spec:
template:
metadata:
labels:
app: adservice
spec:
terminationGracePeriodSeconds: 5
containers:
- name: server
image: gcr.io/microservices-demo-app/adservice
ports:
- containerPort: 9555
env:
- name: PORT
value: "9555"
resources:
requests:
cpu: 200m
memory: 64Mi
limits:
cpu: 300m
memory: 128Mi
readinessProbe:
tcpSocket:
port: 9555
initialDelaySeconds: 5
periodSeconds: 10
livenessProbe:
tcpSocket:
port: 9555
initialDelaySeconds: 10
periodSeconds: 10
---
apiVersion: v1
kind: Service
metadata:
name: adservice
spec:
type: ClusterIP
selector:
app: adservice
ports:
- name: grpc
port: 9555
targetPort: 9555
10 changes: 5 additions & 5 deletions pb/demo.proto
Original file line number Diff line number Diff line change
Expand Up @@ -218,18 +218,18 @@ message PlaceOrderResponse {
OrderResult order = 1;
}

// ------------Ads service------------------
// ------------Ad service------------------

service AdsService {
rpc GetAds(AdsRequest) returns (AdsResponse) {}
service AdService {
rpc GetAds(AdRequest) returns (AdResponse) {}
}

message AdsRequest {
message AdRequest {
// List of important key words from the current page describing the context.
repeated string context_keys = 1;
}

message AdsResponse {
message AdResponse {
repeated Ad ads = 1;
}

Expand Down
2 changes: 2 additions & 0 deletions skaffold.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ build:
workspace: src/frontend
- imageName: gcr.io/microservices-demo-app/loadgenerator
workspace: src/loadgenerator
- imageName: gcr.io/microservices-demo-app/adservice
workspace: src/adservice
deploy:
kubectl:
manifests:
Expand Down
8 changes: 8 additions & 0 deletions src/adservice/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.iml
*.ipr
*.iws
.gradle/**
.idea/**
build/**


15 changes: 15 additions & 0 deletions src/adservice/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# adsservice
FROM openjdk:8
RUN apt-get update && apt-get install net-tools telnet
WORKDIR /app

# Next three steps are for caching dependency downloads
# to improve subsequent docker build.
COPY ["build.gradle", "gradlew", "./"]
COPY gradle gradle
RUN ./gradlew downloadRepos

COPY . .
RUN ./gradlew installDist
EXPOSE 9555
ENTRYPOINT ["/app/build/install/hipstershop/bin/AdService"]
26 changes: 26 additions & 0 deletions src/adservice/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Ad Service

The Ad service provides advertisement based on context keys. If no context keys are provided then it returns random ads.

## Local Build

The Ad service uses gradlew to compile/install/distribute. Gradle wrapper is already part of the source code. To build Ad Service, run
```
cd src/adservice; ./gradlew installDist
```
It will create executable script src/adservice/build/install/hipstershop/bin/AdService

### Upgrade gradle version
If you need to upgrade the version of gradle then run
```
cd src/adservice ; ./gradlew wrapper --gradle-version <new-version>
```

## Docker Build

From repository root, run:

```
docker build --file src/adservice/Dockerfile .
```

122 changes: 122 additions & 0 deletions src/adservice/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
description = 'Ad Service'

buildscript {
repositories {
mavenCentral()
mavenLocal()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.3'
}
}

apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'com.google.protobuf'

repositories {
mavenCentral()
mavenLocal()
}

group = "adservice"
version = "0.1.0-SNAPSHOT" // CURRENT_OPENCENSUS_VERSION

def opencensusVersion = "0.15.0" // LATEST_OPENCENSUS_RELEASE_VERSION
def grpcVersion = "1.10.1" // CURRENT_GRPC_VERSION
def prometheusVersion = "0.3.0"

tasks.withType(JavaCompile) {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
}

ext {
speed = project.hasProperty('speed') ? project.getProperty('speed') : false
offlineCompile = new File("$buildDir/output/lib")
}

dependencies {
if (speed) {
compile fileTree(dir: offlineCompile, include: '*.jar')
} else {
compile "com.google.api.grpc:proto-google-common-protos:1.11.0",
"io.opencensus:opencensus-exporter-stats-prometheus:${opencensusVersion}",
"io.opencensus:opencensus-exporter-stats-stackdriver:${opencensusVersion}",
"io.opencensus:opencensus-exporter-trace-stackdriver:${opencensusVersion}",
"io.opencensus:opencensus-exporter-trace-logging:${opencensusVersion}",
"io.grpc:grpc-protobuf:${grpcVersion}",
"io.grpc:grpc-stub:${grpcVersion}",
"io.grpc:grpc-netty:${grpcVersion}",
"io.grpc:grpc-services:${grpcVersion}",
"io.prometheus:simpleclient_httpserver:${prometheusVersion}"

runtime "io.opencensus:opencensus-impl:${opencensusVersion}",
"io.netty:netty-tcnative-boringssl-static:2.0.8.Final"
}
}

protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.5.1-1'
}
plugins {
grpc {
artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}"
}
}
generateProtoTasks {
all()*.plugins {
grpc {}
}
ofSourceSet('main')
}
}

// Inform IDEs like IntelliJ IDEA, Eclipse or NetBeans about the generated code.
sourceSets {
main {
java {
srcDirs 'hipstershop'
srcDirs 'build/generated/source/proto/main/java/hipstershop'
srcDirs 'build/generated/source/proto/main/grpc/hipstershop'
}
}
}

// Provide convenience executables for trying out the examples.
apply plugin: 'application'

startScripts.enabled = false

// This to cache dependencies during Docker image building. First build will take time.
// Subsequent build will be incremental.
task downloadRepos(type: Copy) {
from configurations.compile
into offlineCompile
from configurations.runtime
into offlineCompile
}

task adService(type: CreateStartScripts) {
mainClassName = 'hipstershop.AdService'
applicationName = 'AdService'
outputDir = new File(project.buildDir, 'tmp')
classpath = jar.outputs.files + project.configurations.runtime
}

task adServiceClient(type: CreateStartScripts) {
mainClassName = 'hipstershop.AdServiceClient'
applicationName = 'AdServiceClient'
outputDir = new File(project.buildDir, 'tmp')
classpath = jar.outputs.files + project.configurations.runtime
}

applicationDistribution.into('bin') {
from(adService)
from(adServiceClient)
fileMode = 0755
}
22 changes: 22 additions & 0 deletions src/adservice/genproto.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash -eu
#
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#!/bin/bash -e

# protos are needed in adservice folder for compiling during Docker build.

mkdir -p proto && \
cp ../../pb/demo.proto src/main/proto
Binary file added src/adservice/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
5 changes: 5 additions & 0 deletions src/adservice/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-bin.zip
Loading

0 comments on commit f35fdbc

Please sign in to comment.