(三)Fegin声明式服务调用
上一篇,讲了SpringClound中的消费者采用Ribbon+Rest来实现,这回我们用组件Feign来实现服务的消费者,Fegin中也是默认集成了Ribbon的;和Eureka结合也能实现负载均衡;
概括来说,Fegin的区别就是基于注解来实现,具备可插拔的特性;
依赖前文说的Eureka,service-hello(一个项目,注册两个实例)

创建Fegin项目;
在Idea里,新建项目,选择Spring initializer.

下面的pom
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
配置properties文件参数;
#服务端口
server.port=8886 #注册服务中心地址
eureka.client.service-url.defaultZone=http://localhost:8882/eureka/ #注册服务名
spring.application.name=service-feign
启动类如下:
@EnableDiscoveryClient
@EnableFeignClients //开启Feign的功能:
@SpringBootApplication
public class SpringCloundEurekaFeginExampleApplication { public static void main(String[] args) {
SpringApplication.run(SpringCloundEurekaFeginExampleApplication.class, args);
}
}
然后我们定义一个fegin的接口,在这个接口中我们通过@ FeignClient(“服务名”)来实现消费服务提供者;
//代表改接口用费"service-hello"的服务 提供
@FeignClient(value = "service-hello")
public interface IFeginService { @RequestMapping(value = "/index")
public String index();
}
我们再在fegin项目中暴露一个访问接口,controller;
@RestController
public class FeginController { @Autowired
private IFeginService feginService; @RequestMapping("/index")
public String index(){
return feginService.index();
}
}
代码基本编写完成,下面我们来启动项目;Eureka,service-hello(两个实例),最后启动service-fegin;



Feign整合了Ribbon和Hystrix,此外,Spring Cloud还对Feign提供了Spring MVC注解的支持,也使得我们在Web中可以使用同一个HttpMessageConverter
总起来说,Feign具有如下特性:
- 可插拔的注解支持,包括Feign注解和JAX-RS注解;
- 支持可插拔的HTTP编码器和解码器;
- 支持Hystrix和它的Fallback;
- 支持Ribbon的负载均衡;
- 支持HTTP请求和响应的压缩。
(三)Fegin声明式服务调用的更多相关文章
- Spring Cloud 2-Feign 声明式服务调用(三)
Spring Cloud Feign 1. pom.xml 2. application.yml 3. Application.java 4. Client.java 简化RestTemplate调 ...
- Spring Cloud Feign 声明式服务调用
目录 一.Feign是什么? 二.Feign的快速搭建 三.Feign的几种姿态 参数绑定 继承特性 四.其他配置 Ribbon 配置 Hystrix 配置 一.Feign是什么? 通过对前面Sp ...
- spring cloud 系列第4篇 —— feign 声明式服务调用 (F版本)
源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.feign 简介 在上一个用例中,我们使用ribbon+restTem ...
- SpringCloud系列-利用Feign实现声明式服务调用
上一篇文章<手把手带你利用Ribbon实现客户端的负载均衡>介绍了消费者通过Ribbon调用服务实现负载均衡的过程,里面所需要的参数需要在请求的URL中进行拼接,但是参数太多会导致拼接字符 ...
- Spring Cloud Feign声明式服务调用(转载)+遇到的问题
转载:原文 总结: 1.pom添加依赖 2.application中填写正确的eureka配置 3.启动项中增加注解 @EnableFeignClients 4.填写正确的调用接口 通过原文使用Fei ...
- SpringCloud 源码系列(6)—— 声明式服务调用 Feign
SpringCloud 源码系列(1)-- 注册中心 Eureka(上) SpringCloud 源码系列(2)-- 注册中心 Eureka(中) SpringCloud 源码系列(3)-- 注册中心 ...
- Spring Cloud Eureka 分布式开发之服务注册中心、负载均衡、声明式服务调用实现
介绍 本示例主要介绍 Spring Cloud 系列中的 Eureka,使你能快速上手负载均衡.声明式服务.服务注册中心等 Eureka Server Eureka 是 Netflix 的子模块,它是 ...
- 声明式服务调用:Spring Cloud Feign
最近在学习Spring Cloud的知识,现将声明式服务调用:Spring Cloud Feign 的相关知识笔记整理如下.[采用 oneNote格式排版]
- SpringCloud之声明式服务调用 Feign(三)
一 Feign简介 Feign是一种声明式.模板化的HTTP客户端,也是netflix公司组件.使用feign可以在远程调用另外服务的API,如果调用本地API一样.我们知道,阿里巴巴的doubbo采 ...
随机推荐
- 30.QT IDE编写
mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QTe ...
- 分布式memcache
使用多台memchache服务器,形成memchache集群.目的是为了提升memchache所能使用的硬件资源数量.多台memcached服务器之间不相互通讯.分布式算法由客户端实现,(php来说, ...
- 使用 Travis-CI 的五个理由
I use the service of travis-ci now for a year. In that time the continuous integration has often poi ...
- MFC+OpenGL可编程管线
[github链接] 网上的代码大都是固定管线渲染的,今天下午整理了下,把setPixelFormat.初始化glew.创建GL 4,2 context等操作封装到一个MFC类OpenGLWidget ...
- hdu 1754 I Hate It【线段树】
维护一个最大值 #include<cstdio> #include<cstring> #include<iostream> #include<algorith ...
- node——文件夹创建
//创建文件夹 var fs=require('fs'); //1.异步 fs.mkdir("./第一个目录",function(err){ if (err) { return c ...
- python链接mysql数据库
1.安装pycharm python3.6 pip 在windows+R cmd where pip pip install mysql-client 如何看自已mysql-client有没有 ...
- python之类的组合
类的组合 学校与课程没有共同点,课程与老师没有共同点,但是学校与课程有关联,课程与老师有关联:学校.课程.老师是三个完全不同的类:课程是属于学校的,老师是教课程的,此时我们就用到类的组合来关联,学校- ...
- php数组 匹配键值
1.array_search() //判断键值是否在数组中,//存在,返回值对应的键;//不存在,返回false;//例子:$type = array( "选考" => 'o ...
- 好文应该收藏-----redis 配置自启动
话不多说,请看原作者笔记,亲测,可用 https://blog.csdn.net/qq_42810276/article/details/81296012