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

What are the considerations of SpringCloud's Feign?

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

Share

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

This article mainly introduces "what matters needing attention in the Feign of SpringCloud". In the daily operation, I believe that many people have doubts about what matters needing attention in the Feign of SpringCloud. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "what matters needing attention in Feign of SpringCloud". Next, please follow the editor to study!

Feign Note * 1. Notes for FeignClients use: * 1. @ EnableFeignClients scans @ FeignClient bean under the package path where the xxxxApplication startup entry is located by default. If it cannot be scanned, you can use Feign to call the api of the external module. You need to scan the path of the FeignClient to add the external package in the reference service `@ EnableFeignClients (basePackages = "cn.tendyron.customer") `in the reference service, otherwise you cannot inject the class declared by bean * 2. @ FeignClient. Use spring.application.name as the name configuration @ FeignClient (name= "xxxx"). If you want to keep context-path, you need to configure the path attribute, for example: @ FeignClient (name= "xxxx", path= "xxxx (context-path)") * 3. @ FeignClient interface corresponding to the implementation class, you need to use @ RestController annotation to declare * 4. Mapper annotation does not support: @ GetMapping,@PostMapping Parameters should be added with @ RequestParam ("xxx") * 5. FeignClient call is essentially a httpClient call. If we expose the interface api and declare the corresponding http mapper information, when the caller invokes, the http request is initiated through the proxy to the corresponding http service of the service provider. Therefore, @ RestController * can be used to declare the implementation of the interface in the service provider's interface. Otherwise, the caller cannot find the path corresponding to http and will report 404. * or build a Controller based on the http information declared by api, and then call the implementation class of the interface, but this is not convenient. Feign interface definition package cn.tendyron.customer.api;import cn.tendyron.common.api.protocol.CommonResult;import cn.tendyron.customer.bo.OrgUserBO;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam / * @ author CRong.L * @ ClassName: TestService: * @ Description: test service, note the standard writing of Feign * @ date 2019-7-5 * / @ FeignClient (name = "customer-center", path = "customer") public interface TestService {/ * 1. Notes for FeignClients usage: * 1. @ EnableFeignClients scans @ FeignClient bean under the package path where xxxxApplication startup entry is located by default, if it cannot be scanned When you use Feign to call the api of an external module, you need to add an external package to `@ EnableFeignClients (basePackages = "cn.tendyron.customer") `in the reference service xxxxApplication. You need to scan the path of the FeignClient, otherwise you cannot inject the class declared by bean * 2. @ FeignClient and configure @ FeignClient (name= "xxxx") with spring.application.name as the name. If you want to keep context-path, you need to configure the path attribute. For example: @ FeignClient (name= "xxxx", path= "xxxx (context-path)") * 3. @ FeignClient API corresponding to the implementation class, need to use @ RestController annotation to declare * 4. Mapper annotation does not support: @ GetMapping,@PostMapping, parameter to add @ RequestParam ("xxx") * 5. FeignClient call is essentially a httpClient call. If we expose the interface api, declare the corresponding http mapper information. When the caller invokes, the http request is initiated through the proxy and goes to the corresponding http service of the service provider. Therefore, @ RestController * can be used in the interface of the service provider to declare the implementation of the interface, otherwise the caller cannot find the path corresponding to http and will report 404. * or build a Controller based on the http information declared by api, and then call the implementation class of the interface, but this is not convenient. * / / * Test Feign Get, normal processing * Note: @ RequestParam ("xx") annotation must be written, and attribute name xx cannot omit * / @ RequestMapping (value = "/ test/getUserBo", method = RequestMethod.GET) CommonResult getUserBo (@ RequestParam ("orgCode") String orgCode, @ RequestParam ("username") String name) / * Test Feign Get Pojo * / @ RequestMapping (value = "/ test/testUserBo", method = RequestMethod.GET) CommonResult testUserBo (OrgUserBO userBO, @ RequestParam ("token") String token); / * * Test Feign Post Pojo * Note: the implementation class should also be preceded by @ RequestBody * / @ RequestMapping (value = "/ test/postUserBo", method = RequestMethod.POST) CommonResult postUserBo (@ RequestBody OrgUserBO userBO) } Interface to implement package cn.tendyron.customer.service.impl;import cn.tendyron.common.api.protocol.CommonResult;import cn.tendyron.common.util.BeanUtils;import cn.tendyron.customer.api.AuthService;import cn.tendyron.customer.api.TestService;import cn.tendyron.customer.bo.OrgUserBO;import cn.tendyron.customer.common.constant.BizErrorCodeEnum;import cn.tendyron.customer.common.constant.Constant;import cn.tendyron.customer.entity.OrgUserDO;import cn.tendyron.customer.support.OrgUserSupport;import com.alibaba.fastjson.JSON Import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.util.Assert;import org.springframework.web.bind.annotation.RestController;import java.util.Objects;import java.util.concurrent.TimeUnit / * @ author CRong.L * @ ClassName: TestServiceImpl * @ Description: * @ date 2019-7-5 * / @ Slf4j@RestControllerpublic class TestServiceImpl implements TestService {@ Autowired RedisTemplate redisTemplate; @ Override public CommonResult getUserBo (String orgCode, String name) {return null;} @ Override public CommonResult testUserBo (OrgUserBO userBO,String token) {log.info ("Feign Get Pojo token: {}", token); return CommonResult.success (userBO) } @ Override public CommonResult postUserBo (OrgUserBO userBO) {return CommonResult.success (userBO);}} at this point, the study of "what are the points for attention in SpringCloud's Feign" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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