概述

Feign 是一个声明式的伪 Http 客户端,它使得写 Http 客户端变得更简单。使用 Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用 Feign 注解和 JAX-RS 注解。Feign 支持可插拔的编码器和解码器。Feign 默认集成了 Ribbon,并和 Eureka 结合,默认实现了负载均衡的效果

  • Feign 采用的是基于接口的注解

  • Feign 整合了 ribbon和hystrix

创建服务消费者

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

       <!--spring cloud starter feign-->
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-feign</artifactId>
       </dependency>

       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>

主要增加对Feign的依赖

Application

通过@EnableFeignClients注解开启Feign功能

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class FeignApplication {

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

application.yml

server:
port: 9002

eureka:
client:
  serviceUrl:
    defaultZone: http://localhost:1111/eureka/
instance:
  lease-renewal-interval-in-seconds: 10 #服务续约
  lease-expiration-duration-in-seconds: 15 #服务剔除>服务续约时间
  hostname: localhost
  instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
spring:
application:
  name: eureka-client-feign
feign:
client:
  config:
    eureka-provider:
    #配置feign日志级别
      logger-level: full
 #开启feign对hystrix的支持,默认为false
hystrix:
  enabled: true
logging:
level:
#必须设置feign的服务包日志级别为debug
  com.ley.springcloud.feign.service: debug

创建Feign接口

通过@FeignClient("服务名")注解来指定调用哪个服务

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(name = "eureka-provider")
public interface HelloService {

   @GetMapping("/hello")
   String hello();
}

创建测试用的Controller

import com.ley.springcloud.feign.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class FeignController {

   @Autowired
   private HelloService helloService;

   @GetMapping("/feign/hello")
   public String hello() {
       return helloService.hello();
  }
}

创建服务消费者(Feign)的更多相关文章

  1. SpringCloud学习系列之二 ----- 服务消费者(Feign)和负载均衡(Ribbon)使用详解

    前言 本篇主要介绍的是SpringCloud中的服务消费者(Feign)和负载均衡(Ribbon)功能的实现以及使用Feign结合Ribbon实现负载均衡. SpringCloud Feign Fei ...

  2. springcloud-Netflix创建服务消费者

    目录 springcloud-Netflix创建服务消费者 Ribbon 创建服务消费者-Ribbon方式 ribbon的架构 Feign 创建包和基本项目结构 创建Feign访问服务的接口和访问co ...

  3. 创建服务消费者(Ribbon)

    概述 在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于 http restful 的.Spring cloud 有两种服务调用方式,一种是 ribbon + restTempla ...

  4. Spring Cloud学习笔记【三】服务消费者Feign

    Feign 是一个声明式的 Web Service 客户端,它的目的就是让 Web Service 调用更加简单.它整合了 Ribbon 和 Hystrix,从而让我们不再需要显式地使用这两个组件.F ...

  5. SpringCloud-创建服务消费者-Feign方式(附代码下载)

    场景 SpringCloud-服务注册与实现-Eureka创建服务注册中心(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...

  6. Spring Cloud(四)服务提供者 Eureka + 服务消费者 Feign

    上一篇文章,讲述了如何通过RestTemplate + Ribbon去消费服务,这篇文章主要讲述如何通过Feign去消费服务. Feign简介 Feign是一个声明式的伪Http客户端,它使得写Htt ...

  7. 服务消费者Feign和Ribbon的区别

    1.Ribbon通过注解@EnableEurekaClient/@EnableDiscoveryClient向服务中心注册:    PS:选用的注册中心是eureka,那么就推荐@EnableEure ...

  8. Spring Cloud (4) 服务消费者-Feign

    Spring Cloud Feign Spring Cloud Feign 是一套基于Netflix Feign实现的声明式服务调用客户端.它使得编写Web服务客户端变得更加简单,我们只需要创建接口并 ...

  9. 8、服务发现&服务消费者Feign

    spring cloud的Netflix中提供了两个组件实现软负载均衡调用,分别是Ribbon和Feign.上一篇和大家一起学习了Ribbon. Ribbon :Spring Cloud Ribbon ...

随机推荐

  1. 【35.39%】【hdu 3333】Turing Tree

    Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...

  2. 人生不过一个字【Life is but a word】

    有人说,人生是一出悲悲喜喜的戏,是一首飘飘忽忽的歌,是一次上去又下来的登山运动,是一场明明灭灭的烟花……是的,有道理,但听起来,太玄虚:悟起来,太深远:做起来,太复杂.              我说 ...

  3. Real-time storage area network

    A cluster of computing systems is provided with guaranteed real-time access to data storage in a sto ...

  4. npm与cnpm混用导致的问题

    npm和cnpm混用之后,再用npm升级模块导致如下错误: 解决办法: 删除node_modules文件下的文件后,重新执行npm install

  5. 西门子S7报文解析

    1.报文的基本格式 1.1 第1和第2个字节是:固定报文头03 00,这里我们就用到三种报文: a.初始化 b. 读 c.写,都是这种格式: 1.2 第3和第4个字节是:整个报文的长度: 其它部分就是 ...

  6. Maven环境下Poi的使用

    Poi的使用方法,网上还是挺多的,官网也有教程. 附一个比较全的: http://www.cnblogs.com/vTree/archive/2011/11/30/2268721.html 需要说明的 ...

  7. Expression Design与Blend制作滚动的小球动画教程

    原文:Expression Design与Blend制作滚动的小球动画教程 一,开发工具 Microsoft Expression Design & Blend 4.0 (3.0亦可). 这两 ...

  8. sql service添加索引

    语法:CREATE [索引类型] INDEX 索引名称ON 表名(列名)WITH FILLFACTOR = 填充因子值0~100GO /*实例*/USE 库名GOIF EXISTS (SELECT * ...

  9. matlab 工具函数 —— axnote(在坐标轴上写文本内容)

    function axnote(string) font_size = get(0, 'DefaultAxesFontSize'); if 1 h1 = text(0.99, 0.05, string ...

  10. ASP.NET CORE系列【六】Entity Framework Core 之数据迁移

    原文:ASP.NET CORE系列[六]Entity Framework Core 之数据迁移 前言 最近打算用.NET Core写一份简单的后台系统,来练练手 然后又用到了Entity Framew ...