Spring Cloud 对 Feign 进行了封装,集成了 Ribbon 并结合 Eureka 可以实现客户端的负载均衡,Spring Cloud 实现的 Feign 客户端类名为 LoadBalancerFeignClient,在该类中,维护着与 SpringClientFactory 相关的实例,通过SpringClientFactory 可以获取负载均衡器,负载均衡器会根据一定的规则来选取处理请求的服务器,最终实现负载均衡功能。

集成了 Hystrix 增加了服务容错处理,并为 Feign 的使用提供了各种默认属性,例如编码器、解码器、日志、注解翻译器、实例创建者和客户端,我们可以简单的使用 Feign,也可以增加自定义的配置,下面我们在 Spring Cloud 中使用 Feign 示例如下:

  • 创建项目

    创建名称为 spring-cloud-feign-client 的 Spring Cloud 项目,修改 POM.xml 中增加以下依赖项:

    <?xmlversion="1.0"encoding="UTF-8"?>

    <projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

     
     

    <groupId>org.lixue</groupId>

    <artifactId>spring-cloud-feign-client</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <packaging>jar</packaging>

     
     

    <name>spring-cloud-feign-client</name>

    <description>DemoprojectforSpringBoot</description>

     
     

    <parent>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-parent</artifactId>

    <version>1.5.4.RELEASE</version>

    <relativePath/><!--lookupparentfromrepository-->

    </parent>

     
     

    <properties>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

    <java.version>1.8</java.version>

    <spring-cloud.version>Dalston.SR5</spring-cloud.version>

    </properties>

     
     

    <dependencies>

    <dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-eureka</artifactId>

    </dependency>

    <dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-feign</artifactId>

    </dependency>

    <dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-test</artifactId>

    <scope>test</scope>

    </dependency>

    </dependencies>

    <dependencyManagement>

    <dependencies>

    <dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-dependencies</artifactId>

    <version>${spring-cloud.version}</version>

    <type>pom</type>

    <scope>import</scope>

    </dependency>

    </dependencies>

    </dependencyManagement>

    <build>

    <plugins>

    <plugin>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-maven-plugin</artifactId>

    </plugin>

    </plugins>

    </build>

    </project>

     
     

  • 创建 Feign 接口

    在 Spring Cloud 中使用 Feign ,需要使用的是 Spring Cloud 的相关注解,例如 @RequestMapping、@RequestParam 等,而不是 Feign 的注解,创建 HelloWorldClient 接口,使用 @FeignClient 注解来标注服务名称(调用服务的 spring.application.name 属性),使用 @RequestMapping 来标注 REST 服务的相关属性;@RequestParam 和 @PathVariable 注解标注请求参数

    package org.lixue;

     
     

    import org.springframework.cloud.netflix.feign.FeignClient;

    import org.springframework.http.MediaType;

    import org.springframework.web.bind.annotation.PathVariable;

    import org.springframework.web.bind.annotation.RequestMapping;

    import org.springframework.web.bind.annotation.RequestMethod;

    import org.springframework.web.bind.annotation.RequestParam;

     
     

    @FeignClient("helloworld-provider")

    public interface HelloWorldClient{

    @RequestMapping(path="/speak?body={body}",method=RequestMethod.GET)

    String speak(@RequestParam(value="body",required=false) String body);

     
     

    @RequestMapping(path="/person/{personId}",method=RequestMethod.GET,

    produces=MediaType.APPLICATION_JSON_UTF8_VALUE)

    Person findById(@PathVariable("personId")Integer personId);

     
     

    @RequestMapping(path="/person/create",method=RequestMethod.POST,

    consumes=MediaType.APPLICATION_JSON_UTF8_VALUE,

    produces=MediaType.APPLICATION_JSON_UTF8_VALUE)

    ReturnValue create(Person person);

    }

     
     

  • 启动类启用 Feign

    Feign 集成了 Ribbon 并结合 Eureka 可以实现客户端的负载均衡,因此在启动类我们需要启动 Eureka客户端和 Feign,使用 @EnableEurekaClient 启用 Eureka 客户端;使用 @EnableFeignClients 启用 Feign

    package org.lixue;

     
     

    import org.springframework.boot.SpringApplication;

    import org.springframework.boot.autoconfigure.SpringBootApplication;

    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

    import org.springframework.cloud.netflix.feign.EnableFeignClients;

     
     

    @SpringBootApplication

    @EnableEurekaClient

    @EnableFeignClients

    public class SpringCloudFeignClientApplication{

    public static void main(String[]args){

    SpringApplication.run(SpringCloudFeignClientApplication.class,args);

    }

    }

     
     

  • 增加配置

    Feign 集成了 Ribbon 并结合 Eureka 可以实现客户端的负载均衡,因此需要增加 Eureka 相关配置,指定 eureka 服务注册中心的地址

    ##配置应用名称

    spring:

    application:

    name:spring-cloud-feign-client

    #配置服务端口

    server:

    #设置eureka服务注册中心的地址,如果多个以逗号分割

    eureka:

    client:

    service-url:

    defaultZone:http://eurekaserver01:9000/eureka/,http://eurekaserver02:9000/eureka/

     
     

  • 创建 REST 服务

    在调用服务时,只需要使用 @Autowired 注解标注 Feign 接口 HelloWorldClient , Spring Cloud 会帮我们创建和维护客户端实例

    package org.lixue;

     
     

    import org.springframework.beans.factory.annotation.Autowired;

    import org.springframework.http.MediaType;

    import org.springframework.web.bind.annotation.*;

     
     

    @RestController

    public class InvokerController{

     
     

    @Autowired

    private HelloWorldClient helloWorldClient;

     
     

    @RequestMapping(path="/invokerSpeak",method=RequestMethod.GET)

    public String invokerSpeak(@RequestParam(name="body",required=false)String body){

    return helloWorldClient.speak(body);

    }

     
     

    @RequestMapping(path="/invokerFindByID/{personID}",method=RequestMethod.GET,

    produces=MediaType.APPLICATION_JSON_UTF8_VALUE)

    public Person invokerFindByID(@PathVariable("personID")Integer personID){

    return helloWorldClient.findById(personID);

    }

     
     

    @RequestMapping(path="/invokerCreate",method=RequestMethod.GET,

    produces=MediaType.APPLICATION_JSON_UTF8_VALUE)

    ReturnValue invokerCreate(){

    Person newPerson=new Person();

    newPerson.setId(3434);

    newPerson.setAge(34);

    newPerson.setName("343434");

    newPerson.setMessage("33333333333333333");

    return helloWorldClient.create(newPerson);

    }

    }

     
     

  • 测试验证

    启动 eureka-server 和 service-provider 项目,启动该项目后,访问地址 http://localhost:8070/invokerSpeak 可以查看到访问,如果启动了多个 service-provider ,多次刷新可以看到,具体的服务调用被轮询调用。

日志配置

启用服务的 Feign 日志,增加配置 Bean 如下:

package org.lixue;

 
 

import feign.Logger;

import org.springframework.context.annotation.Bean;

 
 

public class MyFeignConfiguration{

@Bean

publicLogger.Level level(){

returnLogger.Level.FULL;

}

}

在 Feign 接口的 @FeignClient 注解中,增加 configuration 指向到创建的配置类:

@FeignClient(value="helloworld-provider",configuration=MyFeignConfiguration.class)

public interface HelloWorldClient{

//其他代码如上

}

修改 src/main/resources 目录下的 application.yml 增加日志级别配置:

logging:

level:

org.lixue.HelloWorldClient: debug

 
 

此时就可以看到请求日志的输出,如下:

2018-04-06 12:43:24.578 DEBUG 3968 --- [nio-8070-exec-4] org.lixue.HelloWorldClient : [HelloWorldClient#speak] ---> GET http://helloworld-provider/speak HTTP/1.1

2018-04-06 12:43:24.581 DEBUG 3968 --- [nio-8070-exec-4] org.lixue.HelloWorldClient : [HelloWorldClient#speak] ---> END HTTP (0-byte body)

2018-04-06 12:43:25.112 DEBUG 3968 --- [nio-8070-exec-4] org.lixue.HelloWorldClient : [HelloWorldClient#speak] <--- HTTP/1.1 200 (529ms)

2018-04-06 12:43:25.112 DEBUG 3968 --- [nio-8070-exec-4] org.lixue.HelloWorldClient : [HelloWorldClient#speak] content-length: 21

2018-04-06 12:43:25.113 DEBUG 3968 --- [nio-8070-exec-4] org.lixue.HelloWorldClient : [HelloWorldClient#speak] content-type: text/plain;charset=UTF-8

2018-04-06 12:43:25.113 DEBUG 3968 --- [nio-8070-exec-4] org.lixue.HelloWorldClient : [HelloWorldClient#speak] date: Fri, 06 Apr 2018 04:43:25 GMT

2018-04-06 12:43:25.113 DEBUG 3968 --- [nio-8070-exec-4] org.lixue.HelloWorldClient : [HelloWorldClient#speak] x-application-context: helloworld-provider:8001

2018-04-06 12:43:25.113 DEBUG 3968 --- [nio-8070-exec-4] org.lixue.HelloWorldClient : [HelloWorldClient#speak]

2018-04-06 12:43:25.118 DEBUG 3968 --- [nio-8070-exec-4] org.lixue.HelloWorldClient : [HelloWorldClient#speak] hello world port:8001

2018-04-06 12:43:25.118 DEBUG 3968 --- [nio-8070-exec-4] org.lixue.HelloWorldClient : [HelloWorldClient#speak] <--- END HTTP (21-byte body)

 
 

默认配置

Spring Cloud 为 Feign 的使用提供了各种默认属性,默认情况下,Spring 将会为 Feign 的属性提供以下 Bean 来实现 Feign 的调用

Bean 类型

Bean 名称

实现类

说明

Decoder

feignDecoder

ResponseEntityDecoder

解码器

Encoder

feignEncoder

SpringEncoder

编码器

Logger

feignLogger

Slf4jLogger

日志

Contract

feignContract

SpringMvcContract

注解翻译器

Feign.Builder

feignBuilder

HystrixFeign.Builder

Feign 实例创建者

Client

feignClient

  

Feign 客户端,启用 Ribbon 实现类为LoadBalancerFeignClient 否则为 Feign 默认实现类

Logger.Level

自命名

没有默认提供,只需要提供该类型的 Bean 即可

Retryer

自命名

没有默认提供,只需要提供该类型的 Bean 即可

ErrorDecoder

自命名

没有默认提供,只需要提供该类型的 Bean 即可

Request.Options

自命名

没有默认提供,只需要提供该类型的 Bean 即可

RequestInterceptor

自命名

没有默认提供,只需要提供该类型的 Bean 即可

 
 

Spring Cloud(Dalston.SR5)--Feign 声明式REST客户端的更多相关文章

  1. Spring Cloud(Dalston.SR5)--Feign 与 Hystrix 断路器整合

    创建项目 要使 Feign 与 Hystrix 进行整合,我们需要增加 Feign 和 Hystrix 的依赖,修改 POM.xml 中增加以下依赖项如下: <?xmlversion=" ...

  2. Spring Cloud第七篇 | 声明式服务调用Feign

    本文是Spring Cloud专栏的第七篇文章,了解前六篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Cloud ...

  3. Spring Cloud(Dalston.SR5)--Hystrix 断路器

    Spring Cloud 对 Hystrix 进行了封装,使用 Hystrix 是通过 @HystrixCommand 注解来使用的,被 @HystrixCommand 注解标注的方法,会使用 Asp ...

  4. Spring Cloud(Dalston.SR5)--Config 集群配置中心-刷新配置

    远程 SVN 服务器上面的配置修改后,需要通知客户端来改变配置,需要增加 spring-boot-starter-actuator 依赖并将 management.security.enabled 设 ...

  5. Spring Cloud(Dalston.SR5)--Config 集群配置中心

    Spring Cloud Config 是一个全新的项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,他分为服务端和客户端两个部分.服务端也称为分布式配置中心,是一个独立的微服务 ...

  6. Spring Cloud(Dalston.SR5)--Zuul 网关-微服务集群

    通过 url 映射的方式来实现 zuul 的转发有局限性,比如每增加一个服务就需要配置一条内容,另外后端的服务如果是动态来提供,就不能采用这种方案来配置了.实际上在实现微服务架构时,服务名与服务实例地 ...

  7. Spring Cloud(Dalston.SR5)--Hystrix 监控

    在服务调用者加入 Actuator ,可以对服务调用者的健康情况进行实时监控,例如,断路器是否打开.当前负载情况等. 服务调用者 需要增加 actuator依赖, 修改 POM.xml 中增加以下依赖 ...

  8. Spring Cloud(Dalston.SR5)--Ribbon 中间层负载均衡

    Spring Cloud 集成了 Ribbon 并结合 Eureka 可以实现客户端的负载均衡,使用 @LoadBalanced 修饰的 RestTemplate 类拥有了负载均衡功能,在 Sprin ...

  9. Spring Cloud(Dalston.SR5)--Eureka 服务提供者

    要使微服务应用向注册中心发布自己,首先需要在 pom.xml 配置文件中增加对 spring-boot-starter-eureka 的依赖,然后在主类中增加 @EnableDiscoveryClie ...

随机推荐

  1. git解决not a git repository

    意思是说没有库,需要你创建 git init zzz zzz文件夹就会出现在你的项目中,里面就会有.git文件,将里面的.git剪切到与项目同一级中 关注微信小程序

  2. 编译Thrift支持golang

    本文已经是很久以前的文章了,也不知道新版本thrift如何 Thrift是一个跨语言的服务部署框架,Thrift通过一个中间语言(IDL, 接口定义语言)来定义RPC的接口和数据类型,然后通过一个编译 ...

  3. CodeForces - 1097F:Alex and a TV Show (bitset & 莫比乌斯容斥)

    Alex decided to try his luck in TV shows. He once went to the quiz named "What's That Word?!&qu ...

  4. Mac电脑使用:您的安全性偏好设置仅允许安装来自App Store和被认可的开发者的应用(解决方法)

    在10ms加速器上下载了MAC客户端ShadowsocksX,打开软件,提示打不开,因为来自身份不明的开发者. 以下为解决办法: Step1:打开dock栏里面的“系统偏好设置” Step2:在系统偏 ...

  5. hello1.java内容简单介绍

    双击该Hello.java文件以查看它. 在Hello类,称为管理bean类,提供了getter和setter方法name中的Facelets页面表达式中使用属性.默认情况下,表达式语言引用类名,第一 ...

  6. Ubuntu端口命令---查看端口占用及关闭

    Ubuntu查看端口使用情况,使用netstat命令: 查看已经连接的服务端口(ESTABLISHED) netstat -a 查看所有的服务端口(LISTEN,ESTABLISHED) netsta ...

  7. 获奖感想和JAVA阶段性学习总结

    一.获奖感想 事实上,这次能够获得小黄衫,实在是出乎我的意料.毕竟班级中还有不少比我优秀的人,但我不会妄自菲薄.我知道,这件小黄衫不仅仅是老师对我的奖励,更是对我的一种鞭策,一种激励.它要求我要在以后 ...

  8. WINDOWS系统的正确安装-硬盘格式如何选择

    有一种这样的说法,WIN7改装WIN10必须要重新分区,将硬盘格式化为GPT格式(GUID分区表 ), WIN10改装WIN7必须要重新分区,将硬盘格式化为MBR格式. 这种说法一直困扰着我,于是经过 ...

  9. crontab 例子

    一个简单的 crontab 示例 0,20,40 22-23 * 7 fri-sat /home/ian/mycrontest.sh 在这个示例中,我们的命令在 7 月的每个星期五和星期六晚上 10 ...

  10. 【git】git使用

    1.创建github账户 网站:https://github.com/ 注册省略 2.ssk-key客户端配置 作用:不用每次push,clone代码不需要输入用户名+密码 生成ssh-key ssh ...