feign的简单调用,以及返回值为null的解决方法。


简单记录一下feign实现的远程调用。

1.在项目中引入feign依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

  

  1. 编写配置

#开启hystrix配置
feign.hystrix.enabled=true #根据你的需求添加以下配置 #feign.client.config.default.connect-timeout=10000 #feign.client.config.default.read-timeout=10000 #feign.client.config.default.logger-level=basic #请求连接的超时时间 ribbon.ReadTimeout=30000 #禁用hystrix: hystrix.command.default.execution.timeout.enabled=false

 

3.启动类上添加注解

@SpringBootApplication
//👇 这两个注解很重要
@EnableFeignClients(basePackages = {"com.xxx.xxfeign"}) @EnableDiscoveryClient //👆 @MapperScan("com.xx.xx.dao") @EnableDistributedTransaction public class DocsApplication { public static void main(String[] args) { SpringApplication.run(DocsApplication.class, args); } }

 

4.写好方法, 😃😃😃@RequestParam() 注意添加这个注解,这个注解可以让你传递调用方法参数,不至于调用时候传Null

@RequestMapping(value = "/getInfo", method = RequestMethod.POST)
                                        //👇这个注解很重要!!!
    public CrmUserInfoEntity  GetUserInfo(@RequestParam("id") Long id){ CrmUserInfoEntity all = crmUserInfoIService.findAll(id); return all; } ​
// crmUserInfoIService 的实现方法就不给出了(简单的查询)。
  1. 声明调用

@Component
@FeignClient(value = "你的server-name",configuration = FeignConfiguration.class) public interface CmsFeign { ​ @RequestMapping(value = "/crmUserInfo/getInfo", method = RequestMethod.POST) CrmUserInfoEntity GetUserInfo(@RequestParam("id")Long id); } //configuration = FeignConfiguration.class 这个是自定义的配置,根据你的需求添加,不添加也不会报错; //不添加去除@Component

 

  1. 远程调用实现

    @Resource
    private CmsFeign cmsFeign; ​ public String xxx(){ long i = 9527L; CrmUserInfoEntity info = cmsFeign.GetUserInfo(i); system.out.println("远程调用返回了啥:"+info); return null; }

 

至此feign的基本调用结束 🎉🎉🎉

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM