博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring-Cloud-Finchley | 声明式服务调用 Feign
阅读量:4104 次
发布时间:2019-05-25

本文共 5102 字,大约阅读时间需要 17 分钟。

简介

本篇的 Feign 和 上一篇的 Ribbon 都是用于调用其他服务,但是调用的方式不同,Ribbon 需要搭配 RestTemplate 进行 Http 请求,步骤较为繁琐;Feign 是 Ribbon 的改进,整合了 Ribbon 和 Hystrix,不需要构建 Http 请求,并且采用接口的方式声明即可调用。

实例

1、创建 maven 工程

创建 maven 工程,添加核心的父 maven 依赖,所有子 pom 文件继承这个父 pom 文件。

UTF-8
1.8
1.8
Finchley.RELEASE
org.springframework.boot
spring-boot-starter-parent
2.0.5.RELEASE
junit
junit
4.11
test
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
2、搭建服务注册中心 eureka-server

添加 maven 依赖

com.hly
03-spring-cloud-feign
1.0-SNAPSHOT
UTF-8
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-web

application.yml

server:  port: 8761eureka:  instance:    hostname: localhost  client:    registerWithEureka: false    fetchRegistry: false    serviceUrl:      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/spring:  application:    name: eureka-server

SpringBoot 启动类

@SpringBootApplication@EnableEurekaServerpublic class EurekaServerApplication {	public static void main(String[] args) {		SpringApplication.run(EurekaServerApplication.class, args);	}}
3、搭建服务提供者 eureka-client

添加 maven 依赖

com.hly
03-spring-cloud-feign
1.0-SNAPSHOT
UTF-8
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web

添加配置信息 application.yml

server:  port: 8762spring:  application:    name: eureka-clienteureka:  client:    serviceUrl:      defaultZone: http://localhost:8761/eureka/

SpringBoot 启动类

@SpringBootApplication@RestController@EnableEurekaClientpublic class EurekaClientApplication {	public static void main(String[] args) {		SpringApplication.run(EurekaClientApplication.class, args);	}	@Value("${server.port}")	String port;	@RequestMapping("/hello")	public String home(@RequestParam(value = "name", defaultValue = "hly") String name) {		return "hi " + name + " ,i am from port:" + port;	}}
4、搭建服务消费者 feign 客户端

添加 maven 依赖

com.hly
03-spring-cloud-feign
1.0-SNAPSHOT
UTF-8
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-openfeign

添加 application.yml 配置

eureka:  client:    serviceUrl:      defaultZone: http://localhost:8761/eureka/server:  port: 8765spring:  application:    name: service-feign    # 设置feign客户端超时时间feign: client:  config:   default:    connectTimeout: 1    readTimeout: 1 # feign: # hystrix:    #enabled : true

SpringBoot 启动类

@SpringBootApplication@EnableEurekaClient//让注册中心扫描到,EnableEurekaClient只适用于 eureka,@EnableDiscoveryClient 可以是其他注册中心@EnableDiscoveryClient@EnableFeignClientspublic class EurekaFeignApplication {	public static void main(String[] args) {		SpringApplication.run(EurekaFeignApplication.class, args);	}}

服务层接口

//服务名指定调用哪个服务@FeignClient(value = "eureka-client")public interface ServiceHi {    //这里的映射名和需要调用的服务的映射名一样    @RequestMapping(value = "/hello",method = RequestMethod.GET)    String sayHiFromClientOne(@RequestParam(value = "name") String name);}

控制层

@RestControllerpublic class HiController {    @Autowired    ServiceHi serviceHi;    /**     * 消费服务     * 访问:http://localhost:8765/hi?name=hly 交替输出响应两个服务     * @param name     * @return     */    @GetMapping(value = "/hi")    public String sayHi(@RequestParam String name) {        return serviceHi.sayHiFromClientOne( name );    }}

演示

1、依次启动 注册中心 eureka-server ,启动两个 eureka-client(启动一个后,修改application.yml 配置文件的端口,再次启动一次),接着启动 Feign 客户端,访问:

http://localhost:8765/hi?name=hly ,刷新浏览器,实现负载均。

资料

官网:https://cloud.spring.io/spring-cloud-static/spring-cloud-openfeign/2.2.2.RELEASE/reference/html/#spring-cloud-feign-hystrix-fallback

思维导图

代码下载

03-spring-cloud-feign:

关于

公众号:【星尘Pro】

github:

推荐阅读

转载地址:http://pufsi.baihongyu.com/

你可能感兴趣的文章
C# 简单的矩阵运算
查看>>
gcc 常用选项详解
查看>>
c++输入文件流ifstream用法详解
查看>>
c++输出文件流ofstream用法详解
查看>>
字符编码:ASCII,Unicode 和 UTF-8
查看>>
QT跨MinGW和MSVC两种编译器的解决办法
查看>>
firewalld的基本使用
查看>>
Linux下SVN客户端使用教程
查看>>
i2c-tools
查看>>
Linux分区方案
查看>>
nc 命令详解
查看>>
如何使用 systemd 中的定时器
查看>>
git命令速查表
查看>>
linux进程监控和自动重启的简单实现
查看>>
OpenFeign学习(三):OpenFeign配置生成代理对象
查看>>
OpenFeign学习(四):OpenFeign的方法同步请求执行
查看>>
OpenFeign学习(五):OpenFeign请求结果处理及重试控制
查看>>
OpenFeign学习(六):OpenFign进行表单提交参数或传输文件
查看>>
OpenFeign学习(七):Spring Cloud OpenFeign的使用
查看>>
Ribbon 学习(二):Spring Cloud Ribbon 加载配置原理
查看>>