1.使用feign进行服务间的调用

spring boot2X整合nacos一使用Feign实现服务调用

2.开启gzip压缩

Feign支持对请求与响应的压缩,以提高通信效率,需要在服务消费者配置文件开启压缩支持和压缩文件的类型

添加配置

feign.compression.request.enabled=true
feign.compression.response.enabled=true
feign.compression.request.mime-types=text/xml,application/xml,application/json
feign.compression.request.min-request-size=2048

3.开启日志

配置

logging.level.com.xyz.comsumer.feign.RemoteHelloService=debug

添加FeignLogConfig类

package com.xyz.comsumer.configure;

import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class FeignLogConfig {
@Bean
Logger.Level feignLogger(){
return Logger.Level.FULL;
}
}

输出

2019-12-07 23:23:03.630 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] <--- HTTP/1.1 200 (671ms)
2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] content-length: 14
2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] content-type: text/plain;charset=UTF-8
2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] date: Sat, 07 Dec 2019 15:23:03 GMT
2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] vary: Origin
2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] vary: Access-Control-Request-Method
2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] vary: Access-Control-Request-Headers
2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello]
2019-12-07 23:23:03.632 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] hello,provider
2019-12-07 23:23:03.632 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] <--- END HTTP (14-byte body)

说明:

  Feign日志记录只能响应DEBUG日志级别

对每一个Feign客户端,可以配置一个Logger.Level对象,通过该对象控制日志输出内容。

Logger.Level有如下几种选择:

NONE, 不记录日志 (默认)。

BASIC, 只记录请求方法和URL以及响应状态代码和执行时间。

HEADERS, 记录请求和应答的头的基本信息。

FULL, 记录请求和响应的头信息,正文和元数据。

4.替换JDK默认的URLConnection为okhttp

在默认情况下 spring cloud feign在进行各个子服务之间的调用时,http组件使用的是jdk的HttpURLConnection
服务之间调用使用的HttpURLConnection,效率非常低
为了提高效率,可以通过连接池提高效率
使用okhttp,能提高qps,因为okhttp有连接池和超时时间进行调优
添加依赖
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>

修改配置

禁用默认的http,启用okhttp

feign.okhttp.enabled=true
feign.httpclient.enabled=false

添加FeignOkHttpConfig类

package com.xyz.comsumer.configure;

import feign.Feign;
import okhttp3.ConnectionPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import java.util.concurrent.TimeUnit; @Configuration
@ConditionalOnClass(Feign.class)
@AutoConfigureBefore(FeignAutoConfiguration.class)
public class FeignOkHttpConfig { @Bean
public okhttp3.OkHttpClient okHttpClient(){
return new okhttp3.OkHttpClient.Builder()
.readTimeout(, TimeUnit.SECONDS)
.connectTimeout(, TimeUnit.SECONDS)
.writeTimeout(, TimeUnit.SECONDS)
.retryOnConnectionFailure(true)
.connectionPool(new ConnectionPool())
.build();
}
}

5.超时设置

Feign调用服务的默认时长是1秒钟

hystrix的超时时间

hystrix.command.default.execution.timeout.enabled=true
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000

说明:

  hystrix.command.default.execution.timeout.enabled 执行是否启用超时,默认启用true

  hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds 命令执行超时时间,默认1000ms

  常用的配置还有

    hystrix.command.default.execution.isolation.strategy 隔离策略,默认是Thread, 可选THREAD|SEMAPHORE,建议选择SEMAPHORE

Feign 的负载均衡底层用的就是 Ribbon

ribbon的超时时间

ribbon.ReadTimeout=
ribbon.ConnectTimeout=

6.使用hystrix进行熔断、降级处理

feign启用hystrix,才能熔断、降级

feign.hystrix.enabled=true

hystrix服务降级处理

RemoteHelloServiceFallbackImpl

package com.xyz.comsumer.feign.fallback;

import com.xyz.comsumer.feign.RemoteHelloService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; @Component
public class RemoteHelloServiceFallbackImpl implements RemoteHelloService {
private final Logger logger = LoggerFactory.getLogger(RemoteHelloServiceFallbackImpl.class); @Override
public String hello() {
logger.error("feign 查询信息失败:{}");
return null;
} }

Spring Cloud Feign高级应用的更多相关文章

  1. 微服务实战SpringCloud之Spring Cloud Feign替代HTTP Client

    简介 在项目中我们有时候需要调用第三方的API,微服务架构中这种情况则更是无法避免--各个微服务之间通信.比如一般的项目中,有时候我们会使用 HTTP Client 发送 HTTP 请求来进行调用,而 ...

  2. 笔记:Spring Cloud Feign Ribbon 配置

    由于 Spring Cloud Feign 的客户端负载均衡是通过 Spring Cloud Ribbon 实现的,所以我们可以直接通过配置 Ribbon 的客户端的方式来自定义各个服务客户端调用的参 ...

  3. 笔记:Spring Cloud Feign Hystrix 配置

    在 Spring Cloud Feign 中,除了引入了用户客户端负载均衡的 Spring Cloud Ribbon 之外,还引入了服务保护与容错的工具 Hystrix,默认情况下,Spring Cl ...

  4. 笔记:Spring Cloud Feign 其他配置

    请求压缩 Spring Cloud Feign 支持对请求与响应进行GZIP压缩,以减少通信过程中的性能损耗,我们只需要通过下面二个参数设置,就能开启请求与响应的压缩功能,yml配置格式如下: fei ...

  5. 笔记:Spring Cloud Feign 声明式服务调用

    在实际开发中,对于服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以我们通常会针对各个微服务自行封装一些客户端类来包装这些依赖服务的调用,Spring Cloud Feign 在此基础上做了进 ...

  6. 第六章:声明式服务调用:Spring Cloud Feign

    Spring Cloud Feign 是基于 Netflix Feign 实现的,整合了 Spring Cloud Ribbon 和 Spring Cloud Hystrix,除了提供这两者的强大功能 ...

  7. Spring Cloud Feign Ribbon 配置

    由于 Spring Cloud Feign 的客户端负载均衡是通过 Spring Cloud Ribbon 实现的,所以我们可以直接通过配置 Ribbon 的客户端的方式来自定义各个服务客户端调用的参 ...

  8. Spring Cloud feign

    Spring Cloud feign使用 前言 环境准备 应用模块 应用程序 应用启动 feign特性 综上 1. 前言 我们在前一篇文章中讲了一些我使用过的一些http的框架 服务间通信之Http框 ...

  9. 微服务架构之spring cloud feign

    在spring cloud ribbon中我们用RestTemplate实现了服务调用,可以看到我们还是需要配置服务名称,调用的方法 等等,其实spring cloud提供了更优雅的服务调用方式,就是 ...

随机推荐

  1. Java生鲜电商平台-定时器,定时任务quartz的设计与架构

    Java生鲜电商平台-定时器,定时任务quartz的设计与架构 说明:任何业务有时候需要系统在某个定点的时刻执行某些任务,比如:凌晨2点统计昨天的报表,早上6点抽取用户下单的佣金. 对于Java开源生 ...

  2. 小鸟初学Shell编程(八)环境变量、预定义变量与位置变量

    环境变量 环境变量:每个Shell打开都可以获得到的变量. 我们知道通过export的方式打开可以让子进程读取父进程的变量的值,那怎么样才能让每一个进程都能读取到变量的值呢? 在这呢,系统有一些默认的 ...

  3. 小鸟初学Shell编程(一)认识Shell

    开篇介绍 Linux里非常的有用的一个功能,这个功能就叫Shell脚本. Shell脚本在我日常开发工作里也占了非常重要的角色,项目中一些简单的工作我们都可以使用Shell脚本来完成,比如定时删除日志 ...

  4. warning: Unexpected unnamed function (func-names)

    warning: Unexpected unnamed function (func-names) 看到这个提示基本是就是说你的函数不能是匿名函数,最好可以起一个名字,然后你增加一个函数名称就好了 R ...

  5. maven 学习---Maven构建生命周期

    构建生命周期是一组阶段的序列(sequence of phases),每个阶段定义了目标被执行的顺序.这里的阶段是生命周期的一部分. 举例说明,一个典型的 Maven 构建生命周期是由以下几个阶段的序 ...

  6. kolla-ansible部署openstack allinone单节点

    环境准备 2 network interfaces 8GB main memory 40GB disk space 1.修改hostname hostnamectl set-hostname koll ...

  7. Python元组与字符串操作(9)——随机数、元组、命名元组

    随机数 import random #导入random模块 randint(a,b) 返回[a,b]之间的整数 random.randint(0,9) randrange([start],stop,[ ...

  8. 【餐厅】 What kind of food would you like to eat tonight?

    核心句型 What kind of food would you like to eat tonight? 你今晚想吃哪种菜? What would you like to eat ? 你想吃什么? ...

  9. windows 7系统下查看被占用的端口并解除占用

    运行输入cmd,在命令行输入 netstat -ano 查找所占用端口所在的行,如图本例子被占用端口为9999,记住对应的pid 然后输入 tasklist|findstr pid(此处为9528) ...

  10. 201871010108-高文利《面向对象程序设计(java)》第十三周学习总结

    项目 内容 这个作业属于哪个课程 <任课教师博客主页链接> https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 <作业链接地址> ht ...