feign组件作用
feign组件替代了restTemplate实现远程调用
RestTemplate调用存在的问题
快速入门
1.引入依赖
<!--开启基于feign的远程调用-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2.开启配置
@SpringBootApplication
@EnableFeignClients
@EnableSwagger2
@EnableDiscoveryClient
@MapperScan(basePackages = {
"com.zhw.orderservice.mapper"})
public class OrderApp {
public static void main(String[] args) {
SpringApplication.run(OrderApp.class, args);
}
}
3.编写Fegin客户端
//服务提供者名字
@FeignClient("service-user")
public interface UserClient {
//与服务提供者的方法一样
@GetMapping("/orderservice/tb-user/{id}")
public TbUser getUserById(@PathVariable("id") Integer id);
}
主要是基于SpringMVC的注解声明远程调用的信息,比如:
服务名称:service-user
请求方式:get
请求路径:orderservice/tb-user/{id}
请求参数:Integer id
返回值类型:TbUser
4.用feign代替RestTemplate
@GetMapping