整合了Ribbon和Hystrix,只需创建一个接口并用注解@FeignClient
配置,就和服务提供方(如HELLO-SERVICE)绑定
//和HELLO-SERVICE这个名字的服务绑定.不区分大小写
@FeignClient(name="HELLO-SERVICE", fallback = HelloServiceFallback.class)
public interface HelloService {
@RequestMapping("/hello")
String hello();
@RequestMapping(value = "/hello1", method = RequestMethod.GET)
String hello(@RequestParam("name") String name) ;
@RequestMapping(value = "/hello2", method = RequestMethod.GET)
User hello(@RequestHeader("name") String name, @RequestHeader("age") Integer age);
@RequestMapping(value = "/hello3", method = RequestMethod.POST)
String hello(@RequestBody User user);
}
项目其他地方可以和用本地的service一样用这个
@Autowired
HelloService helloService;
当然 feign需要maven导入相关,和 开启
@EnableFeignClients//这个注解开启
@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
但是 可以发现HelloService
和服务提供方hollo工程的controller长得一样,而且DTO肯定也是重复的,而且这该属于服务提供方来定义
完全可以让hello工程来发布一个api的jar里面有公用的DTO,也有这个HelloService
接口,hello原来的contoller implements HelloService
, 服务消费方 改成
@FeignClient(value = "HELLO-SERVICE")
public interface HelloService extends 服务提供方发布的api.HelloService {
}
使用不变