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