Using the registry-aware Spring Cloud Netflix FeignClient 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 uses services-names instead of the service-urls.

Project setup

spring init -d=cloud-feign,cloud-eureka,web,thymeleaf -n=feign-client feign-client
Using service at https://start.spring.io
Project extracted to '/home/admatic/feign-client'

Let's see the following Feign client interface:

cd feign-client/
mkdir -p src/main/java/com/example/feignclient/service/

cat << EOF > src/main/java/com/example/feignclient/service/HelloServiceClient.java
package com.example.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();
}
EOF

The @FeignClient annotation will work only when you have @EnableFeignClients annotation on the configuration class

cat << EOF > src/main/java/com/example/feignclient/FeignClientApplication.java
package com.example.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);
    }
}
EOF

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:

mkdir -p src/main/java/com/example/feignclient/controller

cat << EOF > src/main/java/com/example/feignclient/controller/HelloWebController.java
package com.example.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.example.feignclient.service.HelloServiceClient;

@Controller
public class HelloWebController {

    @Autowired
    HelloServiceClient helloServiceClient;

    @GetMapping("/say-hello")
    String sayHello(ModelMap model) {
        model.put("message", helloServiceClient.sayHello());
        return "hello";
    }
}
EOF

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.

mkdir -p src/main/resources/templates/

echo '<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>Say Hello Page | admatic.in</title>
    </head>
    <body>
        <h2 th:text="${message}"/>
    </body>
</html>' | tee src/main/resources/templates/hello.html

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

application.yml configuration file is the same as we have used previously.

cat << EOF > src/main/resources/application.yml
spring:
  application:
    name: spring-cloud-feign-client

server:
  port: 8181

eureka:
  client:
    service-url:
      default-zone: http://localhost:8761/eureka
  instance:
    prefer-ip-address: true
EOF

Run the Feign Client

mvn spring-boot:run

curl http://localhost:8181/say-hello
<!DOCTYPE html>
<html>
    <head>
        <title>Say Hello Page | admatic.in</title>
    </head>
    <body>
        <h2>Hello from Admatic from EurekaClient!</h2>
    </body>
</html>

As you can see, RestTemplate called the spring-cloud-eureka-client service registered with Eureka to fetch the Hello from Admatic from EurekaClient! string.

results matching ""

    No results matching ""