上一篇文章,简单概述了服务注册与发现,在微服务架构中,业务都会被拆分成一个独立的服务,服务之间的通讯是基于http restful的,Ribbon可以很好地控制HTTP和TCP客户端的行为,Spring Cloud有两种调用方式,一种是Ribbon+RestTemplate,另一种是Feign(集成Ribbon+Hystrix),本章主要讲解Ribbon

- Ribbon简介

Ribbon 是一个客户端负载均衡器,它可以让你对HTTP和TCP客户机的行为有很大的控制权,如果你正在使用 @FeignClient 那么这一段也适用,因为Feign集成了Ribbon的功能,Ribbon 中的一个中心概念就是指定客户端,每个负载均衡器都是组成组件的一部分,这些组件一起工作以根据需要请求远程服务器。

官方文档:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#spring-cloud-ribbon

- 准备工作

1.创建项目,在上一章已经详细描述了,没看过的可以详细看一下 一起来学SpringCloud之-注册中心(Eureka/Consul)

2.启动Consul,后续所有文章都将以Consul作为服务注册中心

创建Ribbon项目

- pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

- BattcnCloudRibbonApplication.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class BattcnCloudRibbonApplication { @Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
} @GetMapping("/ribbon")
public String findHelloByEmail(String email) {
// VIP模式,不需要填写 IP+端口 Ribbon会去注册中心获取当前可用服务然后做HTTP请求
return "server <<==>> "+restTemplate().getForObject("http://battcn-cloud-hello/hello?email="+email,String.class);
} public static void main(String[] args) {
SpringApplication.run(BattcnCloudRibbonApplication.class, args);
}
}

- bootstrap.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
server:
port: 8764 spring:
application:
name: battcn-cloud-ribbon
cloud:
consul:
host: localhost
port: 8500
enabled: true
discovery:
enabled: true
prefer-ip-address: true

- 测试

启动:battcn-cloud-hello(上一章的项目,稍微做了修改,app.java中添加了日志输出)
启动:battcn-cloud-ribbon(本章项目)

访问:http://localhost:8500/ 显示如下代表服务注册成功

查看注册中心

访问:http://localhost:8763/ribbon?email=123456@qq.com

1
server <<==>> My Name's :battcn-cloud-hello Email:123456@qq.com

如果需要测试负载功能,将 battcn-cloud-hello 打包,然后通过 cmd 方式启动,因为我使用的是IDEA总是提示我关闭重启,因此我是打的JAR,如果是Eclipse的朋友可以直接修改yml文件的端口启动多次

1
2
3
battcn-cloud-hello-8762:java -jar battcn-cloud-hello-1.0.0-SNAPSHOT.jar --server.port=8762

battcn-cloud-hello-8763:java -jar battcn-cloud-hello-1.0.0-SNAPSHOT.jar --server.port=8763

结果

- 请求图

架构图

画图工具:https://www.processon.com/

- 新特性

从版本1.2.0开始,Spring Cloud Netflix现在支持使用属性与Ribbon文档兼容来自定义功能区客户端。

允许在不同环境中更改启动时的行为。

支持的属性如下所示,应以.ribbon.:

NFLoadBalancerClassName:应实现 ILoadBalancer

NFLoadBalancerRuleClassName:应实现 IRule

NFLoadBalancerPingClassName:应实现 IPing

NIWSServerListClassName:应实现 ServerList

NIWSServerListFilterClassName 应实现 ServerListFilter

注意:在这些属性中定义的类优先于使用@RibbonClient(configuration=MyRibbonConfig.class) Spring 定义的bean 以及由Spring Cloud Netflix提供的默认值。要设置IRule服务名称,users您可以设置以下内容:

application.yml

1
2
3
users:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule

详细请看官方文档:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#_customizing_the_ribbon_client_using_properties

- 定义自己的RibbonClient

警告:在FooConfiguration 中使用 @Configuration,需要注意是不是在@ComponentScan主应用程序上下文,否则会被所有的共享@RibbonClients。如果您使用@ComponentScan(或@SpringBootApplication)您需要采取措施避免它被包含(例如将其放在一个单独的,不重叠的包中,或者指定要在其中显式扫描的包@ComponentScan)。

简单点说就是:不要让 @ComponentScan 同时扫描到主程序和自定义的带有@Configuration的文件

1
2
3
com.battcn.BattcnCloudRibbonApplication.java

com.config.FooConfiguration.java

- FooConfiguration

1
2
3
4
5
6
7
@Configuration
public class FooConfiguration {
@Bean
public IRule ribbonRule(IClientConfig clientConfig) {
return new RandomRule();
}
}

- BattcnCloudRibbonApplication

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@SpringBootApplication
@EnableDiscoveryClient
@RestController
@RibbonClient(name = "battcn-cloud-hello",configuration = FooConfiguration.class)
public class BattcnCloudRibbonApplication { @Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
} @GetMapping("/ribbon")
public String findHelloByEmail(String email) {
// VIP模式,不需要填写 IP+端口 Ribbon会去注册中心获取当前可用服务然后做HTTP请求
return "server <<==>> "+restTemplate().getForObject("http://battcn-cloud-hello/hello?email="+email,String.class);
} public static void main(String[] args) {
SpringApplication.run(BattcnCloudRibbonApplication.class, args);
}
}

- 测试

随机访问N次:http://localhost:8764/ribbon?email=123456@qq.com 结果如下(人品只要不是好到爆炸就不会五五开)

自定义Rule

服务消费者(Ribbon)的更多相关文章

  1. 创建服务消费者(Ribbon)

    概述 在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于 http restful 的.Spring cloud 有两种服务调用方式,一种是 ribbon + restTempla ...

  2. SpringCloud-创建服务消费者-Ribbon方式(附代码下载)

    场景 SpringCloud-服务注册与实现-Eureka创建服务注册中心(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...

  3. 7、服务发现&服务消费者Ribbon

    公众号: java乐园 在<服务注册&服务提供者>这一篇可能学习了这么开发一个服务提供者,在生成上服务提供者通常是部署在内网上,即是服务提供者所在的服务器是与互联网完全隔离的.这篇 ...

  4. Spring Cloud (3) 服务消费者-Ribbon

    在上一篇中使用LoadBalancerClient接口实现了获取某个服务的具体实例,并根据实例信息发起服务接口消费请求.但是这样的做法需要我们手工的区编写服务选取.连接拼接等繁琐的工作,对于开发人员来 ...

  5. 「 从0到1学习微服务SpringCloud 」04服务消费者Ribbon+RestTemplate

    系列文章(更新ing): 「 从0到1学习微服务SpringCloud 」01 一起来学呀! 「 从0到1学习微服务SpringCloud 」02 Eureka服务注册与发现 「 从0到1学习微服务S ...

  6. springcloud-Netflix创建服务消费者

    目录 springcloud-Netflix创建服务消费者 Ribbon 创建服务消费者-Ribbon方式 ribbon的架构 Feign 创建包和基本项目结构 创建Feign访问服务的接口和访问co ...

  7. SpringCloud-使用熔断器防止服务雪崩-Ribbon和Feign方式(附代码下载)

    场景 SpringCloud-服务注册与实现-Eureka创建服务注册中心(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...

  8. SpringCloud-创建服务消费者-Feign方式(附代码下载)

    场景 SpringCloud-服务注册与实现-Eureka创建服务注册中心(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...

  9. 「 从0到1学习微服务SpringCloud 」05服务消费者Fegin

    系列文章(更新ing): 「 从0到1学习微服务SpringCloud 」01 一起来学呀! 「 从0到1学习微服务SpringCloud 」02 Eureka服务注册与发现 「 从0到1学习微服务S ...

  10. springcloud干货之服务消费者(ribbon)

    本章介绍springcloud中的服务消费者 springcloud服务调用方式有两种实现方式: 1,restTemplate+ribbon, 2,feign 本来想一篇讲完,发现篇幅有点长,所以本章 ...

随机推荐

  1. Java实现 LeetCode 693 交替位二进制数(位运算)

    693. 交替位二进制数 给定一个正整数,检查他是否为交替位二进制数:换句话说,就是他的二进制数相邻的两个位数永不相等. 示例 1: 输入: 5 输出: True 解释: 5的二进制数是: 101 示 ...

  2. Java实现 蓝桥杯VIP 算法提高 3-2求存款

    算法提高 3-2求存款 时间限制:1.0s 内存限制:256.0MB 问题描述 见计算机程序设计基础(乔林)P50第5题. 接受两个数,一个是用户一年期定期存款金额,一个是按照百分比格式表示的利率,计 ...

  3. MAC/VMware配置双机调试简述

    Configuration 注:建议提前备份所有修改内容,可能会导致无法开机. 我的测试环境: server: windows 10 + windbg client: windows 7 Server ...

  4. java之单点登录(SSO)

    单点登录(SSO):SSO是指在多个应用系统中个,用户只需要登陆一次就可以访问所有相互信任的应用系统.它包括可以将这次主要的登录映射到其他应用中用于同一用户的登陆的机制. SSO的实现过程: 通过上述 ...

  5. zabbix服务无法正常启动

    环境介绍 操作系统 centos 7.4 zabbix 3.4.7   故障现象说明 zabbix agent无法启动   web 监控不到客户端服务器   查看日志报错 提示不能打开日志,不能创建信 ...

  6. 本地配置gitee

    一 下载工具 Git-2.62.0-64-bit.exe 以上工具版本号不需要一样,安装完前两个后重新启动系统,再安装第3个. 二 码云网站注册 https://gitee.com/ 使用邮箱注册 注 ...

  7. Python面试进阶问题,__init__和__new__的区别是什么?

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天这篇是Python专题的第17篇文章,我们来聊聊Python当中一个新的默认函数__new__. 上一篇当中我们讲了如何使用type函数 ...

  8. vue2.0 + Element UI + axios实现表格分页

    注:本文分页组件用原生 html + css 实现,element-ui里有专门的分页组件可以不用自己写,详情见另一篇博客:https://www.cnblogs.com/zdd2017/p/1115 ...

  9. 别再写一摞if-else了!再写开除!两种设计模式带你消灭它!

    代码洁癖狂们!看到一个类中有几十个if-else是不是很抓狂? 设计模式学了用不上吗?面试的时候问你,你只能回答最简单的单例模式,问你有没有用过反射之类的高级特性,回答也是否吗? 这次就让设计模式(模 ...

  10. 通用!Python保存一个对象的方式

    参考资料: https://kite.com/python/answers/how-to-save-a-dictionary-to-a-file-in-python 通过如下的代码,可以将Python ...