Skip to main content

Fuse - Contract First API Design with Apicurio and Fuse/Camel - Part One

This is part one of my two-article series that demonstrates the approach of implementing contract-first API design using Apicurioand Red Hat Fuse.

It covers how to create an OpenAPI standard document as the contract between API providers and consumers using Apicurio. It also shows how to quickly create mock tests using Red Hat Fuse.

There are two common approaches of creating these APIs.
  • Code First
  • Contract First
Coming from a old time ESB developer, these are not new. We have been doing this forever. Before, it was the WSDL that define the contract of the service. we were doing a lot more code first, for me it's simply because it's much easier for me to write couple of Java classes and generate the WSDL for my consumer. 


It's often pretty straightforward if the consumer of your application has finalized how they want the service to be like. But you and I all know this is not often the case. So I had to go back to my code, and make the changes accordingly and pray I did not break anything in the way.

Of course there is always preliminary discussion, but small changes are inevitable, and it's taking the toll of developer to grind through the process of getting everything right.

Thanks to the new OpenAPI Specification, business user, citizen user/developer can use it to negotiate and maybe rung a couple of pre-test with the consumer before it gets handed over to developer. This design approach is called Contract First. It has become more and more popular as it avoids developer wasting time while in the process of negotiating how the service should be provided.

Obviously there are many ways to do Contract First API . I am going to demonstrate how this can be done using Apicurio and Fuse. I will be using Apicurio to define APIs, and automatically generates the Fuse project for quick testing purpose.

Theme: Customer Service API


We will be providing customer service to our consumer. For the sake of this demo, we will only start with retrieving and creating customer service.

Defining Application with OpenAPI Specification in Apicurio

Apicurio is a web-based Open Source tool to design OpenAPI specification. Go to https://www.apicur.io/

If you don't already have an account, please register for one.


After registering, you will be redirected to the main page of Apicurio, so after discussing with the consumer on what they wish to have for the customer service, we can start creating the contract by clicking on "Create New API".



Basically, what you need to do is pretty simple

  • Create the API(Service)
  • Create the data definitions (if any) 
  • Add Paths, and define parameters, operations and return responses to the path

Enter the name of the service, and would be nice to add some of the description to it so it's easier for people to pick up what everything means.


Set the customer definition, show the detail of info we are going exchange. 



Add and define the properties and their data type.


We can start adding the Paths to the doc, 

Adding Parameters and define it's type.






And responses too, (if your path needs it)


Once we are done, it's time to export the API Standard documents.



Generating Fuse Project from Standard API Doc


Go to JBoss Developer Studio, create a new Fuse project by right click on navigation panel, select New and Fuse Integration project.



Provide a name to the project,

We are going to do this in a microservices approach, so select the most popular runtime - Spring Boot, and we will be running this on Cloud Platform - OpenShift.


Select the Spring DSL template.


You will have a sample Fuse project,




Add the generated API Spec doc to directory: src/spec/



Go to the pom.xml file, place the following in it,

<plugins>
....
<plugin>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-restdsl-swagger-plugin</artifactId>
  <version>2.21.0</version>
  <configuration>
    <specificationUri>src/spec/MyCustomer.json</specificationUri>
    <fileName>camel-rest.xml</fileName>
    <outputDirectory>src/main/resources/spring</outputDirectory>    
  </configuration>

</plugin>
....
</plugin>

Generate the XML by running following in command line tool,

  mvn camel-restdsl-swagger:generate-xml



You will find a newly generated camel context named "camel-rest.xml", and it has all the Path implementation in Camel.



Copy everything in between "rests", into the original "camel-context.xml" and place in-between "camelContext". and add the following rest configuration on top of the rest component.

<restConfiguration apiContextPath="api-docs" bindingMode="auto"
            component="undertow" contextPath="/customer"
            enableCORS="true" port="8080">
   <apiProperty key="cors" value="true"/>
   <apiProperty key="api.title" value="Customer Service"/>
   <apiProperty key="api.version" value="1.0.0"/>

</restConfiguration>




Delete the generated "camel-rest.xml". 

Mocking the APIs with Camel

We will mock the return result by adding a constant defined bean in the camel context.
Add a beans.xml under src/main.resource/spring (Right click on the folder, select New->beans.xml File)




Add the follow code snap to the beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="        
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        
    http://camel.apache.org/schema/spring       http://camel.apache.org/schema/spring/camel-spring.xsd

    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<util:list id="CustomerList" list-class="java.util.ArrayList">
   <ref bean="Customer"/>
</util:list>
<util:map id="Customer" map-class="java.util.HashMap">
   <entry key="name" value="Christina"/>
   <entry key="age" value="28"/>
   <entry key="contact" value="765483921"/>

</util:map>
</beans>

Add the camel route in the to "camel-context.xml", the first one returns a mock customer data, the second one takes the input of a customer info.

<route id="rest1-route">
  <from id="restone" uri="direct:rest1"/>
  <setBody id="route-setBody1">
    <simple>bean:CustomerList?method=get(0)</simple>
  </setBody>
</route>
<route id="rest2-route">
  <from id="resttwo" uri="direct:rest2"/>
  <log id="input-log" message=">>> ${body}"/>
    <setBody id="route-setBody2">
      <simple>Customer created</simple>
    </setBody>

</route>

And in the src/main/java/org/mycompany/Application.java added the new beans.xml file.

@ImportResource({"classpath:spring/camel-context.xml",
"classpath:spring/beans.xml"})

public class Application {

Now, it's time to setup the dependency libraries into pom.xml.


<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
    
<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-undertow-starter</artifactId>
</dependency> 
    
<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-jackson-starter</artifactId>
</dependency> 
    
<dependency>
  <groupId>org.apache.camel</groupId>
  <artifactId>camel-swagger-java-starter</artifactId>

</dependency>

and remove the tomcat dependency (we are using undertow now)

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Finally, it's time to test. run the follow in command line:

  mvn sprint-boot:run

And two endpoint will be exposed for testing.

Christina Laptop$ curl http://YOURIP:8080/customer/id/123
{"name":"Christina","age":"28","contact":"765483921"}

Christina Laptop$ curl --header "Content-Type: application/json"   --request PUT   --data '{"name":"Christina","age":28,"contact":"765483921"}'   http://YOURIP:8080/customer/add

"Customer created"

You are now ready for the consumer to start testing the APIs.
Next part I will take you through how to actually implement the API, expose it on Cloud and manage it.

Comments

Ram Kirar said…
Hello! This is my first visit to your blog! This is my first comment here, so I just wanted to give a quick shout out and say I genuinely enjoy reading your articles. Your blog provided us useful information. You have done an outstanding job.
malaysia visa singapore

Popular posts from this blog

Red Hat Fuse - Announcing Fuse 7 Tech preview 3 release.

Red Hat Fuse 7.0 technical preview three is out today! On the pathway to become one of the best cloud-native integration platform, Fuse gives developer freedom to choose how they want to develop the integration solution, where they want to deploy it and capabilities to address new integration personas that do not have development experience.
By supporting the three major runtime, developer is free to work on the runtime of their choice.By supporting standalone and cloud deployment, it simplifies the complexity to distinguish between these environments, allowing application to deploy freely among the environment of your choice. All levels of developers are welcome, you can either dive deep into creating customize complex integration logic, or using the new low code platform to quickly build a simple integration. In this Tech Preview release you get it all.
Fuse StandaloneSpring-boot for microserviceKaraf 4 for OSGi loverJBoss EAP for JavaEE developersFuse on OpenShiftPlugins for easy co…

JBoss EAP 6 - 效能調校 (一) DataSource 的 Connection Pool

效能沒有什麼Best Practice, 反正能調整的就那些。 通常,一個程式的效能大概有70-80% 都跟程式怎麼寫的其實比較有關係。

最近我最疼愛的小貓Puji 因為膀胱結石開刀的時候過世了,心情很差請原諒我的口氣沒有很好,也沒有心情寫部落格。

Puji R.I.P.

=======================正文=======================

這個題目很多人叫我寫,可是這題目好大,這分明就是整死我咩~
所以我會分幾段慢慢寫。

JBoss 的 SubsystemDatasource WebWeb Service EJB Hibernate JMSJCAJVM 調校OS (作業系統)

先來看一下 DataSource Subsystem, DataSource 的部分主要是針對Connection Pool 做調校。

通常,程式都會需要跟資料庫界接,電腦在本機,尤其是在記憶體的運算很快,但是一旦要外部的資源連接,就是會非常的耗資源。所以現在的應用程式伺服器都會有個Pool 放一些先連接好的 資料庫connection,當程式有需要的時候就可以馬上提供,而不用花那些多餘的資源去連接資料庫。

這就是為什麼要針對Connection Pool 去做調校。

以下會討論到的參數,都是跟效能比較有關係,Datasource 還有很多參數,像是檢核connection 是否正確的,我都不會提到。如果你追求的是非常快速的效能,那我建議你一個檢核都不要加。當然,這樣就會為伺服器上面執行的程式帶來風險。這就是你要在效能與正確,安全性上面的取捨了。 (套句我朋友說的話,不可能又要馬兒好,又要馬兒不吃草的..)

最重要的調校參數就是 Connection 的 Pool 數量。(也就是那個Pool 裡面要放幾條的connection.) 這個參數是每一個應用程式都不一樣的。

min-pool-size 

Connection Pool 最少會存留的connection 數量

max-pool-size 

Connection Pool 最多可以開啓的 connection 數量

prefill

事先將connection pool 裡面建立好min-pool-size 的connection.

我的建議是觀察一下平常程式要用到的量設定為 min-pool-size 。
加上…

JBoss Fuse - Fuse workshop 101 - Part One

On my way to Hong Kong for a day of workshop on JBoss Fuse, and as I go through my Slide deck, I cannot find any decent easy workshop for beginners. Therefore I decide make a workshop that is easy for Camel first timer to get their hands dirty.

The first of part of the workshop is an introduction to Camel, it first goes through what is exactly inside JBoss Fuse.

For part one of the workshop, it takes your through the very basic of Camel, one of the very important component inside JBoss Fuse.
Every Camel need to have a runtime container to run in, inside camel we call it a CAMEL CONTEXT.  Inside every Camel context, you can define lots of camel route and registry, don't worry about what those are, we will explain later.


So inside out blueprint xml, you will see a tag called camelContext.



Next up is camel route, they are a chain of command or process defined by you, as a developer.
Inside the camel route, there are consumer endpoints that listens to the incoming messages, producers …