Feign
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的更多相关文章
- Spring Cloud 声明式服务调用 Feign
一.简介 在上一篇中,我们介绍注册中心Eureka,但是没有服务注册和服务调用,服务注册和服务调用本来应该在上一章就应该给出例子的,但是我觉得还是和Feign一起讲比较好,因为在实际项目中,都是使用声 ...
- Feign使用Hystrix无效原因及解决方法
最近项目重构使用了Spring Boot和Spring Cloud.这两者结合确实给项目带来了方便,同时也遇到了一些问题.其中使用feign作为服务消费,但是断路器hystrix一直不起作用让人很费解 ...
- 在dropwizard中使用feign,使用hystrix
前言 用惯了spring全家桶之后,试试dropwizard的Hello World也别有一帆风味.为了增强对外访问API的能力,需要引入open feign.这里简单在dropwizard中使用fe ...
- spring cloud feign不支持@RequestBody+ RequestMethod.GET,报错
1.问题梳理: 异常:org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not ...
- 【微服务】之五:轻松搞定SpringCloud微服务-调用远程组件Feign
上一篇文章讲到了负载均衡在Spring Cloud体系中的体现,其实Spring Cloud是提供了多种客户端调用的组件,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使 ...
- Spring Cloud Feign 整合 Hystrix
在前面随笔Spring Cloud 之 Feign的feign工程基础上进行改造 1.pom.xml依赖不变 2.application.yml文件添加feign.hystrix.enabled=tr ...
- Spring Cloud 之 Feign
新建Spring Boot工程,命名为feign 1.pom.xml添加依赖 <?xml version="1.0" encoding="UTF-8"?& ...
- SpringCloud Feign对Hystrix(断路由)的支持
第一步:首先开启Feign对Hystrix的支持,在properties文件中添加以下配置: feign.hystrix.enabled=true. 第二步:在上一篇Feign的基础上添加Hystri ...
- SpringCloud Feign使用详解
添加依赖: <dependency> <groupId>org.springframework.cloud</groupId> <artifactId> ...
- 工作随笔——自动重发的凶手--feign
公司使用的feign(https://github.com/OpenFeign/feign)作为http客户端. 开发时debug后端程序,发现同一个请求会多次收到. 为了判断是谁在搞鬼,在客户端和服 ...
随机推荐
- 2018-2019-2 《网络对抗技术》Exp4 恶意代码分析20165211
目录 实践内容概述 实践目标 实践内容 实验问题回答 实践过程记录 系统运行监控 使用schtacks指令监控系统运行 使用sysmon工具监控系统运行 恶意软件分析 使用Virus Total分析恶 ...
- Junit Framework -TestRule,自动化测试中如何再次运行失败的测试用例
有时由于服务器,浏览器等问题,会导致自动化测试用例运行失败,此处通过案例讲解如何使用Junit框架中的TestRule来实现重复运行失败的测试用例. 首先定义一个类并让它实现TestRule,代码如下 ...
- 2018 蓝桥杯省赛 B 组模拟赛(五)
A模拟 代码1 #include<bits/stdc++.h> using namespace std; int n = 101; int a[120][120]; int ans = 0 ...
- HDU 4825 Xor Sum(01字典树)题解
思路:先把所有数字存进字典树,然后从最高位贪心. 代码: #include<set> #include<map> #include<stack> #include& ...
- 【做题】hdu5514 Frogs——另类容斥
题意是给出n个m的约数,问[0,m-1]中至少被其中一个约数整除的整数和.(n<=10000,m<=1000000000) 直接容斥的话,是2^n再拖个log的复杂度,加上当前的数大于m时 ...
- What are the differences between Flyweight and Object Pool patterns?
What are the differences between Flyweight and Object Pool patterns? They differ in the way they are ...
- golang解析json配置文件
安装 go get github.com/akkuman/parseConfig 使用说明 环境假设 . ├── config.go ├── config.json config.json内容 { & ...
- LuoguP2161 [SHOI2009]会场预约
题目地址 题目链接 题解 用fhqtreap对区间进行维护. 可以注意到的是,对于当前存在的预约,他们一定是升序排列的(有重叠的都被删了). 那么就可以用按照位置分裂的fhqtreap搞了(预约无论按 ...
- LOJ6285 数列分块入门9(分块)
昨天对着代码看了一晚上 然后今天终于在loj上过了 数列分块入门9题撒花★,°:.☆( ̄▽ ̄)/$:.°★ . 然后相当玄学 块的大小调成\(\sqrt{n}\)会TLE,改成150就过了 啧 然后就 ...
- 题解——HDU 2089 不要62(数位DP)
最近在学数位DP 应该是入门题吧 设\( dp[i][0/1] \)表示到第\( i \)位时,前一位是否是6的满足条件的数的个数 然后就是套路 注意\( limit \)的限制条件以及转移时候信息的 ...