1.引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2.在启动类上添加注解
@EnableFeignClients
3.编写FeignClient接口
@FeignClient("服务名称")
public interface 接口名称 {
@GetMapping("请求路径")
返回值类型 方法名称(@PathVariable("参数") 请求参数);
}
例如:
@FeignClient("userservice")
public interface UserClient {
@GetMapping("/user/{id}")
User findById(@PathVariable("id") Long id);
}
•
服务名称:
userservice
•
请求方式:
GET
•
请求路径:
/user/{id}
•
请求参数:
Long id
•
返回值类型:
User
4.使用Feign发起http请求
4.1注入UserClient的实例
4.2使用 接口名称.方法名称(请求参数) 发起http请求
5.配置Feign日志(配置文件方式)
feign:
client:
config:
default: # 这里用default就是全局配置,如果是写服务名称,则是针对某个微服务的配置
loggerLevel: FULL # 日志级别
以上日志级别配置为FULL极大的影响性能,推荐最好配置为basic