Feign简介

Feign是一个声明式的Web服务客户端,使用Feign可使得Web服务客户端的写入更加方便。
它具有可插拔注释支持,包括Feign注解和JAX-RS注解、Feign还支持可插拔编码器和解码器、Spring Cloud增加了对Spring
MVC注释的支持,并HttpMessageConverters在Spring Web中使用了默认使用的相同方式。Spring
Cloud集成了Ribbon和Eureka,在使用Feign时提供负载平衡的http客户端。

spring cloud中的feign自带负载均衡?

feign

Feign是一个声明式的Web Service客户端,它使得编写Web Serivce客户端变得更加简单。我们只需要使用Feign来创建一个接口并用注解来配置它既可完成。它具备可插拔的注解支持,包括Feign注解和JAX-RS注解。Feign也支持可插拔的编码器和解码器。Spring Cloud为Feign增加了对Spring MVC注解的支持,还整合了Ribbon和Eureka来提供均衡负载的HTTP客户端实现。

在Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign,鉴于feign注解化更方便使用,先讲解feign.

Feign RestTemplate 对比

RestTemplate传送更多的参数时,相当不方便

Feign在参数上非常灵活,本身自带负载均衡

使用Feign

1.加入依赖

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

2.启动文件加入enableFeignClients

@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients
public class FeignMovieApplication { public static void main(String[] args) {
SpringApplication.run(FeignMovieApplication.class, args);
}
}

3.FeignClient接口

spring-boot-user是我的一个微服务的名称:对应的地址是http://192.168.1.105:7900/
@FeignClient("spring-boot-user")
public interface UserFeignClient { // 两个坑:1. @GetMapping不支持 2. @PathVariable得设置value
@RequestMapping(value="/simple/{id}", method=RequestMethod.GET)
public User findById(@PathVariable("id") Long id); @RequestMapping(value="/user", method=RequestMethod.POST)
public User postUser(@RequestBody User user);
}

4.调用

@RestController
public class MovieController { @Autowired
private UserFeignClient userFeignClient; @GetMapping("/movie/{id}")
public User findById(@PathVariable("id") Long id) {
return this.userFeignClient.findById(id);
}
}

application.yml

#port
server.port = 7600
#spring
spring.application.name=spring-boot-movie-feign
#localhost
#user.userServicePath=http://localhost:7900/simple/
#eureka
eureka.client.healthcheck.enable=true
eureka.client.serviceurl.defaultzone=http://localhost:8761/eureka
eureka.instance.preferIpAddress=true

测试:

http://192.168.1.105:7600/movie/1

RestTemplate三种方式使用

①    直接使用RestTemplate,固定URL

 @GetMapping("/say")
public String say(){
RestTemplate restTemplate = new RestTemplate();
String str = restTemplate.getForObject("http://localhost:8081/hello",String.class);
return "say "+str;
} ② 注入LoadBalancerClient通过应用名获取URL @Autowired
private LoadBalancerClient loadBalancerClient; @GetMapping("/say")
public String say(){
ServiceInstance serviceInstance = loadBalancerClient.choose("CLIENT");
RestTemplate restTemplate = new RestTemplate();
String url = String.format("http://%s:%s/hello",serviceInstance.getHost(),serviceInstance.getPort());
String str = restTemplate.getForObject(url,String.class);
} ③配置RestTemplate,URL直接使用应用名 @Component
public class RestTemplateConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
} } @Autowired private RestTemplate restTemplate; @GetMapping("/say")
public String say(){
String response = restTemplate.getForObject("http://CLIENT/hello",String.class);
return "say "+response;
}

Feign的更多相关文章

  1. Spring Cloud 声明式服务调用 Feign

    一.简介 在上一篇中,我们介绍注册中心Eureka,但是没有服务注册和服务调用,服务注册和服务调用本来应该在上一章就应该给出例子的,但是我觉得还是和Feign一起讲比较好,因为在实际项目中,都是使用声 ...

  2. Feign使用Hystrix无效原因及解决方法

    最近项目重构使用了Spring Boot和Spring Cloud.这两者结合确实给项目带来了方便,同时也遇到了一些问题.其中使用feign作为服务消费,但是断路器hystrix一直不起作用让人很费解 ...

  3. 在dropwizard中使用feign,使用hystrix

    前言 用惯了spring全家桶之后,试试dropwizard的Hello World也别有一帆风味.为了增强对外访问API的能力,需要引入open feign.这里简单在dropwizard中使用fe ...

  4. spring cloud feign不支持@RequestBody+ RequestMethod.GET,报错

    1.问题梳理: 异常:org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not ...

  5. 【微服务】之五:轻松搞定SpringCloud微服务-调用远程组件Feign

    上一篇文章讲到了负载均衡在Spring Cloud体系中的体现,其实Spring Cloud是提供了多种客户端调用的组件,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使 ...

  6. Spring Cloud Feign 整合 Hystrix

    在前面随笔Spring Cloud 之 Feign的feign工程基础上进行改造 1.pom.xml依赖不变 2.application.yml文件添加feign.hystrix.enabled=tr ...

  7. Spring Cloud 之 Feign

    新建Spring Boot工程,命名为feign 1.pom.xml添加依赖 <?xml version="1.0" encoding="UTF-8"?& ...

  8. SpringCloud Feign对Hystrix(断路由)的支持

    第一步:首先开启Feign对Hystrix的支持,在properties文件中添加以下配置: feign.hystrix.enabled=true. 第二步:在上一篇Feign的基础上添加Hystri ...

  9. SpringCloud Feign使用详解

    添加依赖: <dependency> <groupId>org.springframework.cloud</groupId> <artifactId> ...

  10. 工作随笔——自动重发的凶手--feign

    公司使用的feign(https://github.com/OpenFeign/feign)作为http客户端. 开发时debug后端程序,发现同一个请求会多次收到. 为了判断是谁在搞鬼,在客户端和服 ...

随机推荐

  1. 公网FTP(filezilla)改端口

    背景:我们如果不修改ftp服务器的端口,很容易被别人测试和攻击. 配置要点:服务端端口设置.主被动设置.服务端和客户端防火墙设置 ftp服务器:filezilla ftp server 1.  监听端 ...

  2. mint-ui之Swipe使用

    <template> <div> <div class="swipe-wrapper"> <mt-swipe :auto="10 ...

  3. 前缀和与差分之IncDec sequence

    参考链接:https://blog.csdn.net/hzk_cpp/article/details/80407014 题目链接:https://www.acwing.com/problem/cont ...

  4. 检测 C++ 内存泄露

    转载:http://www.cnblogs.com/zouzf/p/4152279.html #include "stdafx.h" #include <string> ...

  5. 【Python57--正则1】

    一.正则表达式匹配IP地址 1.search()方法:用于在字符串中搜索正则表达式模式第一次出现的位置 >>> import re >>> re.search(r' ...

  6. maven项目出现红色感叹号报错

    背景 在eclipse部署maven项目的时候,项目出现红色的感叹号导致项目无法启动. 解决步骤 1.右键项目——>Maven——>Update Project ,弹出下框: 点击OK. ...

  7. SpringBoot 解决HttpServletRequest只能读取一次

    业务逻辑,通过filter读取请求的request,获取token,并将token传递后面流程使用 BodyReaderHttpServletRequestWrapper: public class ...

  8. Optical Flow related Tutorials

    Optical Flow related Tutorials 2017-04-01 10:50:55 Reference: 1. http://blog.csdn.net/carson2005/art ...

  9. 使用p4c将P4 14代码转换为16代码

    参考: [Question] How to make conversion between P4 14 and P4 16? 使用p4c将P4 14代码转换为16代码: $ p4test --p4v ...

  10. K-近邻

    概述 KNN算法本身简单有效,是一种lazy-learning算法: 分类器不需要使用训练集进行训练,训练时间复杂度为0: KNN分类的计算复杂度和训练集中的文档数目成正比,也就是说,如果训练集中文档 ...