Feign是Netflix开发的声明式、模块化的HTTP客户端。Feign可以帮我们更加便捷、优雅的调用HTTP API。
在Spring Cloud中,使用Feign非常简单,创建一个接口,并在接口上添加一些注解,代码就完成了。
基本使用
添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
增加配置文件
配置文件与之前类似,不再赘诉。
server:
port: 8081
eureka:
instance:
prefer-ip-address: false
client:
service-url:
defaultZone: http://peer1:8761/eureka,http://peer2:8762/eureka
spring:
application:
name: microservice-consumer-movie
修改启动类,增加@EnableFeignClients
@SpringBootApplication
@EnableFeignClients
public class MicroserviceConsumerMovieApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceConsumerMovieApplication.class, args);
}
}
增加Feign接口
/**
* 博客:https://lisen.cc
* Description:
*
* @Author: 香草物语
* DateTime: 2021-07-18 12:52
*/
@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient {
@RequestMapping(value = "{id}",method = RequestMethod.GET)
User findById(@PathVariable Long id);
}
调用
@RestController
public class MovieController {
@Resource
private DiscoveryClient discoveryClient;
// @Resource
// private RestTemplate restTemplate;
@Resource
private UserFeignClient userFeignClient;
// @Resource
// private LoadBalancerClient loadBalancerClient;
@GetMapping("/user-instance")
public List<ServiceInstance> showInfo() {
return discoveryClient.getInstances("microservice-provider-user");
}
@GetMapping("/user/{id}")
public User findById(@PathVariable Long id) {
// return restTemplate.getForObject("http://microservice-provider-user/"+id, User.class);
return userFeignClient.findById(id);
}
// @GetMapping("log/microservice-provider-user")
// public ServiceInstance userLog() {
// return loadBalancerClient.choose("microservice-provider-user");
// }
}
基于代码Feign自定义配置
创建配置类
创建Feign的配置类UserFeignConfig
,需要注意的是,该类不能添加@Configuration
注解,如果添加了@Configuration
注解,那么该类不能在主应用程序的@ComponentScan
注解扫描包内。
/**
* 博客:https://lisen.cc
* Description:feign配置文件
*
* @Author: 香草物语
* DateTime: 2021-07-18 15:39
*/
public class UserFeignConfig {
/**
* 将契约改成Feign原生的契约,这样就可以使用Feign自带的注解了
* @return
*/
@Bean
public Contract feignContract(){
return new Contract.Default();
}
}
修改Feign接口
/**
* 博客:https://lisen.cc
* Description:
*
* @Author: 香草物语
* DateTime: 2021-07-18 12:52
*/
@FeignClient(name = "microservice-provider-user",configuration = UserFeignConfig.class)
public interface UserFeignClient {
/**
* 使用Feign自带的注解 @RequestLine
* @param id
* @return
*/
@RequestLine("GET /{id}")
User findById(@Param("id") Long id);
}
测试
@RestController
public class MovieController {
@Resource
private DiscoveryClient discoveryClient;
// @Resource
// private RestTemplate restTemplate;
@Resource
private UserFeignClient userFeignClient;
// @Resource
// private LoadBalancerClient loadBalancerClient;
@GetMapping("/user-instance")
public List<ServiceInstance> showInfo() {
return discoveryClient.getInstances("microservice-provider-user");
}
@GetMapping("/user/{id}")
public User findById(@PathVariable Long id) {
// return restTemplate.getForObject("http://microservice-provider-user/"+id, User.class);
return userFeignClient.findById(id);
}
// @GetMapping("log/microservice-provider-user")
// public ServiceInstance userLog() {
// return loadBalancerClient.choose("microservice-provider-user");
// }
}
再次访问http://localhost:8081/user/1
,查看输出
全局配置
以上我们是根据服务进行配置的,我们也可以进行全局配置。在启动类中添加@EnableFeignClients
注解
@SpringBootApplication
@EnableFeignClients(defaultConfiguration = UserFeignConfig.class)
public class MicroserviceConsumerMovieApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceConsumerMovieApplication.class, args);
}
}
使用属性自定义Feign配置
配置指定名称的Feign Client
修改配置文件
server:
port: 8081
eureka:
instance:
prefer-ip-address: false
client:
service-url:
defaultZone: http://peer1:8761/eureka,http://peer2:8762/eureka
spring:
application:
name: microservice-consumer-movie
feign:
client:
config:
microservice-provider-user:
contract: feign.Contract.Default
修改Feign接口
@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient {
/**
* 使用Feign自带的注解 @RequestLine
* @param id
* @return
*/
@RequestLine("GET /{id}")
User findById(@Param("id") Long id);
}
配置全局Feign Client
修改配置文件,将指定名称的Feign Client改成default
即可。
server:
port: 8081
eureka:
instance:
prefer-ip-address: false
client:
service-url:
defaultZone: http://peer1:8761/eureka,http://peer2:8762/eureka
spring:
application:
name: microservice-consumer-movie
feign:
client:
config:
default:
contract: feign.Contract.Default
Feign对压缩的支持
可以通过以下配置对请求或相应进行压缩。
feign:
compression:
request:
enabled: true
mime-types: text/xml,application/xml,application/json
min-request-size: 2048
response:
enabled: true
配置Feign日志
feign:
client:
config:
default:
contract: feign.Contract.Default
logging-level: full
compression:
request:
enabled: true
mime-types: text/xml,application/xml,application/json
min-request-size: 2048
response:
enabled: true
logging:
level:
cc.lisen.microserviceconsumermovie.service.UserFeignClient: debug
评论 (0)