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 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.
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
- An active [Google Cloud] account(https://cloud.google.com).
- Install the Google Cloud CLI.
- Log in via Google CLI (
gcloud init
). - Create a PostgreSQL database on Google Cloud SQL.
- Obtain the PostgreSQL IP and credentials and release the 0.0.0.0/0 network for external access.
- Create an “oderevents” topic in Google Pub/Sub (https://console.cloud.google.com/cloudpubsub).
- Create and retrieve json with the credentials access permissions to the services.
- Creation and publication to Google Artifact Registry and a new service to Google Run will be done by script.
- You can use a PostgreSQL client on-premises 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 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
|
|
- 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.
|
|
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.