GENGEN
主页
vuepress
  • GIT命令
  • python+django
  • vue cli搭建项目
  • babel es6转换es5
  • docker aliyun配置
  • npm 配置
  • linux 常用命令
  • Ubuntu 下Linux 命令
  • github
  • gitee
  • csdn
  • 关于我
主页
vuepress
  • GIT命令
  • python+django
  • vue cli搭建项目
  • babel es6转换es5
  • docker aliyun配置
  • npm 配置
  • linux 常用命令
  • Ubuntu 下Linux 命令
  • github
  • gitee
  • csdn
  • 关于我
  • java基础

    • JDK8 函数式编程
    • JDK8 新特性之Date-Time
    • Servlet 源码分析
    • ArrayList 源码
    • LinkedList 源码
    • HashMap 源码
    • String 源码
    • BigDecimal 源码
    • java 类的加载
    • Class 源码
    • Synchronized锁升级
    • 事务的传播机制
    • knowledge
  • JAVA WEB

    • Java Servlet
    • 权限设计
    • logback日志的链路追踪
  • DATABASE

    • MySQL EXPLAIN详解
    • MySQL 索引
    • MySQL 表锁、行锁
    • MySQL ACID与transcation
    • 分布式事务
    • MySQL MVCC机制
    • Mysql 乐观锁与悲观锁
    • 分布式锁1 数据库分布式锁
    • 分布式锁2 Redis分布式锁
    • 分布式锁3 ZK分布式锁
  • SpringCloud

    • SpringCloud服务注册中心之Eureka
    • SpringCloud服务注册中心之Zookeeper
    • SpringCloud服务调用之Ribbon
    • SpringCloud服务调用之OpenFeign
    • SpringCloud服务降级之Hystrix
    • SpringCloud服务网关之Gateway
    • SpringCloud Config分布式配置中心
    • SpringCloud服务总线之Bus
    • SpringCloud消息驱动之Stream
    • SpringCloud链路追踪之Sleuth
    • SpringCloud Alibaba Nacos
    • SpringCloud Alibaba Sentinel
  • Spring

    • SpringBoot
    • Spring-data-jpa入门
    • SpringCloud问题
    • dispatcherServlet 源码分析
    • @SpringBootApplication注解内部实现与原理
    • spring启动初始化初始化
  • 中间件

    • 分布式协调服务器Zookeeper
    • 服务治理Dubbo
    • 分布式配置管理平台Apollo
    • 消息中间件框架Kafka
    • 分布式调度平台ElasticJob
    • 可视化分析工具Kibana
    • ElacticSearch 基础
    • ElacticSearch进阶
    • ElacticSearch集成
  • 环境部署

    • 应用容器引擎Docker
    • DockerCompose服务编排
    • 负载均衡Nginx
    • Nginx的安装配置
    • K8S基础
  • 代码片段

    • listener 监听模式
    • spingboot 整合redis
    • XSS过滤
    • profile的使用
    • ConfigurationProperties注解
  • 设计模式

    • 工厂模式
    • 单例模式
    • 装饰者模式
    • 适配器模式
    • 模板方法模式
    • 观察者模式
  • 读书笔记

    • 《Spring in Action 4》 读书笔记
    • 《高性能mysql》 读书笔记
  • NoSQL

    • Redis基础
    • Redis高级
    • Redis集群
    • Redis应用
  • MQ

    • rabbitMQ基础
    • rabbitMQ高级
    • rabbitMQ集群
  • JVM

    • JVM体系架构概述
    • 堆参数调整
    • GC 分代收集算法
    • JVM 垃圾回收器
    • JVM 相关问题
  • JUC

    • JUC总览
    • volatile关键字
    • CAS
    • ABA问题
    • collections包下线程安全的集合类
    • Lock 锁
    • LockSupport
    • AQS
    • Fork/Join分支框架
    • JUC tools
    • BlockingQueue 阻塞队列
    • Executor 线程池
    • CompletableFuture
    • 死锁以及问题定位分析
  • Shell

    • shell命令
    • shell基础
  • Activiti

    • IDEA下的Activiti HelloWord
    • 流程定义的CRUD
    • 流程实例的执行
    • 流程变量
  • VUE

    • vue基础
    • vue router
    • Vuex
    • Axios 跨域
    • dialog 弹出框使用
    • vue 动态刷新页面
    • vue 封装分页组件
    • vue 动态菜单
    • vue 常用传值
  • Solidity 智能合约

    • Solidity 基础
    • Solidity ERC-20
    • Solidity 101
  • English

    • 时态

服务调用——OpenFeign

OpenFeign 是什么

  • Feign是一个声名式WebService*客户端,使用Feign能让编写WebService更加简单。
  • 它的使用方法是定义一个服务接口然后在上面添加注解,Feign也支持可拔插式的编码器和解码器,SpringCloud对Feign进行了封装,使其支持SpringMVC标准注解和HttpMessageConverters,Feign可以与Eureka和Ribbon组合使用支持负载均衡。
  • Feign内部是Ribbon,是对Ribbon的增强。OpenFeign 是 Feign的升级。

OpenFeign 能干什么

  • 前面在使用Ribbon + RestTemplate时,利用RestTemplate对http请求封装,形成了一套模板化调用方法,但实际开发中,对于服务依赖的调用不止一处,**往往一个接口被多处调用,所以通常会针对每个微服务自行封装一些客户端来包装这些依赖服务的调用。**Feign在此基础上做了进一步封装,由他来帮我们定义和实现依赖服务的接口,我们只需要增加一个注解(@FeignClient),即可完成服务提供方的接口绑定,简化了使用springcloud Ribbon时,自动封装服务调用客户端的开发量。

OpenFeign使用步骤

  • 还是使用之前7001 7002两个Eureka注册中心以及8001 8002两个payment服务端。
  • feign 使用在客户端,用来对服务端的访问,新增一个Module,用来进行改造。
  • feign是自定义一个servcie 接口,通过service接口的@FeignClient指向在Eureka上注册的微服务名称,接口的方法和被调用服务提供的controller方法一致即可
  • pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud2020</artifactId>
        <groupId>com.ws.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-comsumer-feign-order80</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.ws.springcloud</groupId>
            <artifactId>api</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!--openFeign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>
  • yml
server:
  port: 8000


eureka:
  client:
    # 不注册进EUREKA  false
    register-with-eureka: false
    service-url:
      #设置与eureka交互的地址查询服务和注册服务需要依赖这个地址
      defaultZone: http://eureka7001:7001/eureka/,http://eureka7002:7002/eureka/

# 这里演示,就不注册进eureka中。
  • 入口类添加注解
@SpringBootApplication
@EnableFeignClients//feign注解
public class OrderFeign80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderFeign80.class,args);
    }
}
  • 新增接口
import com.ws.springcloud.common.result.CommonResult;
import com.ws.springcloud.entites.payment.Payment;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@FeignClient(value =  "CLOUD-PAYMENT-SERVICE")//CLOUD-PAYMENT-SERVICE为Eureka上注册的,payment服务端名词
@Component
public interface PaymentFeignService {
    //此处接口和payment服务端controller保持一致,路径也为payment服务端对外暴露的路径
    @GetMapping(value="/payment/get/{id}")
    CommonResult<Payment> getById(@PathVariable("id") Long id);

    @PostMapping(value = "/create")
    CommonResult create(@RequestBody Payment payment);

}
  • 客户端controller中使用
@RestController
@RequestMapping("/feign")
public class OrderFeignController {

    @Autowired
    private PaymentFeignService paymentFeignService;

    @GetMapping(value="/get/{id}")
    CommonResult<Payment> getById(@PathVariable("id") Long id){
        return paymentFeignService.getById(id);
    }
}

OpenFeign 的负载均衡

  • OpenFeign 默认自带负载均衡。

OpenFeign 超时控制

  • OpenFeign 默认的调用超时为1秒钟,如果调用过程超过了1秒,Feign客户端会直接返回报错,为了避免这种情况,我们有时需要对Feign客户端进行超时控制。
  • yml中配置改造
server:
  port: 8000


eureka:
  client:
    # 不注册🥇进EUREKA
    register-with-eureka: false
    service-url:
      #设置与eureka交互的地址查询服务和注册服务需要依赖这个地址
      defaultZone: http://eureka7001:7001/eureka/,http://eureka7002:7002/eureka/
ribbon:
  #建立连接所用时间,两端连接需要时间
  ReadTimeout: 5000
  # 建立连接从服务器获取可用资源的时间
  ConnectTimeout: 5000

OpenFeign 的日志增强

  • Feign提供了日志打印功能,我们可以通过配置调整日志级别,从而了解feign请求过程中http细节。

  • 说白了就是对feign接口调用的情况进行监控和输出

  • 客户端增加日志配置类

@Configuration
public class FeignConfig {
    @Bean
    Logger.Level feignLoggerLevel(){
        return Logger.Level.FULL;
    }
}
  • yml配置
server:
  port: 8000


eureka:
  client:
    # 不注册进EUREKA
    register-with-eureka: false
    service-url:
      #设置与eureka交互的地址查询服务和注册服务需要依赖这个地址
      defaultZone: http://eureka7001:7001/eureka/,http://eureka7002:7002/eureka/
ribbon:
  #建立连接所用时间,两端连接需要时间
  ReadTimeout: 5000
  # 建立连接从服务器获取可用资源的时间
  ConnectTimeout: 5000
logging:
  level:
    # 可以详细设置哪一个接口,也可以到包
    com.ws.springcloud.service.PaymentFeignService: debug
```
Last Updated:
Contributors: 88395515, lidagen
Prev
SpringCloud服务调用之Ribbon
Next
SpringCloud服务降级之Hystrix