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 configure Feign in Spring Cloud

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

Share

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

In this issue, the editor will bring you about how to configure Feign in Spring Cloud. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Ribbon configuration

The configuration of ribbon is actually very simple. You can configure it directly in application.properties, as follows:

# set connection timeout ribbon.ConnectTimeout=600# set read timeout ribbon.ReadTimeout=6000# retry all operation requests ribbon.OkToRetryOnAllOperations=true# switch instance retry number of ribbon.MaxAutoRetriesNextServer=2# retry to the current instance ribbon.MaxAutoRetries=1

The way this parameter is tested is simple. We can sleep for 5 seconds in the hello method of the service provider, and then adjust this parameter to see the effect. The following parameters are the timeout retry parameters we configured. After the timeout, we will first continue to attempt to access the current instance once. If it still fails, the instance access will be switched. You can switch the instance for a total of two times. After two times, if you still do not get the access result, you will report Read timed out executing GET http://hello-service/hello.

However, this configuration is a global configuration, which is valid for all requests. If I want to configure different connection timeouts and read timeouts for different services, we can add the name of the service before the attribute, as follows:

# set connection timeout for hello-service service hello-service.ribbon.ConnectTimeout=600# setting read timeout for hello-service service hello-service.ribbon.ReadTimeout=6000# setting retry for all operation requests of hello-service service hello-service.ribbon.OkToRetryOnAllOperations=true# setting number of retries for switching instances of hello-service service hello-service.ribbon.MaxAutoRetriesNextServer=2# setting for current hello-service service Number of retries of instance hello-service.ribbon.MaxAutoRetries=1Hystrix configuration

The configuration of Hystrix in Feign is similar to that of Ribbon. The basic configuration is as follows:

# set circuit breaker timeout hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000# turn off Hystrix function (do not use with the above configuration) feign.hystrix.enabled=false# disable circuit breaker function hystrix.command.default.execution.timeout.enabled=false

This configuration is also a global configuration. If we want to configure for a certain interface, such as / hello interface, we can write it as follows:

# set the fuse timeout hystrix.command.hello.execution.isolation.thread.timeoutInMilliseconds=10000# and turn off the fuse function hystrix.command.hello.execution.timeout.enabled=false

However, our interface name may be duplicated, and the interface with the same name will share this Hystrix configuration.

OK, we had a previous article devoted to the issue of Hystrix service degradation, so how to configure Hystrix service degradation in Feign? Quite simply, create a new class and implement the HelloService interface, as follows:

@ Componentpublic class HelloServiceFallback implements HelloService {@ Override public String hello () {return "hello error";} @ Override public String hello (String name) {return "error" + name;} @ Override public Book hello (String name, String author, Integer price) {Book book = new Book (); book.setName ("error"); return book } @ Override public String hello (Book book) {return "error book";}}

The logic implemented by the method here is the corresponding service degradation logic. Then specify the service degradation handling class in the @ FeignClient annotation, as follows:

FeignClient (value = "hello-service", fallback = HelloServiceFallback.class) public interface HelloService {@ RequestMapping ("/ hello") String hello (); @ RequestMapping (value = "/ hello1", method = RequestMethod.GET) String hello (@ RequestParam ("name") String name); @ RequestMapping (value = "/ hello2", method = RequestMethod.GET) Book hello (@ RequestHeader ("name") String name, @ RequestHeader ("author") String author, @ RequestHeader ("price") Integer price) @ RequestMapping (value = "/ hello3", method = RequestMethod.POST) String hello (@ RequestBody Book book);}

At this point, we only start eureka-server and feign-consumer, and then access the corresponding interfaces. You can see the following results (note that feign.hystrix.enabled=true needs to be configured in application.properties. The new version of Spring Cloud Feign (Dalston.SR3) defaults to Hystrix):

Other configuration

Spring Cloud Feign supports GZIP compression of requests and responses to improve communication efficiency. The configuration is as follows:

# configure request GZIP Compression feign.compression.request.enabled=true# configuration response GZIP Compression feign.compression.response.enabled=true# configuration Compression supported MIME TYPEfeign.compression.request.mime-types=text/xml,application/xml,application/json# configuration Compression data size Lower limit feign.compression.request.min-request-size=2048

Feign provides a feign.Logger instance for each FeignClient. You can enable the log in the configuration in a simple way, which is divided into two steps:

Step 1: configure log output in application.properties

The following is configured in application.properties to set the log output level:

# enable log format to logging.level.+Feign client path logging.level.org.sang.HelloService=debug

Step 2: configure log Bean in the portal class

Log Bean is configured in the portal class, as follows:

@ BeanLogger.Level feignLoggerLevel () {return Logger.Level.FULL;} the above is how to configure Feign in the Spring Cloud shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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