API Autodiscovery for Mule 4 applications

  • September 21, 2021

API Autodiscovery is used to connect Runtime Manager-deployed applications with an API created on the platform. This helps with tracking and enforcing policies. One Autodiscovery element always points to only one autodiscovery instance in the API Manager.

In this tech article, we’ll see how to apply API Autodiscovery to any application. This process includes:

  • Designing API specifications in Design Center
  • Creating an API using the API Manager
  • Implementing an application in Anypoint Studio
  • Adding the Autodiscover element
  • Deploying an application in Runtime Manager to activate API Autodiscovery

Setup

Designing API specifications in Design Center

  1. Log in to your Anypoint platform account and click Start Designing.
    API Autodiscovery1
  2. Click Create New and select New API Spec.
  3. API Autodiscovery2
  4. A new window will open (see below). Name your API spec and click Create API Specification. A basic API structure will be created.
  5. API Autodiscovery3
    API Autodiscovery4
  6. Write RAML based on your requirement.
  7. API Autodiscovery5
  8. Click Publish and select Publish to Exchange.
  9. API Autodiscovery6
    API Autodiscovery7
  10. Go to Exchange. You’ll see that a newly created API has been published.
    API Autodiscovery8
    API Autodiscovery9

Creating an API using API Manager

  1. Once the API is published, go to the API Manager and click Manage API and then manage API from Exchange.
  2. API Autodiscovery10
  3. Select the name of the API — it will automatically add the details below. Check your Mule application, basic endpoint and MULE 4, and click Save.
  4. API Autodiscovery11
    API Autodiscovery12
  5. The API is now created in the API Manager, but the API status is Inactive. An API ID has been created — as shown below (17063791).
  6. API Autodiscovery13

Implementing an application in Anypoint Studio

  1. Open Anypoint Studio and create a new Mule project by selecting file ->new ->mule project.
  2. API Autodiscovery14
  3. Name your project and click + to import a published API — as shown below.
  4. API Autodiscovery15
    API Autodiscovery17
  5. Click Add Account and log in with your Anypoint username and password where you published the API in the previous step.
    API Autodiscovery18
  6. The Anypoint account has been added. Type the name of your API. Select it, and click Add and Finish.
  7. API Autodiscovery19
  8. Click Finish
    API Autodiscovery20

    You’ll see that the new project is scaffolded (see below). Depending on resource/method pair number of flows created — in this case, only one flow for resource/hello with method has been created, along with two common flows, one of which is an API console flow and the other an API kit router flow.

    API Autodiscovery21
    API Autodiscovery22
  9. Create a new configuration file by clicking file -> new -> mule configuration (see below) and naming it Implementation.
  10. API Autodiscovery23
    API Autodiscovery24
  11. Create a flow with logger and a transform message (see below).
  12. API Autodiscovery25
    API Autodiscovery26
  13. Add a flow reference in get:\hello:demoapp-config flow of the demo-app.xml (see below). It will go to the Implementation flow.
  14. API Autodiscovery27

Adding the Autodiscover element

Create one more Mule configuration file, called global (global.xml), to add all configuration elements in one file.

  1. Go to global of that file and click Create.
  2. Select Autodiscovery.
  3. API Autodiscovery28
  4. Add the API ID from when the API in the API Manager was created. The flow name will be the name of flow where your API lit router is located.
  5. API Autodiscovery29

This will create an Autodiscovery configuration element — see below.

API Autodiscovery30

Find credentials for the environment where you’ll deploy this application

  1. Go to Access Management and click Environment.
  2. Click on the sandbox.
  3. API Autodiscovery32

It will show you credentials for that environment. You’ll need these credentials when you deploy the application to CloudHub.

API Autodiscovery33

Deploying an application in Runtime Manager to activate API Autodiscovery

Now your application is ready.

  1. In Anypoint Studio, select file>export.
  2. Select the folder where you want the JAR to be created and click Finish. This will create a deployable JAR.
  3. API Autodiscovery34
    API Autodiscovery35
  4. Go to the Anypoint platform Runtime Manager. Name your application and select the JAR created in the previous step. Add the following properties in the Properties tab:

    anypoint.platform.client_id=29eef98b74694cf6a7f471db9ec6bb29

    anypoint.platform.client_secret=895446726E7142609d26C43B9fFb589f

    THERE IS A GRAPHIC MISSING HERE. IT’S BROKEN ON THE PAGE

  5. Click Deploy.

    If you’re testing locally with the two properties above, you’ll have to write two more properties in the configuration property file. On cloud, only credentials are required — two can be taken automatically at runtime:

    anypoint.platform.analytics_base_uri=https://analytics-ingest.anypoint.mulesoft.com/

    anypoint.platform.base_uri=https://anypoint.mulesoft.com/

    API Autodiscovery36
    API Autodiscovery37
  6. Once it’s deployed, go to the API Manager. Select that API instance and you’ll see that the API status is Active (see below).
    API Autodiscovery38

    Now, if you apply any policy to it from the API Manager, it will be applied to any applications deployed in Runtime Manager because Autodiscovery has connected this API Manager instance to that deployed application.

    To see how to apply policies, read: Client ID enforcement on Mule 4 API

— By Kishori Patil