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. JavaScript中冒泡与事件委托

    冒泡 事件触发后事件流的三个阶段按顺序依次是: 1.捕获阶段 2.目标阶段 3.冒泡阶段 大盒子包裹小盒子,两个盒子都分别添加点击事件,当点击小盒子,两个盒子的事件都会触发. 事件委托 下级元素委托上 ...

  2. MFC限制edit控件的字符输入长度

    一. 1.Edit 控件添加EN_CHANGE事件 #define MAX_SIZE 200 void CMyDlg::OnChangeEditFeed() { // TODO: 如果该控件是 RIC ...

  3. weblogic10以下,许可证过期解决办法

    weblogic10以后的版本已经不再使用license.bea的方式来进行软件授权,之前的历史版本oracle提供了一个免费的许可证更新. 1.首先进入oracle的官网下载地址http://www ...

  4. luogu P4051 [JSOI2007]字符加密

    前言 其实就是个后缀数组模板题 可还是有几个的地方不太明白 思路 先将子串复制一遍,组成长度为2*n的子串 给出的子串一定会在前n个后缀 而且后面的优先级不会影响前面的相对大小 然后求得sa输出就好 ...

  5. Git rebase使用

    目录 rebase的优点和缺点 分支内合并多个commit为一个新commit使用: 命令: 使用: 将其他分支合并到主分支,表现为线性: 将其他分支多个commit合并到主分支,并形成一个新comm ...

  6. (zhuan) Evolution Strategies as a Scalable Alternative to Reinforcement Learning

    Evolution Strategies as a Scalable Alternative to Reinforcement Learning this blog from: https://blo ...

  7. Latex: "Missing $ inserted" 解决方法

    参考: Latex报"Missing $ inserted"的解决方法 Latex: "Missing $ inserted" 解决方法 原因一:在文中出现&q ...

  8. [分享]Active-HDL 9.2 安装

    Download 点击下载Active-HDL 9.2 How to Install ? 解压后依次进行以下操作 1.运行Active_HDL_9.2sp1_main_setup.exe,允许程序所有 ...

  9. Wordpress搭建

    Install Environment apt install apache2 php mysql-server apt install php-mysql php-fpm Config mysql ...

  10. JavaScript运行机制详解

    JavaScript运行机制详解   var test = function(){ alert("test"); } var test2 = function(){ alert(& ...