Backend Development 7 min read

Master Spring Cloud OpenFeign: Configuring URLs, Timeouts, Logging & Hystrix

This tutorial walks through setting up Spring Boot 2.3.8 with Spring Cloud Hoxton, adding OpenFeign dependencies, configuring service URLs, customizing Feign client defaults, enabling logging, setting request timeouts, and integrating Hystrix circuit breaker with fallbacks and factories.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Master Spring Cloud OpenFeign: Configuring URLs, Timeouts, Logging & Hystrix

Environment: Spring Boot 2.3.8.RELEASE + Spring Cloud Hoxton.SR8

Add the required Maven dependencies:

<code>&lt;dependencies&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
        &lt;artifactId&gt;spring-cloud-starter-openfeign&lt;/artifactId&gt;
    &lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;dependencyManagement&gt;
    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
            &lt;artifactId&gt;spring-cloud-dependencies&lt;/artifactId&gt;
            &lt;version&gt;${spring-cloud.version}&lt;/version&gt;
            &lt;type&gt;pom&lt;/type&gt;
            &lt;scope&gt;import&lt;/scope&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
&lt;/dependencyManagement&gt;</code>

Configure the target service directly via URL

<code>@FeignClient(value = "mv", url = "${feign.url}")
public interface PersonWeb {
    @GetMapping("/person/{id}")
    String get(@PathVariable Integer id);
}</code>
<code>feign:
  url: http://localhost:8001</code>

Target service implementation:

<code>@RestController
public class PersonController {
    @GetMapping("/person/{id}")
    Object get(@PathVariable Integer id) {
        return "Person";
    }
}</code>

Note: The target service returns an

Object

(actually a

String

), so the Feign interface must declare

String

as the return type.

Modify default configuration attributes of

@FeignClient
<code>@FeignClient(value = "mv", url = "${feign.url}", configuration = FeignConfig.class)
public interface PersonWeb { ... }</code>
<code>public class FeignConfig {
    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }
}</code>

The above JavaConfig sets the default logger level; the same can be done via properties.

Logging level configuration (application.yml):

<code>logging:
  level:
    com.pack.controller.PersonWeb: debug</code>

Set request timeout

<code>feign:
  url: http://localhost:8001
  httpclient:
    enabled: true
  client:
    config:
      mv:
        connectTimeout: 2000
        readTimeout: 2000</code>

The

mv

key corresponds to the

value

(or

name

) defined in

@FeignClient

.

Adjust the target service to delay response by 5 seconds:

<code>@GetMapping("/person/{id}")
public Object get(@PathVariable Integer id) {
    try {
        TimeUnit.SECONDS.sleep(5);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return "Person";
}</code>

The console shows timeout errors, confirming that the

readTimeout

setting is effective.

Log level configuration

<code>feign:
  logger-level: basic</code>

This setting overrides the logger level defined in

FeignConfig

.

Circuit‑breaker support

Supported circuit‑breaker implementations include Netfix Hystrix, Resilience4J, Sentinel, and Spring Retry. This example uses Hystrix.

<code>feign:
  hystrix:
    enabled: true</code>
<code>@Component
public class PersonFallback implements PersonWeb {
    @Override
    public String get(Integer id) {
        return "我是返回的默认值";
    }
}</code>
<code>@FeignClient(value = "mv", url = "${feign.url}", configuration = FeignConfig.class, fallback = PersonFallback.class)
public interface PersonWeb { ... }</code>

Hystrix’s default timeout is 1 second, while the target service sleeps for 5 seconds.

Modify Hystrix timeout via configuration:

<code>&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
    &lt;artifactId&gt;spring-cloud-starter-netflix-hystrix&lt;/artifactId&gt;
&lt;/dependency&gt;</code>
<code>hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 3000
      timeout:
        enabled: true</code>

Obtain exception details with a

FallbackFactory

:

<code>@Component
public class PersonFallbackFactory implements FallbackFactory<PersonWeb> {
    @Override
    public PersonWeb create(Throwable cause) {
        cause.printStackTrace();
        return new PersonWeb() {
            @Override
            public String get(Integer id) {
                return "fallback";
            }
        };
    }
}

@FeignClient(value = "mv", url = "${feign.url}", configuration = FeignConfig.class, fallbackFactory = PersonFallbackFactory.class)
public interface PersonWeb { ... }</code>

Feign inheritance support

<code>public interface BaseController {
    @GetMapping("/person/{id}")
    String get(@PathVariable Integer id);
}</code>
<code>@RestController
@RequestMapping("/demo")
public class DemoController implements BaseController {
    @Resource
    private PersonWeb personWeb;

    @GetMapping("/person/{id}")
    public String get(@PathVariable Integer id) {
        return personWeb.get(id);
    }
}</code>
<code>@FeignClient(value = "mv", url = "${feign.url}", configuration = FeignConfig.class, fallbackFactory = PersonFallbackFactory.class)
public interface PersonWeb extends BaseController { }
</code>

Using inheritance in this way is generally not recommended.

Final application entry point:

<code>@SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker
public class SpringBootOpenfeignApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootOpenfeignApplication.class, args);
    }
}
</code>

Done!!

loggingSpring BootOpenFeigntimeoutcircuit breakerHystrixFeignClient
Spring Full-Stack Practical Cases
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.