Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Feign to realize load balancing in Spring Cloud

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

It is believed that many inexperienced people have no idea about how to use Feign to achieve load balancing in Spring Cloud. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Let's take a look at using Feign load balancing. Before the introduction, let's give a brief introduction to Feign and understand what Feign is.

1. What is Feign?

Let's take a look at the official explanation: Feign is a declarative WebService client. Using Feign makes the written WebService client more concise, defining an interface in a methodical way, and then adding comments to it.

Spring Cloud encapsulates Feign to support Spring MVC standard annotations and HttpMessageConverters. Feign can be used in combination with Eureka and Ribbon to support load balancing.

two。 Why use Feign?

Feign is designed to make it easier to write Java Http clients. Previously, when using Ribbon + RestTemplate, a set of templated invocation methods are formed by using RestTemplate's encapsulation of http requests. However, in the actual development, because there may be more than one call to the service dependency, often an interface will be called in multiple places, so it is usually necessary to encapsulate some client classes for each micro-service to wrap these dependent service calls.

Therefore, Feign makes further encapsulation on this basis to help us define and implement the definition of dependent service interfaces. To use Feign, you only need to create an interface and configure it with an annotation.

This is similar to marking the @ Mapper annotation on the interface of the dao layer. In this way, the interface binding to the service provider is completed, which simplifies the amount of development when using Spring Cloud Ribbon. Let's use an example to illustrate.

3. The use of Feign

3.1 dependency Import

Org.springframework.cloud

Spring-cloud-starter-netflix-eureka-client

Org.springframework.cloud

Spring-cloud-starter-openfeign

3.2 Startup class annotation configuration

@ SpringBootApplication

@ EnableEurekaClient

@ EnableFeignClients

Public class OrderConsumerFeign {

Public static void main (String [] args) {

SpringApplication.run (OrderConsumerFeign.class, args)

}

}

The startup class needs to be annotated @ EnableFeignClients, which means that the Feign client is enabled.

3.3The Feign client

Let's recall that the name of the service providing the order is MICROSERVICE-ORDER, and then the two APIs provided in the order service to obtain order information are as follows:

/ * *

* feign client

* @ author shengwu ni

, /

@ FeignClient (value = "MICROSERVICE-ORDER")

Public interface OrderClientService {

@ GetMapping ("/ provider/order/get/ {id}")

TOrder getOrder (@ PathVariable (value = "id") Long id)

@ GetMapping ("/ provider/order/get/list")

List getAll ()

}

As you can see, the Feign client needs to add the @ FeignClient annotation, and the value attribute indicates which microservice is applied to it. Here, it indicates that the load balancer acts on the request order service. Keep in mind that Feign is also a load balancing for the client.

There are two interfaces defined in the method, which are no different from ordinary SpringMVC, and url is the url of the service provided by the order. This is the Feign client, which binds to the interface of the order service by annotating + interface.

3.4 use of Feign client

Once the Feign client interface is defined, we can import it normally through @ Resource in the code, and then call the order service through ordinary method calls, as follows:

@ RestController

@ RequestMapping ("/ consumer/order")

Public class OrderConsumerController {

/ * *

* Feign Client defined above

, /

@ Resource

Private OrderClientService orderClientService

@ GetMapping ("/ get/ {id}")

Public TOrder getOrder (@ PathVariable Long id) {

Return orderClientService.getOrder (id)

}

@ GetMapping ("/ get/list")

Public List getAll () {

Return orderClientService.getAll ()

}

}

Start the eureka cluster, three orders to provide services, and the service consumer with Feign (port 9001). By visiting: http://localhost:9001//consumer/order/get/1 in the browser, you can see that the three order services are accessed in turn. It shows that Feign is a polling scheme with integrated Ribbon by default.

4. How to combine Feign with Ribbon switching equalization algorithm

The above is the use of Feign, very simple, but also in line with our usual habit of calling interfaces. But this is the default polling load balancing algorithm. As we know in the previous section, Ribbon supports many self-contained equalization algorithms, so we just need to choose the appropriate equalization algorithm according to the specific situation in the code. We can even choose our own custom equalization algorithm.

So when using Feign, how to switch to other equalization strategies in Ribbon? Or even switch to a custom policy?

It is also very simple, we can specify it in the application.yml configuration file, as follows:

# feign is combined with ribbon to specify a policy. Feign defaults to polling policy, and the configuration here can be customized.

MICROSERVICE-ORDER:

Ribbon:

NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

MICROSERVICE-ORDER indicates which micro-service is affected. Com.netflix.loadbalancer.RandomRule is the random policy in the previous introduction of Ribbon. Of course, we can also specify other policies, including those defined by ourselves, as long as the corresponding package path is written here, which is very convenient.

Restart the service consumer's program and visit the above test url. You can see that the three order services are called randomly, indicating that the configuration takes effect.

After reading the above, have you mastered how to use Feign to achieve load balancing in Spring Cloud? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report