springcloud-声明式调用服务Feign
springcloud项目例子:链接:https://pan.baidu.com/s/1O1PKrdvrq5c8sQUb7dQ5Pg 密码:ynir
作用:
基于Netflix Feign整合了Ribbon和Hystrix,Spring Cloud Feign对RestTemplate进行了进一步的封装,简化了我们的封装操作
一:创建Feign应用
1.pom:依赖主要是spring-cloud-starter-eureka和spring-cloud-starter-feign
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5..RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<spring-cloud.version>Dalston.SR3</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>
</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>
2.入口函数:@EnableFeignClients注解表示开启Spring Cloud Feign的支持功能
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignConsumerApplication { public static void main(String[] args) {
SpringApplication.run(FeignConsumerApplication.class, args);
}
}
3.声明 服务
1)创建service层,使用注解@FeignClient,之后Controller直接调用他就能访问生产者提供的服务
@FeignClient("hello-service")
public interface HelloService {
@RequestMapping("/hello")
String hello();
}
//生产者提供的服务代码
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello() {
return "hello";
}
2)Controller层调用服务
@RestController
public class FeignConsumerController {
@Autowired
HelloService helloService; @RequestMapping("/hello")
public String hello() {
return helloService.hello();
}
}
4.属性配置
spring.application.name=feign-consumer
server.port=
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/
5.测试
运行服务,生产者,Feign消费者调用消费者接口,发现生产者的服务能给调用成功,Feign服务配置成功!
二:Feign的配置
spring.application.name=feign-consumer
server.port=
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/ ###ribbon测试#######
# 设置连接超时时间
ribbon.ConnectTimeout=
# 设置读取超时时间
ribbon.ReadTimeout=
# 对所有操作请求都进行重试
ribbon.OkToRetryOnAllOperations=true
# 切换实例的重试次数
ribbon.MaxAutoRetriesNextServer=
# 对当前实例的重试次数
ribbon.MaxAutoRetries= # 设置针对hello-service服务的连接超时时间
hello-service.ribbon.ConnectTimeout=
# 设置针对hello-service服务的读取超时时间
hello-service.ribbon.ReadTimeout=
# 设置针对hello-service服务所有操作请求都进行重试
hello-service.ribbon.OkToRetryOnAllOperations=true
# 设置针对hello-service服务切换实例的重试次数
hello-service.ribbon.MaxAutoRetriesNextServer=
# 设置针对hello-service服务的当前实例的重试次数
hello-service.ribbon.MaxAutoRetries= ####Hystrix配置#######
# 设置熔断超时时间
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=
# 关闭Hystrix功能(不要和上面的配置一起使用)
feign.hystrix.enabled=false
# 关闭熔断功能
hystrix.command.default.execution.timeout.enabled=false # 设置熔断超时时间
hystrix.command.hello.execution.isolation.thread.timeoutInMilliseconds=
# 关闭熔断功能
hystrix.command.hello.execution.timeout.enabled=false ####Feign其他配置#######
####Spring Cloud Feign支持对请求和响应进行GZIP压缩,以提高通信效率
# 配置请求GZIP压缩
feign.compression.request.enabled=true
# 配置响应GZIP压缩
feign.compression.response.enabled=true
# 配置压缩支持的MIME TYPE
feign.compression.request.mime-types=text/xml,application/xml,application/json
# 配置压缩数据大小的下限
feign.compression.request.min-request-size= # 开启日志 格式为logging.level.+Feign客户端路径,Feign为每一个FeignClient都提供了一个feign.Logger实例
logging.level.org.sang.HelloService=debug
springcloud-声明式调用服务Feign的更多相关文章
- SpringCloud之Feign声明式调用原理及配置
1 什么是Feign Feign是一种声明式.模板化的HTTP客户端(仅在Application Client中使用).声明式调用是指,就像调用本地方法一样调用远程方法,无需感知操作远程http请求. ...
- SpringCloud之声明式服务调用 Feign(三)
一 Feign简介 Feign是一种声明式.模板化的HTTP客户端,也是netflix公司组件.使用feign可以在远程调用另外服务的API,如果调用本地API一样.我们知道,阿里巴巴的doubbo采 ...
- 学习一下 SpringCloud (二)-- 服务注册中心 Eureka、Zookeeper、Consul、Nacos
(1) 相关博文地址: 学习一下 SpringCloud (一)-- 从单体架构到微服务架构.代码拆分(maven 聚合): https://www.cnblogs.com/l-y-h/p/14105 ...
- 学习一下 SpringCloud (三)-- 服务调用、负载均衡 Ribbon、OpenFeign
(1) 相关博文地址: 学习一下 SpringCloud (一)-- 从单体架构到微服务架构.代码拆分(maven 聚合): https://www.cnblogs.com/l-y-h/p/14105 ...
- 学习一下 SpringCloud (四)-- 服务降级、熔断 Hystrix、Sentinel
(1) 相关博文地址: 学习一下 SpringCloud (一)-- 从单体架构到微服务架构.代码拆分(maven 聚合): https://www.cnblogs.com/l-y-h/p/14105 ...
- springcloud~演化的微服务架构
微服务 将整体功能按着模块划分成多个独立的单元,这些单元可以独立部署,它们之前通过轻量级的web api方式进行通讯,对于微服务框架来说,最流行的就是springcloud和Service Fabri ...
- springcloud与docker微服务架构实战--笔记
看了<微服务那些事>之后,Spring boot和Spring Cloud的关系理清楚了,Spring cloud各个模块的作用也了解了. 但是,Spring cloud 与Docker的 ...
- 跟我学SpringCloud | 第九篇:服务网关Zuul初
SpringCloud系列教程 | 第九篇:服务网关Zuul初探 前面的文章我们介绍了,Eureka用于服务的注册于发现,Feign支持服务的调用以及均衡负载,Hystrix处理服务的熔断防止故障扩散 ...
- SpringCloud之Zuul:服务网关
Zuul在Web项目中的使用见上文<SpringBoot中使用Zuul>,下面例子为Zuul在Spring Cloud的使用. 开发工具:IntelliJ IDEA 2019.2.3 一. ...
- SpringCloud学习笔记:服务支撑组件
SpringCloud学习笔记:服务支撑组件 服务支撑组件 在微服务的演进过程中,为了最大化利用微服务的优势,保障系统的高可用性,需要通过一些服务支撑组件来协助服务间有效的协作.各个服务支撑组件的原理 ...
随机推荐
- 在CentOS中安装输入法
1.需要root权限,所以要用root登录 ,或su root 2.yum install "@Chinese Support" 3.exit 4.回到桌面,system-> ...
- 七个迹象说明你可能受到APT 攻击
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaXF1c2hp/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...
- NodeJS版本EasyDarwin开源流媒体服务器开发心得
title: Node版本EasyDarwin开发心得 date: 2018-03-27 22:46:15 tags: 年后着手Node版本EasyDarwin的开发工作,截止到今天2018年03月2 ...
- docker postgres
启动一个 Postgres 实例 docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d daocloud.i ...
- C# 调用ArcGIS server admin api
一.AGS server admin api 介绍 1.1什么是admin api AGS Server Admin api 官方的称呼是 AGS Server administrator api, ...
- 使用QFile进行文件操作(QFile可以使用FILE *指针,还必须指定AutoCloseHandle)
QFile类我我们提供了操作文件的常用功能.它是一种io设备,可以用来读写文本文件和二进制文件,也可以用来读写Qt的资源文件.QFile类可以单独使用,该类本身提供了read/write函数,但更方便 ...
- 前端基础 DOM & BOM
推荐阅读:http://www.cnblogs.com/yuanchenqi/articles/6893904.html#_label3 BOM对象 window 对象 所有浏览器都支持 window ...
- python多进程编程(一)
multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在python中大部分情况需要使用多进程.Pyt ...
- Android系统移植与调试之------->如何修改Android的默认语言、默认时区
修改device/other/TBDG1073/ system.prop文件 1.设置默认语言 找到device/other/TBDG1073/ system.prop文件,修改属性ro.produc ...
- 一起talk C栗子吧(第七十八回:C语言实例--创建进程)
各位看官们,大家好.上一回中咱们说的是DIY ls命令续的样例.这一回咱们说的样例是:创建进程.闲话休提.言归正转. 让我们一起talk C栗子吧! 看官们.关于进程的概念,我们简单做个简单的介绍:进 ...