Spring Cloud服务消费者(rest+ribbon)
一、ribbon简介
Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients. Feign already uses Ribbon, so if you are using @FeignClient then this section also applies.-----摘自官网
- IClientConfig ribbonClientConfig: DefaultClientConfigImpl
- IRule ribbonRule: ZoneAvoidanceRule
- IPing ribbonPing: NoOpPing
- ServerList ribbonServerList: ConfigurationBasedServerList
- ServerListFilter ribbonServerListFilter: ZonePreferenceServerListFilter
- ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer
二、准备工作

三、建一个服务消费者
在它的pom.xml文件分别引入起步依赖spring-cloud-starter-eureka、spring-cloud-starter-ribbon、spring-boot-starter-web,代码如下:
01
02
03
04
05
06
07
08
09
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
<? xml version = "1.0" encoding = "UTF-8" ?> < project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 [url]http://maven.apache.org/xsd/maven-4.0.0.xsd[/url]" > < modelVersion >4.0.0</ modelVersion > < groupId >com.forezp</ groupId > < artifactId >service-ribbon</ artifactId > < version >0.0.1-SNAPSHOT</ version > < packaging >jar</ packaging > < name >service-ribbon</ name > < description >Demo project for Spring Boot</ description > < parent > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-parent</ artifactId > < version >1.5.2.RELEASE</ version > < relativePath /> <!-- lookup parent from repository --> </ parent > < properties > < project.build.sourceEncoding >UTF-8</ project.build.sourceEncoding > < project.reporting.outputEncoding >UTF-8</ project.reporting.outputEncoding > < java.version >1.8</ java.version > </ properties > < dependencies > < dependency > < groupId >org.springframework.cloud</ groupId > < artifactId >spring-cloud-starter-eureka</ 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-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 >Dalston.RC1</ version > < type >pom</ type > < scope >import</ scope > </ dependency > </ dependencies > </ dependencyManagement > < build > < plugins > < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > </ plugin > </ plugins > </ build > < repositories > < repository > < id >spring-milestones</ id > < name >Spring Milestones</ name > < url >[url]https://repo.spring.io/milestone</ url >[/url] < snapshots > < enabled >false</ enabled > </ snapshots > </ repository > </ repositories > </ project > |
1
2
3
4
5
6
7
8
9
|
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ server: port: 8764 spring: application: name: service-ribbon |
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
|
@SpringBootApplication @EnableDiscoveryClient public class ServiceRibbonApplication { public static void main(String[] args) { SpringApplication.run(ServiceRibbonApplication. class , args); } @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } } |
01
02
03
04
05
06
07
08
09
10
11
|
@Service public class HelloService { @Autowired RestTemplate restTemplate; public String hiService(String name) { return restTemplate.getForObject( "http://SERVICE-HI/hi?name=" +name,String. class ); } } |
01
02
03
04
05
06
07
08
09
10
11
|
@RestController public class HelloControler { @Autowired HelloService helloService; @RequestMapping (value = "/hi" ) public String hi( @RequestParam String name){ return helloService.hiService(name); } } |
hi forezp,i am from port:8762hi forezp,i am from port:8763
四、此时的架构

- 一个服务注册中心,eureka server,端口为8761
- service-hi工程跑了两个实例,端口分别为8762,8763,分别向服务注册中心注册
- sercvice-ribbon端口为8764,向服务注册中心注册
- 当sercvice-ribbon通过restTemplate调用service-hi的hi接口时,因为用ribbon进行了负载均衡,会轮流的调用service-hi:8762和8763 两个端口的hi接口;
- 更多java学习资料可关注:itheimaGZ获取
Spring Cloud服务消费者(rest+ribbon)的更多相关文章
- [异常笔记] spring cloud 服务消费者启动-2018040501
一.异常信息: Error starting ApplicationContext. To display the auto-configuration report re-run your appl ...
- Spring Cloud学习笔记【二】Eureka 服务提供者/服务消费者(ribbon)
Ribbon 是 Netflix 发布的开源项目,主要功能是为 REST 客户端实现负载均衡.它主要包括六个组件: ServerList,负载均衡使用的服务器列表.这个列表会缓存在负载均衡器中,并定期 ...
- spring cloud 2.x版本 Ribbon服务发现教程(内含集成Hystrix熔断机制)
本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 前言 本文基于前两篇文章eureka-server和eureka-client的实现. 参考 ...
- spring cloud学习笔记二 ribbon负载均衡
Ribbon是Netflix发布的负载均衡器,它有助于控制HTTP和TCP的客户端的行为.为Ribbon配置服务提供者地址后,Ribbon就可基于某种负载均衡算法,自动地帮助服务消费者去请求.Ribb ...
- Spring Cloud服务保护
微服务虽然解决了传统单体式应用各个模块之间强耦合的缺点,但同时也引出了新问题,由于微服务各个服务之间是独立部署的,并且一般情况下一个服务往往会依赖多个其他服务,并且服务之间的调用更多的是依赖不稳定的网 ...
- Spring Cloud中Hystrix、Ribbon及Feign的熔断关系是什么?
导读 今天和大家聊一聊在Spring Cloud微服务框架实践中,比较核心但是又很容易把人搞得稀里糊涂的一个问题,那就是在Spring Cloud中Hystrix.Ribbon以及Feign它们三者之 ...
- spring cloud 客户端负载均衡 - Ribbon
Spring Cloud Ribbon是一个基于HTTP和TCP的客户端负载均衡工具,基于Netflix Ribbon实现的,Ribbon不像注册中心.网关那样需要单独部署,它是作为一个工具直接集成到 ...
- Spring Cloud服务注册中心交付至kubernetes
前言 服务发现原则: 各个微服务在启动时,会将自己的网络地址等信息注册到服务发现组件中,服务发现组件会存储这些信息 服务消费者可以从服务发现组件中查询到服务提供者的网络地址,并使用该地址来远程调用服务 ...
- Spring Cloud 服务端注册与客户端调用
Spring Cloud 服务端注册与客户端调用 上一篇中,我们已经把Spring Cloud的服务注册中心Eureka搭建起来了,这一章,我们讲解如何将服务注册到Eureka,以及客户端如何调用服务 ...
随机推荐
- phpstorm 的下载、安装与激活
1.phpstorm的下载地址 https://www.jetbrains.com/phpstorm/ 下载后的安装包如图: 2.phpstorm的安装过程 跟据电脑系统下载安装对应版本 一路点击下一 ...
- python 设置系统/用户环境变量
系统环境变量 winreg.HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' 用户环境变 ...
- 吴裕雄--天生自然 JAVASCRIPT开发学习:函数定义
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- count(1),count(*)和count(列)的比较
转自:https://www.cnblogs.com/Caucasian/p/7041061.html 1.关于count(1),count(*),和count(列名)的区别 相信大家总是在工作中,或 ...
- LGOJ4299 首都
这题是 \(LCT\) 维护子树信息中的 \(LCT\) 维护重心 Description link 题意概述:给定一个森林,要求支持以下操作 1.链接两个点 2.求一个点所在树的重心 3.求所有重心 ...
- 神经网络 参数计算--直接解析CKPT文件读取
1.tensorflow的模型文件ckpt参数获取 import tensoflow as tf from tensorflow.python import pywrap_tensorflow mod ...
- leetcode 690.员工的重要性
题目: 给定一个保存员工信息的数据结构,它包含了员工唯一的id,重要度 和 直系下属的id. 比如,员工1是员工2的领导,员工2是员工3的领导.他们相应的重要度为15, 10, 5.那么员工1的数据结 ...
- js中将json字符串转换成json对象
在我们使用js请求后台控制器传回的结果result值的时候,经常会出现返回结果值为json字符串的情况,字符串无法在js中直接使用 返回样式栗子: 这是一个json字符串:result = " ...
- 63)对于STL基本概念东西 自己百度(没有整理)
基础知识 看 C++进阶课程讲义的那个word文档
- Ubuntu---gedit 打开windows 下 .txt 文件乱码的解决方法
问题出现情况:在windows 下编辑的 .txt 文件复制到 Ubuntu 下打开,默认打开方式为 gedit 软件打开,出现如下乱码: 出现原因:在 windows 系统下,.txt 文件默认编码 ...