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. jQuery整理笔记八----jQuery的Ajax

    Ajax,我一直读的是"阿贾克斯",据当时大学老师讲该读音出处是依据当年风靡欧洲的荷兰足球俱乐部阿贾克斯的名字来的,我认为说法挺靠谱的. jQuery封装了Ajax的交互过程,用户 ...

  2. CodeIgniter框架——访问方式 URI 分配变量 数据库操作

    1.访问方式: CodeIgniter 的访问URL使用的是pathinfo,入口文件/控制器/方法(/参数列表) eg:localhost/index.php/welcome/index/id 第一 ...

  3. CodeIgniter框架——nginx下的配置

    odeigniter(CI)是一个轻量型的PHP优秀框架,但是它是在apache服务器下开发的,在nginx下需要特别的配置才可以使用. 对nginx的配置如下: server { listen 80 ...

  4. 1714 B君的游戏(Nim博弈)

    1714 B君的游戏 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 B君和L君要玩一个游戏.刚开始有n个正整数  ai  . 双方轮流操作.每次操作,选一个正整数 ...

  5. 前台传递给后台的JSON字符串中的引号 “” 在JAVA后台被转义为 "

    前台传递给后台的JSON字符串中的引号 "" 在JAVA后台被转义为 &quot 1.问题: 前台数据,JSON字符串带有引号 "" ,数据被传递到后台 ...

  6. onload事件,解决不能在head写代码

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta http-equiv="con ...

  7. nginx1.4.7+uwsgi+django1.9.2项目部署,liunx系统为ubuntu14.0.4。

    本文基于root用户下进行部署,django项目名称为BDFS 1.  安装依赖包,终端输入命令 1)         环境依赖包 apt-get update apt-get install pyt ...

  8. 2014牡丹江——Known Notation

    题目链接 题意: 输入一个长度不超过1000的字符串,包含数字(1-9)和星号(*).字符串中的空格已经丢失,所以连起来的数字串能够看成很多分开的数.也能够看成连续的数,即能够随意加入空格. 如今有两 ...

  9. 协程 Gevent

    # 协程应用:爬虫 from gevent import monkey;monkey.patch_all() import gevent import requests import time def ...

  10. nodejs获取服务器数据到页面

    const Koa = require('koa'); const Router = require('koa-router'); const app = new Koa(); const route ...