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的更多相关文章

  1. SpringCloud之Feign声明式调用原理及配置

    1 什么是Feign Feign是一种声明式.模板化的HTTP客户端(仅在Application Client中使用).声明式调用是指,就像调用本地方法一样调用远程方法,无需感知操作远程http请求. ...

  2. SpringCloud之声明式服务调用 Feign(三)

    一 Feign简介 Feign是一种声明式.模板化的HTTP客户端,也是netflix公司组件.使用feign可以在远程调用另外服务的API,如果调用本地API一样.我们知道,阿里巴巴的doubbo采 ...

  3. 学习一下 SpringCloud (二)-- 服务注册中心 Eureka、Zookeeper、Consul、Nacos

    (1) 相关博文地址: 学习一下 SpringCloud (一)-- 从单体架构到微服务架构.代码拆分(maven 聚合): https://www.cnblogs.com/l-y-h/p/14105 ...

  4. 学习一下 SpringCloud (三)-- 服务调用、负载均衡 Ribbon、OpenFeign

    (1) 相关博文地址: 学习一下 SpringCloud (一)-- 从单体架构到微服务架构.代码拆分(maven 聚合): https://www.cnblogs.com/l-y-h/p/14105 ...

  5. 学习一下 SpringCloud (四)-- 服务降级、熔断 Hystrix、Sentinel

    (1) 相关博文地址: 学习一下 SpringCloud (一)-- 从单体架构到微服务架构.代码拆分(maven 聚合): https://www.cnblogs.com/l-y-h/p/14105 ...

  6. springcloud~演化的微服务架构

    微服务 将整体功能按着模块划分成多个独立的单元,这些单元可以独立部署,它们之前通过轻量级的web api方式进行通讯,对于微服务框架来说,最流行的就是springcloud和Service Fabri ...

  7. springcloud与docker微服务架构实战--笔记

    看了<微服务那些事>之后,Spring boot和Spring Cloud的关系理清楚了,Spring cloud各个模块的作用也了解了. 但是,Spring cloud 与Docker的 ...

  8. 跟我学SpringCloud | 第九篇:服务网关Zuul初

    SpringCloud系列教程 | 第九篇:服务网关Zuul初探 前面的文章我们介绍了,Eureka用于服务的注册于发现,Feign支持服务的调用以及均衡负载,Hystrix处理服务的熔断防止故障扩散 ...

  9. SpringCloud之Zuul:服务网关

    Zuul在Web项目中的使用见上文<SpringBoot中使用Zuul>,下面例子为Zuul在Spring Cloud的使用. 开发工具:IntelliJ IDEA 2019.2.3 一. ...

  10. SpringCloud学习笔记:服务支撑组件

    SpringCloud学习笔记:服务支撑组件 服务支撑组件 在微服务的演进过程中,为了最大化利用微服务的优势,保障系统的高可用性,需要通过一些服务支撑组件来协助服务间有效的协作.各个服务支撑组件的原理 ...

随机推荐

  1. PHP POST, GET 参数过滤,预防sql注入函数

    1. 实际过滤函数 可适当修改其中的正则表示式 1 static public function filterWords(&$str) 2 { 3 $farr = array( 4 " ...

  2. 深入PHP内核 SAPI探究

    转自 http://www.csdn.net/article/2014-09-26/2821885-exploring-of-the-php-2 SAPI是Server Application Pro ...

  3. redis 底层数据结构 整数集合intset

    整数集合是集合键的底层实现之一,当一个集合只包含整数值元素,并且这个集合的元素数量不多时Redis就会使用整数集合作为集合键的底层实现 整数集合是Redis用于保存整数值的集合抽象数据结构,它可以保存 ...

  4. PDP2

    1. 程序入口 配置文件config.xml中: 2. index.html中显示,引入的cordova.js 就说明了 cordova apps 3. 看 main.ts

  5. Python学习笔记(三)windows下安装theano

    2016.6.28补充: 不论是实验室的电脑还是我的笔记本,只要是windows下,theano.test()都是不通过的.虽然能使用一些theano中的函数,但是我感觉很不好. 所以还是转Ubunt ...

  6. 巨蟒django之CRM3 添加和编辑客户&&公户和私户的展示和转换

    昨日内容回顾: day66 1. 内容回顾 1. 数据的展示 数据通过ORM查询出来 对象列表 QuerySet 1. 普通的字段 对象.字段名 ——> 数据库中的值 2. choices (( ...

  7. 根据字段表 自动创建 表SQL

    create PROC CreateTableSql ) AS ) ,) ,) ) ,) ,) ,@IsIdentity bit ,@IsNull bit ,@IsPrimaryKey bit ),) ...

  8. Django的admin定制

    1,models编写 #encoding=utf-8 from django.db import models # Create your models here. class BookInfo(mo ...

  9. 使用JDT转java代码为AST

    maven依赖 <dependency> <groupId>org.eclipse.jdt</groupId> <artifactId>org.ecli ...

  10. python四个带 key 参数的函数(max、min、map、filter)

    四个带 key 参数的函数: max()点击查看详细 min()点击查看详细 map()点击查看详细 filter()点击查看详细 1)max(iterable, key) key:相当于对可迭代对象 ...