Spring Cloud OpenFeign Basic#
What Is Feign?#
- Feign is a declarative web service client. It makes writing web service clients easier. To use Feign, just create an interface and annotate it. It has pluggable annotation support including Feign annotations and JAX-RS annotations. Feign also supports pluggable encoders and decoders.
Feign
uses tools like Jersey and CXF to write java clients for REST or SOAP services. Furthermore,Feign
allows you to write your own code on top of http libraries such as Apache HC.Feign
connects your code to http APIs with minimal overhead and code via customizable decoders and error handling, which can be written to any text-based http API.Feign
works by processing annotations into a templatized request. Arguments are applied to these templates in a straightforward fashion before output. AlthoughFeign
is limited to supporting text-based APIs, it dramatically simplifies system aspects such as replaying requests. Furthermore, Feign makes it easy to unit test your conversions knowing this.Feign
10.x and above are built on Java 8 and should work on Java 9, 10, and 11. For those that need JDK 6 compatibility, please use Feign 9.x- By default
Feign
will use it's client which is built based onJava Http Client
and annotations to defineContract
between the interface and how the underlying client should work. Feign's default contract defines the following annotations:
Annotation | Interface Target | Usage |
---|---|---|
@RequestLine | Method | Defines the HttpMethod and UriTemplate for request. Expressions, values wrapped in curly-braces {expression} are resolved using their corresponding @Param annotated parameters. |
@Param | Parameter | Defines a template variable, whose value will be used to resolve the corresponding template Expression, by name provided as annotation value. If value is missing it will try to get the name from bytecode method parameter name (if the code was compiled with -parameters flag). |
@Headers | Method | Type Defines a HeaderTemplate; a variation on a UriTemplate. that uses @Param annotated values to resolve the corresponding Expressions. When used on a Type, the template will be applied to every request. When used on a Method, the template will apply only to the annotated method. |
@QueryMap | Parameter | Defines a Map of name-value pairs, or POJO, to expand into a query string. |
@HeaderMap | Parameter | Defines a Map of name-value pairs, to expand into Http Headers |
@Body | Method | Defines a Template, similar to a UriTemplate and HeaderTemplate, that uses @Param annotated values to resolve the corresponding Expressions. |
What Is The Spring Cloud OpenFeign?#
Spring Cloud Openfeign
provides OpenFeign integrations for Spring Boot apps through autoconfiguration and binding to the Spring Environment and other Spring programming model idioms.- Spring Cloud adds support for Spring MVC annotations and for using the same
HttpMessageConverters
used by default in Spring Web. Spring Cloud integrates Eureka, as well as Spring Cloud LoadBalancer to provide a load-balanced http client when using Feign. Basically, the dependencyspring-cloud-starter-openfeign
that we use in Spring Boot applications is built based on theFeign-core
with a lot of powerful features. You can look at the dependency tree below to see all dependencies wrapped inspring-cloud-starter-openfeign
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Spring Cloud OpenFeign
provides the following beans by default for feign:
BeanType | BeanName | ClassName |
---|---|---|
Decoder | feignDecoder | SpringDecoder |
Encoder | feignEncoder | SpringEncoder |
Logger | feignLogger | Slf4jLogger |
MicrometerCapability | micrometerCapability | If feign-micrometer is on the classpath and MeterRegistry is available |
CachingCapability | cachingCapability | If @EnableCaching annotation is used. Can be disabled via feign.cache.enabled. |
Contract | feignContract | SpringMvcContract |
Feign.Builder | feignBuilder | FeignCircuitBreaker.Builder |
Client | feignClient | If Spring Cloud LoadBalancer is on the classpath, FeignBlockingLoadBalancerClient is used. If none of them is on the classpath, the default feign client is used. |
- So with
Contract
isSpringMvcContract
, so now you can useSpring MVC annotaions
to define the client interfaces. Let's see some common annotations in the table below.
Annotation | Path |
---|---|
@RequestMapping | import org.springframework.web.bind.annotation.RequestMapping; |
@PathVariable | import org.springframework.web.bind.annotation.PathVariable; |
@RequestParam | import org.springframework.web.bind.annotation.RequestParam; |
@RequestBody | import org.springframework.web.bind.annotation.RequestBody; |
- Let's take an example:
AdapterServiceApi.java | |
---|---|
1 2 3 4 5 6 |
|
Spring Cloud OpenFeign Dependencies#
- To add Spring Cloud OpenFeign dependencies, you have to add Spring Cloud dependencies first as mentioned in Spring Cloud Introduction. So you have to add this dependency first for Spring Cloud
pom.xml | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
- Then you can add this dependency below for Spring Cloud OpenFeign.
pom.xml | |
---|---|
1 2 3 4 5 |
|
Spring Cloud OpenFeign Default Using#
Enable Spring Cloud OpenFeign#
- Add annotation
@EnableFeignClients
into themain
class as below to enable FeignClient.
BffApplication.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Create FeignClient#
- Now we will create an adapter interface to configure OpenFeign with target api as below:
AdapterServiceApi.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
- The
application.yml
will contain the target URL of REST Server that we want to call to.
application.yml | |
---|---|
1 2 3 4 5 6 7 |
|
Using OpenFeign In Code#
- Now, you created the FeignClient interface. So you can inject for any service classes by
@Autowired
and methods for using.
AdapterServiceImpl.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Testing#
-
Now, Let's start target service application(Service A) and your application that contain the OpenFeign configuration above (Service B). Then when you can an api of Service B then this api will call to an api of Service A and you should see the result from the Service A.
-
Let's call an api of Service A to create an data.
- Then let's call an api of Service B to get the data that has just created on Service A
- As you can see the Service B can call to api of Service A to get data successfully, so it mean the configuration for OpenFeign is correct.