feign实现服务间的负载均衡
feign
Feign是一个声明式的Web Service客户端,它使得编写Web Serivce客户端变得更加简单。我们只需要使用Feign来创建一个接口并用注解来配置它既可完成。它具备可插拔的注解支持,包括Feign注解和JAX-RS注解。Feign也支持可插拔的编码器和解码器。Spring Cloud为Feign增加了对Spring MVC注解的支持,还整合了Ribbon和Eureka来提供均衡负载的HTTP客户端实现。
下面,通过一个例子来展现Feign如何方便的声明对上述computer-service服务的定义和调用。
创建一个Spring Boot工程,配置pom.xml,将上述的配置中的ribbon依赖替换成feign的依赖即可,具体如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
|
在应用主类中通过@EnableFeignClients注解开启Feign功能,具体如下:
|
1
2
3
4
5
6
7
8
9
10
|
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
|
定义compute-service服务的接口,具体如下:
|
1
2
3
4
5
6
7
|
@FeignClient("compute-service")//相当于eureka中的注册的ip
public interface ComputeClient {
@RequestMapping(method = RequestMethod.GET, value = "/add")
Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b);
}
|
- 使用
@FeignClient("compute-service")注解来绑定该接口对应compute-service服务 - 通过Spring MVC的注解来配置compute-service服务下的具体实现。
在web层中调用上面定义的ComputeClient,具体如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
@RestController
public class ConsumerController {
@Autowired
ComputeClient computeClient;
@RequestMapping(value = "/add", method = RequestMethod.GET)
public Integer add() {
return computeClient.add(10, 20);
}
}
|
application.properties中不用变,指定eureka服务注册中心即可,如:
|
1
2
3
4
|
spring.application.name=feign-consumer
server.port=3333
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/,http://localhost:2222/eureka/
|
启动该应用,访问几次:http://localhost:3333/add
再观察日志,可以得到之前使用Ribbon时一样的结果,对服务提供方实现了均衡负载。
这一节我们通过Feign以接口和注解配置的方式,轻松实现了对compute-service服务的绑定,这样我们就可以在本地应用中像本地服务一下的调用它,并且做到了客户端均衡负载。
feign实现服务间的负载均衡的更多相关文章
- spring cloud(服务消费者(利用feign实现服务消费及负载均衡)——初学三)
Feign是一个声明式的Web Service客户端,我们只需要使用Feign来创建一个接口并用注解来配置它既可完成. 它具备可插拔的注解支持,包括Feign注解和JAX-RS注解.Feign也支持可 ...
- SpringCloud微服务(02):Ribbon和Feign组件,实现服务调用的负载均衡
本文源码:GitHub·点这里 || GitEE·点这里 一.Ribbon简介 1.基本概念 Ribbon是一个客户端的负载均衡(Load Balancer,简称LB)器,它提供对大量的HTTP和TC ...
- RPC原来就是Socket——RPC框架到dubbo的服务动态注册,服务路由,负载均衡演化
序:RPC就是使用socket告诉服务端我要调你的哪一个类的哪一个方法然后获得处理的结果.服务注册和路由就是借助第三方存储介质存储服务信息让服务消费者调用.然我们自己动手从0开始写一个rpc功能以及实 ...
- Spring Cloud ---- 服务消费与负载均衡(Rest + Ribbon )
上一篇主要写了基于Eurake的服务的注册,主要就是创建注册中心,创建服务者,将服务者注册到注册中心,完成服务的暴露.这一篇主要写服务的消费与服务消费的负载均衡. 服务的调用方式有两种,Rest + ...
- 学习一下 SpringCloud (三)-- 服务调用、负载均衡 Ribbon、OpenFeign
(1) 相关博文地址: 学习一下 SpringCloud (一)-- 从单体架构到微服务架构.代码拆分(maven 聚合): https://www.cnblogs.com/l-y-h/p/14105 ...
- Spring Cloud系列(三):服务消费与负载均衡
上一篇介绍了服务提供者,有了注册中心和服务提供者,我们就可以进行服务消费了.Spring Cloud可以通过RestTemplate+Ribbon和Feign这两种方式消费服务. 我们仍然在上一篇的项 ...
- kubernetes云平台管理实战: 服务发现和负载均衡(五)
一.rc控制器常用命令 1.rc控制器信息查看 [root@k8s-master ~]# kubectl get replicationcontroller NAME DESIRED CURRENT ...
- 快速理解高性能HTTP服务端的负载均衡技术原理(转)
1.前言 在一个典型的高并发.大用户量的Web互联网系统的架构设计中,对HTTP集群的负载均衡设计是作为高性能系统优化环节中必不可少的方案.HTTP负载均衡的本质上是将Web用户流量进行均衡减压,因此 ...
- Consul + fabio 实现自动服务发现、负载均衡 - DockOne.io
Consul + fabio 实现自动服务发现.负载均衡 - DockOne.io http://dockone.io/article/1567
随机推荐
- 如何让Surface RT支持网站的flash
Go to the desktop version of IE10, hit the click ALT button on your keyboard, click on and then Comp ...
- python快速上手教程
python版本 python目前的版本分为2.7和3.5,两种版本的代码目前无法兼容,查看python版本号: python --version 基本数据类型 数字类型 整型和浮点型数据和其它编程语 ...
- cassandra根据用户名密码登录cqlsh
修改conf目录下cassandra.yaml文件 authenticator: PasswordAuthenticator //将authenticator修改为PasswordAuthentic ...
- SQL 排序规则问题
http://blog.csdn.net/delphigbg/article/details/12744807 MSSQL排序规则总结 什么是排序规则呢? 排序规则根据特定语言和区域设置标准指定对 ...
- [pe531]Chinese leftovers
题意:1e6~1e6+5000之间任意两个之间同余方程组的解.余数为欧拉函数. 解题关键:线性筛预处理,扩展中国剩余定理暴力求解. #include<cstdio> #include< ...
- Robot FrameWork基础学习(四) 元素定位
元素定位 对于web自动化测试来说,就是操作页面的各种元素,在操作元素之间需要先找到元素,换句话说就是定位元素. Selenium2Library提供了非常丰富的定位器: 虽然提供了这么多种定位方式, ...
- C# FileStream分块读取和保存文件
一 FileStream分块读取文件 public byte[] GetFileData(string fileName, long startPosition, long length) { byt ...
- Eclipse下对maven进行配置
前提:安装好maven插件http://www.cnblogs.com/lchzls/p/6281697.html 1.需要修改配置:首先选择Window->Preferences,弹出如下对话 ...
- Java集合Set、Map、HashSet、HashMap、TreeSet、TreeMap等
1.Set和Map的关系: Set代表一种集合元素无序.不可重复的集合,Map代表一种由多个key-value对组成的集合. Set的集合继承体系: Map关系集合 Map集合的key特征:所有key ...
- 数据库路由中间件MyCat - 源代码篇(13)
此文已由作者张镐薪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 4.配置模块 4.2 schema.xml 接上一篇,接下来载入每个schema的配置(也就是每个MyCat ...