feign 是一个让rest服务调用更简洁的开源项目,很多介绍文章或书也称它为声明式REST调用。传统的web service中,通过引用wsdl来自动生成一些client的代理类(或stub代码),feign跟这个有点类似,但是更灵活。
先回顾一下,上节中service-consumer对服务的调用代码:
![](https://yqfile.alicdn.com/img_1c53668bcee393edac0d7b3b3daff1ae.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_405b18b4b6584ae338e0f6ecaf736533.gif?x-oss-process=image/resize,w_1400/format,webp)
1 @GetMapping("/order/{userId}/{orderNo}") 2 public String findOrder(@PathVariable Integer userId, @PathVariable String orderNo) { 3 UserDTO user = restTemplate.getForEntity("http://SERVICE-PROVIDER-DEMO/user/" + userId, UserDTO.class).getBody(); 4 if (user != null) { 5 return user.getUserName() + " 的订单" + orderNo + " 找到啦!"; 6 } 7 8 return "用户不存在!"; 9 }
如果调用的参数比较多,调用的代码会充斥着很多拼装参数这样的代码,不太优雅。另外还有一个问题,通常为了安全起见,一些服务(或服务中的某些方法)可能要求认证后,才能调用,如果每个调用的地方,都要调用登录之类的服务来处理,这类与业务无关的代码就会大量侵入业务逻辑中,不好维护。
下面看看用feign如何改进:
一、添加依赖引用
compile 'org.springframework.cloud:spring-cloud-starter-feign'
二、定义feignClient接口
package com.cnblogs.yjmyzz.spring.cloud.study.service.client; import com.cnblogs.yjmyzz.spring.cloud.study.dto.UserDTO; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @FeignClient(name = "service-provider-demo", configuration = BasicAuthConfiguration.class) public interface UserFeignClient { @RequestMapping(value = "/user/{id}", method = RequestMethod.GET) UserDTO findUser(@PathVariable("id") Integer userId); }
这里面一个BasicAuthConfiguration类,也是自己写的
package com.cnblogs.yjmyzz.spring.cloud.study.service.client; import feign.auth.BasicAuthRequestInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class BasicAuthConfiguration { @Bean public BasicAuthRequestInterceptor basicAuthorizationInterceptor() { return new BasicAuthRequestInterceptor("app01", "passwd01"); } }
这上面的app01, passwd01,就是服务端分配的用户名及密码
附:服务提供方的application.yml中可参考下面这样设置
security: basic: enabled: true user: name: app01 password: passwd01
三、feignClient的使用
package com.cnblogs.yjmyzz.spring.cloud.study.service.controller; import com.cnblogs.yjmyzz.spring.cloud.study.dto.UserDTO; import com.cnblogs.yjmyzz.spring.cloud.study.service.client.UserFeignClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class OrderController { @Autowired private UserFeignClient userFeignClient; @GetMapping("/order/{userId}/{orderNo}") public String findOrder(@PathVariable Integer userId, @PathVariable String orderNo) { UserDTO user = userFeignClient.findUser(userId); if (user != null) { return user.getUserName() + " 的订单" + orderNo + " 找到啦!"; } return "用户不存在!"; } }
最后,main入口类上要增加一个注解
package com.cnblogs.yjmyzz.spring.cloud.study.service; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; @EnableDiscoveryClient @SpringBootApplication @EnableFeignClients public class ServiceConsumer { public static void main(String[] args) { SpringApplication.run(ServiceConsumer.class, args); } }
起作用的主要是@EnableFeignClients这个注解。