In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, I would like to share with you the relevant knowledge about how to call SpringCloud's Feign-based programmable interface. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.
Preface
Feign can replace RestTemplate to complete the call of programmable interface, and the internal integration of Ribbon implements load balancing.
1. Basic use of 1. Citation dependence
Pom file increases openfeign dependency
Org.springframework.cloud spring-cloud-starter-openfeign2. Annotate
Add @ EnableFeignClients annotation to startup class
@ EnableFeignClients@SpringBootApplicationpublic class OrderApplication {public static void main (String [] args) {SpringApplication.run (OrderApplication.class, args);}} 3. Declare an interface
Create a client package that distributes the interfaces used by the package
Package com.cxstar.client;import com.alibaba.fastjson.JSONObject;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping FeignClient ("bookschina-service") @ RequestMapping ("/ bookschina") public interface BookschinaClient {@ GetMapping ("/ spiderBookList/ {searchkey} / {pageno}") JSONObject searchBookschina (@ PathVariable ("searchkey") String searchKey, @ PathVariable ("pageno") Integer pageNo);}
Just make a copy of the controller class in the microservice you need to call, and just fix it.
Ps:
1.@FeignClient ("bookschina-service"): access to spring.application.name=bookschina-service 's microservices
2.@RequestMapping, @ GetMapping, and @ PathVariable are used to map addresses, the same as in controller
3. The above interface corresponds to the controller class method with the mapping address of / bookschina/spiderBookList/ {searchkey} / {pageno} under bookschina-service microservice.
4. Call
The test classes are as follows
Package com.cxstar;import com.alibaba.fastjson.JSONObject;import com.cxstar.client.BookschinaClient;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;@Slf4j@SpringBootTestclass OrderApplicationTests {@ Autowired private BookschinaClient bookschinaClient; void booksChinaTest () {/ / Parameter String searchKey = "Hino Guigo"; Integer pageNo = 1 / / call JSONObject booksJB = bookschinaClient.searchBookschina (searchKey, pageNo); log.info (booksJB.toString ());}} II, advance 1. Log configuration
Add configuration to application file
Feign: client: config: default: # default global configuration, local configuration can be replaced with the requested service name loggerLevel: NONE # log level NONE BASIC HEADERS FULL
Ps:
1.NONE: no log
2.BASIC: record http request destination, send time, return time, total time spent, etc.
3.HEADERS: record request header and response header information on the basis of BASIC
4.FULL: record request body and response body information on the basis of BASIC and HEADERS
5. Try to use NONE or BASIC to optimize the performance log level after launch
two。 Performance optimization
The default implementation of the Feign underlying client is URLConnection, which does not support connection pooling. Every http request requires a three-way handshake and four waves when disconnected, which is a waste of performance. You can use Apache HttpClient instead of URLConnection.
Pom introduces dependency
Io.github.openfeign feign-httpclient
Add configuration to application file
Feign: httpclient: enabled: true # enable feign support for httpclient max-connections: 200 # maximum number of connections max-connections-per-route: 50 # maximum number of connections per request interface
Ps:
Max-connections and max-connections-per-route values need to be set on a case-by-case basis.
These are all the contents of the article "how to call SpringCloud's Feign-based programmable interface". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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.
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.