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 the use of the @ FeignClient () annotation in SpringCloud

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

SpringCloud @FeignClient() annotation is how to use, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need this can learn, I hope you can gain something.

@FeignClient() annotation usage

Because SpringCloud adopts a distributed microservices architecture, it is inevitable that module methods will call each other under each submodule. For example, the service-admin service calls methods of the service-card service.

The @FeignClient() annotation is meant to solve this problem.

The source code for the @FeignClient() annotation requires that it be used on the Interface interface. (The FeinClient annotation is modified by @Target(ElementType.TYPE) to indicate that the FeinClient annotation targets the interface.)

@RequestLine is different from other requests. You only need to write the request method and path to request other services.

@FeignClient(value = "feign-server",configuration = FeignConfig.class) //a configuration file is required public interface TestService { @RequestLine("POST /feign/test") //Corresponding request method and path String feign(@RequestBody UserDO userDO);}@EnableFeignClients@SpringBootConfigurationpublic class FeignConfig { @Bean public Contract contract(){ return new feign.Contract.Default(); }}@FeignClient common attributes are as follows

value: service name

name: Specifies the name of the FeinClient, if the project uses a Ribbon, the name attribute is used as the name of the microservice for service discovery

url: url is generally used for debugging, you can manually specify the address of @FeignClient call

decode404: When http 404 error occurs, if this field bit is true, decoder will be called to decode, otherwise FeignException will be thrown.

configuration: Feign configuration class, you can customize Feign Encoder, Decoder, LogLevel, Contract

fallback: Defines a fault-tolerant processing class. When the remote interface fails or times out, the fault-tolerant logic of the corresponding interface will be invoked. The class specified by fallback must implement the interface marked @ FeinClient.

fallbackFactory: factory class, used to generate fallback class examples, through this attribute we can implement common fault tolerance logic for each interface, reducing duplication of code

path: Defines the uniform prefix for the current FeigClient

It also requires that the startup class of the service have the @ EnableFeigClients annotation for Fegin to take effect.

SpringCloud services call each other @ FeigClient annotation

After SpringCloud builds various microservices, there is usually a need for mutual invocation between services. SpringCloud provides @ FeinClient annotation to solve this problem very elegantly.

First, ensure that several services are registered successfully in an Eureka to form a service farm.

As follows, I have a total of three services registered in the service farm. COMPUTE-SERVICE ; FEIGN-CONSUMER ; TEST-DEMO;

I'm at FEIGN-CONSUMER

Two interfaces in the service that invoke the other two services

Two interfaces, get with parameters and post without parameters, are as follows: this is the get with parameters method in COMPUTE-SERVICE

@RequestMapping(value = "/add" ,method = RequestMethod.GET)public Integer add(@RequestParam Integer a, @RequestParam Integer b) { ServiceInstance instance = client.getLocalServiceInstance(); Integer r = a + b; logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r); return r;}

If you want to call this method in FEIGN-CONSUMER service, you need to create a new interface class in FEIGN-CONSUMER to call a series interface in a project.

@FeignClient("compute-service") public interface ComputeClient { @RequestMapping(method = RequestMethod.GET, value = "/add") Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b); }

The @ FeinClient annotation identifies which service in the current service farm is to be invoked, and the service name is taken from the configuration in the target service.

spring.application.name

Next, in @RequestMapping, set the interface type, interface address, and other attributes of the target interface. Then define the interface parameters and return parameters below

FEIGN-CONSUMER

When the Controller layer calls a method

Inject the interface above, and you can use it directly.

@Autowired ComputeClient computeClient; @RequestMapping(value = "/add", method = RequestMethod.GET) public Integer add() { return computeClient.add(10, 20); }

The post method is the same:

This is the target interface:

@RestController @RequestMapping("/demo") @EnableAutoConfiguration public class HelloController { @RequestMapping(value = "/test",method = RequestMethod.POST) String test1(){ return "hello,test1()"; } }

This is the interface file defined in this project:

@FeignClient("test-Demo") public interface TestDemo { @RequestMapping(method = RequestMethod.POST, value = "/demo/test") String test(); } This is the Controller layer in the project @RestController public class ConsumerController { @Autowired TestDemo testDemo; @Autowired ComputeClient computeClient; @RequestMapping(value = "/add", method = RequestMethod.GET) public Integer add() { return computeClient.add(10, 20); } @RequestMapping(value = "/test", method = RequestMethod.GET) public String test() { return testDemo.test(); } }

The final result is as follows:

OK, that's how the service interface is called!

Did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.

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

Development

Wechat

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

12
Report