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
- Log in to your Anypoint platform account and click Start Designing.
- Click Create New and select New API Spec.
- A new window will open (see below). Name your API spec and click Create API Specification. A basic API structure will be created.
- Write RAML based on your requirement.
- Click Publish and select Publish to Exchange.
- Go to Exchange. You’ll see that a newly created API has been published.
Creating an API using API Manager
- Once the API is published, go to the API Manager and click Manage API and then manage API from Exchange.
- 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.
- 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).
Implementing an application in Anypoint Studio
- Open Anypoint Studio and create a new Mule project by selecting file ->new ->mule project.
- Name your project and click + to import a published API — as shown below.
- Click Add Account and log in with your Anypoint username and password where you published the API in the previous step.
- The Anypoint account has been added. Type the name of your API. Select it, and click Add and Finish.
- Click Finish
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.
- Create a new configuration file by clicking file -> new -> mule configuration (see below) and naming it Implementation.
- Create a flow with logger and a transform message (see below).
- Add a flow reference in get:\hello:demoapp-config flow of the demo-app.xml (see below). It will go to the Implementation flow.
Adding the Autodiscover element
Create one more Mule configuration file, called global (global.xml), to add all configuration elements in one file.
- Go to global of that file and click Create.
- Select Autodiscovery.
- 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.
This will create an Autodiscovery configuration element — see below.
Find credentials for the environment where you’ll deploy this application
- Go to Access Management and click Environment.
- Click on the sandbox.
It will show you credentials for that environment. You’ll need these credentials when you deploy the application to CloudHub.
Deploying an application in Runtime Manager to activate API Autodiscovery
Now your application is ready.
- In Anypoint Studio, select file>export.
- Select the folder where you want the JAR to be created and click Finish. This will create a deployable JAR.
- 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
- 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/
- 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).
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