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学习笔记:服务支撑组件 服务支撑组件 在微服务的演进过程中,为了最大化利用微服务的优势,保障系统的高可用性,需要通过一些服务支撑组件来协助服务间有效的协作.各个服务支撑组件的原理 ...
随机推荐
- python time与datetime.date/datetime模块
https://docs.python.org/3/library/datetime.html 1.用于日期比较大小的方法 方法名 方法说明 用法 __eq__(…) 等于(x==y) x.__eq_ ...
- jmeter对响应结果做正则、json、xpath结果测试
上面的返回结果可用于关联取值测试 具体用法详见http://www.cnblogs.com/xinjing-jingxin/p/8554338.html http://goessner.net/art ...
- python学习【第六篇】python迭代器与生成器
一.什么是迭代器 迭代器协议:对象必须提供一个next方法,执行该方法要么返回迭代中的下一项,要么就引起一个StopIteration异常,以终止迭代(只能往后走不能往前退) 可迭代对象:实现了迭代器 ...
- POJ 3026 Borg Maze【BFS+最小生成树】
链接: http://poj.org/problem?id=3026 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- 通过margin负值去除padding
.pay-type { // 图片布局前通过margin负值去除padding margin: 0 -@page-padding-horizontal; display: inline-flex; } ...
- 如何查看l操作系统是否开启rpc服务
linux操作系统 在linux 5.X以及下的版本你可以通过service portmap status命令查看rpc是否启动.如果提示running,表示正在运行:如果提示stop就是关闭了.如果 ...
- JavaWeb 过滤器应用之页面静态化
页面静态化是把servlet请求的资源所做输出保存到html中, 然后重定向到 html 页面, 二次访问时,这个html已经存在,那么直接重定向,不用再去访问servlet! // StaticFi ...
- pmd 使用笔记
pmd是一块开源的代码静态分析工具,使用java编写,可以自定义规则来进行自己想要的分析.pmd可以单独使用,也可以作为idea.eclipse的插件使用.它的规则分为xpath规则,和java规则. ...
- 前端之 JS 实现全选、反选、取消选中
需求 制作如下可选表格,实现“全选”.“反选”.“取消”功能 代码示例 <!DOCTYPE html> <html lang="zh-CN"> <he ...
- 004-mysql explain详解
一.使用 使用explain + 查询语句 二.解释说明 1)id列[执行顺序] id列数字越大越先执行,如果说数字一样大,那么就从上往下依次执行,id列为null的就表是这是一个结果集,不需要使用它 ...