Building a microservices Architecture Spring Cloud: Service consumption (feign)

Source: Internet
Author: User

Spring Cloud Feign

Spring Cloud Feign is a set of declarative service invocation clients based on the Netflix feign implementation. It makes writing Web service clients easier. We only need to configure it by creating an interface and using annotations to complete the binding to the Web service interface. It features pluggable annotation support, including feign annotations, Jax-rs annotations. It also supports pluggable encoders and decoders. Spring Cloud Feign also extends support for spring MVC annotations, while also consolidating the ribbon and Eureka to provide an HTTP client implementation of a balanced load.

Below, we show an example of how feign can easily declare the definition and invocation of the Eureka-client service.
Give it a try.

In the following example, we will use the previously built Eureka-server as the service registry, Eureka-client as the service provider. And based on the spring Cloud Ribbon implementation of the consumer, we can according to Eureka-consumer implementation of the content can be easily changed to complete, the specific steps are as follows:

Copy a service consumer project according to Eureka-consumer, named: Eureka-consumer-feign. Add the following dependencies in the Pom.xml:

<dependencies>    ...    <dependency>        <groupId></groupId>        <artifactId>spring-cloud-starter-feign</artifactId>    </dependency></dependencies>

Modify the main class of the application. The ability to scan the spring Cloud feign client is enabled via @enablefeignclients annotations:

@EnableFeignClients@EnableDiscoveryClient@SpringBootApplicationpublic class Application {    public static void main(String[] args) {        new SpringApplicationBuilder(Application.class).web(true).run(args);    }}

Create a feign client interface definition. Use the @feignclient annotation to specify the service name to invoke for this interface, and the functions defined in the interface can bind the rest interface of the service provider using spring MVC annotations, such as the following example of the/DC interface that binds the Eureka-client service:

@FeignClient("eureka-client")public interface DcClient {    @GetMapping("/dc")    String consumer();}

Modify the controller. The interface of the service provider is invoked by the defined feign client:

@RestControllerpublic class DcController {    @Autowired    DcClient dcClient;    @GetMapping("/consumer")    public String dc() {        return dcClient.consumer();    }}

The way to implement service invocation through spring Cloud feign is much simpler, with the interface defined by @feignclient to unify the life we need to rely on for the MicroServices interface. And in the specific use of the call to the local method a little bit of the call. Since feign is implemented based on the Ribbon, it comes with client load balancing, and can be extended through the irule of the Ribbon. In addition, feign integrates the Hystrix to implement fault-tolerant protection of the service, and in Dalston version, feign Hystrix is turned off by default. After introducing Hystrix, we will introduce the hystrix in feign and how to configure them.

After you have finished writing your code, the reader can start Eureka-server, Eureka-client, Eureka-consumer-feign, and then access Http://localhost:2101/consumer, To track how the Eureka-consumer-feign service consumes the/DC interface of the Eureka-client service, and can also observe its load balancing effect by initiating multiple eureka-client services.

Building a microservices Architecture Spring Cloud: Service consumption (feign)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.