Google Run
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 the use of Google Run, a platform for serverless containers based on the Google Cloud stack. Implementing this scenario will use a microservice based on the Devprime platform. This microservice will be linked to an active Google Cloud account, with a project containing a Stream built on Google Pub/Sub, a database on Google Cloud SQL (compatible with MySQL, PostgreSQL, and SQL Server), a repository on Google Artifact Registry, and a new service on Google Run.
During this article, we’ll use a microservice with an order-related business rule, and the PostgreSQL database will be integrated with a queue created in Google Pub/Sub. We’ll walk you through the steps to build the microservice, and then we’ll cover the settings you need to make it available in Google Run.
Cheklist and preperation of the initial environment:
- Open an account on Devprime Platform and purchase a Developer/Enterprise) license.
- 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
- An active account on Google Cloud.
- Install the Google Cloud CLI.
- Log in via Google CLI (
gcloud init
). - Create a PostgreSQL database in Google Cloud SQL.
- Get the PostgreSQL IP and credentials and release the network 0.0.0.0/0 for external access.
- Create an “oderevents” topic in Google Pub/Sub (https://console.cloud.google.com/cloudpubsub).
- Create and get json with credentials, access permissions to services.
- Creating and publishing to Google Artifact Registry and a new service to Google Run will be done by script.
- You can use a PostgreSQL client locally to validate access to Google Cloud SQL.
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 intended for this example is ready for use in production, and it is possible to run it locally. The next step is to add your Google Cloud credentials to your microservice.
Adding credentials in the microservice
This microservice will use the credentials to access Google PubSub and Google Cloud SQL and must be reported in the configuration file src/App/appsettings.json
as defined in the Stream Adapter with Google PubSub and State Adapter with PostgreSQL.
Open in Visual Studio Code
code src/App/appsettings.json
- Locate the Ed Stream Adapter settings and include the credentials obtained
in JSON
|
|
- Locate the State Adapter key and enter the Google Cloud SQL access credentials
such as the IP address to be entered in the Host, username and password.
|
|
Applying Database Structure
The database in this scenario is PostgreSQL, and before running the microservice, it is critical to run a script to create the tables in the database that are required by the microservice. You can use the Devprime CLI tool to generate the SQL files and apply them manually or apply them 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 to publish the microservice’s docker image to the private Google Artifact Registry repository and then create 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 publish to Google Cloud.
Choose your preferred file and edit it using Visual Studio Code, changing the initial settings such as Google Cloud project name, application name, and repository.
Open from Visual Studio Code
code .devprime/google/googlerun.ps1
code .devprime/google/googlerun.sh
Running the script
Before you run the script, make sure that docker is running in your environment, that you’re logged into 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 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 process of publishing it to Google Cloud in a matter of seconds.
- Replace this script with a DevOps process by 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.