Feign是Netflix开发的声明式、模板化的HTTP客户端, Feign可以帮助我们更快捷、优雅地实现微服务之间的调用。
一、项目编码
服务消费者content-center(内容中心微服务)向服务提供者user-center(用户中心微服务)发送请求,获取用户的微信昵称,我们通过Feign来实现该需求。
请求路径:http://localhost:8070/user/1
响应的json数据:
{id:"1",username:"admin",age:18}
1.1 在pom.xml中添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
1.2、在ContentCenterApplication中添加@EnableFeignClients注解
package com.example.cloud.contentcenter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class ContentCenterApplication {
public static void main(String[] args) {
SpringApplication.run(ContentCenterApplication.class, args);
}
}
1.3、新建FeignClient实现Feign远程请求
package com.example.cloud.contentcenter.feignclient;
import com.example.cloud.contentcenter.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(name="user-center")
public interface UserCenterFeignClient {
@GetMapping("/user/{id}")
public User findUserById( @PathVariable String id);
}
1.4、新建测试ContentController
package com.example.cloud.contentcenter.controller;
import com.example.cloud.contentcenter.entity.User;
import com.example.cloud.contentcenter.feignclient.UserCenterFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/content")
public class ContentController {
@Autowired
private UserCenterFeignClient userCenterFeignClient;
@GetMapping("/{id}")
public User testRequestUser(@PathVariable String id){
return userCenterFeignClient.findUserById(id);
}
}
1.5、测试效果
请求路径:http://localhost:8050/user/1
二、Feign自定义日志级别
级别 | 打印内容 |
NONE(默认值) | 不记录任何日志 |
BASIC | 仅记录请求方法、URL、请求状态码以及执行时间 |
HEADERS | BASIC级别的基础之上,记录请求和响应的header |
FULL | 记录请求和响应的header、body和元数据 |
2.1、在application.yml添加属性
#设置日志输出级别
logging:
level:
com.itmuch.contentcenter.feignclient.UserCenterFeignClient: debug
feign:
client:
config:
#想要调用的微服务的名称
user-center:
loggerLevel: full
2.2、测试效果
三、多参数请求
package com.example.cloud.contentcenter.feignclient;
import com.example.cloud.contentcenter.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(name="user-center")
public interface UserCenterFeignClient {
@GetMapping("/users")
public int insertUser(User user);
}
注意,在开发中可能会出现多个Feign接口访问同一个微服务的情况,解决方法:
在application.yml中添加属性配置
spring:
#解决Feign多个Client接口指向同一个微服务出现异常的问题
main:
allow-bean-definition-overriding: true
四、脱离Nacos单独使用Feign
4.1、新建FeignClient,请求百度
package com.example.cloud.contentcenter.feignclient;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(name="baidu",url="http://www.baidu.com/")
public interface BaiduFeignClient {
@GetMapping("")
public String baidu();
}
4.2、在测试Controller中调用FeignClient方法
@Autowired
private BaiduFeignClient baiduFeignClient;
@GetMapping("/baidu")
public String testRequestBaidu(){
return baiduFeignClient.baidu();
}
4.3、测试效果