Using the registry-aware Spring Cloud Netflix FeignClient client

Let's look at a simple example about Feign Client. In Chapter 8, Simplify HTTP API with Feign Client, we will discuss more about the Spring Cloud Netflix Feign Client. Feign Client is a discovery-aware Spring RestTemplate using interfaces to communicate with endpoints. This client is a registry server-aware client. It will be used as the Discovery-server-aware Spring RestTemplate using an interface with service endpoints to communicate, and these interfaces will be automatically implemented at runtime. The Spring Cloud Netflix Feign Client is using services-names instead of the service-urls.

Feign already uses Ribbon, so if you are using @FeignClient then this section also applies.

Let's see the simple example of this Feign Client. First, to set up Feign Client on your application, you have to add the following dependencies to your pom.xml:

<dependencies> 
   <dependency> 
         <groupId>org.springframework.boot</groupId> 
         <artifactId>spring-boot-starter-web</artifactId> 
   </dependency> 
   <dependency> 
         <groupId>org.springframework.cloud</groupId> 
         <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 
   </dependency> 
   <dependency> 
         <groupId>org.springframework.cloud</groupId> 
         <artifactId>spring-cloud-starter-openfeign</artifactId> 
   </dependency> 
 
</dependencies> 

As you can see, I have added four Maven dependencies to the pom.xml file. These dependencies are spring-cloud-starter-openfeign,spring-cloud-starter-netflix-eureka-client,spring-boot-starter-web, and spring-boot-starter-thymeleafspring-cloud-starter-openfeign provides Feign client. Let's see the following Feign client interface:

package com.dineshonjava.feignclient.service; 
 
import org.springframework.cloud.openfeign.FeignClient; 
import org.springframework.web.bind.annotation.GetMapping; 
 
@FeignClient("spring-cloud-eureka-client") 
public interface HelloServiceClient { 
    
   @GetMapping("/hello") 
   String sayHello(); 
} 

As you can see, this interface has one method in this example with the @GetMapping annotation. And you can also see this interface is annotated with the @FeignClient annotation with the spring-cloud-eureka-client service name. We don't need to implement this interface in our example as Spring will do it at runtime. But remember, the @FeignClient annotation will work only when you have enabled the Spring Cloud Netflix Feign Client support for your application by using the @EnableFeignClients annotation on the configuration class that is annotated with @Configuration:

package com.dineshonjava.feignclient; 
 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 
import org.springframework.cloud.openfeign.EnableFeignClients; 
 
@SpringBootApplication 
@EnableEurekaClient 
@EnableFeignClients 
public class FeignClientApplication { 
 
   public static void main(String[] args) { 
         SpringApplication.run(FeignClientApplication.class, args); 
   } 
} 

As you can see, the main application class is annotated with three annotations: @SpringBootApplication, @EnableEurekaClient, and @EnableFeignClients. The @SpringBootApplication annotation is used for auto-configuration related to Spring Cloud and @EnableEurekaClient is used to register this application a service to the Eureka server. Finally, the @EnableFeignClients annotation is used to enable the Netflix Feign module to your Spring cloud application.

Let's create an application controller class and auto-wire the Feign client interface into this controller:

package com.dineshonjava.feignclient.controller; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.GetMapping; 
 
import com.dineshonjava.feignclient.service.HelloServiceClient; 
 
@Controller 
public class HelloWebController { 
    
   @Autowired 
   HelloServiceClient helloServiceClient; 
    
   @GetMapping("/say-hello") 
   String sayHello(ModelMap model){ 
         model.put("message", helloServiceClient.sayHello()); 
         return "hello"; 
   } 
}

This web controller has one request handler method, sayHello(), and it populates the model object with the message returned by the feign client, that is, HelloServiceClient, and fetched data from our REST service. Here, dependency for starters, such as spring-boot-starter-web and spring-boot-starter-thymeleaf, is used to present a view.

Let's see the following view for this web application example:

<!DOCTYPE html> 
<html xmlns:th="http://www.thymeleaf.org"> 
    <head> 
        <title>Say Hello Page | Dineshonjava.com</title> 
    </head> 
    <body> 
        <h2 th:text="${message}"/> 
    </body> 
</html> 

This is the thymeleaf view file, which renders the view and value of the message return from the controller.

So .yml configuration file will be same as we have used previously. In this example, I am using the .yml format for the configuration file. So, this file will be the same as the one we used for the REST service, the only differences being the application name and server port. You can see this file as follows:

spring: 
  application: 
    name: spring-cloud-feign-client 
 
server: 
  port: 8080 
 
eureka: 
  client: 
    service-url: 
      default-zone: ${EUREKA_URI:http://localhost:8761/eureka} 
  instance: 
    prefer-ip-address: true

Let's run this application and see the Eureka Dashboard again:

Now you can see that there are three services currently registered with Eureka server—SPRING-CLOUD-EUREKA-CLIENT, SPRING-CLOUD-FEIGN-CLIENT, and SPRING-CLOUD-RIBBON-CLIENT.

After running this example, we'll open our browser, go to http://localhost:8080/say-hello, and it should display something like the following:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.34.62