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 use Feign to invoke service interface in SpringCloud

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about how to use Feign to call the service interface in SpringCloud. I think it is very practical, so I share it with you. I hope you can get something after reading this article.

What about interface calls in the JAVA project?

1) Httpclient

HttpClient is a sub-project under Apache Jakarta Common to provide efficient, up-to-date, feature-rich client programming toolkits that support the Http protocol, and it supports the latest versions and recommendations of the HTTP protocol.

Compared with the URLConnection that comes with traditional JDK, HttpClient improves the ease of use and flexibility, makes it easier for the client to send HTTP requests, and improves the efficiency of development.

2) Okhttp

An open source project that handles web requests is the hottest lightweight framework on Android, contributed by Square to replace HttpUrlConnection and Apache HttpClient. OkHttp has concise API, efficient performance, and supports a variety of protocols (HTTP/2 and SPDY).

3) HttpURLConnection

HttpURLConnection is a standard class of Java, which inherits from URLConnection and can be used to send GET requests and POST requests to specified Web sites. HttpURLConnection is more complex to use and is not as easy to use as HttpClient.

4) RestTemplate

RestTemplate is a client provided by Spring to access Rest services. RestTemplate provides a variety of convenient methods to access remote HTTP services, which can greatly improve the writing efficiency of the client.

The above describes some of the most common ways to call an interface, and the method we are going to introduce next is simpler and more convenient than the one above, which is Feign.

Feign is a declarative REST client that makes REST calls easier. Feign provides templates for HTTP requests. By writing simple interfaces and inserting annotations, you can define the parameters, format, address and other information of HTTP requests.

On the other hand, Feign will fully proxy the HTTP request, and we just need to call it like a method to complete the service request and related processing.

Spring Cloud encapsulates Feign to support SpringMVC standard annotations and HttpMessageConverters. Feign can be used in combination with Eureka and Ribbon to support load balancing.

Integrate Feign in Spring Cloud

The step of integrating Feign in Spring Cloud is fairly simple, starting with adding Feign dependencies, as shown below.

Org.springframework.cloudspring-cloud-starter-openfeign

Add @ EnableFeignClients to the startup class. If your Feign interface definition is not under the same package name as your startup class, you also need to specify the scanned package name @ EnableFeignClients (basePackages= "com.fangjia.api.client"), as shown below.

@ SpringBootApplication@EnableDiscoveryClient@EnableFeignClients (basePackages = "com.fangjia.api.client") public class FshSubstitutionServiceApplication {public static void main (String [] args) {SpringApplication.run (FshSubstitutionServiceApplication.class, args);}} use Feign to call the interface

Define a client for Feign, which exists as an interface, as shown in the following code.

FeignClient (value = "eureka-client-user-service") public interface UserRemoteClient {@ GetMapping ("/ user/hello") String hello ();}

First, let's take a look at the @ FeignClient annotation added to the interface. This annotation identifies that it is currently a client of Feign, and the value attribute is the corresponding service name, that is, the interface in which service you need to invoke.

You can simply copy the definition of the interface when defining the method, and of course, another way is to extract the definition of the interface separately and then implement the interface in Controller.

The interface is also implemented in the calling client, so as to achieve the purpose of interface sharing. What I do here is not common, that is, to create a public project of API Client separately. Based on the agreed pattern, every time an interface is written, a calling Client is written, followed by a common jar, so that no matter which project needs to call the interface, as long as the common interface SDK jar is introduced, there is no need to redefine it.

Once defined, it can be called directly by injecting UserRemoteClient, which is like calling a local method to the developer.

Next, use Feign to call the / user/hello interface, as shown below.

@ Autowiredprivate UserRemoteClient userRemoteClient;@GetMapping ("/ callHello") public String callHello () {/ / return restTemplate.getForObject ("http://localhost:8083/house/hello",String.class);//String result = restTemplate.getForObject (" http://eureka-client-user-service/user/hello",String.class);String result = userRemoteClient.hello (); System.out.println ("call result:" + result); return result;}

Compared with the commented-out code, we can find that our invocation method is getting simpler and simpler, from the specified address at the beginning, then through the service name in Eureka, and now directly through defining the interface.

The above is how to use Feign to invoke the service interface in SpringCloud. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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

Development

Wechat

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

12
Report