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

What is feign?

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "what is feign". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is feign".

A brief introduction to Feign

Feign is a declarative pseudo-Http client that makes it easier to write Http clients. With Feign, you only need to create an interface and annotate it. It has pluggable annotations and can use Feign annotations and JAX-RS annotations. Feign supports pluggable encoders and decoders. Feign integrates Ribbon by default and combines with Eureka to achieve the effect of load balancing by default.

In short:

Feign uses interface-based annotations

Feign integrates ribbon

II. Preparatory work

Continue to use the project in the previous section, start eureka-server with port 8761, and start service-hi twice with ports 8762 and 8773, respectively.

Third, create a feign service

Create a new spring-boot project, named serice-feign. The start of introducing Feign in its pom file depends on spring-cloud-starter-feign, the start of Eureka depends on spring-cloud-starter-eureka, and the start of Web depends on spring-boot-starter-web. The code is as follows:

4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.8.RELEASE com.free serice-feign 0.0.1-SNAPSHOT serice-feign Demo project for Spring Boot 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

In the project configuration file application.yml file, specify that the program name is service-feign, the port number is 8765, and the service registration address is http://localhost:8761/eureka/. The code is as follows:

Server: port: 8765spring: application: name: service-feigneureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/

In the startup class ServiceFeignApplication of the program, add the @ EnableFeignClients annotation to enable the Feign function:

Import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication@EnableDiscoveryClient@EnableFeignClientspublic class SericeFeignApplication {public static void main (String [] args) {SpringApplication.run (SericeFeignApplication.class, args);}}

Define a feign interface that specifies which service to invoke through @ FeignClient ("service name"). For example, the "/ hi" interface of the service-hi service is called in the code as follows:

Service@FeignClient (value = "service-hi") public interface SchedualServiceHi {@ RequestMapping (value = "/ hi", method = RequestMethod.GET) String sayHiFromClientOne (@ RequestParam (value = "name") String name);}

In the controller layer of the Web layer, expose a "/ hi" API interface to consume services through the Feign client SchedualServiceHi defined above. The code is as follows:

RestControllerpublic class HiController {@ Autowired SchedualServiceHi schedualServiceHi; @ RequestMapping (value = "/ hi", method = RequestMethod.GET) public String sayHi (@ RequestParam String name) {return schedualServiceHi.sayHiFromClientOne (name);}}

Start the program and visit the http://localhost:8765/hi?name=zz, browser many times to display alternately:

Hi zz,i am from port:8762

Hi zz,i am from port:8763

Feign source code parsing: http://blog.csdn.net/forezp/article/details/73480304

Download the source code of this article: https://gitee.com/freesystem/SpringCloudDemo/tree/master/serice-feign

Thank you for your reading, the above is the content of "what is feign", after the study of this article, I believe you have a deeper understanding of what is feign, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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