Implementation method of feign of spring cloud using HTTP to request remote services

1、 Feign introduction

In the spring cloud Netflix stack, each micro service exposes its own service in the form of HTTP interface, so the HTTP client must be used when calling remote services. We can use JDK native urlconnection, Apache HTTP client, netty asynchronous HTTP client, and spring resttemplate. However, feign is the most convenient and elegant one to use.

Feign is a declarative, templated HTTP client. Using feign in spring cloud, we can achieve the same coding experience as calling local methods when using HTTP to request remote services. Developers are completely unaware that this is a remote method, let alone an HTTP request.

2、 Use of feign in spring cloud

1. Add dependency

2. Create feignclient

@Feignclient (name = "spring-producer-server / spring"): used to notify feign components to proxy the interface (no interface implementation is required). The name attribute specifies which service we want to call. Users can inject directly through @ Autowired.

@Requestmapping indicates that a get request needs to be sent to / group / {groupid} when calling this method.

@Pathvariable has the same meaning as the corresponding annotation in spring MVC.

Principle: when the spring cloud application starts, feign will scan the interface marked with @ feignclient annotation, generate a proxy, and register it in the spring container. When generating the proxy, feign will create a requesttemplate object for each interface method, which encapsulates all the information required by the HTTP request. The request parameter name, request method and other information are determined in this process. Feign's templating is reflected here.

3. Add annotation on startup class

4. Configuration file application yml

3、 Customize feign configuration

1. Custom configuration

2. Use custom configuration

@Requestline: it is the annotation of feign

4、 Feign log configuration

Create a logger for each feign client created. By default, the name of the logger is the full class name of the interface used to create the feign client. Feign logging responds only to the debug level. logging. level. project. user. UserClient: DEBUG

In the configuration file application Add in YML:

Add a log level in the custom configuration class

PS: feign request timeout

The default timeout time of hystrix is 1 second. If there is no response after this time, it will enter the fallback code. The first request is often slow (because spring's lazy loading mechanism needs to instantiate some classes), and the response time may be greater than 1 second

There are three solutions. Take feign as an example.

Method 1

hystrix. command. default. execution. isolation. thread. timeoutInMilliseconds: 5000

This configuration is to change the timeout of hystrix to 5 seconds

Method 2

hystrix. command. default. execution. timeout. enabled: false

This configuration is used to disable the timeout of hystrix

Method 3

feign. hystrix. enabled: false

This configuration is used to simply disable festrix of feign. This method is not recommended except for some special scenarios.

The above implementation method of feign of spring cloud using HTTP to request remote services is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>