Microservices Communication: Spring Boot + OpenFeign + Client Credentials

Kundan Kumar
2 min readMay 29, 2021
Image Reference: https://www.contractworks.com/blog/effective-communication-strategies-for-contract-managers

In this article, we will learn How to use the OpenFeign client for communication between microservice.

What is OpenFeign?

OpenFeign is a declarative REST client. It makes writing web service clients easier, just create an interface and add annotations on the interface.

1. Add required gradle dependency for OpenFeign

implementation 'org.springframework.cloud:spring-cloud-starter-openfeign:2.2.7.RELEASE' 

2. Add the following details to the application.properties

security.oauth2.client.access-token-uri={your auth token URL}
security.oauth2.client.client-id={your clientId}
security.oauth2.client.client-secret={your client secret}
security.oauth2.client.grant-type=client_credentials

3. Enable Feign clients

Now it is time to enable Feign client in your application. Add @EnableFeignClients annotation to your application.

@SpringBootApplication
@EnableFeignClients
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}

4. Create a Feign client

To create a Feign client in spring, create an interface and use the @FeignClient annotation.

@FeignClient(name = "user-service", url = "http://localhost:8080/")
public interface UserFeignClient {

@RequestMapping(method = RequestMethod.POST, value = "/users")
public User addUser(User user);

@RequestMapping(method = RequestMethod.GET, value = "/users")
public List<User> listUser();
@RequestMapping(method = RequestMethod.GET, value = "/users/{userId}")
public User getUser(@PathVariable("userId") String userId);
}

5. Create a Feign RequestInterceptor

Create a custom RequestInterceptor and add an autherization token in the header.

public class OAuth2FeignRequestInterceptor implements RequestInterceptor {

@Autowired
private OAuth2RestTemplate clientCredentialsRestTemplate;

@Override
public void apply(RequestTemplate template) {
template.header("Authorization", getAuthorizationToken());
}

private String getAuthorizationToken() {
OAuth2AccessToken accessToken = clientCredentialsRestTemplate.getAccessToken();
return String.format("%s %s", accessToken.getTokenType(), accessToken.getValue());
}
}

6. Create a Feign configuration

Now create a custom configuration class for Feign client and create beans.

@Configuration
@EnableConfigurationProperties
public class FeignClientConfiguration {



@Bean
@ConfigurationProperties(prefix = "security.oauth2.client")
public ClientCredentialsResourceDetails clientCredentialsResourceDetails() {
return new ClientCredentialsResourceDetails();
}

@Bean
public RequestInterceptor oAuth2FeignRequestInterceptor() {
return new OAuth2FeignRequestInterceptor();
}

@Bean
public OAuth2RestTemplate clientCredentialsRestTemplate() {
return new OAuth2RestTemplate(clientCredentialsResourceDetails());
}


}

--

--

Kundan Kumar

Passionate software engineer with a desire to learn and grow.