Feign declarative service invocation in spring cloud
In the previous article, we can find that when we call the APIs of other services through resttemplate, the required parameters must be spliced in the requested URL. If there are few parameters, maybe we can tolerate it. Once there are multiple parameters, splicing the request string will be inefficient and silly.
So is there a better solution? The answer is yes. Netflix has provided us with a framework: feign.
Feign is a declarative web service client. Its purpose is to make web service calls easier. Feign provides the template of HTTP request. By writing a simple interface and inserting annotations, you can define the parameters, format, address and other information of HTTP request.
Feign will fully proxy HTTP requests. We only need to call it like a method to complete service requests and related processing. Feign integrates ribbon and hystrix (we'll talk about hystrix later) so that we no longer need to explicitly use these two components.
In general, feign has the following characteristics:
This looks a bit like the request mapping of the controller layer of our spring MVC pattern. We like this model very much. Feign uses @ feignclient to map services.
The first step is to create a feign module based on the original one, and then introduce related dependencies. Introducing feign dependencies will automatically introduce hystrix dependencies, as follows:
application. The YML configuration is as follows:
Then, add several methods to the services of the two modules of provider1 and provider2 in the previous article, as shown in the following code:
Next is the user class required by the above code. The code is as follows:
Next, use feign's @ feignclient ("service name") to map the service call. The code is as follows:
Then inject the feiservice interface into the controller layer to call the remote service. The code is as follows:
Next, mark the Eureka client annotation @ enablediscoveryclient feign client annotation on the feign module's startup class
Then start the startup class, enter localhost: 8083 / consumer in the browser, and the running results are as follows:
You can see that hello1 and hello2 appear in load balancing polling.
Then continue to enter localhost: 8083 / consumer2 on the browser, and the running results are as follows:
Next, we use the service degradation under feign declarative call service, so we must create a feignfallback class to inherit feiservice. The code is as follows:
Then we stop the two service providing modules provider1 and provider2. The running results are as follows:
We can see that these calls have been degraded.
If we want to control the parameters of the hystrix precisely, for example, the parameters combined with the hystrix, we can configure a configuration = XXX class in the feignclient annotation Class attribute, which class exactly specifies the attribute.
Or in application YML is configured as follows:
Here, we can call most of our scenarios, but if we write fine scenarios, we still need to use the native hystrix. Follow our previous usage of hystrix. Don't use feign client to call, as follows:
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.