Spring cloud Feign

整合了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 {
}

使用不变

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容