Feign的入门使用
定义:
注:截图来自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);
}
}
注意:
- 当使用Feign的时候,不能使用GetMapping一类注解,否则会报404错误
官方实例
- SpringBootApplication与ComponentScan注解同时间使用的时候SpringBootApplication注解扫描包的功能会失效,所以慎重使用。如果引用了Maven外部项目,一定要扫描包的话。ComponentScan注解中除了写外部项目的包名,还一定要写自身项目需要扫描的包名。