微服务-Fegin

在之前我们两服务之间调用的时候用的是restTemplate,但是这个方式调用存在很多的问题

  String url = "http://userservice/user/" + order.getUserId();
  • 代码可读性差,编码体验不统一
  • 参数复杂的url难以维护

所以我们大力推出我们今天的主角--Fegin

       Feign是一个声明式的http客户端,其作用就是帮助我们优雅的实现http请求的发送,解决上面的问题

定义和使用Fegin客户端

1.引入依赖

 <!--feign客户端依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

2.在A服务的启动类中添加注解开启Figin的功能

@MapperScan("cn.itcast.order.mapper")
@SpringBootApplication
@EnableFeignClients//Feign的客户端
public class OrderApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }
}

3.编写Feign客户端:

@FeignClient(value = "userservice")
public interface UserClient {

    @GetMapping("/user/{id}")
    User findById(@PathVariable("id") Long id);
}

/*
基于SpringMVC的注解来声明远程调用的信息
服务名称:userservice
请求方式:GET
请求路径:/user/{id}
请求参数:Long id
返回值类型:User
*/

4.使用Fegin客户端代替RestTemplate


    @Autowired
    private UserClient userClient;

    public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
        // 2.用Feign远程调用
        User user = userClient.findById(order.getUserId());
        // 3.封装user到Order
        order.setUser(user);
        // 4.返回
        return order;
    }

Fegin的使用步骤

  • 引入依赖
  • 添加@EnableFeginClient注解
  • 编写FeginClient接口
  • 使用FeginClient中定义的方法代替RestTemplate

自定义配置来覆盖默认配置,可修改为:

类型 作用 说明
fegin.Logger.Decoder 修改日志级别 包含四种不同的级别:NONE、BASIC、HEADERS、FULL

feign.codec.Decoder

响应结果的解析器

http远程调用的结果做解析,例如解析json字符串为java对象

feign.codec.Encoder

请求参数编码

将请求参数编码,便于通过http请求发送

feign. Contract

支持的注解格式

默认是SpringMVC的注解

feign. Retryer

失败重试机制

请求失败的重试机制,默认是没有,不过会使用Ribbon的重试

自定义Fegin的配置

一般配置Fegin日志一般有两种方式:

方法一:

1.全局生效:

feign:
  client:
    config: 
      default: #这里default就是全局变量,如果是写服务名称,则是针对某个微服务的配置
        loggerLevel: FULL #日志级别

2.局部生效

feign:
  client:
    config: 
      userservice: #这里userseervice就是局部变量
        loggerLevel: FULL #日志级别

方法二(java代码方式):

public class DefaultFeignConfiguration {
    @Bean
    public Logger.Level logLevel(){
        return Logger.Level.BASIC;
    }
}

1.如果是全局配置,则将它放到@EnableFeginClients这个注解中:

@EnableFeignClients(clients = UserClient.class,defaultConfiguration = DefaultFeign

2.如果是局部配置,则把它放到@FeginClient这个注解中:

@FeignClient(value = "userservice",configuration = DefaultFeignConfiguration.class)

Fegin性能优化

Fegin的底层的客户端实现:

  • URLConnection:默认实现,不支持连接池
  • Apache HttpClient:支持连接池
  • OKHttp:支持连接池

优化Fegin的性能主要包括:

  • 使用连接池代替默认的URLConnection
  • 日志级别,最好用basic或者none

Fegin添加FttpClient的支持:

引入依赖:

 <!--引入HttpClient依赖-->
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-httpclient</artifactId>
        </dependency>

配置连接池:

feign:
  client:
    config:
      default: #这里default就是全局变量,如果是写服务名称,则是针对某个微服务的配置
        loggerLevel: FULL #日志级别
  httpclient:
    enabled: true # 支持HttpClient的开关
    max-connections: 200 # 最大连接数
    max-connections-per-route: 50 # 单个路径的最大连接数

Fegin的最佳实践

@EnableFeignClients(clients ="com.ffyc.fegin.clients")

       如果按照第二种方法将Fegin客户端独立出去,在启动SpringBootAppliaction的扫描包范围时,这些Fegin客户端无法使用,所以我们需要去解决这个问题

方式一(指定FeginClient所在包):

@EnableFeignClients(clients ="ffyc.com.fegin.clients")

方式二(指定FeginClient字节码):

@EnableFeignClients(clients ={ UserClient.class})

猜你喜欢

转载自blog.csdn.net/dfdbb6b/article/details/132340949
今日推荐