We maybe heard a lots about using OpenFeign and OpenApi in Spring Boot projects with micro-services architect. So in this section, we will try to integrate OpenFeign with OpenApi into our Spring Boot project for generating server - client apis.
If you are not familiar with these concepts, you should topics in links below:
Before we deep dive into the implementation we should go to the situations that we will need to integrate OpenApi and OpenFeign.
In this example, assume that we received an openapi specification from a technical analysis guy, with some apis, requests, responses and models. Then instead of writing code manually, we will use OpenApi generator plugin on Spring Boot service which will help us to generate controller and models and we will use them for building our Spring Boot service application quickly, we will name this service as openfeign-openapi-server.
After the code implementation on openfeign-openapi-server. Another spring boot service will need to configure spring cloud OpenFeign to use apis from openfeign-openapi-server. So instead of writing code manually, we also use the OpenApi generator plugin on Spring Boot service to generate openfeign client and models from the openapi specification of openfeign-openapi-server. Let’s call this service as openfeign-openapi-client.
Usually, the service openfeign-openapi-client will call to openfeign-openapi-server by url, using this way is okay but when openfeign-openapi-server changes ports or url, so we have to update openfeign-openapi-client configuration. To avoid this issue, spring cloud OpenFeign support us to use the name of target service instead of url to make connection. To do this we need an Eureka Server with registrations of openfeign-openapi-server and openfeign-openapi-client. You can view the image below for more details.
To create Eureka Server, let's add some dependencies as below.
Note Netflix Eureka Server dependencies require you to add Spring Cloud dependency first as mentioned in Spring Cloud Introduction. So maybe you have to add the dependency below first.
Then In your application.yml. Let's add some configuration. The details of configuration are put in the comments.
application.yml
1 2 3 4 5 6 7 8 9101112131415
#server run at port 8761server:port:8761spring:application:#application namename:eureka-servereureka:client:#self register is falseregister-with-eureka:false#self fetch registry is falsefetch-registry:false
To view more configuration that you can use in openapi generator plugin, you can view this page.
In this openfeign-openapi-server we just simply read the openapi specification yaml file to generate apis and models, then we also generate openapi ui to interact with these apis without using postman.
You can get sample openapi specification yaml in this link
After you import the dependencies, then you need to add the annotation @EnableDiscoveryClient in your file main as below:
server:port:8081spring:mandatory-file-encoding:UTF-8http:encoding:charset:UTF-8enabled:truedatasource:url:jdbc:h2:file:./testdbusername:sapassword:passworddriver-class-name:org.h2.Driverplatform:h2jpa:database-platform:org.hibernate.dialect.H2Dialecthibernate.ddl-auto:updategenerate-ddl:trueshow-sql:trueh2:console:enabled:truepath:/h2settings:trace:falseweb-allow-others:false#spring cloud configurationapplication:name:openfeign-openapi-servercloud:discovery:enabled:trueloadbalancer:retry:enable:truemaxRetriesOnSameServiceInstance:5backoff:maxBackoff:5000#connect eureka server configurationeureka:client:serviceUrl:defaultZone:http://localhost:8761/eurekafetch-registry:true
The property “eureka.client.serviceUrl.defaultZone” will be the url for register of eureka server.
Now let’s build your service then check generated code at target/generated-sources/openapi/src/main/java/**, then you should see your generated codes as in the image below:
Next, start your eureka-server and openfeign-openapi-server and go to http://localhost:8761/eureka/ you will see openfeign-openapi-server has been registered into eureka-server.
Then when you go to http://localhost:8081/swagger-ui/index.html you should see the openapi ui is displayed as below:
Now, to implement codes for generated api class, you need to create a controller then you will implement the generated api class and @Override methods corresponding with apis as below:
So, let’s start eureka-server and openfeign-openapi-server again, and go to http://localhost:8081/swagger-ui/index.html then trigger creating customer api and getting customer api, you should see result as below:
So that’s all for openfeign-openapi-server, now we will move to openfeign-openapi-client.
Then in your application.yml, you will need to add some configurations for openfeign server name, spring cloud, spring cloud load loadbalancer and eureka client as below:
Now, let’s build your openfeign-openapi-client, then check generated code at target/generated-sources/openapi/src/main/java/** and you should see your generated codes as in the image below:
Next, to openfeign can recognize and use your generated client apis, you should create a configuration class and make some configuration as below:
So in this class we will use ApplicationContext and generated client api class ServerApi to create ServerApi feign client. At this point you will confuse that how client can connect to server, so connecting to openfeign-openapi-client will be handle apart by eureka-server. both openfeign-openapi-client and openfeign-openapi-server are connected to eureka-server, so its know exactly the application names and urls correspondingly. Thus, in spring cloud openfeign, we just use the application name of openfeign-openapi-server then we can make the connection to. Doing this way, we won’t worry about changing url or port on openfeign-openapi-server anymore.
Note: we should use application name with all capital letters because eureka-server will automatically capitalize all application names of clients when they connect to.
Finally, we will create a controller with an api, this api will use openfeign client that we configure above to get data from openfeign-openapi-server. The controller is as below:
Let’s start 3 services, then go to http://localhost:8761/eureka to check the client connections. You should see result as below:
Now, using postman to call to the export api of openfeign-openapi-server, you will see the result as below:
curl --location --request GET 'http://localhost:8082/v1/client/customers'
So it means, all configuration has been successful, request from postman will go to openfeign-openapi-client, then openfeign-openapi-client will call to openfeign-openapi-server by application name with supports of eureka-server.
The OpenAPI Specification (OAS) defines a standard, language-agnostic interface to RESTful APIs which allows both humans and computers to discover and understand the capabilities of the service without access to source code, documentation, or through network traffic inspection. When properly defined, a consumer can understand and interact with the remote service with a minimal amount of implementation logic
With OpenApi Generator in Spring Boot: we can generate source code for boths server side and client side which will help use reduce writing codes, making developing application faster. Moreover, we also can generate OpenApi UI for server apis that we defined in OpenApi specification file.
Combining OpenFeign and spring cloud eureka server will help us using generated OpenApi client easily and avoid issues that come from server side as changing domain url and ports. Moreover, we also can keep tract services that are running/available in our system.