Google Run

Google Run offers a serverless container approach to deploying microservices without the need to use Kubernetes, allowing you to accelerate the publication of microservices based on the Devprime platform.

Introduction

The Devprime platform accelerates software developer productivity by offering a complete software architecture design, components with intelligent behaviors, accelerators for code deployment, and updates with new features.

In this article, we’ll cover using Google Run, a platform for serverless containers based on the Google Cloud stack. The implementation of this scenario will use a microservice based on the Devprime platform. This microservice will be linked to an active account in Google Cloud, with a project containing a Stream created in Google Pub/Sub, a database in Google Cloud SQL (compatible with MySQL, PostgreSQL, and SQL Server), a repository in Google Artifact Registry, and a new service in Google Run.

During this article, we’ll use a microservice with a request-related business rule, and the PostgreSQL database will be integrated with a queue created in Google Pub/Sub. We’ll demonstrate the steps to create the microservice, and then cover the configurations needed to make it available in Google Run.

Google Run

Checklist and preperation of the initial environment:

  • Open an account on the Devprime platform and purchase a [(Developer/Enterprise)] license(https://devprime.io/pricing).
  • Install an updated version of .NET (Linux, macOS, and Windows)
  • Install and/or update Visual Studio Code and/or Visual Studio 2023 Community / Professional / Enterprise.
  • Install and/or update docker (For Windows, use WSL2).
  • Install Powershell or use Bash on Linux
  • Install and activate the latest version of the Devprime CLI.
  • Create a folder for your projects and set read and write permissions.
  • See the article “Creating the First Microservice” to explore getting started with the Devprime platform.

Google Cloud Checklist

Creating a new microservice

  • Building the first microservice
    dp new ms-order --stream googlepubsub --state postgresql
  • Adding a sample business rule
    dp marketplace order
  • Initializing and accelerating microservice deployments
    dp init

At this point, the microservice for this example is ready for production use, and it can be run locally. The next step will be to add the Google Cloud credentials to the microservice.

Adding credentials in the microservice
This microservice will use the credentials to access Google PubSub and Google Cloud SQL and must be informed in the configuration file src/App/appsettings.json as defined in the Stream Adapter with Google PubSub and the PostgreSQL State Adapter.

Open in Visual Studio Code
code src/App/appsettings.json

  • Locate the Adapter ed Stream settings and include the credentials obtained
    In json
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
"DevPrime_Stream": [
    {
      "Alias": "Stream1",
      "Enable": "true",
      "Default": "true",
      "StreamType": "GooglePubSub",
      "Project_ID": "",
      "Private_Key_ID": "",
      "Private_Key": "",
      "Client_Email": "",
      "Client_ID": "",
      "Auth_URI": "",
      "Token_URI": "",
      "Auth_Provider_x509_Cert_Url": "",
      "Client_X509_Cert_Url": "",
      "Retry": "3",
      "Fallback": "State1",
      "Threads": "10",
      "Subscribe": []
    }
  ],
  • Locate the Adapter key of State and enter the Google Cloud SQL access credentials
    such as the IP address to be entered on the Host, username and password.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
"DevPrime_State": [
    {
      "enable": "true",
      "alias": "State1",
      "dbtype": "postgresql",
      "type" : "db",
      "connection": "Host=localhost;Username=postgres;Password=LltF8Nx*yo;Database=ms-order",
      "timeout": "5",
      "retry": "2",
      "durationofbreak": "45"
    }
  ],

Applying Database Structure
The database in this scenario is PostgreSQL and before running the microservice it is essential to run a script to create the tables in the database necessary for the microservice. You can use the Devprime CLI tool to generate the SQL files and apply manually or apply directly to the database.

Run the command to apply to the database.
dp state apply state1

Exporting settings for publishing to Google Run
The Devprime CLI provides a utility to make it easier to create a script for publishing the microservice’s docker image to the Google Artifact Registry private repository and then creating a new service in Google Run.

Run the export command
dp export googlerun

After exporting, two files (Powershell or Bash) will be created that contain the script to run the publication to Google Cloud.

Choose the file of your choice and edit it using Visual Studio Code, changing the initial settings, such as Google Cloud project name, application name, and repository.

Open via Visual Studio Code

  • code .devprime/google/googlerun.ps1
  • code .devprime/google/googlerun.sh

Running the script
Before running the script, make sure that docker is running in your environment, that you’re logged in to it, and that the Google Cloud CLI is installed and active. The script must be run from the root directory of the project.

Run one of the following commands:

  • .\.devprime\google\googlerun.ps1 (Windows)
  • .devprime/google/googlerun.sh (MacOS and Linux)

Final Thoughts

In this example, you can see how easy it is to develop a new microservice using the Devprime platform and its accelerators, followed by the publishing process to Google Cloud in a matter of seconds.

  • Replace this script with a DevOps process, automating the procedure.
  • In the production environment, implement security measures such as an API Gateway and a vault for secure credential storage.
  • Adopt the procedures by isolating the network to export only the public services.
Last modified August 20, 2024 (2f9802da)