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

The method of calling Feign remotely by client

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the knowledge of "the method of remote calling Feign from the client". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

What is Feign when the client calls Feign remotely?

Feign is Netflix's open source declarative HTTP client

Github: Feign source code

Why do I need Feign?

The readability of the original code is not high

Complex URL is difficult to maintain (https://user-center/s?wd={userId}&rsv_spt=1&rsv_iqid=0x93bff3cd000cf3da&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_sug3=4&rsv_sug1=4&rsv_sug7=100&rsv_t=10c2risCimsUZC0RBruMerdnQRN1gRxFI%2BywuD0L3LwGGNd2dR8XE6x%2FyFOjHnR0oEi0&rsv_sug2=0&inputT=1535&rsv_sug4=1535&rsv_sug=2)

It is difficult to cope with the rapid changes in demand

The coding experience is quite different from our writing JAVA.

For example, before refactoring code / / replace ResponseEntity userEntity = restTemplate.getForEntity ("http://user-center/users/{userId}", UserDTO.class, userId); UserDTO userDTO = new UserDTO (); if (null! = userEntity) {userDTO = userEntity.getBody () Log.info ("ShareService#findById userDTO: {}", userDTO);}

Add dependency

Org.springframework.cloud spring-cloud-starter-openfeign

Add an annotation to the startup class @ EnableFeignClients

Add configuration

Implement the client of the corresponding microservice

/ * * IUserCenterFeignClient for defines user-center feign client * * @ author Isaac.Zhang | if the beginning * @ since 2019-7-15 * / @ FeignClient (name = "user-center") public interface IUserCenterFeignClient {/ * Feign client will convert the request to * http://user-center/users/{userId} * @ param userId user id * @ return and return the user object * / @ GetMapping (path = "/ users/ {userId}") public UserDTO findById (@ PathVariable Long userId) }

Code after replacement

/ / use FeignClient to replace RestTemplate call UserDTO userDTO = this.userCenterFeignClient.findById (userId); composition of Feign

Configuration of Feign configuration items supported by Java Code

Customize the Feign log level

Level content

Demo

* * Tip * *: the problem of parent-child context overlap may occur

Add configuration in client

/ * * IUserCenterFeignClient for defines user-center feign client * * @ author Isaac.Zhang | if the beginning * @ since 2019-7-15 * / @ FeignClient (name = "user-center", configuration = UserCenterFeignConfiguration.class) public interface IUserCenterFeignClient {.}

Write java configuration file

/ * in the custom user-center service request of UserCenterFeignConfiguration for, the configuration information of feign * {@ link @ Configuration} cannot be added, otherwise, like ribbon, the problem of context overlap will occur, resulting in global sharing of configuration * if you want to add this comment This class needs to be placed under the package that cannot be scanned by the main program * * @ author Isaac.Zhang | if the beginning * @ since 2019-7-15 * / public class UserCenterFeignConfiguration {@ Bean public Logger.Level level () {/ / configure the feign log level, record the header, body and metadata return Logger.Level.FULL of the request and response }}

Be sure to add the client full path to the configuration file

Logging: level: # com.sxzhongf: debug com.sxzhongf.sharedcenter.feignclients.IUserCenterFeignClient: debug

Print information

2019-07-15 15 c.s.s.f.IUserCenterFeignClient 06 DEBUG 11.650 20286-[nio-8010-exec-1] c.s.s.f.IUserCenterFeignClient: [IUserCenterFeignClient#findById]

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

Internet Technology

Wechat

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

12
Report