In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use Feign to call remote HTTP services". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Feign to invoke remote HTTP services".
Introduction to Feign
Feign is a declarative, templated HTTP client. Using Feign in Spring Cloud, we can achieve the same coding experience when using HTTP to request a remote service as when calling a local method, and developers are completely unaware that this is a remote method, let alone a HTTP request. For example:
@ Autowiredprivate AdvertGropRemoteService service; / / remote service public AdvertGroupVO foo (Integer groupId) {return service.findByGroupId (groupId); / / invoke remote service via HTTP}
No beep, just go to the code:
Maven configuration:
Org.apache.httpcomponents httpclient com.netflix.feign feign-httpclient 8.18.0 com.netflix.feign feign-core 8.18.0 com.netflix.feign feign-gson 8. 18.0 com.google.code.gson gson 2.8.2
By default, Feign uses JDK's native URLConnection to send HTTP requests, and there is no connection pool, but a persistent connection is maintained for each address, that is, using HTTP's persistence connection. We can replace the original http client of Feign with Apache's HTTP Client to gain performance-related control capabilities such as connection pooling and timeout. Spring Cloud supports this replacement from the Brixtion.SR5 version, first declaring Apache HTTP Client and feign-httpclient dependencies in the project.
Feign definition:
Import com.migr.common.ws.ResponseBean;import feign.Param;import feign.RequestLine;/** * Created by Administrator on 2017-10-27. * / public interface AOranService {@ RequestLine ("POST / exec?m= {m} & reqJson= {reqJson} & token= {token}") / / get submit ResponseBean findParentOrgan (@ Param ("m") String m, @ Param ("reqJson") String reqJson, @ Param ("token") String token);}
How to call:
/ / this paragraph can be used as an audio tool class or singleton implementation / / http://base.dianxiaohuocy.net/api is the interface address AOranService oranService = Feign.builder () .decoder (new GsonDecoder ()) .encoder (new GsonEncoder ()) .target (AOranService.class, "http://base.dianxiaohuocy.net/api"); Map reqMap = new HashMap ()) ReqMap.put ("vendorId", "3"); reqMap.put ("organCode", "10101"); reqMap.put ("organLevel", "2"); String token = "H8DH9Snx9877SDER5667"; String reqJson = JsonUtil.g.toJson (reqMap) / / above / / the following is the method ResponseBean str = oranService.findParentOrgan that actually calls the interface through Feign ("findParentOrgan", reqJson, token)
The interface called:
@ RequestMapping (value = "/ exec") @ ResponseBody public Object exec (@ RequestParam (value = "m", required = true) String m, @ RequestParam (value = "reqJson", required = true) String reqJson, @ RequestParam (value = "token", required = true) String token) {/ / Internal implementation is not necessary to pay attention to}
Thank you for your reading, the above is the content of "how to use Feign to invoke remote HTTP services". After the study of this article, I believe you have a deeper understanding of how to use Feign to call remote HTTP services, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.