Heroku

Deploying Two Microservices on Heroku Using Devprime, MongoDB Atlas, and Confluent Kafka

Heroku offers a cloud platform for publishing applications and we will be developing two microservices using the Devprime platform, MongoDB and Kafka and [asynchronous communication]. /../how-to/stream/rabbitmq/asynchronous-microservices-communication/).

Items needed in your environment

  • Install .NET SDK 6 or higher
  • Visual Studio Code
  • An active account on Heroku
  • An active account on the Devprime platform and Developer or Enterprise use license.
  • An account in MongoDB Atlas
  • An account on Confluent Cloud
  • Devprime CLI installed and active (dp auth)
  • On-premises docker active + GIT
  • Powershell or Bash

Creating access and obtaining credentials

1) Go to Heroku
a) Create a new app and save the name.
b) Create a second app and save the name.

Heroku Apps
c) Get the access token from “API KEY

2) Go to MongoDB Atlas
a) Create a free mongodb database
b) Obtain access credentials

3) Go to Confluent Cloud and create a Kafka service
a) Create a free Kafka streaming service
b) Add the topic with the name ‘orderevents’
c) Add a topic with the name ‘paymentevents’
Confluent Kafka
d) Obtain Confluent Cloud access credentials

4) Install Heroku CLI and log in
heroku container:login

**Creating the ‘Order’ microservices using Devprime CLI
We will use the Devprime CLI for creating the microservices.

  • Creating the microservice
    dp new dp-order --stream kafka --state mongodb
    Enter the dp-order folder to view the microservice
  • Adding business rules
    dp marketplace order
  • Speeding up implementations
    dp init

Alter the settings by adding the MongoDB/Kafka credentials
a) In the project folder, open the configuration file
code .\src\App\appsettings.json
b) In the State item add the MongoDB credentials
c) In the Stream item, add the Kafka credentials

Run the microservices locally.
.\run.ps1 ou ./run.sh (Linux, MacOS)

Make a test post
a) Open the web browser in http://localhost:5000 or httsp://localhost:5001
b) Click on post and then on ‘Try it out’
c) Enter the data and send

If everything has gone well so far, then you can move on to the rest of the setup and publishing in the Heroku environment.

Adapting the project’s dockerfile to support Heroku
a) Locate and remove the line below
ENTRYPOINT ["dotnet", "App.dll"]
b) Add the line below to the end
CMD ASPNETCORE_URLS=http://*:$PORT dotnet App.dll

Export Settings
DP Export Heroku
Devprime heroku microservices

Publishing Settings to Heroku
a) Locate and open the created file
code .\.Devprime\heroku\instructions.txt
b) Locate the <app-name> tag and replace it with the name of your app-name1
c) Locate the <token> tag and replace the Heroku access token
d) Now we will create the ‘Config Vars’ environment variables in Heroku

  • Copy the curl command into the changed in the previous steps
  • Run it from the command line.
  • Notice the difference from curl in Windows Command, Powershell, Linux.
    curl -X PATCH https://api.heroku.com/apps/<app-name>/config-vars -H "Content-Type: application/json" -H "Accept: application/vnd.heroku+json; version=3" -H "Authorization: Bearer <token>" -d @.\.Devprime\heroku\heroku.json

‘Config Vars’ settings from your app-name1 on Heroku
Portal -> App -> Settings -> Config Vars

Config Vars

docker Image Compilation and Publishing
a) Before running the command, change the <app-name>

  • Compilation and shipping
    heroku container:push web --app <app-name>
  • Change to release
    heroku container:release web --app <app-name>

At this point, you can already view the microservice in the portal
Heroku Apps

Accessing the project’s public url
heroku open

Viewing the app-name1 Microservice Log
a) Before running the command, change the <app-name>
heroku logs --tail --app <app-name>

Creating a new payment microservice'
The process below speeds up the creation and already executes the ‘dp init’
dp new dp-payment --state mongodb --stream kafka --marketplace payment --init
At the end, enter the dp-payment folder

Alter the settings and credentials
a) In the project folder, open the configuration file
code .\src\App\appsettings.json
b) Change the ports of item Devprime_Web to ‘https://localhost:5002; http://localhost:5003’
c) In the State item, add the MongoDB credentials
d) In the Stream item, add the Kafka credentials
e) Subscribe to queues ‘orderevents’.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
"Devprime_Stream": [
    {
      "Alias": "Stream1",
      "Enable": "true",
      "Default": "true",
      "StreamType": "Kafka",
      "HostName": "<chang value>",
      "User": "<chang value>",
      "Password": "<chang value>",
      "Port": "9092",
      "Retry": "3",
      "Fallback": "State1",
      "Subscribe": [ { "Queues": "orderevents" } ]
    }
  ],

Recebimento de eventos no adapter de Stream
a) Implementing an event in Stream
dp add event OrderCreated -as PaymentService
b) Change the DTO to ‘OrderCreatedEventDTO’
code .\src\Core\Application\Services\Payment\Model\OrderCreatedEventDTO.cs

1
2
3
4
5
6
public class OrderCreatedEventDTO                     
  {                                                     
    public Guid OrderID { get; set; }
    public string CustomerName { get; set; }
    public double Value { get; set; }  
  }

Configurando o Subscribe no Stream
a) Open the Event Stream configuration
code .\src\Adapters\Stream\EventStream.cs
b) Change the implementation in Subscribe

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
    public override void StreamEvents()
    {
        Subscribe<IPaymentService>("Stream1", "OrderCreated", (payload, paymentService, Dp) =>
        {
            var dto = System.Text.Json.JsonSerializer.Deserialize<OrderCreatedEventDTO>(payload);
            var command = new Payment()
            {
                CustomerName = dto.CustomerName,
                OrderID = dto.OrderID,
                Value = dto.Value
            };
            paymentService.Add(command);
        });
    }

Modify the configuration in the project’s dockerfile
a) Locate and remove the line below
ENTRYPOINT ["dotnet", "App.dll"]
b) Add the line below
CMD ASPNETCORE_URLS=http://*:$PORT dotnet App.dll

Export Settings
dp export heroku

Publishing Settings to Heroku
a) Locate and open the created file
code .\.Devprime\heroku\instructions.txt
b) Locate the <app-name> tag and replace it with the name of your app-name2
c) Locate the <token> tag and replace the Heroku access token
d) Now we will create the ‘Config Vars’ environment variables in Heroku

  • Copy the curl command into the changed in the previous steps
  • Run it from the command line.
  • Notice the difference from curl in Windows Command, Powershell, Linux.
    curl -X PATCH https://api.heroku.com/apps/<app-name>/config-vars -H "Content-Type: application/json" -H "Accept: application/vnd.heroku+json; version=3" -H "Authorization: Bearer <token>" -d @.\.Devprime\heroku\heroku.json

docker Image Compilation and Publishing
a) Before running the command, change the <app-name>

  • Compilation and shipping
    heroku container:push web --app <app-name>
  • Change to release
    heroku container:release web --app <app-name>

Heroku Apps

Optional steps to stop services or view processes
a) Before running the command, change the <app-name>

heroku ps --app <app-name>
heroku ps:stop web.1 --app <app-name>
heroku ps:start web.1 --app <app-name>

Final Thoughts

  • During this Heroku journey, we’ve developed two microservices.
  • To automate your devops strategy, use GitHub Actions.
Last modified January 10, 2024 (967dcac3)