Feign简介:

  声明式的Rest  WEB 服务的客户端, https://github.com/OpenFeign/feign。Spring Cloud 提供了Spring-cloud-starter-openfeign的支持

Feign的简单使用

  pom文件

    <!--引入版本号-->
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<!--dependency引入--> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency> <dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency> <!--eureka的支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency> <!--dependencyManagement 引入spring cloud 的dependency-->
<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>

  开启Feign的支持

    @EnableFeignClients

@SpringBootApplication
@Slf4j
@EnableFeignClients // 开启Feign支持
public class CustomerServiceApplication {}

注入CloseableHttpClient

  

@Bean
public CloseableHttpClient httpClient() {
return HttpClients.custom()
.setConnectionTimeToLive(30, TimeUnit.SECONDS)
.evictIdleConnections(30, TimeUnit.SECONDS)
.setMaxConnTotal(200)
.setMaxConnPerRoute(20)
.disableAutomaticRetries()
// 存活策略
.setKeepAliveStrategy(new CustomConnectionKeepAliveStrategy())
.build();
} //CustomConnectionKeepAliveStrategy
public class CustomConnectionKeepAliveStrategy implements ConnectionKeepAliveStrategy {
private final long DEFAULT_SECONDS = 30; @Override
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
return Arrays.asList(response.getHeaders(HTTP.CONN_KEEP_ALIVE))
.stream()
.filter(h -> StringUtils.equalsIgnoreCase(h.getName(), "timeout")
&& StringUtils.isNumeric(h.getValue()))
.findFirst()
.map(h -> NumberUtils.toLong(h.getValue(), DEFAULT_SECONDS))
.orElse(DEFAULT_SECONDS) * 1000;
}
}

定义Feign接口

@FeignClient(name = "my-service", contextId = "order")
public interface OrderService {
@GetMapping("/order/{id}")
Order getOrder(@PathVariable("id") Long id); @PostMapping(path = "/order/", consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
Order create(@RequestBody NewOrderRequest newOrder);
}

使用Feign

/**注入刚刚定义的OrderService*/
@Autowired
private OrderService orderService; // 使用 List<Coffee> coffees = coffeeService.getAll(); // 传对象参数
NewOrderRequest orderRequest = NewOrderRequest.builder()
.customer("Li Lei")
.items(Arrays.asList("capuccino"))
.build();
Order order = orderService.create(orderRequest); // 单个参数
Order order = orderService.getOrder(id);

SpringCloud 使用Feign访问服务的更多相关文章

  1. SpringCloud使用Feign实现服务间通信

    SpringCloud的服务间通信主要有两种办法,一种是使用Spring自带的RestTemplate,另一种是使用Feign,这里主要介绍后者的通信方式. 整个实例一共用到了四个项目,一个Eurek ...

  2. SpringCloud使用Feign调用服务时,@FeignClient注解无法使用

    关于解决这个问题的理论根源传送门:https://blog.csdn.net/alinyua/article/details/80070890我在这里只提供解决方案 0. 结论和解决方案 Spring ...

  3. 使用springcloud的feign调用服务时出现的错误:关于实体转换成json错误的介绍

    http://blog.csdn.net/java_huashan/article/details/46428971 原因:实体中没有添加无参的构造函数 fastjson的解释: http://www ...

  4. SpringCloud(3)服务消费者(Feign)

    上一篇文章,讲述了如何通过 RestTemplate+Ribbon 去消费服务,这篇文章主要讲述如何通过Feign去消费服务. 1.Feign简介 Feign是一个声明式的伪Http客户端,它使得写H ...

  5. springcloud费话之Eureka服务访问(restTemplate)

    目录: springcloud费话之Eureka基础 springcloud费话之Eureka集群 springcloud费话之Eureka服务访问(restTemplate) springcloud ...

  6. SpringCloud(5)---Feign服务调用

    SpringCloud(5)---Feign服务调用 上一篇写了通过Ribbon进行服务调用,这篇其它都一样,唯一不一样的就是通过Feign进行服务调用. 注册中心和商品微服务不变,和上篇博客一样,具 ...

  7. SpringCloud与Docker微服务架构实战笔记

    一  微服务架构概述 1. 单体应用架构存在的问题 结合:https://www.cnblogs.com/jialanshun/p/10637454.html一起看,在该篇博客中搜索“单块架构的优缺点 ...

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

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

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

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

随机推荐

  1. C#面试 笔试题 四

    1.请你简单的说明数据库建立索引的优缺点 使用索引可以加快数据的查询速度,不过由于数据插入过程中会建索引,所以会降低数据的插入.更新速度,索引还会占磁盘空间. 2.什么是WEB服务控件?使用WEB服务 ...

  2. 【学习总结】Python-3-Python数字运算与数学函数

    菜鸟教程-Python3-Python数字 注:这一节链接中的内容比较多,表格中的具体函数耐心点进去看看 1-变量在使用前必须先"定义"(即赋予变量一个值),否则会出现错误 2-不 ...

  3. Beta阶段成果展示——第八组

    Beta阶段成果展示 游戏公网IP:http://119.29.32.204/krad.html(欢迎大家测试!) Beta阶段体现在成果上的工作主要为界面美化,玩家引导,按键封闭等等. 本文将以截图 ...

  4. 源码分析--HashSet(JDK1.8)

    HashSet为无序不可重复集合.底层几乎全部借助HashMap实现,比较简单.本篇简要分析一下HashSet源码. 首先是成员变量: 1.真正保存数据的HashMap实例 private trans ...

  5. 2018-2-13-win10-uwp-分治法

    title author date CreateTime categories win10 uwp 分治法 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17:2 ...

  6. 金蝶KIS客户端修改IP连接服务器的方法

    问题现象:服务器IP变更后,金蝶KIS客户端打开时提示多个错误,并会自动关闭,无法联网登录 1. 到下面位置修改注册表 Windows Registry Editor Version 5.00 [HK ...

  7. 从零开始之uboot、移植uboot2017.01(一、移植前的准备)

    手边的是一个S5PV210的开发板,想尝试移植一个比较新的uboot 下载最新版本uboot2018. ftp://ftp.denx.de/pub/u-boot/ 编译器下载 http://www.v ...

  8. centos 6.5 关闭防火墙

    关闭防火墙分为临时关闭和永久关闭.临时关闭重启系统后恢复正常,永久关闭重启系统后仍然是关闭状态 临时关闭与开启 service iptables stop service iptables start ...

  9. Django学习——collectstatic错误

    Error fetching command 'collectstatic': You're using the staticfiles app without having set the STAT ...

  10. dubbo所用到的技术

    服务注册:zookeeper 协议:dubbo  Hessian  Rmi 网络编程:netty 动态代理:jdk和Javassist 序列化:Hessian  Dubbo  Json  Java S ...