In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how to use Feign to achieve load balancing in Spring Cloud. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
Introduction to Feign
Feign is a declarative web service client. It makes writing web service clients easier. To use Feign create an interface and annotate it. It has pluggable annotation support including Feign annotations and JAX-RS annotations. Feign also supports pluggable encoders and decoders. Spring Cloud adds support for Spring MVC annotations and for using the same HttpMessageConverters used by default in Spring Web. Spring Cloud integrates Ribbon and Eureka to provide a load balanced http client when using Feign.
Feign is a declarative web service client that makes it easier to write web clients. To use Feign, simply create an interface and annotate it. Feign has a pluggable annotation feature, and you can use Feign annotations and JAX-RS annotations. Feign also supports pluggable encoders and decoders. Spring Cloud adds support for Spring MVC annotations and defaults to the same HttpMessageConverters as Spring Web. When using Feign, Spring Cloud integrates Ribbon and Eureka to provide a load-balanced http client.
In short:
Feign uses interface-based annotations
Feign integrates ribbon and has the ability of load balancing.
Integrated with Hystrix, with the ability of fusing
Preparatory work
Continue to build on the project in the first section, start eureka-server with port 9090, and start two eureka-client with ports 8040 and 8041.
Create a Feign service
Create a new project using Spring Initializr and call it feign-service. Check Eureka Discovery Client in Spring Cloud Discovery, OpenFeign in Spring Cloud Routing, and Spring Web in Web:
After the creation is successful, the project pom.xml is as follows:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.9.RELEASE com.noodles.mars feign-service 0.0.1-SNAPSHOT feign-service Feign Service 1.8 Greenwich.SR3 org.springframework.boot spring-boot-starter-web Org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.cloud spring-cloud-starter-openfeign org.springframework.boot spring-boot-starter-test test org.springframework. Cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
Feign-service configuration service center address, application name, port, configuration file content:
Server: port: 8080eureka: client: service-url: defaultZone: http://localhost:9090/eureka/spring: application: name: feign-client
Note @ EnableDiscoveryClient on the project startup class to enable registration with the service center; note @ EnableFeignClients to enable the Feign function:
@ SpringBootApplication@EnableEurekaClient@EnableFeignClientspublic class FeignServiceApplication {public static void main (String [] args) {SpringApplication.run (FeignServiceApplication.class, args);}}
Define a feign interface that specifies which service to invoke through @ FeignClient ("service application name"). For example, the / hello interface of the hello-erueka-client service is called in the code as follows:
FeignClient (value = "hello-eureka-client") public interface FeignService {@ GetMapping (value = "/ hello") String hello (@ RequestParam (value = "name") String name);}
Define a Controller that provides a "/ hello" Rest API interface, and invokes the service provider through the Feign defined above:
@ RestControllerpublic class FeignController {private final FeignService feignService; @ Autowired public FeignController (FeignService feignService) {this.feignService = feignService;} @ GetMapping (value = "/ hello") public String hello (@ RequestParam ("name") String name) {return feignService.hello (name);}}
Visit http://localhost:8080/hello?name=Mars multiple times on the browser:
Hello, My name is Mars, I'm from port: 8040Hello, My name is Mars, I'm from port: 8041 so much about how to use Feign to achieve load balancing in Spring Cloud. I hope the above content can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.