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,以及客户端如何调用服务 ...
随机推荐
- 获取网站IP地址(Linux,C)
#include <netdb.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> ...
- python 进程和线程(2)
这篇博客是按照博客<进程和线程(1)>中内容用futures改写 with futures.ProcessPoolExecutor() as executor:可以两篇博客对照看. 2改 ...
- k8s yaml 文件中字段类型:
1.<Object> 对象类型 metadata: name: namespace: 2.<[]Object> 对象列表类型 containers: - name: ...
- Win10 MySQL5.7中文乱码问题
https://blog.csdn.net/hh___56789/article/details/87900923 最好把 utf8 都换成utf8mb4 ,以免以后遇到意想不到的错误.utf8有漏洞 ...
- 两个exe共享内存数据
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- Microsoft SQL Server Management Studio连接后报“ viewInfo (Microsoft.SqlServer.Management.SqlStudio.Expl”
解决办法: 在路径:C:\Users\你的用户名\AppData\Local\Temp\”新建文件夹并命名为2,如果已经有 2 则看清楚是否是文件而不是文件夹,删掉文件改为文件夹: 如果是找不到\Us ...
- PAT Advanced 1064 Complete Binary Search Tree (30) [⼆叉查找树BST]
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- Eclipse换DarkestDark主题之后,无法使用快捷键
问题出现: 在选用Eclipse插件的MarketPlace的时候,发现一个下载量很高的主题,看起来很不错,毫不犹豫的就下下来了. 说时迟那时快,下下来之后果然好看,不过问题也来了,快捷键失效了,Ct ...
- luogu P3835 【模板】可持久化平衡树
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> usin ...
- Java 面向对象概述原理: 多态、Object类,转型(8)
Java 面向对象概述原理: 多态.Object类,转型(8) http://docs.oracle.com/javase/tutorial/java/IandI/override.html Java ...