Heroku

Publishing two microservices on Heroku using DevPrime, Atlas MongoDB and Confluent Kafka

Heroku offers a cloud platform for application publishing and we will be developing two microservices using the DevPrime, MongoDB and Kafka platform and asynchronous communication.

Items needed in your environment

  • Install .NET SDK 6 or higher
  • Visual Studio Code
  • An active account on the Heroku
  • An active account on the platform DevPrime and developer or enterprise use license.
  • An account in the MongoDB Atlas
  • An account in the Confluent Cloud
  • DevPrime CLI installed and active (dp auth)
  • Active Local Docker + GIT
  • Powershell or Bash

Creating accessand obtaining credentials

1) Access the 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 in “API KEY

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

3) Access the Confluent Cloud and create a Kafka service
a) Create a free Kafka stream service
b) Add the topic with the name ‘orderevents’
c) Add a topic named ‘paymentevents’
Confluent Kafka
d) Get confluent cloud access credentials

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

Creating Microservices ‘Order’ using DevPrime CLI
We will use the DevPrime CLI for the creation of 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
  • Accelerating implementations
    dp init

Delete settings by adding 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 kafka credentials

Run 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 post and then ‘Try it out’
c) Place the data and send

If everything has been well so far then you can move forward with the rest of the setup and publishing in the Heroku environment.

Adhering to the project dockerfile for Heroku support
a) Find 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
Microservices devprime heroku

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

  • Copy the curl command in the changed in the previous steps
  • You run on the command line.
  • Notice the difference in 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 of your app-name1 on Heroku
Portal -> App -> Settings -> Config Vars

Config Vars

Docker image compilation and publishing
a) Before executing the command change the <app-name>

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

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

Accessing the public project url
heroku open

Viewing the app-name1 microservice log
a) Before executing 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 runs the ‘dp init’
dp new dp-payment --state mongodb --stream kafka --marketplace payment --init
At the end enter the dp-payment folder

Delete settings and credentials
a) In the project folder open the configuration file
code .\src\App\appsettings.json
b) Change the ports the 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 kafka credentials
e) Include in subscribe the 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" } ]
    }
  ],

Receiving events in the Stream adapter
a) Implementing an event in stream
dp add event OrderCreated -as PaymentService
b) Change the DTO ‘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; }  
  }

Setting Subscribe in Stream
a) Open the Event Stream setting
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 dockerfile
a) Find 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 on Heroku
a) Find and open the created file
code .\.devprime\heroku\instructions.txt
b) Find the tag <app-name> and replace it with the name of your app-name2
c) Locate the tag <token> and replace the heroku access token
d) Now we will create the environment variables ‘Config Vars’ in Heroku

  • Copy the curl command in the changed in the previous steps
  • You run on the command line.
  • Notice the difference in 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 executing the command change the <app-name>

  • Compilation and submission
    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 executing 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 considerations

  • During this Heroku journey we developed two microservices.
  • To automate your devops strategy, use GitHub Actions.
Last modified March 10, 2022 (615ec2b)