1、快速入门:
简介:Feign整合了Ribbon和Hystrix,除了提供这两者的强大功能之外,还提供了一种声明式的web服务客户端定义方式。
使用步骤:
1、引入依赖
2、在应用主类上添加@EnableFeignClients注解,如下:
package com.wuyonghu.feign;
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;
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
3、定义一个接口,使用@FeignClient注解来绑定服务:
package com.wuyonghu.feign.service;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
// 使用该注解,表示这里要使用哪个服务提供者
@FeignClient("wuyonghu-test-one")
public interface HelloService {
// 要调用该服务提供者的哪个方法
@RequestMapping("/hello")
String hello();
}
4、在controller中调用service中的方法:
package com.wuyonghu.feign.controller;
import com.wuyonghu.feign.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConsunerController {
@Autowired
HelloService helloService;
@RequestMapping(value = "/feign-consumer", method = RequestMethod.GET)
public String helloCustomer() {
return helloService.hello();
}
}