Wednesday, February 12, 2020

Feign Client - Spring Boot (Microservice) inter service call

In this article I'm planing to explain the way we can implement Feign Client to invoke inter service call.

We have eurekaServer, zuulServer, clientServer, serverServer. Initially, we need to register our all servers with the eurekaServer.
zuulServer register as proxyService
clientServer register as oneService
serverServer register as twoService
twoService has a end-point as below:
 http method: POST

 url: /api/person/{personId}/bank

1st of all, we need to add dependency to the application. In this example I'm going to use maven dependency as below.
<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-feign</artifactId>
      <version>#{feign.version}</version>
</dependency>

According to the above end-point, we need to implement the feign-client as below
 @FeignClient(name = "twoservice", path = "/api",
        fallback = TwoServiceFallBack.class,
        configuration = DefaultFeignConfig.class)
public interface TwoServiceClient {

    @RequestMapping(path = "/person/{personId}/bank", method = RequestMethod.POST)
    CustomResponseMessage addBankInfo(@PathVariable("personId") String personId, @RequestBody BankDTO bankDTO);

}

And also fall-back class below (when calling the service, if something happen, you can handle the situation here).
@Component
public class TwoServiceClientFallBack implements TwoServiceClient {

    @Override
    public CustomResponseMessage addBankInfo(String personId, BankDTO bankDTO) {
     //
        return null;
    }
}

CustomResponseMessage -> response payload.
BankDTO -> request body

Hope this will helpful.

No comments:

Post a Comment