Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is feign?

Shulou Source: shulou.com Published: 2022-06-02 10:21:37 09月26日 Update

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!

Tags: Comments services code interfaces clients clients projects files programs learning content source code ports balance functions slogans addresses that is ideas situations Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Redmi Huawei MySQL Xiaomi