How to structure multiple services?

771 views
Skip to first unread message

Robie Ie

unread,
Sep 6, 2017, 4:52:21 PM9/6/17
to Google App Engine
I have a project setup as follows:

/root-folder --
  /common-code
  /service-1 --
    /code
    /app.yaml
  /service-2 --
    /code
    /app.yaml
  /service-3 --
    /code
    /app.yaml

Problem is I can't reference /common-code since all the app.yamls reference the current folder...

How do I achieve such a structure?
Message has been deleted

Kenworth (Google Cloud Platform)

unread,
Sep 6, 2017, 7:12:04 PM9/6/17
to Google App Engine
Based on your structure, you can deploy multiple services from /root-folder using:

gcloud app deploy common-code/service-1/app.yaml common-code/service-2/app.yaml common-code/service-3/app.yaml

You can find more discussions about microservices on this thread and a sample GitHub project on this link.

Robie Ie

unread,
Sep 6, 2017, 11:02:39 PM9/6/17
to Google App Engine
Sorry if I confused you, I am aware of how to deploy multiple yaml files.

Problem is that the gcloud deploy only deploys the files in that service.

I have common code sitting in a top level folder, that the services reference.

Thus when it is deployed, only the code residing in the same folder as the app.yaml is deployed.

Any ideas?

Kenworth (Google Cloud Platform)

unread,
Sep 7, 2017, 4:49:48 PM9/7/17
to google-a...@googlegroups.com
One of the concepts of using multiple services is code isolation. The deployed code is completely independent between services and versions. I recommend reading the article about Organizing Configuration Files which discusses the directory structure of multi-services as shown by the following image: 



Robie Ie

unread,
Sep 8, 2017, 9:02:25 AM9/8/17
to Google App Engine
Hmm ok I have an idea of how to solve this but requires a little more from app.yaml

Is it possible to change the command that app.yaml uses to start your app? Currently it calls "npm run start".

Anton Bacaj

unread,
Sep 8, 2017, 12:06:57 PM9/8/17
to Google App Engine
I've experimented with google cloud and have figured this out already.

Folder structure is as follows:

















Now before you deploy declare an environment variable called "SERVICE_NAME: 'service-name-goes-here'" in your .yaml files.
When you deploy you run:
gcloud app deploy ./service-1.yaml ./service-2.yaml

In package.json you setup the start script to use the launcher.js file.

  "scripts": {
   
"start": "cross-env NODE_PATH=. NODE_ENV=production node launcher.js"
 
}

Inside launcher.js you write some code like this:
const service = process.env.SERVICE_NAME;
console
.log("Starting service: " + service);


switch (service) {
 
case 'service-1':
 
require('./service-1/main.js');
 
return;
 
case 'service-2':
 
require('./service-2/main.js');
 
return;
 
default:
 console
.error("Unrecognized SERVICE_NAME: " + service);
 process
.exit(1);}

Essentially what we have done is deployed ALL of the code for every service to the app engine, but the starting of the service is defined in launcher.js.
So the service that will start is based on what the .yaml has defined SERVICE_NAME as.

This has worked for me and I have 5+ services sharing common modules.






On Wednesday, September 6, 2017 at 4:52:21 PM UTC-4, Robie Ie wrote:
Reply all
Reply to author
Forward
0 new messages