【SpringCloud】 - Feign 踩坑記錄:404 ,調用不成功 , 接口定義規範 等問題記錄

Feign注意事項

*  1、FeignClients使用注意事項:
 *  1. @EnableFeignClients 默認掃描 xxxxApplication啓動入口 所在包路徑下的 @FeignClient bean,若沒法掃描到, 能夠在使用Feign調用外部模塊的api時候,須要在引用服務中 xxxxApplication 中的 `@EnableFeignClients(basePackages = "cn.tendyron.customer")` 添加外部包須要掃描FeignClient的路徑,不然沒法注入bean
 *  2. @FeignClient 聲明的類,使用 spring.application.name 做爲 name配置 @FeignClient(name="xxxx"),若是想保留 context-path , 則須要配置 path 屬性 ,如:@FeignClient(name="xxxx" , path="xxxx(context-path)")
 *  3. @FeignClient 接口對應的實現類,須要使用 @RestController註解 聲明
 *  4. mapper註解不支持 : @GetMapping,@PostMapping  , 參數要加 @RequestParam(「xxx」)
 *  5. FeignClient 調用,實質是httpClient調用 ,若咱們暴露的接口api,聲明瞭對應的 http mapper 信息,在調用方調用時候,經過代理 發起了 http請求,到服務提供方對應的http服務上去,因此在服務提供方的接口,能夠使用 @RestController
 *     來聲明接口的 實現,不然調用方沒法找到 http 對應的路徑,會報404 ;
 *     或者 根據 api 聲明的http 信息,構建一個 Controller ,再來調用接口的實現類,可是這樣不太方便;

Feign接口定義

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: 測試服務,注意 Feign的標準寫法
 * @date 2019/7/5
 */
@FeignClient(name = "customer-center",path = "customer")
public interface TestService {

    /**
     *  1、FeignClients使用注意事項:
     *  1. @EnableFeignClients 默認掃描 xxxxApplication啓動入口 所在包路徑下的 @FeignClient bean,若沒法掃描到, 能夠在使用Feign調用外部模塊的api時候,須要在引用服務中 xxxxApplication 中的 `@EnableFeignClients(basePackages = "cn.tendyron.customer")` 添加外部包須要掃描FeignClient的路徑,不然沒法注入bean
     *  2. @FeignClient 聲明的類,使用 spring.application.name 做爲 name配置 @FeignClient(name="xxxx"),若是想保留 context-path , 則須要配置 path 屬性 ,如:@FeignClient(name="xxxx" , path="xxxx(context-path)")
     *  3. @FeignClient 接口對應的實現類,須要使用 @RestController註解 聲明
     *  4. mapper註解不支持 : @GetMapping,@PostMapping  , 參數要加 @RequestParam(「xxx」)
     *  5. FeignClient 調用,實質是httpClient調用 ,若咱們暴露的接口api,聲明瞭對應的 http mapper 信息,在調用方調用時候,經過代理 發起了 http請求,到服務提供方對應的http服務上去,因此在服務提供方的接口,能夠使用 @RestController
     *     來聲明接口的 實現,不然調用方沒法找到 http 對應的路徑,會報404 ;
     *     或者 根據 api 聲明的http 信息,構建一個 Controller ,再來調用接口的實現類,可是這樣不太方便;
     */


    /**
     * 測試Feign Get ,普通處理
     * 注意: @RequestParam("xx") 註解必定要寫,並且屬性名xx不能省略
     */
    @RequestMapping(value = "/test/getUserBo",method = RequestMethod.GET)
    CommonResult<OrgUserBO> getUserBo(@RequestParam("orgCode") String orgCode , @RequestParam("username") String name);


    /**
     * 測試Feign Get Pojo
     */
    @RequestMapping(value = "/test/testUserBo",method = RequestMethod.GET)
    CommonResult<OrgUserBO> testUserBo(OrgUserBO userBO , @RequestParam("token") String token);


    /**
     * 測試Feign Post Pojo
     * 注意:實現類也要在參數前加 @RequestBody
     */
    @RequestMapping(value = "/test/postUserBo",method = RequestMethod.POST)
    CommonResult<OrgUserBO> postUserBo(@RequestBody  OrgUserBO userBO);

}

接口實現

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
@RestController
public class TestServiceImpl implements TestService {

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public CommonResult<OrgUserBO> getUserBo(String orgCode, String name) {
        return null;
    }

    @Override
    public CommonResult<OrgUserBO> testUserBo(OrgUserBO userBO,String token) {
        log.info("Feign Get Pojo token:{}",token);
        return CommonResult.success(userBO);
    }

    @Override
    public CommonResult<OrgUserBO> postUserBo(OrgUserBO userBO) {

        return CommonResult.success(userBO);
    }
}
相關文章
相關標籤/搜索