API Auto Discovery For Mule 4 Application

Author: Kishori Patil

Content:

  • API Auto Discovery is used to connect Runtime manager deployed applications with API created on the platform
  • This will help you for tracking, enforcing policies when you apply for it
  • One Autodiscovery element always points to only one autodiscovery instance in API Manager
  • In this blog, we will see how to apply API Auto discovery to any application in details
  • Designing API Specifications in Design Center
  • Creating an API using API Manager
  • Implementation Of Application in Anypoint Studio
  • Adding Autodiscover element
  • Deploying Application in Runtime manager to activate API Auto Discovery

SETUP :

  • Designing API Specifications in Design Center
  1. Login to your Anypoint platform account and click on “Start Designing”

2. Click on create new and select New API Spec

3. It will open a new window as below. Give a name to your API spec and click on create API Specification. It will create a Basic Structure of API

4. Write RAML as per your requirement

5. Then click on publish button and select publish to exchange

6. Goto Exchange and you can see that a newly created API is published

  • Creating an API using API Manager
  1. Once the API is published goto API Manager and click on Manage API> manage API from Exchange

2. Select the name of the API and it will automatically take the below details. Check on mule application, basic endpoint, and MULE 4, and click on save

3. API is created in API manager but as you can see API status is Inactive. API ID is created for this API as shown below (17063791)

  • Implementation Of Application in Anypoint Studio
  1. Open your Anypoint studio and create a new mule project by selecting file>new>mule project

2. Give a name to your project and click on + present below import a published API as follow

3. Then click on Add Account and log in with your Anypoint username and Password where you have published the API in the previous step

4. Now the Anypoint account is added. Type name of your API. Select that and click on Add and finish

5. Again click on finish

6. You can see the new project is scaffolded as follows. Depending upon resource/method pair number of flows are created. In our case, only one flow for resource /hello with method get is created and 2 common flows one of which is API console flow and other one is API kit router flow

7. Create a new configuration file by clicking on file> new > mule configuration file as follows and name it as Implementation

8. Create a flow with logger and a transform message as below

9. Add flow reference in get:\hello:demoapp-config flow of demo-app.xml as follows. It will go to the Implementation flow

  • Adding Autodiscover element:
  1. Now create one more mule configuration file called global (global.xml)to add all configuration elements in one file. Goto global of that file and click on create. Select autodiscovery

2. Add the API ID we got when we created the API in API manager and flow name will be the name of flow where your API KIT router is present

3. This will create an autodiscovery config element as follows

  • Find credentials for Environment where you will deploy this application
  1. Go to Access Management and click on Environment. Then click on the sandbox

2. It will show you credentials for that environment. You will need these credentials when you deploy the application to cloudhub

  • Deploying Application in Runtime manager to activate API auto discovery
  1. Now your application is ready. Select file>export in Anypoint studio. Select the folder where you want the jar to be created and click finish. This will create a deployable jar

2. Now go to Anypoint platform> Runtime manager. Give name to your app and select the created jar in the previous step. Add the following properties and click on deploy. Add the following properties in the properties tab

anypoint.platform.client_id=29eef98b74694cf6a7f471db9ec6bb29

anypoint.platform.client_secret=895446726E7142609d26C43B9fFb589f

3. If you are testing locally with the above 2 properties you have to write 2 more properties in the config property file. On cloud only credentials are required as below 2 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/

4. Once it is deployed go to API Manager. Select that API instance and you can see the API status as Active as follows

5. Now if you apply any policy to it from the API manager, it will be applied to applications deployed in runtime manager. As auto discovery has connected this API manager instance to that deployed application

To see how to apply policies you can check Client Id enforcement on Mule 4 API