微服务架构之spring cloud ribbon
现在负载均衡是通用的解决分压的技术方案,实现方式一般分为服务端或者客户端,服务端大部分是使用中间件实现,spring cloud ribbon 是一个客户端负载均衡组件。跟spring cloud eureka、spring cloud feign 搭配的很默契,下一篇我们再讲解spring cloud feign。
(一) 版本说明
a) Spring boot 2.0.6.RELEASE
b) Spring cloud Finchley.SR2
c) Java version 1.8
d) Spring-cloud-starter-netflix-ribbon 2.0.2.RELEASE
(二) 项目配置
1. 服务端项目配置
a) 服务端主要是提供服务功能,这里是把当前的端口返回给调用者,同时把自己注册到服务中心,调用者通过服务中心调用。
b) POM设置
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
c) application.yml配置文件
eureka: datacenter: ctm environment: dev instance: hostname: 192.168.1.78 prefer-ip-address: true ip-address: 192.168.1.129 lease-renewal-interval-in-seconds: lease-expiration-duration-in-seconds: instance-id: ${eureka.instance.ip-address}:${server.port} client: service-url: defaultZone: http://${eureka.instance.hostname}:1001/eureka/,http://${eureka.instance.hostname}:1002/eureka/,http://${eureka.instance.hostname}:1003/eureka/ fetch-registry: true register-with-eureka: true healthcheck: enabled: true
d) 主要参数说明
i. eureka.instance.prefer-ip-address 使用IP显示注册信息
ii. eureka.instance.ip-address 实例IP地址,
iii. eureka.instance.instance-id 自定义实例id,服务之间调用就是使用该配置,多个实例必须保证唯一性
iv. eureka.client.service-url.defaultZone 注册中心地址
e) 服务提供者API
@Value("${server.port}") private String getPort; @GetMapping(name = "index", value = "/index") public String Index() { return getPort; }
2. 消费端项目配置
a) POM设置
<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>
i. 这里把服务消费端也注册到了服务治理中心,消费者同时也是其它服务的提供者。
b) application.yml配置文件
eureka: datacenter: ctm environment: dev instance: hostname: 192.168.1.78 prefer-ip-address: true ip-address: 192.168.1.129 lease-renewal-interval-in-seconds: 10 lease-expiration-duration-in-seconds: 30 instance-id: ${eureka.instance.ip-address}:${server.port} client: service-url: defaultZone: http://${eureka.instance.hostname}:1001/eureka/,http://${eureka.instance.hostname}:1002/eureka/,http://${eureka.instance.hostname}:1003/eureka/ fetch-registry: true register-with-eureka: true healthcheck: enabled: true
c) 服务消费者API
@Bean @LoadBalanced RestTemplate restTemplate(){ return new RestTemplate(); } @Service public class RibbonService { @Autowired RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "fallBackIndex") public String Index(){ return restTemplate.getForObject("http://DEMOSERVICEIMPL/index",String.class); } public String fallBackIndex(){ return "hi,ribbon,error!"; } }
3. 重点提示
a) 为了演示负载,3个服务提供者必须设置3个不同的端口,并且其它相同,不然会被认为是不同的服务,起不到均衡的作用。
b) LoadBalanced 该注解实现了负载均衡功能,默认策略是轮询
4. 项目运行
a) 运行服务提供者
i. 运行3个服务者实例后,会在服务中心看到如下效果,服务提供者已经注册成功
b) 运行服务消费者
i. 运行消费者,如下图所示
c) 打开PostMan,输入消费者地址,多刷新几次,即可看到负载效果
这样spring cloud ribbon负载组件就介绍完了,如果在开发中遇到问题,也可以留言共同探讨共同进步。
微服务架构之spring cloud ribbon的更多相关文章
- 微服务架构-选择Spring Cloud,放弃Dubbo
Spring Cloud 在国内中小型公司能用起来吗?从 2016 年初一直到现在,我们在这条路上已经走了一年多. 在使用 Spring Cloud 之前,我们对微服务实践是没有太多的体会和经验的.从 ...
- 微服务架构集大成者—Spring Cloud (转载)
软件是有生命的,你做出来的架构决定了这个软件它这一生是坎坷还是幸福. 本文不是讲解如何使用Spring Cloud的教程,而是探讨Spring Cloud是什么,以及它诞生的背景和意义. 1 背景 2 ...
- 微服务架构之spring cloud 介绍
在当前的软件开发行业中,尤其是互联网,微服务是非常炽热的一个词语,市面上已经有一些成型的微服务框架来帮助开发者简化开发工作量,但spring cloud 绝对占有一席之地,不管你是否为java开发,大 ...
- 微服务架构之spring cloud eureka
Spring Cloud Eureka是spring cloud的核心组件,负责服务治理功能,起到中心枢纽作用,其它组件都依赖eureka来获取服务,然后再根据项目需求实现自己的业务,eureka在整 ...
- 微服务架构之spring cloud feign
在spring cloud ribbon中我们用RestTemplate实现了服务调用,可以看到我们还是需要配置服务名称,调用的方法 等等,其实spring cloud提供了更优雅的服务调用方式,就是 ...
- 微服务架构之spring cloud zipkin
Spring Cloud Zipkin是微服务的链路跟踪组件,帮助详细了解一次request&response的总计时,及每个微服务的消耗时间.微服务名称.异常信息等等过程信息. (一) 版本 ...
- 微服务架构之spring cloud turbine
在前面介绍了spring cloud hystrix及其hystrix dashboard,但都是对单个项目的监控,对于一个为项目而言,必定有很多微服务,一个一个去看非常的不方便,如果有一个能集中熔断 ...
- 微服务架构之spring cloud gateway
Spring Cloud Gateway是spring cloud中起着非常重要的作用,是终端调用服务的入口,同时也是项目中每个服务对外暴露的统一口径,我们可以在网关中实现路径映射.权限验证.负载均衡 ...
- 微服务架构之spring cloud hystrix&hystrix dashboard
在前面介绍spring cloud feign中我们已经使用过hystrix,只是没有介绍,spring cloud hystrix在spring cloud中起到保护微服务的作用,不会让发生的异常无 ...
随机推荐
- pytest+jenkins安装+allure导出报告
环境安装: windows7+64位 pytest:4.0.2 allure的安装:allure的python库pytest-allure-adaptor jenkins的安装:2.138.2 JDK ...
- apache 子域名自动与子域名同名的目录绑定
假设有域名domain.com,已经泛解析子域名*.domain.com到该主机的ip,web根目录为/var/www/,在访问a.domain.com时,能自动绑定/var/www/a/目录,访问b ...
- js字符串去重
js字符串去重: 1. 去掉字符串前后所有空格: function Trim(str) { return str.replace(/(^\s*)|(\s*$)/g, ""); } ...
- 原创:centos7.1下 ZooKeeper 集群安装配置+Python实战范例
centos7.1下 ZooKeeper 集群安装配置+Python实战范例 下载:http://apache.fayea.com/zookeeper/zookeeper-3.4.9/zookeepe ...
- idea中web.xml报错 Servlet should have a mapping
配置springmvc时,报错,实际mapping已经写了,错误截图如下: 搜索无果,后来发现是工程的web.xml位置配置错误,因为我之前换过根目录位置. 修改方法: 打开Project Struc ...
- JavaScript设计模式-22.观察者模式
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- unity优化测试插件推荐:内存分析,数据监控,弱网模拟
1.内存分析插件,unity官方出品 官方地址:https://bitbucket.org/Unity-Technologies/memoryprofiler 我整理的:https://downloa ...
- Linux常用命令之sed(2)
Sed SED的英文全称是 Stream EDitor,它是一个简单而强大的文本解析转换工具,在1973-1974年期间由贝尔实验室的Lee E. McMahon开发,今天,它已经运行在所有的主流操作 ...
- 【LeetCode题解】20_有效的括号(Valid-Parentheses)
目录 20_有效的括号(Valid-Parentheses) 描述 解法 思路 Java 实现 Python 实现 复杂度分析 20_有效的括号(Valid-Parentheses) 描述 给定一个只 ...
- cursor : 普通,带参,可更新的游标。使用游标遍历时,强烈建议用for循环!!!
cursor: 源数据表account中仅有两条记录: 如果输出在判断前,则出错,将最后一条记录输出两次,如下: 所以,一定要先判断notfound再输出结果: exit when (c%notfou ...