Using Feign Rest Client for Service invocation
In this Tutorial will Learn Importatnt Spring Boot Cloud Component Called Feign
F-E-I-G-N
in previous Code It was Prety complex code we have written to call a REST Service, there was a lot of manual stuff that we had to do , to call a very very simple service
Map<String, String> uriVariables = new HashMap<String, String>();
uriVariables.put("from", from);
uriVariables.put("to", to);
ResponseEntity<CurrencyConversionBean> responseEntity = new RestTemplate()
.getForEntity("http://localhost:8000/currency-exchange/from/{from}/to/{to}",
CurrencyConversionBean.class,
uriVariables);
CurrencyConversionBean response = responseEntity.getBody();
There is no complexity was there but we had to still write a lot of code for it
So if we talk about the Microservices than there will be a lot of calls to other services.
so if you want to make your code much simpler than this Than one of the important problem solved by
FEIGN
FEIGN makes it Very easy to invoke other Microservices/RestFul Services
Other aditional things that FEIGN provide, it provides integration with something called Ribbon
Ribbon is client Side Load Balancing Framework
FEIGN is the one of the component that spring cloid inherit from NETFLIX
STEP 1: Add the FEIGN Depndency in your CurrencyConversionService Microservice
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
Just Make sure that Dependency Management should be present for Spring cloud
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Now one of the importatnt things we have to do is Enable FEIGN by adding
@EnableFeignClients at the Main class
@SpringBootApplication
@EnableFeignClients("com.khan.microserservices.currencyconversionservice")
public class CurrencyConversionServiceApplication {
public static void main(String[] args) {
SpringApplication.run(CurrencyConversionServiceApplication.class, args);
}
}
Now with this Annotation we have FEIGN Enable Project
Now we can use Feign to Invoke the services, just like we use a repository to talk to JPA
We need to create Feign Proxy to be able to talk to a external Microservice
So Lets create Feign Proxy Now
Create New Interface Like Given Below
@FeignClient(name="", url="")
public interface CurrencyExchangeServiceProxy {
}
name will be the same which is present in other microservice property file
spring.application.name=currency-exchange-service
and Url will be the localhost:8000
@FeignClient(name="currency-exchange-service", url="localhost:8000")
public interface CurrencyExchangeServiceProxy {
}
now we have to define the method which will talk to the currency-exchange-sercie
@FeignClient(name="currency-exchange-service", url="localhost:8000")
public interface CurrencyExchangeServiceProxy {
@GetMapping("currency-exchange/from/{from}/to/{to}")
public CurrencyConversionBean retrieveExchangeValue(@PathVariable String from, @PathVariable String to);
}
in given above interface we have copied the method from currencyExchagneController Class method
just simple modification is instead of ExchangeValue as return we have add CurrencyConversionBean
as return from the method
Now add the controller class method which use the FEIGN simply
@RestController
public class CurrencyConversionController {
@Autowired
private CurrencyExchangeServiceProxy currencyExchangeServiceProxy;
/******************* START This Code without Using FEIGN START ********************/
@GetMapping("/currency-converter/from/{from}/to/{to}/quantity/{quantity}")
public CurrencyConversionBean convertCurrency(@PathVariable String from, @PathVariable String to, @PathVariable BigDecimal quantity) {
//Feign -Problem 1
Map<String, String> uriVariables = new HashMap<String, String>();
uriVariables.put("from", from);
uriVariables.put("to", to);
ResponseEntity<CurrencyConversionBean> responseEntity = new RestTemplate()
.getForEntity("http://localhost:8000/currency-exchange/from/{from}/to/{to}",
CurrencyConversionBean.class,
uriVariables);
CurrencyConversionBean response = responseEntity.getBody();
return new CurrencyConversionBean(response.getId(), from, to, response.getConversionMultiple(), quantity,
quantity.multiply(response.getConversionMultiple()), response.getPort());
}
/******************* END This Code without Using FEIGN END ********************/
/*************************** START CODE with FEIGN ******************************/
@GetMapping("/currency-converter-feign/from/{from}/to/{to}/quantity/{quantity}")
public CurrencyConversionBean convertCurrencyFeign(@PathVariable String from, @PathVariable String to, @PathVariable BigDecimal quantity) {
CurrencyConversionBean response = currencyExchangeServiceProxy.retrieveExchangeValue(from, to);
return new CurrencyConversionBean(response.getId(), from, to, response.getConversionMultiple(), quantity,
quantity.multiply(response.getConversionMultiple()), response.getPort());
}
/*************************** END CODE with FEIGN ******************************/
}
Feign helps us to simplify the client code to talk to a RESTful
Web Service
suppose if you want to serve the 10-20 100 services than all the detail how to talk to those
service you can go in just one place in your CurrencyExchangeServiceProxy interface
All the Rest of the application need not know that currency exchange service is a RESTfull
service i am talking to a other application, we just talk to proxy no need to worry about how proxy
getting the details from the service.
Comments
Post a Comment