Unlock Advanced OpenFeign Features in Spring Cloud: Metrics, Caching, and More
This tutorial walks through configuring Spring Cloud OpenFeign with Spring Boot, covering basic setup, advanced capabilities like Micrometer metrics, Redis caching, @SpringQueryMap, @MatrixVariable, and @CollectionFormat, and shows how to view metrics via Prometheus or Actuator.
Environment
SpringBoot 2.7.16 + SpringCloud 2021.0.7
1. Introduction
Feign is a declarative web service client that simplifies writing HTTP clients. By defining an interface and annotating it, you can use Feign with pluggable annotations, encoders, and decoders. Spring Cloud adds support for Spring MVC annotations, Eureka, CircuitBreaker, and LoadBalancer.
Dependency
<code><dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency></code>Enable Feign
<code>@SpringBootApplication
@EnableFeignClients
public class SpringCloudComprehensiveApplication {
}</code>Feign Interface Definition
<code>@FeignClient(
url = "http://localhost:8088/demos",
name = "demoService",
configuration = DemoFeignConfiguration.class,
fallback = DemoFeignFallback.class,
fallbackFactory = DemoFeignFallbackFactory.class,
primary = true)
public interface DemoFeign {
@GetMapping("/info/{id}")
Object info(@PathVariable("id") Integer id);
@PostMapping("/map")
Object map(@RequestBody Map<String, Object> params);
}</code>The above configuration satisfies most project needs. The following sections describe less‑common but useful advanced features.
2. Advanced Features
2.1 Feign Metrics
When all three conditions are met, a MicrometerCapability bean is created so Feign can publish metrics to Micrometer:
feign-micrometer is on the classpath.
A MeterRegistry bean exists in the container.
Feign metrics are enabled (default true ).
Metrics can be viewed with Prometheus or, if not integrated, via Spring Boot Actuator.
Metric name screenshot:
2.2 Feign Caching
Adding @EnableCaching registers a CachingCapability bean, allowing Feign to honor @Cacheable and related annotations.
<code>@Cacheable(cacheNames = "infos", key = "#id")
@GetMapping("/info/{id}")
Object info(@PathVariable("id") Integer id);
</code>On first request the data is fetched remotely; subsequent calls are served from cache. By default it uses in‑memory cache, but Redis can be configured:
<code>spring:
cache:
type: redis
redis:
key-prefix: 'feign:'
time-to-live: 60s
</code>2.3 @SpringQueryMap Support
OpenFeign’s original @QueryMap is not compatible with Spring. Spring Cloud OpenFeign provides @SpringQueryMap to map a POJO or Map to query parameters.
<code>@GetMapping("/format")
Object format(@SpringQueryMap Params params);
</code>Controller example:
<code>@GetMapping("/format")
public Object format(Params params) {
return this.demoFeign.format(params);
}
public class Params {
private Long id;
private String name;
}
</code>2.4 @MatrixVariable Support
Spring Cloud OpenFeign also supports Spring’s @MatrixVariable . When a method parameter is a map, the matrix variables are built by joining key‑value pairs with ‘=’.
<code>@GetMapping("/m3/{params}")
Object matrix3(@MatrixVariable Map<String, List<String>> params);
</code>Note: the placeholder name must match the variable name in @MatrixVariable .
2.5 @CollectionFormat Support
The @CollectionFormat annotation lets you specify how collection parameters are serialized (CSV, PIPES, etc.).
<code>@GetMapping("/cf")
@CollectionFormat(feign.CollectionFormat.CSV)
Object cf(@RequestParam("ids") List<String> ids);
</code>Calling:
<code>this.demoFeign.cf(List.of("S1", "S2", "S3"));
</code>Produces request:
<code>: [DemoFeign#cf] --> GET http://localhost:8088/demos/cf?ids=S1,S2,S3 HTTP/1.1
</code>Changing the format to PIPES:
<code>@CollectionFormat(feign.CollectionFormat.PIPES)
</code>Results in ids=S1|S2|S3 .
These examples cover the most useful advanced capabilities of Spring Cloud OpenFeign.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.