Render

Learn how to publish a microservice based on the Devprime platform to the Render.com host using the Web service, a PostgreSQL, docker, and Github database.

Devprime using Render

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 the Host Render service, an application hosting platform that offers features such as Web Services, Static Sites, Background Workers, Cron Jobs, docker, PostgreSQL, and Redis.

During this article, we’ll use a microservice with an order-related business rule and the PostgreSQL database. We’ll walk you through the steps to build the microservice, the hosting services on Render, and then the configurations needed to make it available using the free plan.

Cheklist and preperation of the initial environment:

Creating a microservice to use in the example

The first step is to create a new microservice that we can use as a template to publish to the host. The name of this microservice will be set to “ms-render”, as demonstrated in the command below.

  • Building the first microservice
    dp new ms-render --stream rabbitmq --state postgresql
  • Adding an example business rule “Order”
    dp marketplace order
  • Initializing and accelerating microservice deployments
    dp init

After the creation of the new microservice, go to the “ms-render” project folder, where you can view all the implementations by Visual Studio Code, as demonstrated in the article related to creating the first microservice.

Devprime using Render

At this time we will not use RabbitMQ and will disable it in the Stream Adapter settings via the config file in “src\App\appsettings.json”.

  1. Enter the new post ms-order and open it from Visual Studio Code
    code src\App\appsettings.json

  2. Locate the “DevPrime_Stream” key and in the “Enable” option put “false”.

  3. Change the “OrderCreated” Handler by commenting on the line “Dp.Stream.Send(destination, eventName, eventData);” so that it does not trigger the event to be sent by the Stream.

Open the Handler from Visual Studio Code
src\Core\Application\EventHandlers\Order\OrderCreatedEventHandler.cs

IMPORTANT:
In this example, we are using a standard relational database and therefore it is necessary to apply the table structure to the database using SQL scripts or using the dp state apply command.

Creating the PostgreSQL database in Render.com

In this example, we’ll be using a PostgreSQL database on a free instance. Start the creation process and configure the following parameters: PostgreSQL Name/Database/User/Region/Version. At the end of the procedure, you will have access to the settings as illustrated in the image below, which is a crucial step in obtaining the necessary credentials.

Devprime using Render

The values below are just examples to exemplify how to get PostgreSQL database configurations into Render.com and then transport them to the microservice according to the configuration.

Item Description Value
Hostname Internal URL DPG-ckv4bi3amefc73cu3e5g-a
Port TCP port 5432
Database Database Name msrender
Username Access User msrender
Password Password 0KxiNwzyNyIviwVizfjrROkmMjnd41yp
Internal Database URL Internal Connection String postgres://msrender:0KxiNwzyNyIviwVizfjrROkmMjnd41yp@dpg-ckv4bi3amefc73cu3e5g-a/msrender
External Database URL External Connection String postgres://msrender:0KxiNwzyNyIviwVizfjrROkmMjnd41yp@dpg-ckv4bi3amefc73cu3e5g-a.oregon-postgres.render.com/msrender
Connection Devprime Connection Composition Host=dpg-ckv4bi3amefc73cu3e5g-a.oregon-postgres.render.com; Username=msrender; Password=0KxiNwzyNyIviwVizfjrROkmMjnd41yp; Database=msrender

In the example below, we demonstrate how to apply the PostgreSQL configuration to the microservice via the “src\App\appsettings.json” configuration file. To access the local environment, it is necessary to use the external url of the database.

Open it from Visual Studio Code and put your connection in JSON.
code src\App\appsettings.json

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
    "DevPrime_State": [

    {
      "enable": "true",
      "alias": "State1",
      "dbtype": "postgresql",
      "type" : "db",
      "connection": "Host=dpg-ckv4bi3amefc73cu3e5g-a.oregon-postgres.render.com;
      Username=msrender;
      Password=0KxiNwzyNyIviwVizfjrROkmMjnd41yp;Database=msrender",
      "timeout": "5",
      "retry": "2",
      "durationofbreak": "45"
    }
  ],

Applying the PostgreSQL Data Structure in Render.com

In the previous step you got the credentials and configured it in “src\App\appsettings.json” and at that point we will apply the data structure so that we can test the microservice.

Run the command below
dp state apply state1

At the end of the process, the table structure will be created in the PostgreSQL database in Render

Devprime using Render

Run the microservice locally and do a Post/Get

In the previous step we set up the database pointing to the PostgreSQL service in Render, so we can already perform a test by writing and reading information.

Start the microservice by running one of the scripts below:
.\run.ps1 (Windows) ou ./run.sh (Linux, macOS)

Access the local url of the microservice at https://localhost:5001 or http://localhost:5000

Devprime running microservices

Exporting the settings in yaml file

The application configuration is passed to the Render in key/value format. At this point, we’ll use the “dp export kubernetes” command to export YAML files templates to Kubernetes. This will allow us to copy the following parameters: devprime_app, devprime_observability, devprime_web, devprime_stream1, devprime_state1, devprime_Custom, and devPrime_Services.

Run the command
dp export kubernetes

Export will create two files:

  • deployment.yml
  • service.yml

The image below demonstrates the example of the “deployment.yml” file.

Devprime dp export kubernetes

To view the variables, open the “Devpoyment” file in Visual Studio Code.
code .devprime\kubernetes\deployment.yml

Uploading the code to a private repository on Github

We recommend using a private Github repository when linking Render when building Web Services. Devprime offers a command that removes the credentials from the local configuration file.

To delete the project credentials, run the following command:

dp clean

Creating a Web Services in Render

Now it’s time to create a new Web Service in Render using the option to link to source code made available through a private repository on GitHub. As you proceed, you’ll have the option to integrate with your GitHub account and choose a specific repository that will be used for publishing to Render. The Render uses the dockerfile present in the root folder of the project to build the image of our microservice.

Devprime Create a new Web Service Render.com

After moving forward, the next step is to set some initial parameters for creating the new Web Service in Render.

Item Value
Name Microservice Name
Region Oregon
Branch Main
Root Directory Blank
Runtime docker
Instance Type Free

At the end of the form, locate the “Advanced” option and choose “Add Environment Variable” and add the variables we dealt with in the previous step. devprime_app, devprime_observability, devprime_web, devprime_stream1, devprime_state1, devprime_custom and devprime_services.

To view the variables, open the “Devpoyment” file in Visual Studio Code.
code .devprime\kubernetes\deployment.yml

Devprime Create a new Web Service Render.com

At the end of the docker image build process and with the correct environment variables, the service will be live and will already have a public URL available to test the service.

Devprime Create a new Web Service Render.com

Final thoughts

Here’s a brief example of publishing a microservice to the Devprime platform hosted on Render. Use this as a reference for publishing other services. You can add additional features such as RabbitMQ, Redis, and especially,enable security using an identity provider, such as Keycloak, Auth0, and others, to secure access to APIs.

Last modified April 16, 2024 (2b35fcc8)