spring-cloud-starter-feign的入门使用(记录容易报404错误的写法)

Feign的入门使用

SpringCloud入门案例-GitHub(含本文代码)

定义:

来自官方网站截图

注:截图来自SpringCloud官方网站

定义中强调了:1.Feign是一种声明式Web Service客户端。2.Feign使用接口+注解进行开发
好比DAO开发模式:

@Mapper
public interface dao{
	public void get();
}

入门使用

既然定义在Web Service客户端,那么就在客户端使用
对比Ribbon+RestTemplate客户端代码
Ribbon+RestTemplate代码:

@RestController
public class DeptController_Consumer {

    //    private static final String REST_URL_PREFIX = "http://localhost:8001";
    //使用微服务的服务名访问服务
    private static final String REST_URL_PREFIX = "http://MICROSERVICECLOUD-PROVIDER-DEPT";

    /**
     * (url,requestMap,ResponseBean.class):三个参数分别表示:REST请求服务地址,请求参数,HTTP响应转换(被转换成的对象类型)
     */
    @Autowired
    private RestTemplate restTemplate;

//    @PostMapping("/consumer/dept/add")

    @GetMapping("/consumer/dept/add")
    public boolean add(Dept dept){
        return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class);
    }

    @GetMapping("/consumer/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id){
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class);
    }

    @SuppressWarnings("unchecked")
    @GetMapping("/consumer/dept/list")
    public List<Dept> list(){
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class);
    }

    //测试服务发现
    @GetMapping("/dept/discovery")
    public Object discovery(){
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/discovery", Object.class);
    }
}

这种方式与Controller调用Service写法是相悖的,且其他类需要调用的服务的时候不方便。使用Feign能够更好的面向接口编程。代码如下:

@RestController
public class DeptController_Consumer_Feign {
    @Autowired
    private DeptClientService service;

    //方便测试都使用的是HTTP的Get方法
    //注意:这里不能使用GetMapping,会报404错误的
//    @GetMapping("/consumer/dept/add")
    @RequestMapping(value = "/consumer/dept/add", method = RequestMethod.GET)
    public boolean add(Dept dept){
        return service.add(dept);
    }

//    @GetMapping("/consumer/dept/get/{id}")
    @RequestMapping(value = "/consumer/dept/get/{id}", method = RequestMethod.GET)
    public Dept get(@PathVariable("id") Long id){
        return service.get(id);
    }

    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/consumer/dept/list", method = RequestMethod.GET)
    public List<Dept> list(){
        return service.list();
    }
}

这样又回到了我们熟悉的controller调用service的味道了!
这时候会问DeptClientService从哪来?这个服务类通常可能有多个消费者调用,每个微服务里面都写的话降低了代码复用的效果,所以一般都抽取到公共的微服务项目中,使用maven引用公共微服务项目即可。代码如下:

//使用Feign的时候一定注意,必须使用@RequestMapping(value, method)的形式,否则报404
//value表示调用的服务名,与eureka中注册的服务名有关
@FeignClient(value = "MICROSERVICECLOUD-PROVIDER-DEPT")
public interface DeptClientService {

//    @GetMapping("/consumer/dept/add")
    @RequestMapping(value = "/consumer/dept/add", method = RequestMethod.GET)
    public boolean add(Dept dept);

//    @GetMapping("/consumer/dept/get/{id}")
    @RequestMapping(value = "/consumer/dept/get/{id}", method = RequestMethod.GET)
    public Dept get(@PathVariable("id") Long id);

    @SuppressWarnings("unchecked")
//    @GetMapping("/consumer/dept/list")
    @RequestMapping(value = "/consumer/dept/list", method = RequestMethod.GET)
    public List<Dept> list();
}

既然要使用Feign,在消费端启动服务的时候必须加载Feign相关配置

@SpringBootApplication
@EnableEurekaClient
//扫描与feign相关组件包(由于Feign接口抽取到了公共微服务,这里的包名写公共微服务项目中的包名)
@EnableFeignClients(basePackages = {"com.ittx.feign.deptservice"})
//@ComponentScan("com.ittx.springcloud")
public class DeptConsumerFeignMainApplication {
    public static void main(String[] args) {
        SpringApplication.run(DeptConsumerFeignMainApplication.class, args);
    }
}

注意:

  1. 当使用Feign的时候,不能使用GetMapping一类注解,否则会报404错误
    在这里插入图片描述
    官方实例
    在这里插入图片描述
  2. SpringBootApplication与ComponentScan注解同时间使用的时候SpringBootApplication注解扫描包的功能会失效,所以慎重使用。如果引用了Maven外部项目,一定要扫描包的话。ComponentScan注解中除了写外部项目的包名,还一定要写自身项目需要扫描的包名。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值