feign组件
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("getUserByEureka/{oid}")
private OrderWithUser getUserByEureka(@PathVariable Integer oid) {
//1.获取订单信息
TbOrders result = ordersService.getById(oid);
//2.远程调用获取订单的用户信息,很优雅只是调用方法
TbUser user = userClient.getUserById(result.getUid());
//3.将用户信息和订单信息封装
OrderWithUser orderWithUser = new OrderWithUser();
BeanUtils.copyProperties(result,orderWithUser);
orderWithUser.setUser(user);
//4.返回订单和用户信息
return orderWithUser;
}
自定义配置
配置日志
关于openfegin的日志可以参看这篇文章: https://www.cnblogs.com/maybesuch/p/12175002.html.
或者官方文档
方式一 配置文件
#开启servcie-user服务的日志级别,只针对service-user服务的配置
feign.client.config.service-user.logger-level=full
#针对全局的服务日志配置
feign.client.config.default.logger-level=full
#将接口所在的包的日志级别设置为debug级别
logging.level.com.zhw=debug
方式二 java代码的方式
1.定义配置类
package com.zhw.orderservice.config;
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//注意该类不需要加@Configuration注解
public class OpenFeignConfig {
@Bean
Logger.Level feignLoggerLevel(){
// 设置日志
return Logger.Level.FULL;
}
}
2.修改配置文件
#将接口所在的包的日志级别设置为debug级别
logging.level.com.zhw=debug
3.针对某一微服务的配置
package com.zhw.orderservice.client;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhw.base.entity.TbUser;
import com.zhw.orderservice.config.OpenFeignConfig;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(value = "service-user",configuration = OpenFeignConfig.class)
public interface UserClient {
@GetMapping("/orderservice/tb-user/{id}")
public TbUser getUserById(@PathVariable("id") Integer id);
}
4.全局的feign日志配置
package com.zhw.orderservice;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import com.netflix.loadbalancer.RoundRobinRule;
import com.zhw.orderservice.config.OpenFeignConfig;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
//定义 openfeign全局配置
@EnableFeignClients(defaultConfiguration = OpenFeignConfig.class)
@EnableSwagger2
@EnableDiscoveryClient
@MapperScan(basePackages = {"com.zhw.orderservice.mapper"})
public class OrderApp {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(OrderApp.class, args);
}
feign性能优化
1.引入依赖
<!-- openfeign默认不支持连接池,引入httpclient支持连接池子-->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
2.修改配置文件
#启用httpclient
feign.httpclient.enabled=true
# 配置总共有多少个连接
feign.httpclient.max-connections=200
# 每一个服务最大由多少个连接
feign.httpclient.max-connections-per-route=50