上一篇文章,讲述了Ribbon去做负载请求的服务消费者,本章讲述声明性REST客户端:Feign的简单使用方式

- Feign简介

Feign是一个声明式的Web服务客户端。这使得Web服务客户端的写入更加方便 。它具有可插拔注释支持,包括Feign注释和JAX-RS注释。Feign还支持可插拔编码器和解码器。Spring Cloud增加了对Spring MVC注释的支持,并HttpMessageConverters在Spring Web中使用了默认使用的相同方式。Spring Cloud集成了Ribbon和Eureka,在使用Feign时提供负载平衡的http客户端。

官方文档:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#spring-cloud-feign

- 准备工作

1.启动Consul,所有文章都将以Consul作为服务注册中心

2.创建 battcn-feign-hellobattcn-feign-hi

battcn-feign-hello

- pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

- BattcnFeignHelloApplication.java

1
2
3
4
5
6
7
8
@SpringBootApplication
@EnableDiscoveryClient
public class BattcnFeignHelloApplication { public static void main(String[] args) {
SpringApplication.run(BattcnFeignHelloApplication.class, args);
}
}

创建一个打招呼的的Controller

- HelloController.java

1
2
3
4
5
6
7
8
9
10
11
@RestController
@RequestMapping("/hello")
public class HelloController { @ResponseStatus(HttpStatus.OK)
@GetMapping
public String findStudentByName(@RequestParam("name") String name) {
// TODO:不做具体代码实现,只打印Log
return "挽歌- My Name's" + name;
}
}

- bootstrap.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
server:
port: 8765 spring:
application:
name: battcn-feign-hello
cloud:
consul:
host: localhost
port: 8500
enabled: true
discovery:
enabled: true
prefer-ip-address: true

访问:http://localhost:8765/hello?name=Levin

结果:挽歌- My Name's Levin 表示我们第一个服务运行一切正常

battcn-feign-hi

- pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

- BattcnFeignHiApplication.java

1
2
3
4
5
6
7
8
9
@EnableDiscoveryClient
@EnableFeignClients(basePackages = {"com.battcn.client"})
@SpringBootApplication
public class BattcnFeignHiApplication { public static void main(String[] args) {
SpringApplication.run(BattcnFeignHiApplication.class, args);
}
}

- HelloClient.java

创建一个声明式FeignClient的接口 HelloClient

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@FeignClient(name = "battcn-feign-hello")
public interface HelloClient { /**
* 在新版本中,Feign 已经可以解析 RestFul 标准的接口API,比如GET POST DELETE PATCH PUT
* 旧版中
* @RequestMapping(method = RequestMethod.GET, value = "/hello")
* 或者是
* @RequestMapping(method = RequestMethod.POST, value = "/hello")
*
* 早期文章:http://blog.battcn.com/2017/07/07/springcloud-feign-analysis/
* /
@ResponseStatus(HttpStatus.OK)
@GetMapping("/hello")
String findStudentByName(@RequestParam("name") String name);
}

- HiController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@RestController
@RequestMapping("/hi")
public class HiController { @Autowired
HelloClient helloClient; @ResponseStatus(HttpStatus.OK)
@GetMapping
public String find(@RequestParam("name") String name) {
// TODO:只是演示Feign调用的方法
return "Hi," + helloClient.findStudentByName(name);
}
}

- bootstrap.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
server:
port: 8766 spring:
application:
name: battcn-feign-hi
cloud:
consul:
host: localhost
port: 8500
enabled: true
discovery:
enabled: true
prefer-ip-address: true

访问:http://localhost:8766/hi?name=Levin

结果:Hi,挽歌- My Name'sLevin 表示第二个服务(hi)通过FeignClient调用服务(hello)一切正常

- 源码分析

org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(Object... args),该方法的 args 就是我们传递的请求参数

InvocableHandlerMethod

feign.Target -> HardCodedTarget 解析出 FeignClient中的 name 形成VIP模式 GET http://battcn-feign-hello/hello?name=Levin HTTP/1.1 然后发送请求

HardCodedTarget

feign.SynchronousMethodHandler -> this.client.execute(request, this.options);

第一个就是:feign.Request的一些信息,比如headermethodbodyurl 等一些基本属性,因为这里是feign的Request所以我们servlet中的请求头是无法传递过来的(下篇会讲述这写小技巧)

第二个就是:connectTimeoutMillis 和 readTimeoutMillis 很多人肯定不陌生

SynchronousMethodHandler

通过三面三个步骤,我们不难看出 Feign 就是解析注解然后发送HTTP(阻断器,OKHTTP模式留到下篇),有兴趣的可以自己Debug(如果该处有错误也希望各位指正)

服务消费者(Feign-上)的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. 创建服务消费者(Feign)

    概述 Feign 是一个声明式的伪 Http 客户端,它使得写 Http 客户端变得更简单.使用 Feign,只需要创建一个接口并注解.它具有可插拔的注解特性,可使用 Feign 注解和 JAX-RS ...

  7. 一起来学Spring Cloud | 第四章:服务消费者 ( Feign )

    上一章节,讲解了SpringCloud如何通过RestTemplate+Ribbon去负载均衡消费服务,本章主要讲述如何通过Feign去消费服务. 一.Feign 简介: Feign是一个便利的res ...

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

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

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

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

  10. 玩转SpringCloud(F版本) 二.服务消费者(2)feign

    上一篇博客讲解了服务消费者的ribbon+restTemplate模式的搭建,此篇文章将要讲解服务消费者feign模式的搭建,这里是为了普及知识 平时的项目中两种消费模式选择其一即可 本篇博客基于博客 ...

随机推荐

  1. Java实现 LeetCode 238 除自身以外数组的乘积

    238. 除自身以外数组的乘积 给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元 ...

  2. Java实现 蓝桥杯VIP 算法提高 最长公共子序列

    算法提高 最长公共子序列 时间限制:1.0s 内存限制:256.0MB 问题描述 给定两个字符串,寻找这两个字串之间的最长公共子序列. 输入格式 输入两行,分别包含一个字符串,仅含有小写字母. 输出格 ...

  3. Java实现 LeetCode 116 填充每个节点的下一个右侧节点指针

    116. 填充每个节点的下一个右侧节点指针 给定一个完美二叉树,其所有叶子节点都在同一层,每个父节点都有两个子节点.二叉树定义如下: struct Node { int val; Node *left ...

  4. svg 贝塞尔曲线图解(记录)

    path路径绘制中,绘制贝塞尔曲线的命令包括: Q 二次贝赛尔曲线 x1,y1 x,y T 平滑二次贝塞尔曲线 x,y C 曲线(curveto) x1,y1 x2,y2 x,y S 平滑曲线 x2, ...

  5. (十)HTTP.sys远程代码执行

    01 漏洞描述 上篇文章介绍了Host头攻击,今天我们讲一讲HTTP.sys远程代码执行漏洞. HTTP.sys是Microsoft Windows处理HTTP请求的内核驱动程序,为了优化IIS服务器 ...

  6. class 类前向声明

    /*   使用前向引用声明虽然可以解决一些问题,但它并不是万能的.需要注意的是,   尽管使用了前向引用声明,但是在提供一个完整的类声明之前,不能声明该类的对象,   也不能在内联成员函数中使用该类的 ...

  7. 简谈Java语言的继承

    Java语言的继承 这里简谈Java语言的三大特性之二——继承. Java语言的三大特性是循序渐进的.是有顺序性的,应该按照封装-->继承-->多态这样的顺序依次学习 继承的定义 百度百科 ...

  8. 一文带你快速搞懂动态字符串SDS,面试不再懵逼

    目录 redis源码分析系列文章 前言 API使用 embstr和raw的区别 SDSHdr的定义 SDS具体逻辑图 SDS的优势 更快速的获取字符串长度 数据安全,不会截断 SDS关键代码分析 获取 ...

  9. SwiftUI - iOS10本地推送通知教程UserNotifications在Swift中的实现方式

    简介 消息推送相信在很多人的眼里都不陌生了吧?像即时聊天微信,好友发信息给你时会在顶部弹下小窗口提醒你.也像是在影院APP预订了电影票,在开场前一小时你也会收到提醒.这类推送是需要经过后端发送请求的, ...

  10. css如何实现水平垂直居中

    方法 居中元素定宽高固定 PC兼容性 移动端兼容性 absolute + 负margin 是 ie6+, chrome4+, firefox2+ 安卓2.3+, iOS6+ absolute + ma ...