Spring Cloud 为开发者提供了在分布式系统中的一些常用的组件(例如配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁定,决策竞选,分布式会话集群状态)。使用Spring Cloud开发人员可以快速地完成实现这些模式的服务和应用程序。它们在任何分布式环境中都能很好地工作

Feign

注解式的 Feign 使得 Java HTTP 客户端编写更方便。Feign 灵感来源于安卓网络编程框架 RetrofitJAXRS-2.0 和 WebSocket,支持可插拔编码器和解码器,降低 HTTP API 的复杂度,通过最少的资源和代码来实现和 HTTP API 的连接。通过可定制的解码器和错误处理,可以编写任意的HTTP API。Spring Cloud Feign 封装了 Ribbon 这一组件,所以在使用 Feign 同时还能提供负载均衡的功能,这一切只需要一个 @FeignClient 即可完成。

早期版本的 Feign 被 Spring Cloud 团队集成在 spring-cloud-netflix 子项目下,但如今 Spring Cloud 团队将 Spring Cloud Feign独立成一个单独的 spring-cloud-openfeign 项目

Try

准备三个工程,分别是 eureka-serverorder-serverproduct-server

Eureka Server

详情参考第一章,或从文末的 GITHUB 链接获取对应篇幅的完整代码

Product Server

一个普通的 Eureka Client 即可,详情参考上一章,或从文末的 GITHUB 链接获取对应篇幅的完整代码

Order Server

这个例子也是在上一章的基础之上做了扩展

依赖

对比上一章,此处多了一个 spring-cloud-starter-openfeign 的依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>

配置文件

在 src/main/resources 目录下创建一个 bootstrap.yml 的文件,写上 eureka 相关配置信息

1
2
3
4
5
6
7
8
9
10
11
12
server:
port: 7072
spring:
application:
name: order-server
eureka:
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}
client:
service-url:
defaultZone: http://localhost:7071/eureka/

ProductClient 接口

创建一个 ProductClient ,是不是感觉和 XxxxService 看起来类似(用法都类似),都是接口文件只不过在这个文件的上方多了一个 @FeignClient 注解,多种写法,总有一款适合你

  • name:指定 FeignClient 的名称,该属性会作为微服务的名称,用于服务发现
  • value:同 name 字段互通
  • serviceId:指定服务ID,每个注册到注册中心上的客户端都会有对应的 serviceId 一般是 spring.application.name,与 name 和 value 互通
  • url: 一般用于调试,可以指定一个详细地址(http://localhost:8080/products)
  • path: 请求统一路径,可以看成 @RequestMapping("/products")
  • decode404:404 错误时,调用 decoder 进行解码,否则抛出 FeignException
  • fallback:发生错误时,回调 hystrix 类/方法(后面会详细介绍)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.battcn.api;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; /**
* @author Levin
* @since 2018/9/26 0026
*/
@FeignClient(name = "product-server/products", decode404 = true)
//@FeignClient(name = "products", url = "http://localhost:7073/products")
//@FeignClient(value = "product", serviceId = "product-server", path = "/products", decode404 = true)
public interface ProductClient { /**
* 根据产品ID查询产品信息
*
* @param productId ID
* @return 查询结果
*/
@GetMapping("/{product_id}")
String selectProductById(@PathVariable("product_id") Long productId);
}

OrderController

直接使用 @Autowired 注入进去即可,然后调用就好了,对比较 Ribbon 这里我们看不到 RestTemplate 的代码了,也无需自己做解码映射,Spring Cloud Feign 默认都替我们实现好了,我们只需要遵循既定的标准即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.battcn.controller;

import com.battcn.api.ProductClient;
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; /**
* @author Levin
* @since 2018/9/26 0026
*/
@RestController
@RequestMapping("/orders")
public class OrderController { @Autowired
private ProductClient productClient; @GetMapping
public String query() {
return this.productClient.selectProductById(10L);
}
}

主函数

通过 @EnableFeignClients 注解开启对 Feign 的支持,用习惯 Dubbo 的朋友喜欢将 API 打包成独立的 JAR ,这个时候需要指定 basePackage 属性。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.battcn;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients; /**
* @author Levin
*/
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class OrderApplication { public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
} }

注解式HTTP请求Feign (F版)的更多相关文章

  1. Spring MVC 使用介绍(六)—— 注解式控制器(二):请求映射与参数绑定

    一.概述 注解式控制器支持: 请求的映射和限定 参数的自动绑定 参数的注解绑定 二.请求的映射和限定 http请求信息包含六部分信息: ①请求方法: ②URL: ③协议及版本: ④请求头信息(包括Co ...

  2. Spring常用注解式开发

    1.组件注册@Configuration.@Bean给容器中注册组件. 注解,@Configuration告诉Spring这是一个配置类,相当于bean.xml配置文件. 注解,@Bean给Sprin ...

  3. SpringMVC学习系列(9) 之 实现注解式权限验证

    对大部分系统来说都需要权限管理来决定不同用户可以看到哪些内容,那么如何在Spring MVC中实现权限验证呢?当然我们可以继续使用servlet中的过滤器Filter来实现.但借助于Spring MV ...

  4. 谈谈MVVM和链式网络请求架构

    前言 前一段时间一直在学习iOS的架构.为什么呢? 公司的架构一直是MVC,当我们正式上线的时候,项目已经有了超十万行代码.主要的VC一般都有2000行代码以上. 关键是,目前版本我们只做了三分之一的 ...

  5. spring(7)--注解式控制器的数据验证、类型转换及格式化

    7.1.简介 在编写可视化界面项目时,我们通常需要对数据进行类型转换.验证及格式化. 一.在Spring3之前,我们使用如下架构进行类型转换.验证及格式化: 流程: ①:类型转换:首先调用Proper ...

  6. spring(6)--注解式控制器

    6.1.注解式控制器简介 一.Spring2.5之前,我们都是通过实现Controller接口或其实现来定义我们的处理器类.已经@Deprecated.   二.Spring2.5引入注解式处理器支持 ...

  7. SpringMVC实现注解式权限验证

    SpringMVC学习系列(9) 之 实现注解式权限验证 对大部分系统来说都需要权限管理来决定不同用户可以看到哪些内容,那么如何在Spring MVC中实现权限验证呢?当然我们可以继续使用servle ...

  8. springMVC3学习(十)--注解式控制器

    Spring2.5引入注解式处理器支持,通过@Controller和@RequestMapping注解定义 我们的处理器类.并且提供了一组强大的注解 需要通过处理器映射DefaultAnnotatio ...

  9. SpringMVC实现注解式权限验证(转)

    SpringMVC学习系列(9) 之 实现注解式权限验证   对大部分系统来说都需要权限管理来决定不同用户可以看到哪些内容,那么如何在Spring MVC中实现权限验证呢?当然我们可以继续使用serv ...

随机推荐

  1. PAT A除以B

    本题要求计算A/B,其中A 是不超过 1000 位的正整数,B 是 1 位正整数.你需要输出商数Q 和余数R,使得 A=B*Q+R 成立. 输入格式: 输入在一行中依次给出A 和B,中间以 1 空格分 ...

  2. Jmeter之Json提取器详解(史上最全)

    参考资料:https://www.bbsmax.com/A/D854lmBw5E/ Jsonpath在线测试:http://jsonpath.com/ 实际工作中用到的一些场景: 提取某个特定的值 提 ...

  3. win7 64位系统怎么使用debug

    安装DOSbox软件 下载个debug.exe然后把这放到D盘或E盘的根目录下 然后启动dosbox软件,在下面输入 mount c d:\ enter键 c: enter键 输入debug命令就ok ...

  4. 09 . Nginx配置LNMP和LNMT架构

    安装LNMP架构 环境清单 list CentOS7.3 proxysql-2.0.12-1-centos7.x86_64.rpm mysql-5.7.23-1.el7.x86_64.rpm-bund ...

  5. 【图机器学习】cs224w Lecture 16 - 图神经网络的局限性

    目录 Capturing Graph Structure Graph Isomorphism Network Vulnerability to Noise 转自本人:https://blog.csdn ...

  6. Istio的运维-诊断工具(istio 系列五)

    Istio的运维-诊断工具 在参考官方文档的时候发现环境偶尔会出现问题,因此插入一章与调试有关的内容,便于简单问题的定位.涵盖官方文档的诊断工具章节 目录 Istio的运维-诊断工具 使用istioc ...

  7. 【Transferable NAS with RL】2018-CVPR-Learning Transferable Architectures for Scalable Image Recognition

    Transferable NAS with RL 2018-CVPR-Learning Transferable Architectures for Scalable Image Recognitio ...

  8. Ehab and a 2-operation task【数论思想】

    Ehab and a 2-operation task 题目链接(点击) You're given an array aa of length nn. You can perform the foll ...

  9. CENTOS的备份和恢复

    CENTOS的备份和恢复其实非常简单,我们只要把全部文件用TAR打包就行,下次需要恢复的适合再解压开覆盖就可以了 下面详解CENTOS备份和还原的过程 tar打包命令的特点:1.保留权限2.适合备份整 ...

  10. 色彩空间转换 rgb转ycbcr422/ycbcr422转rgb

    在图像处理过程中通常需要会对图像类型进行互相转换,在此给出两种转换的工程代码. 1.在将ycbCr422转rgb时,通常先将ycbcr422转换成ycbcr444再讲ycbcr444转成rgb 1.1 ...