Spring Cloud学习笔记【二】Eureka 服务提供者/服务消费者(ribbon)
Ribbon 是 Netflix 发布的开源项目,主要功能是为 REST 客户端实现负载均衡。它主要包括六个组件:
- ServerList,负载均衡使用的服务器列表。这个列表会缓存在负载均衡器中,并定期更新。当 Ribbon 与 Eureka 结合使用时,ServerList 的实现类就是 DiscoveryEnabledNIWSServerList,它会保存 Eureka Server 中注册的服务实例表。
- ServerListFilter,服务器列表过滤器。这是一个接口,主要用于对 Service Consumer 获取到的服务器列表进行预过滤,过滤的结果也是 ServerList。Ribbon 提供了多种过滤器的实现。
- IPing,探测服务实例是否存活的策略。
- IRule,负载均衡策略,其实现类表述的策略包括:轮询、随机、根据响应时间加权等,我们也可以自己定义负载均衡策略,比如我们就利用自己实现的策略,实现了服务的版本控制和直连配置。实现好之后,将实现类重新注入到 Ribbon 中即可。
- ILoadBalancer,负载均衡器。这也是一个接口,Ribbon 为其提供了多个实现,比如 ZoneAwareLoadBalancer。而上层代码通过调用其 API 进行服务调用的负载均衡选择。一般 ILoadBalancer 的实现类中会引用一个 IRule。
- RestClient,服务调用器。顾名思义,这就是负载均衡后,Ribbon 向 Service Provider 发起 REST 请求的工具。
Ribbon 工作时会做四件事情:
- 优先选择在同一个 Zone 且负载较少的 Eureka Server;
- 定期从 Eureka 更新并过滤服务实例列表;
- 根据用户指定的策略,在从 Server 取到的服务注册列表中选择一个实例的地址;
- 通过 RestClient 进行服务调用。
服务提供者
创建一个Spring Starter Project,命名service-producer,添加依赖
<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>
</dependencies>
配置属性(application.yml)
server:
port: 8080 #启动其他实例时需要修改端口号
spring:
application:
name: service-producer
eureka:
client:
serviceUrl:
defaultZone: http://admin:123456@localhost:8761/eureka/
控制层创建一个controller,对外提供一个接口(这里比较简单就只返回服务的端口号)
package com.carry.springcloud.controller; import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class ProducerController { @Value("${server.port}")
String serverPort; @GetMapping("/getPortInfo")
public String produce() {
return "调用服务的端口号为:" + serverPort;
}
}
启动类加上@EnableEurekaClient注解即可
package com.carry.springcloud; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @EnableEurekaClient
@SpringBootApplication
public class ServiceProducerApplication { public static void main(String[] args) {
SpringApplication.run(ServiceProducerApplication.class, args);
}
}
测试
打开浏览器访问localhost:8080/getPortInfo,出现以下结果说明是OK的
服务消费者(Ribbon)
创建一个Spring Starter Project,命名service-consumer-ribbon,添加依赖
<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-netflix-ribbon</artifactId>
</dependency>
</dependencies>
配置属性(application.yml)
server:
port: 8082
spring:
application:
name: service-consumer-ribbon
eureka:
client:
serviceUrl:
defaultZone: http://admin:123456@localhost:8761/eureka/
在启动类中@Bean 将 restTemplate注入到ioc容器, 并使用@LoadBalanced 注解声明开启 负载均衡
package com.carry.springcloud; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @EnableEurekaClient
@SpringBootApplication
public class ServiceConsumerRibbonApplication { public static void main(String[] args) {
SpringApplication.run(ServiceConsumerRibbonApplication.class, args);
} @Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
编写一个controller,注入RestTemplate用其调用服务提供者接口
package com.carry.springcloud.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; @RestController
public class RibbonController { @Autowired
RestTemplate restTemplate; @GetMapping("/getPoducerInfo")
public String getPoducerInfo() {
String result = this.restTemplate.getForObject("http://service-producer/getPortInfo", String.class);
return result;
}
}
注:上面代码中restTemplate.getForObject第一个参数url规则为:协议http+服务名(即application.yml配置spring.application.name的值)+接口值
测试
1、启动Eureka服务
2、启动两个服务提供者service-producer实例,端口分别为8080和8081
3、启动服务消费者service-consumer-ribbon
4、浏览器中访问 localhost:8761,注册成功
5、访问服务消费者localhost:8082/getPoducerInfo
再次访问
结果显示是轮询调用两个服务提供者实例,这是因为默认的负载均衡算法是轮询,也可自行修改负载均衡算法,例如:随机算法,权重,只需要在application.yml里配置即可。
Spring Cloud学习笔记【二】Eureka 服务提供者/服务消费者(ribbon)的更多相关文章
- Spring Cloud学习笔记【三】服务消费者Feign
Feign 是一个声明式的 Web Service 客户端,它的目的就是让 Web Service 调用更加简单.它整合了 Ribbon 和 Hystrix,从而让我们不再需要显式地使用这两个组件.F ...
- spring cloud学习笔记二 ribbon负载均衡
Ribbon是Netflix发布的负载均衡器,它有助于控制HTTP和TCP的客户端的行为.为Ribbon配置服务提供者地址后,Ribbon就可基于某种负载均衡算法,自动地帮助服务消费者去请求.Ribb ...
- Spring Cloud 学习笔记 (一)-- Eureka 服务器
开局一张图,截取了本人学习资料中的一张图,很好地展示了Eureka的架构. Eureka服务器 管理服务的作用.细分为服务注册,服务发现. 所有的客户端在Eureka服务器上注册服务,再从Eureka ...
- Spring Cloud学习笔记【七】服务网关 Zuul(路由)
Spring Cloud Zuul 路由是微服务架构的不可或缺的一部分,提供动态路由.监控.弹性.安全等的边缘服务.Zuul 是 Netflix 出品的一个基于 JVM 路由和服务端的负载均衡器. 准 ...
- Spring Cloud学习笔记【八】服务网关 Zuul(过滤器)
在上篇文章中我们了解了 Spring Cloud Zuul 作为网关所具备的最基本功能:路由(Router),下面我们将关注 Spring Cloud Zuul 的另一核心功能:过滤器(Filter) ...
- spring cloud 2.x版本 Eureka Server服务注册中心教程
本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 1.创建服务注册中心 1.1 新建Spring boot工程:eureka-server 1 ...
- Spring Cloud 学习笔记一
一.spring cloud 搭建注册中心(Eureka server) 1.spring cloud中提供了多种分步式服务组件,其都依赖于注册中心(eureka),注册中心的服务者与发现者都通过Eu ...
- Spring Cloud学习笔记-002
搭建Spring Cloud注册中心:Eureka 服务注册:在服务治理框架中,通常都会构建一个注册中心,每个服务单元向注册中心登记自己提供的服务,将主机与端口号.版本号.通信协议等一些附加信息告诉注 ...
- Spring Cloud学习笔记-006
服务容错保护:Spring Cloud Hystrix 在微服务架构中,我们将系统拆分成了很多服务单元,各单元的应用间通过服务注册与订阅的方式互相依赖.由于每个单元都在不同的进程中运行,依赖通过远程调 ...
- Spring cloud 学习笔记
前奏 1. 什么是微服务? 微服务化的核心就是将传统的一站式应用,根据业务拆分成一个一个的服务,彻底地去耦合,==每一个微服务提供单个业务功能的服务==,一个服务做一件事,从技术角度看就是一种 ...
随机推荐
- HTML样式以及使用
HTML的样式包含: 1,标签{style ,link} 2.属性{rel="styleSheet"外部样式表,type="text/css",margin-l ...
- Making User-Managed Backups-17.4、Making User-Managed Backups of Online Tablespaces and Datafiles
17.4.Making User-Managed Backups of Online Tablespaces and Datafiles 当数据库打开时,能够备份一个在线表空间全部和一个指定的数据文件 ...
- Hints
If you played with the Fibonacci function, you might have noticed that the bigger the argument you p ...
- html页面全屏化显示
<html><head><script>// toggle full screen function toggleFullScreen() { if (!docum ...
- T-SQL函数类型——系统函数
1 ??? 为什么 123 和'123'的ISNUMERIC()返回结果相同. SELECT ISNUMERIC(123) --结果为1SELECT ISNUMERIC('123') --结果为1S ...
- Python的matplotlib库画图不能显示中文问题解决
有两种解决办法: 一种是在代码里设置为能显示中文的字体,如微软雅黑(msyh.ttf)和黑体(simsun.ttc) 如下在要画图的代码前添加: import matplotlib.pyplot as ...
- 什么是Monad?
为了理解什么是Monad,最好需要了解什么是Monoid.这两篇互为姐妹篇,因为Monad的定义是:A monad is just a monoid in the category of endofu ...
- MyEclipse for mac 快捷键
原文出处:http://blog.csdn.net/ray_seu/article/details/17384463 一直比较欣赏myeclipse的快捷键,网上搜索了一圈,发现windows平台下面 ...
- UI Framework-1: Aura
Aura (obsolete) This document is still good for a high level overview, with contact information, but ...
- [洛谷P1343]地震逃生
题目大意:有n个点m条单向边,每条边有一个容量.现有x人要分批从1走到n,问每批最多能走多少人,分几批运完(或输出无法运完). 解题思路:一看就是网络流的题目.每批最多能走多少人,即最大流.分几批运完 ...